diff options
Diffstat (limited to 'tools/c-index-test')
-rw-r--r-- | tools/c-index-test/CMakeLists.txt | 25 | ||||
-rw-r--r-- | tools/c-index-test/Makefile | 24 | ||||
-rw-r--r-- | tools/c-index-test/c-index-test.c | 106 |
3 files changed, 155 insertions, 0 deletions
diff --git a/tools/c-index-test/CMakeLists.txt b/tools/c-index-test/CMakeLists.txt new file mode 100644 index 0000000..4c72465 --- /dev/null +++ b/tools/c-index-test/CMakeLists.txt @@ -0,0 +1,25 @@ +set(LLVM_NO_RTTI 1) + +set( LLVM_USED_LIBS + CIndex + clangIndex + clangFrontend + clangSema + clangAST + clangLex + clangBasic + ) + +set( LLVM_LINK_COMPONENTS + bitreader + mc + ) + +add_clang_executable(c-index-test + c-index-test.c + ) + +set_target_properties(c-index-test + PROPERTIES + LINKER_LANGUAGE CXX) + diff --git a/tools/c-index-test/Makefile b/tools/c-index-test/Makefile new file mode 100644 index 0000000..81fee40 --- /dev/null +++ b/tools/c-index-test/Makefile @@ -0,0 +1,24 @@ +##===- tools/index-test/Makefile ---------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../../../.. + +TOOLNAME = c-index-test +CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include +CXXFLAGS = -fno-rtti +NO_INSTALL = 1 + +# No plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(LEVEL)/Makefile.config + +LINK_COMPONENTS := bitreader mc +USEDLIBS = CIndex.a clangIndex.a clangFrontend.a clangSema.a clangAST.a clangLex.a clangBasic.a + +include $(LLVM_SRC_ROOT)/Makefile.rules diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c new file mode 100644 index 0000000..c514b63 --- /dev/null +++ b/tools/c-index-test/c-index-test.c @@ -0,0 +1,106 @@ +/* c-index-test.c */ + +#include "clang-c/Index.h" +#include <stdio.h> +#include <string.h> + +extern char *basename(const char *); + +static void PrintCursor(CXCursor Cursor) { + if (clang_isInvalid(Cursor.kind)) + printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind)); + else { + CXDecl DeclReferenced; + printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind), + clang_getCursorSpelling(Cursor)); + DeclReferenced = clang_getCursorDecl(Cursor); + if (DeclReferenced) + printf(":%d:%d", clang_getDeclLine(DeclReferenced), + clang_getDeclColumn(DeclReferenced)); + } +} + +static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) +{ + if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { + printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)), + clang_getCursorLine(Cursor), + clang_getCursorColumn(Cursor)); + PrintCursor(Cursor); + printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl)); + } +} +static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, + CXClientData Filter) +{ + if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { + printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)), + clang_getCursorLine(Cursor), + clang_getCursorColumn(Cursor)); + PrintCursor(Cursor); + printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit))); + + clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); + + if (Cursor.kind == CXCursor_FunctionDefn) { + const char *startBuf, *endBuf; + unsigned startLine, startColumn, endLine, endColumn; + clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, + &startLine, &startColumn, + &endLine, &endColumn); + { + /* Probe the entire body, looking for both decls and refs. */ + unsigned curLine = startLine, curColumn = startColumn; + CXCursor Ref; + + while (startBuf <= endBuf) { + if (*startBuf == '\n') { + startBuf++; + curLine++; + curColumn = 1; + } else if (*startBuf != '\t') + curColumn++; + + Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor), + curLine, curColumn); + if (Ref.kind != CXCursor_FunctionDecl) { + printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)), + curLine, curColumn); + PrintCursor(Ref); + printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl)); + } + startBuf++; + } + } + } + } +} + +/* + * First sign of life:-) + */ +int main(int argc, char **argv) { + if (argc != 3) { + printf("Incorrect usage of c-index-test (requires 3 arguments)\n"); + return 0; + } + { + CXIndex Idx = clang_createIndex(); + CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]); + enum CXCursorKind K = CXCursor_NotImplemented; + + if (!strcmp(argv[2], "all")) { + clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0); + return 1; + } + /* Perform some simple filtering. */ + 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; + } +} |