[cfe-commits] r93537 - in /cfe/trunk: lib/Index/ResolveLocation.cpp test/Index/resolve-loc.c tools/c-index-test/c-index-test.c

Douglas Gregor dgregor at apple.com
Fri Jan 15 11:40:18 PST 2010


Author: dgregor
Date: Fri Jan 15 13:40:17 2010
New Revision: 93537

URL: http://llvm.org/viewvc/llvm-project?rev=93537&view=rev
Log:
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.

Modified:
    cfe/trunk/lib/Index/ResolveLocation.cpp
    cfe/trunk/test/Index/resolve-loc.c
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/lib/Index/ResolveLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/ResolveLocation.cpp?rev=93537&r1=93536&r2=93537&view=diff

==============================================================================
--- cfe/trunk/lib/Index/ResolveLocation.cpp (original)
+++ cfe/trunk/lib/Index/ResolveLocation.cpp Fri Jan 15 13:40:17 2010
@@ -264,7 +264,7 @@
     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) {

Modified: cfe/trunk/test/Index/resolve-loc.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/resolve-loc.c?rev=93537&r1=93536&r2=93537&view=diff

==============================================================================
--- cfe/trunk/test/Index/resolve-loc.c (original)
+++ cfe/trunk/test/Index/resolve-loc.c Fri Jan 15 13:40:17 2010
@@ -15,23 +15,25 @@
   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

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=93537&r1=93536&r2=93537&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Fri Jan 15 13:40:17 2010
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 /******************************************************************************/
 /* Utility functions.                                                         */
@@ -586,6 +587,71 @@
   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 void print_usage(void) {
   fprintf(stderr,
     "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
+    "       c-index-test -cursor-at=<site> <compiler arguments>\n"
     "       c-index-test -test-file-scan <AST file> <source file> "
           "[FileCheck prefix]\n"
     "       c-index-test -test-load-tu <AST file> <symbol filter> "
@@ -608,7 +675,8 @@
     "       c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
            "[FileCheck prefix]\n"
     "       c-index-test -test-load-source <symbol filter> {<args>}*\n"
-    "       c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n"
+    "       c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
+  fprintf(stderr,
     " <symbol filter> 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 @@
 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)





More information about the cfe-commits mailing list