Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit b997435

Browse files
committed
1 parent d02b8c6 commit b997435

File tree

3 files changed

+26
-104
lines changed

3 files changed

+26
-104
lines changed

bindings/python/clang/cindex.py

+24-96
Original file line numberDiff line numberDiff line change
@@ -214,45 +214,25 @@ def from_result(res, fn=None, args=None):
214214
assert isinstance(res, _CXString)
215215
return conf.lib.clang_getCString(res)
216216

217-
class Location(object):
218-
"""A Location is a specific kind of source location. A SourceLocation
219-
refers to several kinds of locations (e.g. spelling location vs. expansion
220-
location)."""
221-
222-
def __init__(self, file, line, column, offset):
223-
self._file = File(file) if file else None
224-
self._line = int(line.value)
225-
self._column = int(column.value)
226-
self._offset = int(offset.value)
227-
228-
229-
@property
230-
def file(self):
231-
"""Get the file represented by this source location."""
232-
return self._file
233-
234-
@property
235-
def line(self):
236-
"""Get the line represented by this source location."""
237-
return self._line
238-
239-
@property
240-
def column(self):
241-
"""Get the column represented by this source location."""
242-
return self._column
243-
244-
@property
245-
def offset(self):
246-
"""Get the file offset represented by this source location."""
247-
return self._offset
248217

249218
class SourceLocation(Structure):
250219
"""
251220
A SourceLocation represents a particular location within a source file.
252221
"""
253222
_fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)]
254-
_expansion = None
255-
_spelling = None
223+
_data = None
224+
225+
def _get_instantiation(self):
226+
if self._data is None:
227+
f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint()
228+
conf.lib.clang_getInstantiationLocation(self, byref(f), byref(l),
229+
byref(c), byref(o))
230+
if f:
231+
f = File(f)
232+
else:
233+
f = None
234+
self._data = (f, int(l.value), int(c.value), int(o.value))
235+
return self._data
256236

257237
@staticmethod
258238
def from_position(tu, file, line, column):
@@ -272,73 +252,25 @@ def from_offset(tu, file, offset):
272252
"""
273253
return conf.lib.clang_getLocationForOffset(tu, file, offset)
274254

275-
@property
276-
def expansion(self):
277-
"""
278-
The source location where then entity this object is referring to is
279-
expanded.
280-
"""
281-
if not self._expansion:
282-
file = c_object_p()
283-
line = c_uint()
284-
column = c_uint()
285-
offset = c_uint()
286-
conf.lib.clang_getExpansionLocation(self,
287-
byref(file),
288-
byref(line),
289-
byref(column),
290-
byref(offset))
291-
292-
self._expansion = Location(file, line, column, offset)
293-
return self._expansion
294-
295-
@property
296-
def spelling(self):
297-
"""
298-
The source location where then entity this object is referring to is
299-
written.
300-
"""
301-
if not self._spelling:
302-
file = c_object_p()
303-
line = c_uint()
304-
column = c_uint()
305-
offset = c_uint()
306-
conf.lib.clang_getSpellingLocation(self,
307-
byref(file),
308-
byref(line),
309-
byref(column),
310-
byref(offset))
311-
312-
self._spelling = Location(file, line, column, offset)
313-
return self._spelling
314-
315255
@property
316256
def file(self):
317-
"""Get the file represented by this source location.
318-
319-
DEPRECATED: use expansion.file."""
320-
return self.expansion.file
257+
"""Get the file represented by this source location."""
258+
return self._get_instantiation()[0]
321259

322260
@property
323261
def line(self):
324-
"""Get the line represented by this source location.
325-
326-
DEPRECATED: use expansion.line."""
327-
return self.expansion.line
262+
"""Get the line represented by this source location."""
263+
return self._get_instantiation()[1]
328264

329265
@property
330266
def column(self):
331-
"""Get the column represented by this source location.
332-
333-
DEPRECATED: use expansion.column."""
334-
return self.expansion.column
267+
"""Get the column represented by this source location."""
268+
return self._get_instantiation()[2]
335269

336270
@property
337271
def offset(self):
338-
"""Get the file offset represented by this source location.
339-
340-
DEPRECATED: use expansion.offset."""
341-
return self.expansion.offset
272+
"""Get the file offset represented by this source location."""
273+
return self._get_instantiation()[3]
342274

343275
def __eq__(self, other):
344276
return conf.lib.clang_equalLocations(self, other)
@@ -1611,7 +1543,8 @@ def mangled_name(self):
16111543
@property
16121544
def location(self):
16131545
"""
1614-
Return the source locations of the entity pointed at by the cursor.
1546+
Return the source location (the starting character) of the entity
1547+
pointed at by the cursor.
16151548
"""
16161549
if not hasattr(self, '_loc'):
16171550
self._loc = conf.lib.clang_getCursorLocation(self)
@@ -3759,11 +3692,7 @@ def cursor(self):
37593692
("clang_getInclusions",
37603693
[TranslationUnit, callbacks['translation_unit_includes'], py_object]),
37613694

3762-
("clang_getExpansionLocation",
3763-
[SourceLocation, POINTER(c_object_p), POINTER(c_uint), POINTER(c_uint),
3764-
POINTER(c_uint)]),
3765-
3766-
("clang_getSpellingLocation",
3695+
("clang_getInstantiationLocation",
37673696
[SourceLocation, POINTER(c_object_p), POINTER(c_uint), POINTER(c_uint),
37683697
POINTER(c_uint)]),
37693698

@@ -4225,7 +4154,6 @@ def register_enumerations():
42254154
'FixIt',
42264155
'Index',
42274156
'LinkageKind',
4228-
'Location',
42294157
'SourceLocation',
42304158
'SourceRange',
42314159
'TLSKind',

bindings/python/tests/cindex/test_location.py

-7
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,3 @@ def test_extent():
9393
location3 = SourceLocation.from_position(tu, file, 1, 6)
9494
range3 = SourceRange.from_locations(location1, location3)
9595
assert range1 != range3
96-
97-
def test_spelling_location():
98-
tu = get_tu('''#define ONE int one
99-
ONE;''')
100-
one = get_cursor(tu, 'one')
101-
assert one.location.spelling.line == 1
102-
assert one.location.expansion.line == 2

tools/libclang/CXSourceLocation.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ void clang_getSpellingLocation(CXSourceLocation location,
318318

319319
const SourceManager &SM =
320320
*static_cast<const SourceManager*>(location.ptr_data[0]);
321-
SourceLocation SpellLoc = SM.getSpellingLoc(Loc);
321+
// FIXME: This should call SourceManager::getSpellingLoc().
322+
SourceLocation SpellLoc = SM.getFileLoc(Loc);
322323
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);
323324
FileID FID = LocInfo.first;
324325
unsigned FileOffset = LocInfo.second;

0 commit comments

Comments
 (0)