[cfe-commits] r158307 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/test_location.py
Gregory Szorc
gregory.szorc at gmail.com
Mon Jun 11 04:11:48 PDT 2012
Author: gps
Date: Mon Jun 11 06:11:48 2012
New Revision: 158307
URL: http://llvm.org/viewvc/llvm-project?rev=158307&view=rev
Log:
[clang.py] Implement SourceLocation.from_offset
Modified:
cfe/trunk/bindings/python/clang/cindex.py
cfe/trunk/bindings/python/tests/cindex/test_location.py
Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=158307&r1=158306&r2=158307&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Mon Jun 11 06:11:48 2012
@@ -169,6 +169,16 @@
"""
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.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]
Modified: cfe/trunk/bindings/python/tests/cindex/test_location.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_location.py?rev=158307&r1=158306&r2=158307&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_location.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_location.py Mon Jun 11 06:11:48 2012
@@ -60,6 +60,15 @@
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')
More information about the cfe-commits
mailing list