]> granicus.if.org Git - clang/commitdiff
[clang.py] Implement SourceLocation.from_offset
authorGregory Szorc <gregory.szorc@gmail.com>
Mon, 11 Jun 2012 11:11:48 +0000 (11:11 +0000)
committerGregory Szorc <gregory.szorc@gmail.com>
Mon, 11 Jun 2012 11:11:48 +0000 (11:11 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158307 91177308-0d34-0410-b5e6-96231b3b80d8

bindings/python/clang/cindex.py
bindings/python/tests/cindex/test_location.py

index 7a637112a25179d9a45a65c12fe38fd7e0445542..02cec2d796e32980502383da2e4806cdfa2195e1 100644 (file)
@@ -169,6 +169,16 @@ class SourceLocation(Structure):
         """
         return SourceLocation_getLocation(tu, file, line, column)
 
+    @staticmethod
+    def from_offset(tu, file, offset):
+        """Retrieve a SourceLocation from a given character offset.
+
+        tu -- TranslationUnit file belongs to
+        file -- File instance to obtain offset from
+        offset -- Integer character offset within file
+        """
+        return SourceLocation_getLocationForOffset(tu, file, offset)
+
     @property
     def file(self):
         """Get the file represented by this source location."""
@@ -2110,6 +2120,10 @@ SourceLocation_equalLocations = lib.clang_equalLocations
 SourceLocation_equalLocations.argtypes = [SourceLocation, SourceLocation]
 SourceLocation_equalLocations.restype = bool
 
+SourceLocation_getLocationForOffset = lib.clang_getLocationForOffset
+SourceLocation_getLocationForOffset.argtypes = [TranslationUnit, File, c_uint]
+SourceLocation_getLocationForOffset.restype = SourceLocation
+
 # Source Range Functions
 SourceRange_getRange = lib.clang_getRange
 SourceRange_getRange.argtypes = [SourceLocation, SourceLocation]
index 528676ef14b57f0a06a213748a480ca6f9d6da57..9e9ef487af18e03f73c8e1de0403cf906e14f2d6 100644 (file)
@@ -60,6 +60,15 @@ def test_location():
     location3 = SourceLocation.from_position(tu, file, 1, 4)
     assert location2 != location3
 
+    offset_location = SourceLocation.from_offset(tu, file, 5)
+    cursor = Cursor.from_location(tu, offset_location)
+    verified = False
+    for n in [n for n in tu.cursor.get_children() if n.spelling == 'one']:
+        assert n == cursor
+        verified = True
+
+    assert verified
+
 def test_extent():
     tu = get_tu(baseInput)
     one = get_cursor(tu, 'one')