[cfe-commits] r143322 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/test_location.py

Tobias Grosser grosser at fim.uni-passau.de
Sun Oct 30 17:31:33 PDT 2011


Author: grosser
Date: Sun Oct 30 19:31:32 2011
New Revision: 143322

URL: http://llvm.org/viewvc/llvm-project?rev=143322&view=rev
Log:
cindex.py: Allow to create a cursor from file/row/column

We add a constructor to create a SourceLocation from a position in
a file and we use this SourceLocation to retrieve a cursor.

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=143322&r1=143321&r2=143322&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sun Oct 30 19:31:32 2011
@@ -115,6 +115,14 @@
             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 @@
     """
     _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 @@
                                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]

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=143322&r1=143321&r2=143322&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_location.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_location.py Sun Oct 30 19:31:32 2011
@@ -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 @@
         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)])





More information about the cfe-commits mailing list