From: Douglas Gregor Date: Tue, 19 Jan 2010 22:07:56 +0000 (+0000) Subject: Introduce the notion of an "unexposed" declaration into the CIndex X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=301221313be4f97327e931ead9794dd3a8bce160;p=clang Introduce the notion of an "unexposed" declaration into the CIndex API. This is a catch-all for any declaration known to Clang but not specifically part of the CIndex API. We'll use the same approach with expressions, statements, references, etc., as needed. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93924 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 022d5bb479..a417209d83 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -55,26 +55,58 @@ typedef void *CXStmt; /* A specific statement within a function/method */ enum CXCursorKind { /* Declarations */ CXCursor_FirstDecl = 1, + /** \brief A typedef */ CXCursor_TypedefDecl = 1, + /** \brief A C or C++ struct. */ CXCursor_StructDecl = 2, + /** \brief A C or C++ union. */ CXCursor_UnionDecl = 3, + /** \brief A C++ class. */ CXCursor_ClassDecl = 4, + /** \brief An enumeration. */ CXCursor_EnumDecl = 5, + /** + * \brief A field (in C) or non-static data member (in C++) in a + * struct, union, or C++ class. + */ CXCursor_FieldDecl = 6, + /** \brief An enumerator constant. */ CXCursor_EnumConstantDecl = 7, + /** \brief A function. */ CXCursor_FunctionDecl = 8, + /** \brief A variable. */ CXCursor_VarDecl = 9, + /** \brief A function or method parameter. */ CXCursor_ParmDecl = 10, + /** \brief An Objective-C @interface. */ CXCursor_ObjCInterfaceDecl = 11, + /** \brief An Objective-C @interface for a category. */ CXCursor_ObjCCategoryDecl = 12, + /** \brief An Objective-C @protocol declaration. */ CXCursor_ObjCProtocolDecl = 13, + /** \brief An Objective-C @property declaration. */ CXCursor_ObjCPropertyDecl = 14, + /** \brief An Objective-C instance variable. */ CXCursor_ObjCIvarDecl = 15, + /** \brief An Objective-C instance method. */ CXCursor_ObjCInstanceMethodDecl = 16, + /** \brief An Objective-C class method. */ CXCursor_ObjCClassMethodDecl = 17, + /** \brief An Objective-C @implementation. */ CXCursor_ObjCImplementationDecl = 18, + /** \brief An Objective-C @implementation for a category. */ CXCursor_ObjCCategoryImplDecl = 19, - CXCursor_LastDecl = 19, + /** + * \brief A declaration whose specific kind is not exposed via this + * interface. + * + * Unexposed declarations have the same operations as any other kind + * of declaration; one can extract their location information, + * spelling, find their definitions, etc. However, the specific kind + * of the declaration is not reported. + */ + CXCursor_UnexposedDecl = 20, + CXCursor_LastDecl = 20, /* References */ CXCursor_FirstRef = 40, /* Decl references */ diff --git a/test/Index/TestClassDecl.m b/test/Index/TestClassDecl.m index 8bbe949948..884289de96 100644 --- a/test/Index/TestClassDecl.m +++ b/test/Index/TestClassDecl.m @@ -16,7 +16,7 @@ void function(Foo * arg) } // CHECK-scan: {start_line=1 start_col=1 end_line=7 end_col=1} Invalid Cursor => NoDeclFound -// CHECK-scan: {start_line=8 start_col=1 end_line=8 end_col=7} Invalid Cursor => NotImplemented +// CHECK-scan: {start_line=8 start_col=1 end_line=8 end_col=7} UnexposedDecl=:8:1 // CHECK-scan: {start_line=8 start_col=8 end_line=8 end_col=10} ObjCClassRef=Foo:10:1 // CHECK-scan: {start_line=8 start_col=11 end_line=9 end_col=1} Invalid Cursor => NoDeclFound // CHECK-scan: {start_line=10 start_col=1 end_line=11 end_col=4} ObjCInterfaceDecl=Foo:10:1 diff --git a/test/Index/TestClassForwardDecl.m b/test/Index/TestClassForwardDecl.m index 65b7c6f515..12f67fff66 100644 --- a/test/Index/TestClassForwardDecl.m +++ b/test/Index/TestClassForwardDecl.m @@ -13,7 +13,7 @@ void function(Foo * arg) } // CHECK-scan: {start_line=1 start_col=1 end_line=7 end_col=1} Invalid Cursor => NoDeclFound -// CHECK-scan: {start_line=8 start_col=1 end_line=8 end_col=7} Invalid Cursor => NotImplemented +// CHECK-scan: {start_line=8 start_col=1 end_line=8 end_col=7} UnexposedDecl=:8:1 // CHECK-scan: {start_line=8 start_col=8 end_line=8 end_col=10} ObjCClassRef=Foo:8:8 // CHECK-scan: {start_line=8 start_col=11 end_line=9 end_col=1} Invalid Cursor => NoDeclFound // CHECK-scan: {start_line=10 start_col=1 end_line=10 end_col=4} FunctionDecl=function:10:6 (Definition) diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index a7cbb8a4fc..b4759a5127 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -208,7 +208,7 @@ static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) { else if (isa(D)) return CXCursor_EnumConstantRef; else - return CXCursor_NotImplemented; + return CXCursor_UnexposedDecl; } // Translation Unit Visitor. @@ -712,7 +712,10 @@ static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr, extern "C" { CXString clang_getDeclSpelling(CXDecl AnonDecl) { assert(AnonDecl && "Passed null CXDecl"); - NamedDecl *ND = static_cast(AnonDecl); + Decl *D = static_cast(AnonDecl); + NamedDecl *ND = dyn_cast(D); + if (!ND) + return CIndexer::createCXString(""); if (ObjCMethodDecl *OMD = dyn_cast(ND)) return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(), @@ -871,6 +874,7 @@ const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; case CXCursor_ObjCImplementationDecl: return "ObjCImplementationDecl"; case CXCursor_ObjCCategoryImplDecl: return "ObjCCategoryImplDecl"; + case CXCursor_UnexposedDecl: return "UnexposedDecl"; case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; case CXCursor_ObjCClassRef: return "ObjCClassRef"; diff --git a/tools/CIndex/CXCursor.cpp b/tools/CIndex/CXCursor.cpp index 00636f74d5..f284b248cd 100644 --- a/tools/CIndex/CXCursor.cpp +++ b/tools/CIndex/CXCursor.cpp @@ -44,10 +44,10 @@ static CXCursorKind GetCursorKind(Decl *D) { case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryImplDecl; case Decl::ObjCClass: // FIXME - return CXCursor_NotImplemented; + return CXCursor_UnexposedDecl; case Decl::ObjCForwardProtocol: // FIXME - return CXCursor_NotImplemented; + return CXCursor_UnexposedDecl; case Decl::ObjCImplementation: return CXCursor_ObjCImplementationDecl; case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; case Decl::ObjCIvar: return CXCursor_ObjCIvarDecl; @@ -68,6 +68,8 @@ static CXCursorKind GetCursorKind(Decl *D) { case TagDecl::TK_enum: return CXCursor_EnumDecl; } } + + return CXCursor_UnexposedDecl; } llvm_unreachable("Invalid Decl"); @@ -161,6 +163,7 @@ ASTContext &cxcursor::getCursorContext(CXCursor Cursor) { case CXCursor_ObjCClassMethodDecl: case CXCursor_ObjCImplementationDecl: case CXCursor_ObjCCategoryImplDecl: + case CXCursor_UnexposedDecl: return static_cast(Cursor.data[0])->getASTContext(); case CXCursor_ObjCSuperClassRef: