r304978 - [libclang] Expose typedef and address space functions

Sven van Haastregt via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 8 07:22:04 PDT 2017


Author: svenvh
Date: Thu Jun  8 09:22:04 2017
New Revision: 304978

URL: http://llvm.org/viewvc/llvm-project?rev=304978&view=rev
Log:
[libclang] Expose typedef and address space functions

Expose the following functions:
 - clang_getTypedefName
 - clang_getAddressSpace

Patch by Simon Perretta.

Differential Revision: https://reviews.llvm.org/D33598

Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_type.py
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/libclang/CXType.cpp
    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=304978&r1=304977&r2=304978&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Thu Jun  8 09:22:04 2017
@@ -2162,6 +2162,12 @@ class Type(Structure):
 
         return conf.lib.clang_isFunctionTypeVariadic(self)
 
+    def get_address_space(self):
+        return conf.lib.clang_getAddressSpace(self)
+
+    def get_typedef_name(self):
+        return conf.lib.clang_getTypedefName(self)
+
     def is_pod(self):
         """Determine whether this Type represents plain old data (POD)."""
         return conf.lib.clang_isPODType(self)
@@ -3665,6 +3671,11 @@ functionList = [
    Type,
    Type.from_result),
 
+  ("clang_getTypedefName",
+   [Type],
+   _CXString,
+   _CXString.from_result),
+
   ("clang_getTypeKindSpelling",
    [c_uint],
    _CXString,

Modified: cfe/trunk/bindings/python/tests/cindex/test_type.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_type.py?rev=304978&r1=304977&r2=304978&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_type.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_type.py Thu Jun  8 09:22:04 2017
@@ -37,37 +37,44 @@ def test_a_struct():
     assert not fields[0].type.is_const_qualified()
     assert fields[0].type.kind == TypeKind.INT
     assert fields[0].type.get_canonical().kind == TypeKind.INT
+    assert fields[0].type.get_typedef_name() == ''
 
     assert fields[1].spelling == 'b'
     assert not fields[1].type.is_const_qualified()
     assert fields[1].type.kind == TypeKind.TYPEDEF
     assert fields[1].type.get_canonical().kind == TypeKind.INT
     assert fields[1].type.get_declaration().spelling == 'I'
+    assert fields[1].type.get_typedef_name() == 'I'
 
     assert fields[2].spelling == 'c'
     assert not fields[2].type.is_const_qualified()
     assert fields[2].type.kind == TypeKind.LONG
     assert fields[2].type.get_canonical().kind == TypeKind.LONG
+    assert fields[2].type.get_typedef_name() == ''
 
     assert fields[3].spelling == 'd'
     assert not fields[3].type.is_const_qualified()
     assert fields[3].type.kind == TypeKind.ULONG
     assert fields[3].type.get_canonical().kind == TypeKind.ULONG
+    assert fields[3].type.get_typedef_name() == ''
 
     assert fields[4].spelling == 'e'
     assert not fields[4].type.is_const_qualified()
     assert fields[4].type.kind == TypeKind.LONG
     assert fields[4].type.get_canonical().kind == TypeKind.LONG
+    assert fields[4].type.get_typedef_name() == ''
 
     assert fields[5].spelling == 'f'
     assert fields[5].type.is_const_qualified()
     assert fields[5].type.kind == TypeKind.INT
     assert fields[5].type.get_canonical().kind == TypeKind.INT
+    assert fields[5].type.get_typedef_name() == ''
 
     assert fields[6].spelling == 'g'
     assert not fields[6].type.is_const_qualified()
     assert fields[6].type.kind == TypeKind.POINTER
     assert fields[6].type.get_pointee().kind == TypeKind.INT
+    assert fields[6].type.get_typedef_name() == ''
 
     assert fields[7].spelling == 'h'
     assert not fields[7].type.is_const_qualified()
@@ -75,6 +82,7 @@ def test_a_struct():
     assert fields[7].type.get_pointee().kind == TypeKind.POINTER
     assert fields[7].type.get_pointee().get_pointee().kind == TypeKind.POINTER
     assert fields[7].type.get_pointee().get_pointee().get_pointee().kind == TypeKind.INT
+    assert fields[7].type.get_typedef_name() == ''
 
 def test_references():
     """Ensure that a Type maintains a reference to a TranslationUnit."""
@@ -404,3 +412,12 @@ def test_decay():
     assert a.kind == TypeKind.INCOMPLETEARRAY
     assert a.element_type.kind == TypeKind.INT
     assert a.get_canonical().kind == TypeKind.INCOMPLETEARRAY
+
+def test_addrspace():
+    """Ensure the address space can be queried"""
+    tu = get_tu('__attribute__((address_space(2))) int testInteger = 3;', 'c')
+
+    testInteger = get_cursor(tu, 'testInteger')
+
+    assert testInteger is not None, "Could not find testInteger."
+    assert testInteger.type.get_address_space() == 2

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=304978&r1=304977&r2=304978&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Jun  8 09:22:04 2017
@@ -32,7 +32,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 41
+#define CINDEX_VERSION_MINOR 42
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \
@@ -3417,6 +3417,16 @@ CINDEX_LINKAGE unsigned clang_isVolatile
 CINDEX_LINKAGE unsigned clang_isRestrictQualifiedType(CXType T);
 
 /**
+ * \brief Returns the address space of the given type.
+ */
+CINDEX_LINKAGE unsigned clang_getAddressSpace(CXType T);
+
+/**
+ * \brief Returns the typedef name of the given type.
+ */
+CINDEX_LINKAGE CXString clang_getTypedefName(CXType CT);
+
+/**
  * \brief For pointer types, returns the type of the pointee.
  */
 CINDEX_LINKAGE CXType clang_getPointeeType(CXType T);

Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=304978&r1=304977&r2=304978&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Thu Jun  8 09:22:04 2017
@@ -21,6 +21,7 @@
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/Type.h"
+#include "clang/Basic/AddressSpaces.h"
 #include "clang/Frontend/ASTUnit.h"
 
 using namespace clang;
@@ -394,6 +395,27 @@ unsigned clang_isRestrictQualifiedType(C
   return T.isLocalRestrictQualified();
 }
 
+unsigned clang_getAddressSpace(CXType CT) {
+  QualType T = GetQualType(CT);
+
+  // For non language-specific address space, use separate helper function.
+  if (T.getAddressSpace() >= LangAS::FirstTargetAddressSpace) {
+    return T.getQualifiers().getAddressSpaceAttributePrintValue();
+  }
+  return T.getAddressSpace();
+}
+
+CXString clang_getTypedefName(CXType CT) {
+  QualType T = GetQualType(CT);
+  const TypedefType *TT = T->getAs<TypedefType>();
+  if (TT) {
+    TypedefNameDecl *TD = TT->getDecl();
+    if (TD)
+      return cxstring::createDup(TD->getNameAsString().c_str());
+  }
+  return cxstring::createEmpty();
+}
+
 CXType clang_getPointeeType(CXType CT) {
   QualType T = GetQualType(CT);
   const Type *TP = T.getTypePtrOrNull();

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=304978&r1=304977&r2=304978&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Thu Jun  8 09:22:04 2017
@@ -147,6 +147,7 @@ clang_findReferencesInFile
 clang_findReferencesInFileWithBlock
 clang_formatDiagnostic
 clang_free
+clang_getAddressSpace
 clang_getAllSkippedRanges
 clang_getArgType
 clang_getArrayElementType
@@ -259,6 +260,7 @@ clang_getTypeDeclaration
 clang_getTypeKindSpelling
 clang_getTypeSpelling
 clang_getTypedefDeclUnderlyingType
+clang_getTypedefName
 clang_hashCursor
 clang_indexLoc_getCXSourceLocation
 clang_indexLoc_getFileLocation




More information about the cfe-commits mailing list