[cfe-commits] r134460 - in /cfe/trunk: bindings/python/clang/cindex.py bindings/python/tests/cindex/test_cursor_kind.py bindings/python/tests/cindex/test_translation_unit.py include/clang-c/Index.h tools/libclang/CIndex.cpp tools/libclang/libclang.darwin.exports tools/libclang/libclang.exports

Douglas Gregor dgregor at apple.com
Tue Jul 5 20:00:34 PDT 2011


Author: dgregor
Date: Tue Jul  5 22:00:34 2011
New Revision: 134460

URL: http://llvm.org/viewvc/llvm-project?rev=134460&view=rev
Log:
Improve the Python bindings for libclang in a few ways, from Eli
Bendersky. Specifically: 

* Implemented a new function in libclang: clang_isAttribute

* Fixing TranslationUnit.get_includes to only go through the argument
* buffer when it contains something. This fixed a crash on Windows 

* clang_getFileName returns CXString, not char*. Made appropriate
* fixes in cindex.py - now the relevant tests pass and we can see the
* full locations correctly again (previously there was garbage in
* place of the file name) 
* Exposed clang_getCursorDisplayName to the python bindings


Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py
    cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/libclang.darwin.exports
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=134460&r1=134459&r2=134460&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Tue Jul  5 22:00:34 2011
@@ -321,6 +321,10 @@
         """Test if this is a statement kind."""
         return CursorKind_is_stmt(self)
 
+    def is_attribute(self):
+        """Test if this is an attribute kind."""
+        return CursorKind_is_attribute(self)
+
     def is_invalid(self):
         """Test if this is an invalid kind."""
         return CursorKind_is_inv(self)
@@ -978,8 +982,9 @@
         headers.
         """
         def visitor(fobj, lptr, depth, includes):
-            loc = lptr.contents
-            includes.append(FileInclusion(loc.file, File(fobj), loc, depth))
+            if depth > 0:
+                loc = lptr.contents
+                includes.append(FileInclusion(loc.file, File(fobj), loc, depth))
 
         # Automatically adapt CIndex/ctype pointers to python objects
         includes = []
@@ -1074,7 +1079,7 @@
     @property
     def name(self):
         """Return the complete file and path name of the file."""
-        return File_name(self)
+        return _CXString_getCString(File_name(self))
 
     @property
     def time(self):
@@ -1147,6 +1152,10 @@
 CursorKind_is_stmt.argtypes = [CursorKind]
 CursorKind_is_stmt.restype = bool
 
+CursorKind_is_attribute = lib.clang_isAttribute
+CursorKind_is_attribute.argtypes = [CursorKind]
+CursorKind_is_attribute.restype = bool
+
 CursorKind_is_inv = lib.clang_isInvalid
 CursorKind_is_inv.argtypes = [CursorKind]
 CursorKind_is_inv.restype = bool
@@ -1183,6 +1192,11 @@
 Cursor_spelling.restype = _CXString
 Cursor_spelling.errcheck = _CXString.from_result
 
+Cursor_displayname = lib.clang_getCursorDisplayName
+Cursor_displayname.argtypes = [Cursor]
+Cursor_displayname.restype = _CXString
+Cursor_displayname.errcheck = _CXString.from_result
+
 Cursor_loc = lib.clang_getCursorLocation
 Cursor_loc.argtypes = [Cursor]
 Cursor_loc.restype = SourceLocation
@@ -1253,7 +1267,7 @@
 # File Functions
 File_name = lib.clang_getFileName
 File_name.argtypes = [File]
-File_name.restype = c_char_p
+File_name.restype = _CXString
 
 File_time = lib.clang_getFileTime
 File_time.argtypes = [File]

Modified: cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py?rev=134460&r1=134459&r2=134460&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_cursor_kind.py Tue Jul  5 22:00:34 2011
@@ -18,10 +18,14 @@
 
     for k in CursorKind.get_all_kinds():
         group = [n for n in ('is_declaration', 'is_reference', 'is_expression',
-                             'is_statement', 'is_invalid')
+                             'is_statement', 'is_invalid', 'is_attribute')
                  if getattr(k, n)()]
 
-        if k == CursorKind.TRANSLATION_UNIT:
+        if k in (   CursorKind.TRANSLATION_UNIT,
+                    CursorKind.MACRO_DEFINITION,
+                    CursorKind.MACRO_INSTANTIATION,
+                    CursorKind.INCLUSION_DIRECTIVE,
+                    CursorKind.PREPROCESSING_DIRECTIVE):
             assert len(group) == 0
         else:
             assert len(group) == 1

Modified: cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py?rev=134460&r1=134459&r2=134460&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py Tue Jul  5 22:00:34 2011
@@ -58,24 +58,27 @@
     spellings = [c.spelling for c in tu.cursor.get_children()]
     assert spellings[-1] == 'x'
 
+def normpaths_equal(path1, path2):
+    """ Compares two paths for equality after normalizing them with
+        os.path.normpath
+    """
+    return os.path.normpath(path1) == os.path.normpath(path2)
 
 def test_includes():
     def eq(expected, actual):
         if not actual.is_input_file:
-            return expected[0] == actual.source.name and \
-                   expected[1] == actual.include.name
+            return  normpaths_equal(expected[0], actual.source.name) and \
+                    normpaths_equal(expected[1], actual.include.name)
         else:
-            return expected[1] == actual.include.name
+            return normpaths_equal(expected[1], actual.include.name)
 
     src = os.path.join(kInputsDir, 'include.cpp')
     h1 = os.path.join(kInputsDir, "header1.h")
     h2 = os.path.join(kInputsDir, "header2.h")
     h3 = os.path.join(kInputsDir, "header3.h")
-    inc = [(None, src), (src, h1), (h1, h3), (src, h2), (h2, h3)]
+    inc = [(src, h1), (h1, h3), (src, h2), (h2, h3)]
 
     index = Index.create()
     tu = index.parse(src)
     for i in zip(inc, tu.get_includes()):
         assert eq(i[0], i[1])
-
-

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=134460&r1=134459&r2=134460&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Jul  5 22:00:34 2011
@@ -1474,6 +1474,11 @@
 CINDEX_LINKAGE unsigned clang_isStatement(enum CXCursorKind);
 
 /**
+ * \brief Determine whether the given cursor kind represents an attribute.
+ */
+CINDEX_LINKAGE unsigned clang_isAttribute(enum CXCursorKind);
+
+/**
  * \brief Determine whether the given cursor kind represents an invalid
  * cursor.
  */

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=134460&r1=134459&r2=134460&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Jul  5 22:00:34 2011
@@ -3562,6 +3562,10 @@
   return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
 }
 
+unsigned clang_isAttribute(enum CXCursorKind K) {
+    return K >= CXCursor_FirstAttr && K <= CXCursor_LastAttr;
+}
+
 unsigned clang_isTranslationUnit(enum CXCursorKind K) {
   return K == CXCursor_TranslationUnit;
 }

Modified: cfe/trunk/tools/libclang/libclang.darwin.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.darwin.exports?rev=134460&r1=134459&r2=134460&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.darwin.exports (original)
+++ cfe/trunk/tools/libclang/libclang.darwin.exports Tue Jul  5 22:00:34 2011
@@ -112,6 +112,7 @@
 _clang_getTypeDeclaration
 _clang_getTypeKindSpelling
 _clang_hashCursor
+_clang_isAttribute
 _clang_isConstQualifiedType
 _clang_isCursorDefinition
 _clang_isDeclaration

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=134460&r1=134459&r2=134460&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Tue Jul  5 22:00:34 2011
@@ -112,6 +112,7 @@
 clang_getTypeDeclaration
 clang_getTypeKindSpelling
 clang_hashCursor
+clang_isAttribute
 clang_isConstQualifiedType
 clang_isCursorDefinition
 clang_isDeclaration





More information about the cfe-commits mailing list