diff options
Diffstat (limited to 'bindings/python')
-rw-r--r-- | bindings/python/clang/cindex.py | 16 | ||||
-rw-r--r-- | bindings/python/tests/cindex/test_location.py | 14 |
2 files changed, 29 insertions, 1 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index c66e3563c9..9ab9ae8a17 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -115,6 +115,14 @@ class SourceLocation(Structure): self._data = (f, int(l.value), int(c.value), int(o.value)) return self._data + @staticmethod + def from_position(tu, file, line, column): + """ + Retrieve the source location associated with a given file/line/column in + a particular translation unit. + """ + return SourceLocation_getLocation(tu, file, line, column) + @property def file(self): """Get the file represented by this source location.""" @@ -817,6 +825,10 @@ class Cursor(Structure): """ _fields_ = [("_kind_id", c_int), ("xdata", c_int), ("data", c_void_p * 3)] + @staticmethod + def from_location(tu, location): + return Cursor_get(tu, location) + def __eq__(self, other): return Cursor_eq(self, other) @@ -1580,6 +1592,10 @@ SourceLocation_loc.argtypes = [SourceLocation, POINTER(c_object_p), POINTER(c_uint), POINTER(c_uint), POINTER(c_uint)] +SourceLocation_getLocation = lib.clang_getLocation +SourceLocation_getLocation.argtypes = [TranslationUnit, File, c_uint, c_uint] +SourceLocation_getLocation.restype = SourceLocation + # Source Range Functions SourceRange_getRange = lib.clang_getRange SourceRange_getRange.argtypes = [SourceLocation, SourceLocation] diff --git a/bindings/python/tests/cindex/test_location.py b/bindings/python/tests/cindex/test_location.py index 47c1c6021f..300136f0cd 100644 --- a/bindings/python/tests/cindex/test_location.py +++ b/bindings/python/tests/cindex/test_location.py @@ -1,4 +1,4 @@ -from clang.cindex import Index +from clang.cindex import Index, File, SourceLocation, Cursor baseInput="int one;\nint two;\n" @@ -35,6 +35,18 @@ def test_location(): if n.spelling == 'two': assert_location(n.location,line=2,column=5,offset=14) + # define the expected location ourselves and see if it matches + # the returned location + tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)]) + + file = File.from_name(tu, 't.c') + location = SourceLocation.from_position(tu, file, 1, 5) + cursor = Cursor.from_location(tu, location) + + for n in tu.cursor.get_children(): + if n.spelling == 'one': + assert n == cursor + def test_extent(): index = Index.create() tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)]) |