From: Ted Kremenek Date: Sat, 16 Jan 2010 00:36:30 +0000 (+0000) Subject: Migrate Decl* -> cursorkind logic into CXCursor.cpp, and drastically tighten TUVisitor. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=edc8aa68ef91aeea686c5aadf64ef902c38318dd;p=clang Migrate Decl* -> cursorkind logic into CXCursor.cpp, and drastically tighten TUVisitor. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93599 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index fd5db0b74f..f799ba8046 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -145,16 +145,18 @@ private: // be suppressed. unsigned MaxPCHLevel; - void Call(enum CXCursorKind CK, NamedDecl *ND) { - // Filter any declarations that have a PCH level greater than what we allow. - if (ND->getPCHLevel() > MaxPCHLevel) - return; - - // Filter any implicit declarations (since the source info will be bogus). - if (ND->isImplicit()) - return; + void Call(const CXCursor &C) { + if (const Decl *D = getCursorDecl(C)) { + // Filter any declarations that have a PCH level greater than what + // we allow. + if (D->getPCHLevel() > MaxPCHLevel) + return; + + // Filter any implicit declarations (since the source info will be bogus). + if (D->isImplicit()) + return; + } - CXCursor C = { CK, { ND, 0, 0 } }; Callback(Root, C, CData); } @@ -162,78 +164,27 @@ public: TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) : Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {} + void VisitDecl(Decl *D); + void VisitObjCClassDecl(ObjCClassDecl *D) { + // FIXME: Do something. + } void VisitDeclContext(DeclContext *DC); - void VisitFunctionDecl(FunctionDecl *ND); - void VisitObjCCategoryDecl(ObjCCategoryDecl *ND); - void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND); - void VisitObjCImplementationDecl(ObjCImplementationDecl *ND); - void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND); - void VisitObjCProtocolDecl(ObjCProtocolDecl *ND); - void VisitTagDecl(TagDecl *ND); void VisitTranslationUnitDecl(TranslationUnitDecl *D); - void VisitTypedefDecl(TypedefDecl *ND); - void VisitVarDecl(VarDecl *ND); }; +void TUVisitor::VisitDecl(Decl *D) { + Call(MakeCXCursor(D)); +} + void TUVisitor::VisitDeclContext(DeclContext *DC) { for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) Visit(*I); } - -void TUVisitor::VisitFunctionDecl(FunctionDecl *ND) { - Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn - : CXCursor_FunctionDecl, ND); -} - -void TUVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { - Call(CXCursor_ObjCCategoryDecl, ND); -} - -void TUVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) { - Call(CXCursor_ObjCCategoryDefn, ND); -} - -void TUVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *ND) { - Call(CXCursor_ObjCClassDefn, ND); -} - -void TUVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { - Call(CXCursor_ObjCInterfaceDecl, ND); -} - -void TUVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { - Call(CXCursor_ObjCProtocolDecl, ND); -} - -void TUVisitor::VisitTagDecl(TagDecl *ND) { - switch (ND->getTagKind()) { - case TagDecl::TK_struct: - Call(CXCursor_StructDecl, ND); - break; - case TagDecl::TK_class: - Call(CXCursor_ClassDecl, ND); - break; - case TagDecl::TK_union: - Call(CXCursor_UnionDecl, ND); - break; - case TagDecl::TK_enum: - Call(CXCursor_EnumDecl, ND); - break; - } -} - + void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) { VisitDeclContext(dyn_cast(D)); } - -void TUVisitor::VisitTypedefDecl(TypedefDecl *ND) { - Call(CXCursor_TypedefDecl, ND); -} - -void TUVisitor::VisitVarDecl(VarDecl *ND) { - Call(CXCursor_VarDecl, ND); -} // Declaration visitor. class CDeclVisitor : public DeclVisitor { diff --git a/tools/CIndex/CXCursor.cpp b/tools/CIndex/CXCursor.cpp index 0c72e96900..00313edc45 100644 --- a/tools/CIndex/CXCursor.cpp +++ b/tools/CIndex/CXCursor.cpp @@ -15,6 +15,7 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/Expr.h" +#include "llvm/Support/ErrorHandling.h" using namespace clang; @@ -29,6 +30,37 @@ CXCursor cxcursor::MakeCXCursor(CXCursorKind K, Decl *D, Stmt *S) { return C; } +static CXCursorKind GetCursorKind(Decl *D) { + switch (D->getKind()) { + case Decl::Function: + return cast(D)->isThisDeclarationADefinition() + ? CXCursor_FunctionDefn : CXCursor_FunctionDecl; + case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl; + case Decl::ObjCCategoryImpl: return CXCursor_ObjCCategoryDefn; + case Decl::ObjCImplementation: return CXCursor_ObjCClassDefn; + case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl; + case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl; + case Decl::Typedef: return CXCursor_TypedefDecl; + case Decl::Var: return CXCursor_VarDecl; + default: + if (TagDecl *TD = dyn_cast(D)) { + switch (TD->getTagKind()) { + case TagDecl::TK_struct: return CXCursor_StructDecl; + case TagDecl::TK_class: return CXCursor_ClassDecl; + case TagDecl::TK_union: return CXCursor_UnionDecl; + case TagDecl::TK_enum: return CXCursor_EnumDecl; + } + } + } + + llvm_unreachable("Invalid Decl"); + return CXCursor_NotImplemented; +} + +CXCursor cxcursor::MakeCXCursor(Decl *D) { + return MakeCXCursor(GetCursorKind(D), D); +} + Decl *cxcursor::getCursorDecl(CXCursor Cursor) { return (Decl *)Cursor.data[0]; } diff --git a/tools/CIndex/CXCursor.h b/tools/CIndex/CXCursor.h index be1ac8b6ea..da51f13cdc 100644 --- a/tools/CIndex/CXCursor.h +++ b/tools/CIndex/CXCursor.h @@ -27,6 +27,7 @@ namespace cxcursor { CXCursor MakeCXCursor(CXCursorKind K, clang::Decl *D); CXCursor MakeCXCursor(CXCursorKind K, clang::Decl *D, clang::Stmt *S); +CXCursor MakeCXCursor(clang::Decl *D); Decl *getCursorDecl(CXCursor Cursor); Expr *getCursorExpr(CXCursor Cursor);