diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2016-01-16 00:20:02 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2016-01-16 00:20:02 +0000 |
commit | c5f92b66c89ee5446679b86e636bbb6813fcc22e (patch) | |
tree | 97ba89c859a1e8b56aad8563d156a16b58fbc226 /tools/c-index-test | |
parent | 2cc0493989d8cf7838ff0c572827ef43c2201e7f (diff) |
[libclang] Introduce APIs for evaluating a cursor and checking if a macro is builtin/function.
rdar://24091595
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@257968 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/c-index-test')
-rw-r--r-- | tools/c-index-test/c-index-test.c | 265 |
1 files changed, 195 insertions, 70 deletions
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 2a6002537e..b2f9120baf 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -2287,7 +2287,11 @@ typedef struct { unsigned column; } CursorSourceLocation; -static int inspect_cursor_at(int argc, const char **argv) { +typedef void (*cursor_handler_t)(CXCursor cursor); + +static int inspect_cursor_at(int argc, const char **argv, + const char *locations_flag, + cursor_handler_t handler) { CXIndex CIdx; int errorCode; struct CXUnsavedFile *unsaved_files = 0; @@ -2301,7 +2305,7 @@ static int inspect_cursor_at(int argc, const char **argv) { unsigned I; /* Count the number of locations. */ - while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) + while (strstr(argv[NumLocations+1], locations_flag) == argv[NumLocations+1]) ++NumLocations; /* Parse the locations. */ @@ -2309,7 +2313,7 @@ static int inspect_cursor_at(int argc, const char **argv) { Locations = (CursorSourceLocation *)malloc( NumLocations * sizeof(CursorSourceLocation)); for (Loc = 0; Loc < NumLocations; ++Loc) { - const char *input = argv[Loc + 1] + strlen("-cursor-at="); + const char *input = argv[Loc + 1] + strlen(locations_flag); if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, &Locations[Loc].line, &Locations[Loc].column, 0, 0))) @@ -2368,72 +2372,7 @@ static int inspect_cursor_at(int argc, const char **argv) { return -1; if (I + 1 == Repeats) { - CXCompletionString completionString = clang_getCursorCompletionString( - Cursor); - CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); - CXString Spelling; - const char *cspell; - unsigned line, column; - clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); - printf("%d:%d ", line, column); - PrintCursor(Cursor, NULL); - PrintCursorExtent(Cursor); - Spelling = clang_getCursorSpelling(Cursor); - cspell = clang_getCString(Spelling); - if (cspell && strlen(cspell) != 0) { - unsigned pieceIndex; - printf(" Spelling=%s (", cspell); - for (pieceIndex = 0; ; ++pieceIndex) { - CXSourceRange range = - clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); - if (clang_Range_isNull(range)) - break; - PrintRange(range, 0); - } - printf(")"); - } - clang_disposeString(Spelling); - if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1) - printf(" Selector index=%d", - clang_Cursor_getObjCSelectorIndex(Cursor)); - if (clang_Cursor_isDynamicCall(Cursor)) - printf(" Dynamic-call"); - if (Cursor.kind == CXCursor_ObjCMessageExpr) { - CXType T = clang_Cursor_getReceiverType(Cursor); - CXString S = clang_getTypeKindSpelling(T.kind); - printf(" Receiver-type=%s", clang_getCString(S)); - clang_disposeString(S); - } - - { - CXModule mod = clang_Cursor_getModule(Cursor); - CXFile astFile; - CXString name, astFilename; - unsigned i, numHeaders; - if (mod) { - astFile = clang_Module_getASTFile(mod); - astFilename = clang_getFileName(astFile); - name = clang_Module_getFullName(mod); - numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod); - printf(" ModuleName=%s (%s) system=%d Headers(%d):", - clang_getCString(name), clang_getCString(astFilename), - clang_Module_isSystem(mod), numHeaders); - clang_disposeString(name); - clang_disposeString(astFilename); - for (i = 0; i < numHeaders; ++i) { - CXFile file = clang_Module_getTopLevelHeader(TU, mod, i); - CXString filename = clang_getFileName(file); - printf("\n%s", clang_getCString(filename)); - clang_disposeString(filename); - } - } - } - - if (completionString != NULL) { - printf("\nCompletion string: "); - print_completion_string(completionString, stdout); - } - printf("\n"); + handler(Cursor); free(Locations[Loc].filename); } } @@ -2447,6 +2386,184 @@ static int inspect_cursor_at(int argc, const char **argv) { return 0; } +static void inspect_print_cursor(CXCursor Cursor) { + CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); + CXCompletionString completionString = clang_getCursorCompletionString( + Cursor); + CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); + CXString Spelling; + const char *cspell; + unsigned line, column; + clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); + printf("%d:%d ", line, column); + PrintCursor(Cursor, NULL); + PrintCursorExtent(Cursor); + Spelling = clang_getCursorSpelling(Cursor); + cspell = clang_getCString(Spelling); + if (cspell && strlen(cspell) != 0) { + unsigned pieceIndex; + printf(" Spelling=%s (", cspell); + for (pieceIndex = 0; ; ++pieceIndex) { + CXSourceRange range = + clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); + if (clang_Range_isNull(range)) + break; + PrintRange(range, 0); + } + printf(")"); + } + clang_disposeString(Spelling); + if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1) + printf(" Selector index=%d", + clang_Cursor_getObjCSelectorIndex(Cursor)); + if (clang_Cursor_isDynamicCall(Cursor)) + printf(" Dynamic-call"); + if (Cursor.kind == CXCursor_ObjCMessageExpr) { + CXType T = clang_Cursor_getReceiverType(Cursor); + CXString S = clang_getTypeKindSpelling(T.kind); + printf(" Receiver-type=%s", clang_getCString(S)); + clang_disposeString(S); + } + + { + CXModule mod = clang_Cursor_getModule(Cursor); + CXFile astFile; + CXString name, astFilename; + unsigned i, numHeaders; + if (mod) { + astFile = clang_Module_getASTFile(mod); + astFilename = clang_getFileName(astFile); + name = clang_Module_getFullName(mod); + numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod); + printf(" ModuleName=%s (%s) system=%d Headers(%d):", + clang_getCString(name), clang_getCString(astFilename), + clang_Module_isSystem(mod), numHeaders); + clang_disposeString(name); + clang_disposeString(astFilename); + for (i = 0; i < numHeaders; ++i) { + CXFile file = clang_Module_getTopLevelHeader(TU, mod, i); + CXString filename = clang_getFileName(file); + printf("\n%s", clang_getCString(filename)); + clang_disposeString(filename); + } + } + } + + if (completionString != NULL) { + printf("\nCompletion string: "); + print_completion_string(completionString, stdout); + } + printf("\n"); +} + +static void display_evaluate_results(CXEvalResult result) { + switch (clang_EvalResult_getKind(result)) { + case CXEval_Int: + { + int val = clang_EvalResult_getAsInt(result); + printf("Kind: Int , Value: %d", val); + break; + } + case CXEval_Float: + { + double val = clang_EvalResult_getAsDouble(result); + printf("Kind: Float , Value: %f", val); + break; + } + case CXEval_ObjCStrLiteral: + { + const char* str = clang_EvalResult_getAsStr(result); + printf("Kind: ObjCString , Value: %s", str); + break; + } + case CXEval_StrLiteral: + { + const char* str = clang_EvalResult_getAsStr(result); + printf("Kind: CString , Value: %s", str); + break; + } + case CXEval_CFStr: + { + const char* str = clang_EvalResult_getAsStr(result); + printf("Kind: CFString , Value: %s", str); + break; + } + default: + printf("Unexposed"); + break; + } +} + +static void inspect_evaluate_cursor(CXCursor Cursor) { + CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); + CXString Spelling; + const char *cspell; + unsigned line, column; + CXEvalResult ER; + + clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); + printf("%d:%d ", line, column); + PrintCursor(Cursor, NULL); + PrintCursorExtent(Cursor); + Spelling = clang_getCursorSpelling(Cursor); + cspell = clang_getCString(Spelling); + if (cspell && strlen(cspell) != 0) { + unsigned pieceIndex; + printf(" Spelling=%s (", cspell); + for (pieceIndex = 0; ; ++pieceIndex) { + CXSourceRange range = + clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); + if (clang_Range_isNull(range)) + break; + PrintRange(range, 0); + } + printf(")"); + } + clang_disposeString(Spelling); + + ER = clang_Cursor_Evaluate(Cursor); + if (!ER) { + printf("Not Evaluatable"); + } else { + display_evaluate_results(ER); + clang_EvalResult_dispose(ER); + } + printf("\n"); +} + +static void inspect_macroinfo_cursor(CXCursor Cursor) { + CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); + CXString Spelling; + const char *cspell; + unsigned line, column; + clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); + printf("%d:%d ", line, column); + PrintCursor(Cursor, NULL); + PrintCursorExtent(Cursor); + Spelling = clang_getCursorSpelling(Cursor); + cspell = clang_getCString(Spelling); + if (cspell && strlen(cspell) != 0) { + unsigned pieceIndex; + printf(" Spelling=%s (", cspell); + for (pieceIndex = 0; ; ++pieceIndex) { + CXSourceRange range = + clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); + if (clang_Range_isNull(range)) + break; + PrintRange(range, 0); + } + printf(")"); + } + clang_disposeString(Spelling); + + if (clang_Cursor_isMacroBuiltin(Cursor)) { + printf("[builtin macro]"); + } else if (clang_Cursor_isMacroFunctionLike(Cursor)) { + printf("[function macro]"); + } + printf("\n"); +} + static enum CXVisitorResult findFileRefsVisit(void *context, CXCursor cursor, CXSourceRange range) { if (clang_Range_isNull(range)) @@ -4121,6 +4238,8 @@ static void print_usage(void) { "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" " c-index-test -code-completion-timing=<site> <compiler arguments>\n" " c-index-test -cursor-at=<site> <compiler arguments>\n" + " c-index-test -evaluate-cursor-at=<site> <compiler arguments>\n" + " c-index-test -get-macro-info-cursor-at=<site> <compiler arguments>\n" " c-index-test -file-refs-at=<site> <compiler arguments>\n" " c-index-test -file-includes-in=<filename> <compiler arguments>\n"); fprintf(stderr, @@ -4186,7 +4305,13 @@ int cindextest_main(int argc, const char **argv) { if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1]) return perform_code_completion(argc, argv, 1); if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) - return inspect_cursor_at(argc, argv); + return inspect_cursor_at(argc, argv, "-cursor-at=", inspect_print_cursor); + if (argc > 2 && strstr(argv[1], "-evaluate-cursor-at=") == argv[1]) + return inspect_cursor_at(argc, argv, "-evaluate-cursor-at=", + inspect_evaluate_cursor); + if (argc > 2 && strstr(argv[1], "-get-macro-info-cursor-at=") == argv[1]) + return inspect_cursor_at(argc, argv, "-get-macro-info-cursor-at=", + inspect_macroinfo_cursor); if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1]) return find_file_refs_at(argc, argv); if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1]) |