[cfe-commits] r145972 - in /cfe/trunk: include/clang-c/Index.h test/Index/asm-attribute.c test/Index/vector-types.c tools/libclang/CIndex.cpp tools/libclang/CXCursor.cpp tools/libclang/CXType.cpp tools/libclang/libclang.exports

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue Dec 6 14:05:01 PST 2011


Author: akirtzidis
Date: Tue Dec  6 16:05:01 2011
New Revision: 145972

URL: http://llvm.org/viewvc/llvm-project?rev=145972&view=rev
Log:
[libclang] API enhancements by Joe Groff!

- Exposes a CXType_Vector type kind for vector types.
- Adds generalized versions of the clang_getArrayElementType and clang_getArraySize functions, named clang_getElementType and clang_getNumElements, which work on array, vector, or complex types.
- Adds additional functions for querying function types. clang_isFunctionTypeVariadic returns true if a function type is variadic. clang_getFunctionCallingConv returns an enumeration value indicating the calling convention of the function type. clang_getNumArgTypes returns the number of static argument types, and clang_getArgType gets the type of an argument.
- Adds a clang_getTypedefDeclUnderlyingType function to get the underlying type from a TypedefDecl cursor.
- Adds a clang_getEnumDeclIntegerType function to get the integer type from an EnumDecl cursor.
- Adds clang_getEnumConstantDeclValue and clang_getEnumConstantDeclUnsignedValue functions to get the value of an EnumConstantDecl as a signed or unsigned long long, respectively.
- Exposes a CXCursor_AsmLabelAttr cursor kind for __asm__("label") attributes.
- Alters clang_getCursorSpelling to return the label value for AsmLabelAttr-kind cursors.

Added:
    cfe/trunk/test/Index/asm-attribute.c
    cfe/trunk/test/Index/vector-types.c
Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/CXCursor.cpp
    cfe/trunk/tools/libclang/CXType.cpp
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=145972&r1=145971&r2=145972&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Dec  6 16:05:01 2011
@@ -1911,7 +1911,8 @@
   CXCursor_CXXFinalAttr                  = 404,
   CXCursor_CXXOverrideAttr               = 405,
   CXCursor_AnnotateAttr                  = 406,
-  CXCursor_LastAttr                      = CXCursor_AnnotateAttr,
+  CXCursor_AsmLabelAttr                  = 407,
+  CXCursor_LastAttr                      = CXCursor_AsmLabelAttr,
      
   /* Preprocessing */
   CXCursor_PreprocessingDirective        = 500,
@@ -2383,10 +2384,29 @@
   CXType_ObjCObjectPointer = 109,
   CXType_FunctionNoProto = 110,
   CXType_FunctionProto = 111,
-  CXType_ConstantArray = 112
+  CXType_ConstantArray = 112,
+  CXType_Vector = 113
 };
 
 /**
+ * \brief Describes the calling convention of a function type
+ */
+enum CXCallingConv {
+  CXCallingConv_Default = 0,
+  CXCallingConv_C = 1,
+  CXCallingConv_X86StdCall = 2,
+  CXCallingConv_X86FastCall = 3,
+  CXCallingConv_X86ThisCall = 4,
+  CXCallingConv_X86Pascal = 5,
+  CXCallingConv_AAPCS = 6,
+  CXCallingConv_AAPCS_VFP = 7,
+
+  CXCallingConv_Invalid = 100,
+  CXCallingConv_Unexposed = 200
+};
+
+
+/**
  * \brief The type of an element in the abstract syntax tree.
  *
  */
@@ -2401,6 +2421,42 @@
 CINDEX_LINKAGE CXType clang_getCursorType(CXCursor C);
 
 /**
+ * \brief Retrieve the underlying type of a typedef declaration.
+ *
+ * If the cursor does not reference a typedef declaration, an invalid type is
+ * returned.
+ */
+CINDEX_LINKAGE CXType clang_getTypedefDeclUnderlyingType(CXCursor C);
+
+/**
+ * \brief Retrieve the integer type of an enum declaration.
+ *
+ * If the cursor does not reference an enum declaration, an invalid type is
+ * returned.
+ */
+CINDEX_LINKAGE CXType clang_getEnumDeclIntegerType(CXCursor C);
+
+/**
+ * \brief Retrieve the integer value of an enum constant declaration as a signed
+ *  long long.
+ *
+ * If the cursor does not reference an enum constant declaration, LLONG_MIN is returned.
+ * Since this is also potentially a valid constant value, the kind of the cursor
+ * must be verified before calling this function.
+ */
+CINDEX_LINKAGE long long clang_getEnumConstantDeclValue(CXCursor C);
+
+/**
+ * \brief Retrieve the integer value of an enum constant declaration as an unsigned
+ *  long long.
+ *
+ * If the cursor does not reference an enum constant declaration, ULLONG_MAX is returned.
+ * Since this is also potentially a valid constant value, the kind of the cursor
+ * must be verified before calling this function.
+ */
+CINDEX_LINKAGE unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C);
+
+/**
  * \determine Determine whether two CXTypes represent the same type.
  *
  * \returns non-zero if the CXTypes represent the same type and 
@@ -2458,13 +2514,44 @@
 CINDEX_LINKAGE CXString clang_getTypeKindSpelling(enum CXTypeKind K);
 
 /**
+ * \brief Retrieve the calling convention associated with a function type.
+ *
+ * If a non-function type is passed in, CXCallingConv_Invalid is returned.
+ */
+CINDEX_LINKAGE enum CXCallingConv clang_getFunctionTypeCallingConv(CXType T);
+
+/**
  * \brief Retrieve the result type associated with a function type.
+ *
+ * If a non-function type is passed in, an invalid type is returned.
  */
 CINDEX_LINKAGE CXType clang_getResultType(CXType T);
 
 /**
- * \brief Retrieve the result type associated with a given cursor.  This only
- *  returns a valid type of the cursor refers to a function or method.
+ * \brief Retrieve the number of non-variadic arguments associated with a function type.
+ *
+ * If a non-function type is passed in, UINT_MAX is returned.
+ */
+CINDEX_LINKAGE unsigned clang_getNumArgTypes(CXType T);
+
+/**
+ * \brief Retrieve the type of an argument of a function type.
+ *
+ * If a non-function type is passed in or the function does not have enough parameters,
+ * an invalid type is returned.
+ */
+CINDEX_LINKAGE CXType clang_getArgType(CXType T, unsigned i);
+
+/**
+ * \brief Return 1 if the CXType is a variadic function type, and 0 otherwise.
+ *
+ */
+CINDEX_LINKAGE unsigned clang_isFunctionTypeVariadic(CXType T);
+
+/**
+ * \brief Retrieve the result type associated with a given cursor.
+ *
+ * This only returns a valid type if the cursor refers to a function or method.
  */
 CINDEX_LINKAGE CXType clang_getCursorResultType(CXCursor C);
 
@@ -2475,6 +2562,22 @@
 CINDEX_LINKAGE unsigned clang_isPODType(CXType T);
 
 /**
+ * \brief Return the element type of an array, complex, or vector type.
+ *
+ * If a type is passed in that is not an array, complex, or vector type,
+ * an invalid type is returned.
+ */
+CINDEX_LINKAGE CXType clang_getElementType(CXType T);
+
+/**
+ * \brief Return the number of elements of an array or vector type.
+ *
+ * If a type is passed in that is not an array or vector type,
+ * -1 is returned.
+ */
+CINDEX_LINKAGE long long clang_getNumElements(CXType T);
+
+/**
  * \brief Return the element type of an array type.
  *
  * If a non-array type is passed in, an invalid type is returned.

Added: cfe/trunk/test/Index/asm-attribute.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/asm-attribute.c?rev=145972&view=auto
==============================================================================
--- cfe/trunk/test/Index/asm-attribute.c (added)
+++ cfe/trunk/test/Index/asm-attribute.c Tue Dec  6 16:05:01 2011
@@ -0,0 +1,6 @@
+int foo(int x) __asm__("_foo_");
+
+// RUN: c-index-test -test-load-source all %s | FileCheck %s
+// CHECK: asm-attribute.c:1:5: FunctionDecl=foo:1:5 Extent=[1:1 - 1:32]
+// FIXME: Location below.
+// CHECK: <invalid loc>:0:0: asm label=_foo_ Extent=[1:24 - 1:31]

Added: cfe/trunk/test/Index/vector-types.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/vector-types.c?rev=145972&view=auto
==============================================================================
--- cfe/trunk/test/Index/vector-types.c (added)
+++ cfe/trunk/test/Index/vector-types.c Tue Dec  6 16:05:01 2011
@@ -0,0 +1,6 @@
+int __attribute__((vector_size(16))) x;
+typedef int __attribute__((vector_size(16))) int4_t;
+
+// RUN: c-index-test -test-print-typekind %s | FileCheck %s
+// CHECK: VarDecl=x:1:38 typekind=Vector [isPOD=1]
+// CHECK: TypedefDecl=int4_t:2:46 (Definition) typekind=Typedef [canonical=Vector] [isPOD=1]

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=145972&r1=145971&r2=145972&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Dec  6 16:05:01 2011
@@ -3012,6 +3012,11 @@
     return createCXString(AA->getAnnotation());
   }
 
+  if (C.kind == CXCursor_AsmLabelAttr) {
+    AsmLabelAttr *AA = cast<AsmLabelAttr>(cxcursor::getCursorAttr(C));
+    return createCXString(AA->getLabel());
+  }
+
   return createCXString("");
 }
 
@@ -3335,6 +3340,8 @@
       return createCXString("attribute(override)");
   case CXCursor_AnnotateAttr:
     return createCXString("attribute(annotate)");
+  case CXCursor_AsmLabelAttr:
+    return createCXString("asm label");
   case CXCursor_PreprocessingDirective:
     return createCXString("preprocessing directive");
   case CXCursor_MacroDefinition:

Modified: cfe/trunk/tools/libclang/CXCursor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXCursor.cpp?rev=145972&r1=145971&r2=145972&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXCursor.cpp (original)
+++ cfe/trunk/tools/libclang/CXCursor.cpp Tue Dec  6 16:05:01 2011
@@ -46,6 +46,7 @@
     case attr::Final: return CXCursor_CXXFinalAttr;
     case attr::Override: return CXCursor_CXXOverrideAttr;
     case attr::Annotate: return CXCursor_AnnotateAttr;
+    case attr::AsmLabel: return CXCursor_AsmLabelAttr;
   }
 
   return CXCursor_UnexposedAttr;

Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=145972&r1=145971&r2=145972&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Tue Dec  6 16:05:01 2011
@@ -85,6 +85,7 @@
     TKCASE(FunctionNoProto);
     TKCASE(FunctionProto);
     TKCASE(ConstantArray);
+    TKCASE(Vector);
     default:
       return CXType_Unexposed;
   }
@@ -173,6 +174,74 @@
   return MakeCXType(QualType(), TU);
 }
 
+CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
+  using namespace cxcursor;
+  CXTranslationUnit TU = cxcursor::getCursorTU(C);
+
+  if (clang_isDeclaration(C.kind)) {
+    Decl *D = cxcursor::getCursorDecl(C);
+
+    if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
+      QualType T = TD->getUnderlyingType();
+      return MakeCXType(T, TU);
+    }
+
+    return MakeCXType(QualType(), TU);
+  }
+
+  return MakeCXType(QualType(), TU);
+}
+
+CXType clang_getEnumDeclIntegerType(CXCursor C) {
+  using namespace cxcursor;
+  CXTranslationUnit TU = cxcursor::getCursorTU(C);
+
+  if (clang_isDeclaration(C.kind)) {
+    Decl *D = cxcursor::getCursorDecl(C);
+
+    if (EnumDecl *TD = dyn_cast<EnumDecl>(D)) {
+      QualType T = TD->getIntegerType();
+      return MakeCXType(T, TU);
+    }
+
+    return MakeCXType(QualType(), TU);
+  }
+
+  return MakeCXType(QualType(), TU);
+}
+
+long long clang_getEnumConstantDeclValue(CXCursor C) {
+  using namespace cxcursor;
+
+  if (clang_isDeclaration(C.kind)) {
+    Decl *D = cxcursor::getCursorDecl(C);
+
+    if (EnumConstantDecl *TD = dyn_cast<EnumConstantDecl>(D)) {
+      return TD->getInitVal().getSExtValue();
+    }
+
+    return LLONG_MIN;
+  }
+
+  return LLONG_MIN;
+}
+
+unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C) {
+  using namespace cxcursor;
+
+  if (clang_isDeclaration(C.kind)) {
+    Decl *D = cxcursor::getCursorDecl(C);
+
+    if (EnumConstantDecl *TD = dyn_cast<EnumConstantDecl>(D)) {
+      return TD->getInitVal().getZExtValue();
+    }
+
+    return ULLONG_MAX;
+  }
+
+  return ULLONG_MAX;
+}
+
 CXType clang_getCanonicalType(CXType CT) {
   if (CT.kind == CXType_Invalid)
     return CT;
@@ -332,6 +401,7 @@
     TKIND(FunctionNoProto);
     TKIND(FunctionProto);
     TKIND(ConstantArray);
+    TKIND(Vector);
   }
 #undef TKIND
   return cxstring::createCXString(s);
@@ -341,9 +411,80 @@
   return A.data[0] == B.data[0] && A.data[1] == B.data[1];;
 }
 
+unsigned clang_isFunctionTypeVariadic(CXType X) {
+  QualType T = GetQualType(X);
+  if (T.isNull())
+    return 0;
+
+  if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>())
+    return (unsigned)FD->isVariadic();
+
+  if (T->getAs<FunctionNoProtoType>())
+    return 1;
+  
+  return 0;
+}
+
+CXCallingConv clang_getFunctionTypeCallingConv(CXType X) {
+  QualType T = GetQualType(X);
+  if (T.isNull())
+    return CXCallingConv_Invalid;
+  
+  if (const FunctionType *FD = T->getAs<FunctionType>()) {
+#define TCALLINGCONV(X) case CC_##X: return CXCallingConv_##X
+    switch (FD->getCallConv()) {
+      TCALLINGCONV(Default);
+      TCALLINGCONV(C);
+      TCALLINGCONV(X86StdCall);
+      TCALLINGCONV(X86FastCall);
+      TCALLINGCONV(X86ThisCall);
+      TCALLINGCONV(X86Pascal);
+      TCALLINGCONV(AAPCS);
+      TCALLINGCONV(AAPCS_VFP);
+    default:
+      return CXCallingConv_Unexposed;
+    }
+#undef TCALLINGCONV
+  }
+  
+  return CXCallingConv_Invalid;
+}
+
+unsigned clang_getNumArgTypes(CXType X) {
+  QualType T = GetQualType(X);
+  if (T.isNull())
+    return UINT_MAX;
+  
+  if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
+    return FD->getNumArgs();
+  }
+  
+  if (T->getAs<FunctionNoProtoType>()) {
+    return 0;
+  }
+  
+  return UINT_MAX;
+}
+
+CXType clang_getArgType(CXType X, unsigned i) {
+  QualType T = GetQualType(X);
+  if (T.isNull())
+    return MakeCXType(QualType(), GetTU(X));
+
+  if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
+    unsigned numArgs = FD->getNumArgs();
+    if (i >= numArgs)
+      return MakeCXType(QualType(), GetTU(X));
+    
+    return MakeCXType(FD->getArgType(i), GetTU(X));
+  }
+  
+  return MakeCXType(QualType(), GetTU(X));
+}
+
 CXType clang_getResultType(CXType X) {
   QualType T = GetQualType(X);
-  if (!T.getTypePtrOrNull())
+  if (T.isNull())
     return MakeCXType(QualType(), GetTU(X));
   
   if (const FunctionType *FD = T->getAs<FunctionType>())
@@ -366,7 +507,7 @@
 
 unsigned clang_isPODType(CXType X) {
   QualType T = GetQualType(X);
-  if (!T.getTypePtrOrNull())
+  if (T.isNull())
     return 0;
   
   CXTranslationUnit TU = GetTU(X);
@@ -375,6 +516,49 @@
   return T.isPODType(AU->getASTContext()) ? 1 : 0;
 }
 
+CXType clang_getElementType(CXType CT) {
+  QualType ET = QualType();
+  QualType T = GetQualType(CT);
+  const Type *TP = T.getTypePtrOrNull();
+
+  if (TP) {
+    switch (TP->getTypeClass()) {
+    case Type::ConstantArray:
+      ET = cast<ConstantArrayType> (TP)->getElementType();
+      break;
+    case Type::Vector:
+      ET = cast<VectorType> (TP)->getElementType();
+      break;
+    case Type::Complex:
+      ET = cast<ComplexType> (TP)->getElementType();
+      break;
+    default:
+      break;
+    }
+  }
+  return MakeCXType(ET, GetTU(CT));
+}
+
+long long clang_getNumElements(CXType CT) {
+  long long result = -1;
+  QualType T = GetQualType(CT);
+  const Type *TP = T.getTypePtrOrNull();
+
+  if (TP) {
+    switch (TP->getTypeClass()) {
+    case Type::ConstantArray:
+      result = cast<ConstantArrayType> (TP)->getSize().getSExtValue();
+      break;
+    case Type::Vector:
+      result = cast<VectorType> (TP)->getNumElements();
+      break;
+    default:
+      break;
+    }
+  }
+  return result;
+}
+
 CXType clang_getArrayElementType(CXType CT) {
   QualType ET = QualType();
   QualType T = GetQualType(CT);

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=145972&r1=145971&r2=145972&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Tue Dec  6 16:05:01 2011
@@ -47,6 +47,7 @@
 clang_findReferencesInFile
 clang_findReferencesInFileWithBlock
 clang_formatDiagnostic
+clang_getArgType
 clang_getArrayElementType
 clang_getArraySize
 clang_getCString
@@ -96,10 +97,15 @@
 clang_getDiagnosticRange
 clang_getDiagnosticSeverity
 clang_getDiagnosticSpelling
+clang_getElementType
+clang_getEnumConstantDeclValue
+clang_getEnumConstantDeclUnsignedValue
+clang_getEnumDeclIntegerType
 clang_getExpansionLocation
 clang_getFile
 clang_getFileName
 clang_getFileTime
+clang_getFunctionTypeCallingConv
 clang_getIBOutletCollectionType
 clang_getIncludedFile
 clang_getInclusions
@@ -109,9 +115,11 @@
 clang_getNullCursor
 clang_getNullLocation
 clang_getNullRange
+clang_getNumArgTypes
 clang_getNumCompletionChunks
 clang_getNumDiagnostics
 clang_getNumDiagnosticsInSet
+clang_getNumElements
 clang_getNumOverloadedDecls
 clang_getOverloadedDecl
 clang_getOverriddenCursors
@@ -134,6 +142,7 @@
 clang_getTranslationUnitSpelling
 clang_getTypeDeclaration
 clang_getTypeKindSpelling
+clang_getTypedefDeclUnderlyingType
 clang_hashCursor
 clang_IndexAction_create
 clang_IndexAction_dispose
@@ -158,6 +167,7 @@
 clang_isDeclaration
 clang_isExpression
 clang_isFileMultipleIncludeGuarded
+clang_isFunctionTypeVariadic
 clang_isInvalid
 clang_isPODType
 clang_isPreprocessing





More information about the cfe-commits mailing list