From af08ddc8f1c53fed8d8d0ad82aa2a0bb7d654bd1 Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Thu, 3 Sep 2009 15:49:00 +0000 Subject: [PATCH] - Add back some harmless code that part of a reverted commit (r80859). I'll investigate the lifetime snafu (with ASTUnit) separately. - Traverse category methods, add a "class ref" and make the little test harness a bit more flexible. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80921 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang-c/Index.h | 5 +++- tools/CIndex/CIndex.cpp | 42 +++++++++++++++++++++++++++++-- tools/CIndex/CIndex.exports | 1 + tools/c-index-test/c-index-test.c | 40 +++++++++++++++++++++-------- 4 files changed, 74 insertions(+), 14 deletions(-) diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 9efa791e89..9af3169157 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -75,7 +75,8 @@ enum CXCursorKind { CXCursor_ObjCProtocolRef = 41, CXCursor_ObjCMessageRef = 42, CXCursor_ObjCSelectorRef = 43, - CXCursor_LastRef = 43 + CXCursor_ObjCClassRef = 44, + CXCursor_LastRef = 44 }; /* A cursor into the CXTranslationUnit. */ @@ -91,6 +92,8 @@ typedef void *CXEntity; CXIndex clang_createIndex(); +const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit); + CXTranslationUnit clang_createTranslationUnit( CXIndex, const char *ast_filename ); diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index 82f7fb8e2b..5d1ee9684f 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -67,6 +67,9 @@ public: break; } } + void VisitVarDecl(VarDecl *ND) { + Call(CXCursor_VarDecl, ND); + } void VisitFunctionDecl(FunctionDecl *ND) { Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn : CXCursor_FunctionDecl, ND); @@ -95,6 +98,9 @@ class CDeclVisitor : public DeclVisitor { CXClientData CData; void Call(enum CXCursorKind CK, NamedDecl *ND) { + // Disable the callback when the context is equal to the visiting decl. + if (CDecl == ND && !clang_isReference(CK)) + return; CXCursor C = { CK, ND }; Callback(CDecl, C, CData); } @@ -102,11 +108,18 @@ public: CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : CDecl(C), Callback(cback), CData(D) {} + void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { + // Issue callbacks for the containing class. + Call(CXCursor_ObjCClassRef, ND); + // FIXME: Issue callbacks for protocol refs. + VisitDeclContext(dyn_cast(ND)); + } void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { - // Issue callbacks for super class and protocols. + // Issue callbacks for super class. if (D->getSuperClass()) Call(CXCursor_ObjCSuperClassRef, D); + // FIXME: Issue callbacks for protocol refs. VisitDeclContext(dyn_cast(D)); } void VisitTagDecl(TagDecl *D) { @@ -129,17 +142,28 @@ public: void VisitFieldDecl(FieldDecl *ND) { Call(CXCursor_FieldDecl, ND); } + void VisitVarDecl(VarDecl *ND) { + Call(CXCursor_VarDecl, ND); + } + void VisitParmVarDecl(ParmVarDecl *ND) { + Call(CXCursor_ParmDecl, ND); + } void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) { Call(CXCursor_ObjCPropertyDecl, ND); } void VisitObjCIvarDecl(ObjCIvarDecl *ND) { Call(CXCursor_ObjCIvarDecl, ND); } + void VisitFunctionDecl(FunctionDecl *ND) { + if (ND->isThisDeclarationADefinition()) { + VisitDeclContext(dyn_cast(ND)); + } + } void VisitObjCMethodDecl(ObjCMethodDecl *ND) { if (ND->getBody()) { Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn : CXCursor_ObjCClassMethodDefn, ND); - // FIXME: load body. + VisitDeclContext(dyn_cast(ND)); } else Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl : CXCursor_ObjCClassMethodDecl, ND); @@ -167,6 +191,13 @@ CXTranslationUnit clang_createTranslationUnit( return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); } +const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) +{ + assert(CTUnit && "Passed null CXTranslationUnit"); + //ASTUnit *CXXUnit = static_cast(CTUnit); + //return CXXUnit->getOriginalSourceFileName().c_str(); + return ""; +} void clang_loadTranslationUnit(CXTranslationUnit CTUnit, CXTranslationUnitIterator callback, @@ -259,6 +290,12 @@ const char *clang_getCursorSpelling(CXCursor C) assert(OID && "clang_getCursorLine(): Missing interface decl"); return OID->getSuperClass()->getIdentifier()->getName(); } + case CXCursor_ObjCClassRef: + { + ObjCCategoryDecl *OID = dyn_cast(ND); + assert(OID && "clang_getCursorLine(): Missing category decl"); + return OID->getClassInterface()->getIdentifier()->getName(); + } default: return ""; } @@ -292,6 +329,7 @@ const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) case CXCursor_ObjCClassDefn: return "ObjCClassDefn"; case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn"; case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; + case CXCursor_ObjCClassRef: return "ObjCClassRef"; default: return ""; } } diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports index 3f87a82b3a..7c9660b4d0 100644 --- a/tools/CIndex/CIndex.exports +++ b/tools/CIndex/CIndex.exports @@ -19,3 +19,4 @@ _clang_isReference _clang_isDefinition _clang_getCursorSpelling _clang_getCursorKindSpelling +_clang_getTranslationUnitSpelling diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 2d2be158d0..dbf31bea5a 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -2,26 +2,32 @@ #include "clang-c/Index.h" #include +#include + +static void PrintCursor(CXCursor Cursor) { + printf("%s => %s\n", clang_getCursorKindSpelling(Cursor.kind), + clang_getCursorSpelling(Cursor)); +} static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) { if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { - printf("%s => %s", clang_getCursorKindSpelling(Cursor.kind), - clang_getCursorSpelling(Cursor)); - printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor), - clang_getCursorLine(Cursor), - clang_getCursorColumn(Cursor)); + PrintCursor(Cursor); + printf(" Context: %s\n", clang_getDeclSpelling(Dcl)); + printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor), + clang_getCursorLine(Cursor), + clang_getCursorColumn(Cursor)); } } static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, CXClientData Filter) { if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { - printf("%s => %s", clang_getCursorKindSpelling(Cursor.kind), - clang_getCursorSpelling(Cursor)); - printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor), - clang_getCursorLine(Cursor), - clang_getCursorColumn(Cursor)); + PrintCursor(Cursor); + printf(" Context: %s\n", clang_getTranslationUnitSpelling(Unit)); + printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor), + clang_getCursorLine(Cursor), + clang_getCursorColumn(Cursor)); clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); } @@ -34,6 +40,18 @@ int main(int argc, char **argv) { CXIndex Idx = clang_createIndex(); CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]); - clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); + if (argc == 2) + clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); + else if (argc == 3) { + enum CXCursorKind K = CXCursor_Invalid; + + if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl; + else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl; + else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl; + else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl; + else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl; + + clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K); + } return 1; } -- 2.40.0