Skip to content

Commit 43900d3

Browse files
committed
Add gcc.Location.offset_column
1 parent e0659a0 commit 43900d3

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

docs/locations.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ Locations
6060
if decl.location.in_system_header:
6161
return
6262

63+
.. py:method:: offset_column(self, offset)
64+
65+
Generate a new :py:class:`gcc.Location` based on the caret location
66+
of this location, offsetting the column by the given amount.
67+
6368
From GCC 6 onwards, these values can represent both a caret and a range,
6469
e.g.::
6570

gcc-c-api/gcc-location.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ gcc_location_get_finish (gcc_location loc)
8080
return gcc_private_make_location (get_finish (loc.inner));
8181
}
8282

83+
GCC_IMPLEMENT_PUBLIC_API(gcc_location)
84+
gcc_location_offset_column (gcc_location loc, int offset)
85+
{
86+
return gcc_private_make_location
87+
(linemap_position_for_loc_and_offset (line_table, loc.inner,
88+
offset));
89+
}
90+
8391
GCC_IMPLEMENT_PUBLIC_API (void) gcc_set_input_location (gcc_location loc)
8492
{
8593
input_location = loc.inner;

gcc-c-api/location.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
</attribute>
4444
<attribute name="finish" kind="location">
4545
</attribute>
46+
47+
<function name="offset_column" returntype="location">
48+
<parameter name="offset" type="int"/>
49+
</function>
50+
4651
</type>
4752

4853
<attribute name="input_location" kind="location" access="rw">

gcc-python-location.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ PyGccLocation_hash(struct PyGccLocation * self)
164164
return self->loc.inner;
165165
}
166166

167+
PyObject *
168+
PyGccLocation_offset_column(PyGccLocation *self, PyObject *args)
169+
{
170+
int offset;
171+
172+
if (!PyArg_ParseTuple(args, "i", &offset)) {
173+
return NULL;
174+
}
175+
176+
return PyGccLocation_New(gcc_location_offset_column(self->loc, offset));
177+
}
178+
167179
PyObject *
168180
PyGccLocation_New(gcc_location loc)
169181
{

gcc-python-wrappers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ PyGccLocation_richcompare(PyObject *o1, PyObject *o2, int op);
144144
long
145145
PyGccLocation_hash(struct PyGccLocation * self);
146146

147+
PyObject *
148+
PyGccLocation_offset_column(PyGccLocation *self, PyObject *args);
149+
147150
PyObject *
148151
PyGccRichLocation_add_fixit_replace(PyGccRichLocation *self, PyObject *args,
149152
PyObject *kwargs);

generate-location-c.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ def generate_location():
102102
'Boolean: is this location within a system header?')
103103
cu.add_defn(getsettable.c_defn())
104104

105+
methods = PyMethodTable('PyGccLocation_methods', [])
106+
methods.add_method('offset_column',
107+
'(PyCFunction)PyGccLocation_offset_column',
108+
'METH_VARARGS',
109+
"")
110+
cu.add_defn(methods.c_defn())
111+
105112
pytype = PyGccWrapperTypeObject(identifier = 'PyGccLocation_TypeObj',
106113
localname = 'Location',
107114
tp_name = 'gcc.Location',
@@ -111,6 +118,7 @@ def generate_location():
111118
tp_hash = '(hashfunc)PyGccLocation_hash',
112119
tp_repr = '(reprfunc)PyGccLocation_repr',
113120
tp_str = '(reprfunc)PyGccLocation_str',
121+
tp_methods = methods.identifier,
114122
tp_richcompare = 'PyGccLocation_richcompare',
115123
tp_dealloc = 'PyGccWrapper_Dealloc')
116124
cu.add_defn(pytype.c_defn())

0 commit comments

Comments
 (0)