From: Douglas Gregor Date: Fri, 15 Jan 2010 19:40:17 +0000 (+0000) Subject: Add -cursor-at=file:line:column command line option to c-index-test, X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f2c87bd0368775927ead93e0dee3e4f3ca3f9a63;p=clang Add -cursor-at=file:line:column command line option to c-index-test, to directly check the results of clang_getCursor(). Also, start migrating some index-test tests over to c-index test [*] and some grep-using tests over to FileCheck. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93537 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Index/ResolveLocation.cpp b/lib/Index/ResolveLocation.cpp index 81a5de44bf..4bb15943bb 100644 --- a/lib/Index/ResolveLocation.cpp +++ b/lib/Index/ResolveLocation.cpp @@ -264,7 +264,7 @@ ASTLocation DeclLocResolver::VisitFunctionDecl(FunctionDecl *D) { return ASTLocation(D); // Second, search through the declarations that are part of the function. - // If we find he location there, we won't have to search through its body. + // If we find the location there, we won't have to search through its body. for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); I != E; ++I) { diff --git a/test/Index/resolve-loc.c b/test/Index/resolve-loc.c index 68504ee0d2..1b40ec7962 100644 --- a/test/Index/resolve-loc.c +++ b/test/Index/resolve-loc.c @@ -15,23 +15,25 @@ struct S { int field_var; }; - // RUN: %clang_cc1 -emit-pch %s -o %t.ast -// RUN: index-test %t.ast -point-at %s:3:8 | grep top_var -// RUN: index-test %t.ast -point-at %s:5:15 | grep top_func_decl -// RUN: index-test %t.ast -point-at %s:5:25 | grep param1 -// RUN: index-test %t.ast -point-at %s:7:17 | grep top_func_def -// RUN: index-test %t.ast -point-at %s:7:23 | grep param2 -// RUN: index-test %t.ast -point-at %s:8:10 | grep local_var1 -// RUN: index-test %t.ast -point-at %s:9:15 | grep for_var - +// RUN: c-index-test \ +// RUN: -cursor-at=%s:3:8 -cursor-at=%s:5:15 -cursor-at=%s:5:25 \ +// RUN: -cursor-at=%s:7:17 -cursor-at=%s:7:23 -cursor-at=%s:8:10 \ +// RUN: -cursor-at=%s:9:15 -cursor-at=%s:10:9 -cursor-at=%s:15:10 \ +// RUN: %s | FileCheck %s +// CHECK: VarDecl=top_var +// CHECK: FunctionDecl=top_func_decl +// CHECK: ParmDecl=param1 +// CHECK: FunctionDecl=top_func_def +// CHECK: ParmDecl=param2 +// CHECK: VarDecl=local_var1 +// CHECK: VarDecl=for_var +// CHECK: VarDecl=local_var2 +// CHECK: FieldDecl=field_var + +// FIXME: Eliminate these once clang_getCursor supports them. // RUN: index-test %t.ast -point-at %s:9:43 > %t // RUN: grep '++for_var' %t -// RUN: index-test %t.ast -point-at %s:10:9 | grep local_var2 - // RUN: index-test %t.ast -point-at %s:10:30 > %t // RUN: grep 'for_var + 1' %t - -// fields test. -// RUN: index-test %t.ast -point-at %s:15:10 | grep field_var diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 01a0d43929..727ad66d57 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -4,6 +4,7 @@ #include #include #include +#include /******************************************************************************/ /* Utility functions. */ @@ -586,6 +587,71 @@ int perform_code_completion(int argc, const char **argv) { return 0; } +typedef struct { + char *filename; + unsigned line; + unsigned column; +} CursorSourceLocation; + +int inspect_cursor_at(int argc, const char **argv) { + CXIndex CIdx; + int errorCode; + struct CXUnsavedFile *unsaved_files = 0; + int num_unsaved_files = 0; + CXTranslationUnit TU; + CXCursor Cursor; + CursorSourceLocation *Locations = 0; + unsigned NumLocations = 0, Loc; + + /* Count the number of locations. */ + while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) + ++NumLocations; + + /* Parse the locations. */ + assert(NumLocations > 0 && "Unable to count locations?"); + Locations = (CursorSourceLocation *)malloc( + NumLocations * sizeof(CursorSourceLocation)); + for (Loc = 0; Loc < NumLocations; ++Loc) { + const char *input = argv[Loc + 1] + strlen("-cursor-at="); + if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, + &Locations[Loc].line, + &Locations[Loc].column))) + return errorCode; + } + + if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, + &num_unsaved_files)) + return -1; + + if (num_unsaved_files > 0) { + fprintf(stderr, "cannot remap files when looking for a cursor\n"); + return -1; + } + + CIdx = clang_createIndex(0, 1); + TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], + argc - num_unsaved_files - 2 - NumLocations, + argv + num_unsaved_files + 1 + NumLocations); + if (!TU) { + fprintf(stderr, "unable to parse input\n"); + return -1; + } + + for (Loc = 0; Loc < NumLocations; ++Loc) { + Cursor = clang_getCursor(TU, Locations[Loc].filename, + Locations[Loc].line, Locations[Loc].column); + PrintCursor(Cursor); + printf("\n"); + free(Locations[Loc].filename); + } + + clang_disposeTranslationUnit(TU); + clang_disposeIndex(CIdx); + free(Locations); + free_remapped_files(unsaved_files, num_unsaved_files); + return 0; +} + /******************************************************************************/ /* Command line processing. */ /******************************************************************************/ @@ -601,6 +667,7 @@ static CXTranslationUnitIterator GetVisitor(const char *s) { static void print_usage(void) { fprintf(stderr, "usage: c-index-test -code-completion-at= \n" + " c-index-test -cursor-at= \n" " c-index-test -test-file-scan " "[FileCheck prefix]\n" " c-index-test -test-load-tu " @@ -608,7 +675,8 @@ static void print_usage(void) { " c-index-test -test-load-tu-usrs " "[FileCheck prefix]\n" " c-index-test -test-load-source {}*\n" - " c-index-test -test-load-source-usrs {}*\n\n" + " c-index-test -test-load-source-usrs {}*\n\n"); + fprintf(stderr, " values:\n%s", " all - load all symbols, including those from PCH\n" " local - load all symbols except those in PCH\n" @@ -623,6 +691,8 @@ static void print_usage(void) { int main(int argc, const char **argv) { if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) return perform_code_completion(argc, argv); + if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) + return inspect_cursor_at(argc, argv); else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { CXTranslationUnitIterator I = GetVisitor(argv[1] + 13); if (I)