diff options
author | Steve Naroff <snaroff@apple.com> | 2009-08-31 00:59:03 +0000 |
---|---|---|
committer | Steve Naroff <snaroff@apple.com> | 2009-08-31 00:59:03 +0000 |
commit | 89922f86f4e7da383af2a62ef04ad8b93b941220 (patch) | |
tree | 5dc260ad76be4746a95a21fe57d91968ed9c82b7 /tools | |
parent | a31d5f70e02d89631d07be162796a2d6bd74e561 (diff) |
More fleshing out the C-based indexing API (under construction).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@80529 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r-- | tools/CIndex/CIndex.cpp | 128 | ||||
-rw-r--r-- | tools/CIndex/CIndex.exports | 3 | ||||
-rw-r--r-- | tools/c-index-test/c-index-test.c | 9 |
3 files changed, 111 insertions, 29 deletions
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index a02e80987b..9ef51b9db9 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -21,6 +21,68 @@ using namespace clang; using namespace idx; +namespace { + +// Translation Unit Visitor. +class TUVisitor : public DeclVisitor<TUVisitor> { + CXTranslationUnit TUnit; + CXTranslationUnitIterator Callback; +public: + TUVisitor(CXTranslationUnit CTU, CXTranslationUnitIterator cback) : + TUnit(CTU), Callback(cback) {} + + void VisitTranslationUnitDecl(TranslationUnitDecl *D) { + VisitDeclContext(dyn_cast<DeclContext>(D)); + } + void VisitDeclContext(DeclContext *DC) { + for (DeclContext::decl_iterator + I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) + Visit(*I); + } + void VisitTypedefDecl(TypedefDecl *ND) { + CXCursor C = { CXCursor_TypedefDecl, ND }; + Callback(TUnit, C); + } + void VisitTagDecl(TagDecl *ND) { + CXCursor C = { ND->isEnum() ? CXCursor_EnumDecl : CXCursor_RecordDecl, ND }; + Callback(TUnit, C); + } + void VisitFunctionDecl(FunctionDecl *ND) { + CXCursor C = { CXCursor_FunctionDecl, ND }; + Callback(TUnit, C); + } + void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { + CXCursor C = { CXCursor_ObjCInterfaceDecl, ND }; + Callback(TUnit, C); + } + void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { + CXCursor C = { CXCursor_ObjCCategoryDecl, ND }; + Callback(TUnit, C); + } + void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { + CXCursor C = { CXCursor_ObjCProtocolDecl, ND }; + Callback(TUnit, C); + } +}; + +// Top-level declaration visitor. +class TLDeclVisitor : public DeclVisitor<TLDeclVisitor> { +public: + void VisitDeclContext(DeclContext *DC) { + for (DeclContext::decl_iterator + I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) + Visit(*I); + } + void VisitEnumConstantDecl(EnumConstantDecl *ND) { + } + void VisitFieldDecl(FieldDecl *ND) { + } + void VisitObjCIvarDecl(ObjCIvarDecl *ND) { + } +}; + +} + extern "C" { CXIndex clang_createIndex() @@ -40,33 +102,19 @@ CXTranslationUnit clang_createTranslationUnit( return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg); } -namespace { - -class IdxVisitor : public DeclVisitor<IdxVisitor> { -public: - void VisitNamedDecl(NamedDecl *ND) { - printf("NamedDecl (%s:", ND->getDeclKindName()); - if (ND->getIdentifier()) - printf("%s)\n", ND->getIdentifier()->getName()); - else - printf("<no name>)\n"); - } -}; - -} void clang_loadTranslationUnit( - CXTranslationUnit CTUnit, void (*callback)(CXTranslationUnit, CXCursor)) + CXTranslationUnit CTUnit, CXTranslationUnitIterator callback) { assert(CTUnit && "Passed null CXTranslationUnit"); ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit); ASTContext &Ctx = CXXUnit->getASTContext(); - IdxVisitor DVisit; + TUVisitor DVisit(CTUnit, callback); DVisit.Visit(Ctx.getTranslationUnitDecl()); } -void clang_loadDeclaration(CXDecl, void (*callback)(CXDecl, CXCursor)) +void clang_loadDeclaration(CXDecl, CXDeclIterator) { } @@ -106,34 +154,60 @@ CXEntity clang_getEntity(const char *URI) // CXCursor clang_getCursorFromDecl(CXDecl) { - return 0; + return CXCursor(); } CXEntity clang_getEntityFromDecl(CXDecl) { return 0; } -enum CXDeclKind clang_getDeclKind(CXDecl) -{ - return CXDecl_any; -} -const char *clang_getDeclSpelling(CXDecl) -{ - return ""; +const char *clang_getDeclSpelling(CXDecl AnonDecl) +{ + assert(AnonDecl && "Passed null CXDecl"); + NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl); + if (ND->getIdentifier()) + return ND->getIdentifier()->getName(); + else + return ""; +} +const char *clang_getKindSpelling(enum CXCursorKind Kind) +{ + switch (Kind) { + case CXCursor_FunctionDecl: return "FunctionDecl"; + case CXCursor_TypedefDecl: return "TypedefDecl"; + case CXCursor_EnumDecl: return "EnumDecl"; + case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; + case CXCursor_RecordDecl: return "RecordDecl"; + case CXCursor_FieldDecl: return "FieldDecl"; + case CXCursor_VarDecl: return "VarDecl"; + case CXCursor_ParmDecl: return "ParmDecl"; + case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; + case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; + case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; + case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; + case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; + case CXCursor_ObjCMethodDecl: return "ObjCMethodDecl"; + default: return "<not implemented>"; + } } + // // CXCursor Operations. // CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, unsigned line, unsigned column) { - return 0; + return CXCursor(); } CXCursorKind clang_getCursorKind(CXCursor) { - return CXCursor_Declaration; + return CXCursor_Invalid; } +unsigned clang_isDeclaration(enum CXCursorKind K) +{ + return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl; +} unsigned clang_getCursorLine(CXCursor) { return 0; diff --git a/tools/CIndex/CIndex.exports b/tools/CIndex/CIndex.exports index 41b67a2467..ac18cc8e32 100644 --- a/tools/CIndex/CIndex.exports +++ b/tools/CIndex/CIndex.exports @@ -7,7 +7,6 @@ _clang_getCursorKind _clang_getCursorLine _clang_getCursorSource _clang_getDeclarationName -_clang_getDeclKind _clang_getDeclSpelling _clang_getEntity _clang_getEntityFromDecl @@ -15,3 +14,5 @@ _clang_getURI _clang_loadDeclaration _clang_loadTranslationUnit _clang_createTranslationUnit +_clang_isDeclaration +_clang_getKindSpelling diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 71bdcc5c64..3126b319ef 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -1,5 +1,12 @@ #include "clang-c/Index.h" +#include <stdio.h> + +static void PrintDecls(CXTranslationUnit Unit, CXCursor Cursor) { + if (clang_isDeclaration(Cursor.kind)) + printf("%s => %s\n", clang_getKindSpelling(Cursor.kind), + clang_getDeclSpelling(Cursor.decl)); +} /* * First sign of life:-) @@ -7,6 +14,6 @@ int main(int argc, char **argv) { CXIndex Idx = clang_createIndex(); CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]); - clang_loadTranslationUnit(TU, 0); + clang_loadTranslationUnit(TU, PrintDecls); return 1; } |