forked from davidmalcolm/gcc-python-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcc-python-location.c
203 lines (174 loc) · 5.11 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
/*
Copyright 2011, 2012, 2013 David Malcolm <[email protected]>
Copyright 2011, 2012, 2013 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;
*/
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 self->loc.inner;
}
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 */
}
/*
PEP-7
Local variables:
c-basic-offset: 4
indent-tabs-mode: nil
End:
*/