Skip to content

Commit 301dab2

Browse files
authored
Merge pull request #114 from davidmalcolm/fixing-before-gcc-7
Fix build problems with gcc < 7
2 parents 257ad36 + 10f197f commit 301dab2

File tree

6 files changed

+81
-16
lines changed

6 files changed

+81
-16
lines changed

gcc-c-api/gcc-location.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ GCC_IMPLEMENT_PUBLIC_API (bool) gcc_location_get_in_system_header (gcc_location
6262
return in_system_header_at (loc.inner);
6363
}
6464

65+
/* get_pure_location and get_finish were added to gcc's input.h in
66+
gcc's r238792 (aka f17776ffcba650feb512137e3e22a04f3f433c84).
67+
get_start was added to gcc's input.h in gcc's r239831
68+
(aka aca2a315073c72fb7c9ab1be779c290cc91f564c).
69+
I believe that makes them available in gcc 7 onwards. */
70+
71+
#if (GCC_VERSION >= 7000)
72+
6573
GCC_IMPLEMENT_PUBLIC_API (gcc_location)
6674
gcc_location_get_caret (gcc_location loc)
6775
{
@@ -80,6 +88,14 @@ gcc_location_get_finish (gcc_location loc)
8088
return gcc_private_make_location (get_finish (loc.inner));
8189
}
8290

91+
#endif
92+
93+
/* linemap_position_for_loc_and_offset was added in gcc's r217383
94+
(aka 766928aa6ac2c846c2d098ef4ef9e220feb4dcab).
95+
It's present in gcc 5.1. */
96+
97+
#if (GCC_VERSION >= 5000)
98+
8399
GCC_IMPLEMENT_PUBLIC_API(gcc_location)
84100
gcc_location_offset_column (gcc_location loc, int offset)
85101
{
@@ -88,6 +104,8 @@ gcc_location_offset_column (gcc_location loc, int offset)
88104
offset));
89105
}
90106

107+
#endif
108+
91109
GCC_IMPLEMENT_PUBLIC_API (void) gcc_set_input_location (gcc_location loc)
92110
{
93111
input_location = loc.inner;

gcc-python-diagnostics.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,12 @@ PyGcc_warning(PyObject *self, PyObject *args, PyObject *kwargs)
144144
PyObject *
145145
PyGcc_inform(PyObject *self, PyObject *args, PyObject *kwargs)
146146
{
147-
PyObject *obj;
148147
const char *msg;
149148
const char *keywords[] = {"location",
150149
"message",
151150
NULL};
151+
#if (GCC_VERSION >= 6000)
152+
PyObject *obj;
152153

153154
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
154155
"Os:inform", (char**)keywords,
@@ -170,6 +171,20 @@ PyGcc_inform(PyObject *self, PyObject *args, PyObject *kwargs)
170171
("type of location must be either gcc.Location"
171172
" or gcc.RichLocation"));
172173
}
174+
#else
175+
PyGccLocation *loc_obj;
176+
177+
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
178+
"O!s:inform", (char**)keywords,
179+
&PyGccLocation_TypeObj, &loc_obj,
180+
&msg)) {
181+
return NULL;
182+
}
183+
184+
gcc_inform(loc_obj->loc, msg);
185+
186+
Py_RETURN_NONE;
187+
#endif
173188
}
174189

175190
/*

gcc-python-location.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
typedef unsigned int source_location;
3434
*/
3535

36+
#if (GCC_VERSION >= 7000)
37+
3638
int
3739
PyGccLocation_init(PyGccLocation *self, PyObject *args, PyObject *kwargs)
3840
{
@@ -58,6 +60,8 @@ PyGccLocation_init(PyGccLocation *self, PyObject *args, PyObject *kwargs)
5860
return 0;
5961
}
6062

63+
#endif
64+
6165
PyObject *
6266
PyGccLocation_repr(struct PyGccLocation * self)
6367
{
@@ -189,6 +193,8 @@ PyGccLocation_hash(struct PyGccLocation * self)
189193
return self->loc.inner;
190194
}
191195

196+
#if (GCC_VERSION >= 5000)
197+
192198
PyObject *
193199
PyGccLocation_offset_column(PyGccLocation *self, PyObject *args)
194200
{
@@ -201,6 +207,8 @@ PyGccLocation_offset_column(PyGccLocation *self, PyObject *args)
201207
return PyGccLocation_New(gcc_location_offset_column(self->loc, offset));
202208
}
203209

210+
#endif /* #if (GCC_VERSION >= 5000) */
211+
204212
PyObject *
205213
PyGccLocation_New(gcc_location loc)
206214
{
@@ -230,8 +238,11 @@ PyGcc_WrtpMarkForPyGccLocation(PyGccLocation *wrapper)
230238
/* empty */
231239
}
232240

241+
233242
/* rich_location. */
234243

244+
#if (GCC_VERSION >= 6000)
245+
235246
PyObject *
236247
PyGccRichLocation_add_fixit_replace(PyGccRichLocation *self, PyObject *args,
237248
PyObject *kwargs)
@@ -246,7 +257,9 @@ PyGccRichLocation_add_fixit_replace(PyGccRichLocation *self, PyObject *args,
246257
return NULL;
247258
}
248259

249-
self->richloc.add_fixit_replace (new_content);
260+
self->richloc.add_fixit_replace (get_range_from_loc (line_table,
261+
self->richloc.get_loc (0)),
262+
new_content);
250263

251264
Py_RETURN_NONE;
252265
}
@@ -275,6 +288,8 @@ PyGcc_WrtpMarkForPyGccRichLocation(PyGccRichLocation *wrapper)
275288
/* empty */
276289
}
277290

291+
#endif /* #if (GCC_VERSION >= 6000) */
292+
278293
/*
279294
PEP-7
280295
Local variables:

gcc-python-wrappers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ PyGccLocation_hash(struct PyGccLocation * self);
150150
PyObject *
151151
PyGccLocation_offset_column(PyGccLocation *self, PyObject *args);
152152

153+
#if (GCC_VERSION >= 6000)
154+
153155
PyObject *
154156
PyGccRichLocation_add_fixit_replace(PyGccRichLocation *self, PyObject *args,
155157
PyObject *kwargs);
@@ -158,6 +160,8 @@ int
158160
PyGccRichLocation_init(PyGccRichLocation *self, PyObject *args,
159161
PyObject *kwargs);
160162

163+
#endif
164+
161165
/* gcc-python-cfg.c: */
162166
PyObject *
163167
PyGccBasicBlock_repr(struct PyGccBasicBlock * self);

gcc-python.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,17 @@ DECLARE_SIMPLE_WRAPPER(PyGccLocation,
185185
location,
186186
gcc_location, loc)
187187

188+
/* class rich_location was added to libcpp in gcc 6. */
189+
190+
#if (GCC_VERSION >= 6000)
191+
188192
DECLARE_SIMPLE_WRAPPER(PyGccRichLocation,
189193
PyGccRichLocation_TypeObj,
190194
rich_location,
191195
rich_location, richloc)
192196

197+
#endif
198+
193199
DECLARE_SIMPLE_WRAPPER(PyGccGimple,
194200
PyGccGimple_TypeObj,
195201
gimple,

generate-location-c.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# <http://www.gnu.org/licenses/>.
1717

1818
from cpybuilder import *
19+
from testcpychecker import get_gcc_version
1920
from wrapperbuilder import PyGccWrapperTypeObject
2021

2122
cu = CompilationUnit()
@@ -61,24 +62,22 @@ def generate_location():
6162
return PyGccInt_FromLong(gcc_location_get_column(self->loc));
6263
}
6364
""")
64-
65-
cu.add_defn("""
65+
if get_gcc_version() >= 7000:
66+
cu.add_defn("""
6667
static PyObject *
6768
PyGccLocation_get_caret(struct PyGccLocation *self, void *closure)
6869
{
6970
return PyGccLocation_New(gcc_location_get_caret(self->loc));
7071
}
7172
""")
72-
73-
cu.add_defn("""
73+
cu.add_defn("""
7474
static PyObject *
7575
PyGccLocation_get_start(struct PyGccLocation *self, void *closure)
7676
{
7777
return PyGccLocation_New(gcc_location_get_start(self->loc));
7878
}
7979
""")
80-
81-
cu.add_defn("""
80+
cu.add_defn("""
8281
static PyObject *
8382
PyGccLocation_get_finish(struct PyGccLocation *self, void *closure)
8483
{
@@ -90,31 +89,33 @@ def generate_location():
9089
[PyGetSetDef('file', 'PyGccLocation_get_file', None, 'Name of the source file'),
9190
PyGetSetDef('line', 'PyGccLocation_get_line', None, 'Line number within source file'),
9291
PyGetSetDef('column', 'PyGccLocation_get_column', None, 'Column number within source file'),
93-
PyGetSetDef('caret', 'PyGccLocation_get_caret', None, 'Location of caret'),
94-
PyGetSetDef('start', 'PyGccLocation_get_start', None, 'Starting location of range'),
95-
PyGetSetDef('finish', 'PyGccLocation_get_finish', None, 'End location of range'),
9692
],
9793
identifier_prefix='PyGccLocation',
9894
typename='PyGccLocation')
95+
if get_gcc_version() >= 7000:
96+
getsettable.gsdefs += [PyGetSetDef('caret', 'PyGccLocation_get_caret', None, 'Location of caret'),
97+
PyGetSetDef('start', 'PyGccLocation_get_start', None, 'Starting location of range'),
98+
PyGetSetDef('finish', 'PyGccLocation_get_finish', None, 'End location of range')]
9999
getsettable.add_simple_getter(cu,
100100
'in_system_header',
101101
'PyBool_FromLong(gcc_location_get_in_system_header(self->loc))',
102102
'Boolean: is this location within a system header?')
103103
cu.add_defn(getsettable.c_defn())
104104

105105
methods = PyMethodTable('PyGccLocation_methods', [])
106-
methods.add_method('offset_column',
107-
'(PyCFunction)PyGccLocation_offset_column',
108-
'METH_VARARGS',
109-
"")
106+
if get_gcc_version() >= 5000:
107+
methods.add_method('offset_column',
108+
'(PyCFunction)PyGccLocation_offset_column',
109+
'METH_VARARGS',
110+
"")
110111
cu.add_defn(methods.c_defn())
111112

112113
pytype = PyGccWrapperTypeObject(identifier = 'PyGccLocation_TypeObj',
113114
localname = 'Location',
114115
tp_name = 'gcc.Location',
115116
struct_name = 'PyGccLocation',
116117
tp_new = 'PyType_GenericNew',
117-
tp_init = '(initproc)PyGccLocation_init',
118+
tp_init = '(initproc)PyGccLocation_init' if get_gcc_version() >= 7000 else None,
118119
tp_getset = getsettable.identifier,
119120
tp_hash = '(hashfunc)PyGccLocation_hash',
120121
tp_repr = '(reprfunc)PyGccLocation_repr',
@@ -130,6 +131,12 @@ def generate_rich_location():
130131
#
131132
# Generate the gcc.RichLocation class:
132133
#
134+
135+
# class rich_location was added to libcpp in gcc 6.
136+
GCC_VERSION = get_gcc_version()
137+
if GCC_VERSION < 6000:
138+
return
139+
133140
global modinit_preinit
134141
global modinit_postinit
135142

0 commit comments

Comments
 (0)