-
Notifications
You must be signed in to change notification settings - Fork 58
/
gcc-python-diagnostics.c
200 lines (165 loc) · 5.88 KB
/
gcc-python-diagnostics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Copyright 2011-2013, 2017 David Malcolm <[email protected]>
Copyright 2011-2013, 2017 Red Hat, Inc.
This is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include "gcc-python.h"
#include "gcc-python-wrappers.h"
#include "diagnostic.h"
#include "gcc-c-api/gcc-diagnostics.h"
/*
I initially attempted to directly wrap gcc's:
diagnostic_report_diagnostic()
given that that seems to be the abstraction within gcc/diagnostic.h: all
instances of (struct diagnostic_info) within the gcc source tree seem to
be allocated on the stack, within functions exposed in gcc/diagnostic.h
However, diagnostic_report_diagnostic() ultimately calls into the
pretty-printing routines, trying to format varargs, which doesn't make much
sense for us: we have first-class string objects and string formatting at
the python level.
Thus we instead just wrap "error_at" and its analogs
*/
PyObject*
PyGcc_permerror(PyObject *self, PyObject *args)
{
PyGccLocation *loc_obj = NULL;
const char *msg = NULL;
PyObject *result_obj = NULL;
bool result_b;
if (!PyArg_ParseTuple(args,
"O!"
"s"
":permerror",
&PyGccLocation_TypeObj, &loc_obj,
&msg)) {
return NULL;
}
/* Invoke the GCC function: */
result_b = gcc_permerror(loc_obj->loc, msg);
result_obj = PyBool_FromLong(result_b);
return result_obj;
}
PyObject *
PyGcc_error(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyGccLocation *loc_obj;
const char *msg;
const char *keywords[] = {"location",
"message",
NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O!s:error", (char**)keywords,
&PyGccLocation_TypeObj, &loc_obj,
&msg)) {
return NULL;
}
gcc_error_at(loc_obj->loc, msg);
Py_RETURN_NONE;
}
PyObject *
PyGcc_warning(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyGccLocation *loc_obj;
const char *msg;
PyObject *opt_obj = &_Py_NoneStruct;
int opt_code;
const char *keywords[] = {"location",
"message",
"option",
NULL};
bool was_reported;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O!s|O:warning", (char**)keywords,
/* code "O!": */
&PyGccLocation_TypeObj, &loc_obj,
/* code: "s": */
&msg,
/* optional args: */
/* code: "O": */
&opt_obj)) {
return NULL;
}
assert(opt_obj);
/* If a gcc.Option was given, extract the code: */
if (Py_TYPE(opt_obj) == (PyTypeObject*)&PyGccOption_TypeObj) {
opt_code = ((PyGccOption*)opt_obj)->opt.inner;
/* Ugly workaround; see this function: */
if (0 == PyGcc_option_is_enabled((enum opt_code)opt_code)) {
return PyBool_FromLong(0);
}
} else {
if (opt_obj == &_Py_NoneStruct) {
/* No gcc.Option given: an unconditionally enabled warning: */
opt_code = 0;
} else {
/* Some other object was given: */
return PyErr_Format(PyExc_TypeError,
("option must be either None,"
" or of type gcc.Option"));
}
}
was_reported = warning_at(loc_obj->loc.inner, opt_code, "%s", msg);
return PyBool_FromLong(was_reported);
}
PyObject *
PyGcc_inform(PyObject *self, PyObject *args, PyObject *kwargs)
{
const char *msg;
const char *keywords[] = {"location",
"message",
NULL};
#if (GCC_VERSION >= 6000)
PyObject *obj;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"Os:inform", (char**)keywords,
&obj, &msg)) {
return NULL;
}
/* Check for location vs rich_location. */
if (Py_TYPE (obj) == (PyTypeObject *)&PyGccLocation_TypeObj) {
PyGccLocation *loc_obj = (PyGccLocation *)obj;
gcc_inform(loc_obj->loc, msg);
Py_RETURN_NONE;
} else if (Py_TYPE (obj) == (PyTypeObject *)&PyGccRichLocation_TypeObj) {
PyGccRichLocation *richloc_obj = (PyGccRichLocation *)obj;
#if (GCC_VERSION >= 8000)
inform (&richloc_obj->richloc, "%s", msg);
#else
inform_at_rich_loc (&richloc_obj->richloc, "%s", msg);
#endif
Py_RETURN_NONE;
} else {
return PyErr_Format(PyExc_TypeError,
("type of location must be either gcc.Location"
" or gcc.RichLocation"));
}
#else
PyGccLocation *loc_obj;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O!s:inform", (char**)keywords,
&PyGccLocation_TypeObj, &loc_obj,
&msg)) {
return NULL;
}
gcc_inform(loc_obj->loc, msg);
Py_RETURN_NONE;
#endif
}
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/