diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index de2b5d6898..1b9ba4c4f7 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -104,8 +104,11 @@ CXTranslationUnit clang_createTranslationUnit( clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames); } */ -typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor); -void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator); +typedef void *CXClientData; +typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor, + CXClientData); +void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator, + CXClientData); /* Usage: clang_loadDeclaration(). Will load the declaration, issuing a diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index f8d223a510..9577391288 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -28,9 +28,16 @@ namespace { class TUVisitor : public DeclVisitor { CXTranslationUnit TUnit; CXTranslationUnitIterator Callback; + CXClientData CData; + + void Call(enum CXCursorKind CK, NamedDecl *ND) { + CXCursor C = { CK, ND }; + Callback(TUnit, C, CData); + } public: - TUVisitor(CXTranslationUnit CTU, CXTranslationUnitIterator cback) : - TUnit(CTU), Callback(cback) {} + TUVisitor(CXTranslationUnit CTU, + CXTranslationUnitIterator cback, CXClientData D) : + TUnit(CTU), Callback(cback), CData(D) {} void VisitTranslationUnitDecl(TranslationUnitDecl *D) { VisitDeclContext(dyn_cast(D)); @@ -40,29 +47,23 @@ public: 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 VisitTypedefDecl(TypedefDecl *ND) { + Call(CXCursor_TypedefDecl, ND); } void VisitTagDecl(TagDecl *ND) { - CXCursor C = { ND->isEnum() ? CXCursor_EnumDecl : CXCursor_RecordDecl, ND }; - Callback(TUnit, C); + Call(ND->isEnum() ? CXCursor_EnumDecl : CXCursor_RecordDecl, ND); } void VisitFunctionDecl(FunctionDecl *ND) { - CXCursor C = { CXCursor_FunctionDecl, ND }; - Callback(TUnit, C); + Call(CXCursor_FunctionDecl, ND); } void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) { - CXCursor C = { CXCursor_ObjCInterfaceDecl, ND }; - Callback(TUnit, C); + Call(CXCursor_ObjCInterfaceDecl, ND); } void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) { - CXCursor C = { CXCursor_ObjCCategoryDecl, ND }; - Callback(TUnit, C); + Call(CXCursor_ObjCCategoryDecl, ND); } void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) { - CXCursor C = { CXCursor_ObjCProtocolDecl, ND }; - Callback(TUnit, C); + Call(CXCursor_ObjCProtocolDecl, ND); } }; @@ -105,13 +106,14 @@ CXTranslationUnit clang_createTranslationUnit( void clang_loadTranslationUnit( - CXTranslationUnit CTUnit, CXTranslationUnitIterator callback) + CXTranslationUnit CTUnit, CXTranslationUnitIterator callback, + CXClientData CData) { assert(CTUnit && "Passed null CXTranslationUnit"); ASTUnit *CXXUnit = static_cast(CTUnit); ASTContext &Ctx = CXXUnit->getASTContext(); - TUVisitor DVisit(CTUnit, callback); + TUVisitor DVisit(CTUnit, callback, CData); DVisit.Visit(Ctx.getTranslationUnitDecl()); } diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 1ef8e924bc..30700d8ae3 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -1,14 +1,18 @@ +/* c-index-test.c */ #include "clang-c/Index.h" #include -static void PrintDecls(CXTranslationUnit Unit, CXCursor Cursor) { +static void PrintDecls(CXTranslationUnit Unit, CXCursor Cursor, + CXClientData Filter) { if (clang_isDeclaration(Cursor.kind)) { - printf("%s => %s", clang_getKindSpelling(Cursor.kind), - clang_getDeclSpelling(Cursor.decl)); - printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor), - clang_getCursorLine(Cursor), - clang_getCursorColumn(Cursor)); + if (Cursor.kind == *(enum CXCursorKind *)Filter) { + printf("%s => %s", clang_getKindSpelling(Cursor.kind), + clang_getDeclSpelling(Cursor.decl)); + printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor), + clang_getCursorLine(Cursor), + clang_getCursorColumn(Cursor)); + } } } @@ -18,6 +22,9 @@ static void PrintDecls(CXTranslationUnit Unit, CXCursor Cursor) { int main(int argc, char **argv) { CXIndex Idx = clang_createIndex(); CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]); - clang_loadTranslationUnit(TU, PrintDecls); + + /* Use client data to only print ObjC interfaces */ + enum CXCursorKind filterData = CXCursor_ObjCInterfaceDecl; + clang_loadTranslationUnit(TU, PrintDecls, &filterData); return 1; }