forked from davidmalcolm/gcc-python-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcc-python-location.c
299 lines (249 loc) · 7.77 KB
/
gcc-python-location.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
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 "gcc-c-api/gcc-location.h"
/*
Wrapper for GCC's "location_t"
GCC's input.h has:
typedef source_location location_t;
GCC's line-map.h has:
A logical line/column number, i.e. an "index" into a line_map:
typedef unsigned int source_location;
*/
#if (GCC_VERSION >= 7000)
int
PyGccLocation_init(PyGccLocation *self, PyObject *args, PyObject *kwargs)
{
const char *keywords[] = {"caret", "start", "finish",
NULL};
PyGccLocation *caret_obj;
PyGccLocation *start_obj;
PyGccLocation *finish_obj;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O!O!O!", (char**)keywords,
&PyGccLocation_TypeObj, &caret_obj,
&PyGccLocation_TypeObj, &start_obj,
&PyGccLocation_TypeObj, &finish_obj)) {
return -1;
}
self->loc
= gcc_private_make_location (make_location (caret_obj->loc.inner,
start_obj->loc.inner,
finish_obj->loc.inner));
return 0;
}
#endif
PyObject *
PyGccLocation_repr(struct PyGccLocation * self)
{
return PyGccString_FromFormat("gcc.Location(file='%s', line=%i)",
gcc_location_get_filename(self->loc),
gcc_location_get_line(self->loc));
}
PyObject *
PyGccLocation_str(struct PyGccLocation * self)
{
return PyGccString_FromFormat("%s:%i",
gcc_location_get_filename(self->loc),
gcc_location_get_line(self->loc));
}
PyObject *
PyGccLocation_richcompare(PyObject *o1, PyObject *o2, int op)
{
struct PyGccLocation *locobj1;
struct PyGccLocation *locobj2;
int cond;
PyObject *result_obj;
const char *file1;
const char *file2;
if (Py_TYPE(o1) != (PyTypeObject*)&PyGccLocation_TypeObj) {
result_obj = Py_NotImplemented;
goto out;
}
if (Py_TYPE(o2) != (PyTypeObject*)&PyGccLocation_TypeObj) {
result_obj = Py_NotImplemented;
goto out;
}
locobj1 = (struct PyGccLocation *)o1;
locobj2 = (struct PyGccLocation *)o2;
/* First compare by filename, then by line, then by column */
file1 = gcc_location_get_filename(locobj1->loc);
file2 = gcc_location_get_filename(locobj2->loc);
if (file1 != file2) {
/* Compare by file: */
switch (op) {
case Py_LT:
case Py_LE:
/* we merge the LT and LE cases since we've already
established that the values are not equal */
cond = (strcmp(file1, file2) < 0);
break;
case Py_GT:
case Py_GE:
cond = (strcmp(file1, file2) > 0);
break;
case Py_EQ:
cond = 0;
break;
case Py_NE:
cond = 1;
break;
default:
result_obj = Py_NotImplemented;
goto out;
}
} else {
/* File equality; compare by line: */
int line1 = gcc_location_get_line(locobj1->loc);
int line2 = gcc_location_get_line(locobj2->loc);
if (line1 != line2) {
switch (op) {
case Py_LT:
case Py_LE:
cond = (line1 < line2);
break;
case Py_GT:
case Py_GE:
cond = (line1 > line2);
break;
case Py_EQ:
cond = 0;
break;
case Py_NE:
cond = 1;
break;
default:
result_obj = Py_NotImplemented;
goto out;
}
} else {
/* File and line equality; compare by column: */
int col1 = gcc_location_get_column(locobj1->loc);
int col2 = gcc_location_get_column(locobj2->loc);
switch (op) {
case Py_LT:
case Py_LE:
cond = (col1 < col2);
break;
case Py_GT:
case Py_GE:
cond = (col1 > col2);
break;
case Py_EQ:
cond = (col1 == col2);
break;
case Py_NE:
cond = (col1 != col2);
break;
default:
result_obj = Py_NotImplemented;
goto out;
}
}
}
result_obj = cond ? Py_True : Py_False;
out:
Py_INCREF(result_obj);
return result_obj;
}
long
PyGccLocation_hash(struct PyGccLocation * self)
{
return (unsigned long)(LOCATION_FILE(self->loc.inner)) ^ LOCATION_LINE(self->loc.inner);
}
#if (GCC_VERSION >= 5000)
PyObject *
PyGccLocation_offset_column(PyGccLocation *self, PyObject *args)
{
int offset;
if (!PyArg_ParseTuple(args, "i", &offset)) {
return NULL;
}
return PyGccLocation_New(gcc_location_offset_column(self->loc, offset));
}
#endif /* #if (GCC_VERSION >= 5000) */
PyObject *
PyGccLocation_New(gcc_location loc)
{
struct PyGccLocation *location_obj = NULL;
if (gcc_location_is_unknown(loc)) {
Py_RETURN_NONE;
}
location_obj = PyGccWrapper_New(struct PyGccLocation,
&PyGccLocation_TypeObj);
if (!location_obj) {
goto error;
}
location_obj->loc = loc;
return (PyObject*)location_obj;
error:
return NULL;
}
void
PyGcc_WrtpMarkForPyGccLocation(PyGccLocation *wrapper)
{
/* empty */
}
/* rich_location. */
#if (GCC_VERSION >= 6000)
PyObject *
PyGccRichLocation_add_fixit_replace(PyGccRichLocation *self, PyObject *args,
PyObject *kwargs)
{
const char *keywords[] = {"new_content",
NULL};
const char *new_content;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"s", (char**)keywords,
&new_content)) {
return NULL;
}
self->richloc.add_fixit_replace (get_range_from_loc (line_table,
self->richloc.get_loc (0)),
new_content);
Py_RETURN_NONE;
}
int
PyGccRichLocation_init(PyGccRichLocation *self, PyObject *args,
PyObject *kwargs)
{
const char *keywords[] = {"location",
NULL};
PyGccLocation *loc_obj;
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O!", (char**)keywords,
&PyGccLocation_TypeObj, &loc_obj)) {
return -1;
}
// FIXME: also need a manual dtor call
new (&self->richloc) rich_location (line_table, loc_obj->loc.inner);
return 0;
}
void
PyGcc_WrtpMarkForPyGccRichLocation(PyGccRichLocation *wrapper)
{
/* empty */
}
#endif /* #if (GCC_VERSION >= 6000) */
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/