]> granicus.if.org Git - clang/commitdiff
Add -cursor-at=file:line:column command line option to c-index-test,
authorDouglas Gregor <dgregor@apple.com>
Fri, 15 Jan 2010 19:40:17 +0000 (19:40 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 15 Jan 2010 19:40:17 +0000 (19:40 +0000)
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

lib/Index/ResolveLocation.cpp
test/Index/resolve-loc.c
tools/c-index-test/c-index-test.c

index 81a5de44bf5601c28fe4a7c877556ef6f9598d7d..4bb15943bb4d1de1b58c953b6f6f6013291a9797 100644 (file)
@@ -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) {
index 68504ee0d2bc5953aa6bcf9714a1b23740b777c7..1b40ec796256ee4514b3e3bcb354c5e6228bfa8b 100644 (file)
@@ -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
index 01a0d439297b0f225131085638060d2dac1f1054..727ad66d57406b62ac694ee2af9b79ddbf63f21d 100644 (file)
@@ -4,6 +4,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 /******************************************************************************/
 /* 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=<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 @@ static void print_usage(void) {
     "       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 @@ 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)