[cfe-commits] r89136 - in /cfe/trunk: test/Index/c-index-getCursor-test.m tools/c-index-test/c-index-test.c

Ted Kremenek kremenek at apple.com
Tue Nov 17 11:37:37 PST 2009


Author: kremenek
Date: Tue Nov 17 13:37:36 2009
New Revision: 89136

URL: http://llvm.org/viewvc/llvm-project?rev=89136&view=rev
Log:
Add c-index-test mode "-test-file-scan", which scans a source file and calls clang_getCursor() on every character.

Added:
    cfe/trunk/test/Index/c-index-getCursor-test.m
Modified:
    cfe/trunk/tools/c-index-test/c-index-test.c

Added: cfe/trunk/test/Index/c-index-getCursor-test.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/c-index-getCursor-test.m?rev=89136&view=auto

==============================================================================
--- cfe/trunk/test/Index/c-index-getCursor-test.m (added)
+++ cfe/trunk/test/Index/c-index-getCursor-test.m Tue Nov 17 13:37:36 2009
@@ -0,0 +1,160 @@
+// RUN: clang-cc -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fblocks -emit-pch -x objective-c %s -o %t.ast
+// RUN: c-index-test -test-file-scan %t.ast %s | FileCheck %s
+ at interface Foo 
+{
+}
+
+- foo;
++ fooC;
+
+ at end
+
+ at interface Bar : Foo 
+{
+}
+
+ at end
+
+ at interface Foo (FooCat)
+- (int) catMethodWithFloat:(float) fArg;
+- (float) floatMethod;
+ at end
+
+ at protocol Proto
+- pMethod;
+ at end
+
+ at protocol SubP <Proto>
+- spMethod;
+ at end
+
+ at interface Baz : Bar <SubP>
+{
+    int _anIVar;
+}
+
+- (Foo *) bazMethod;
+
+ at end
+
+enum {
+  someEnum
+};
+
+int main (int argc, const char * argv[]) {
+	Baz * bee;
+	id a = [bee foo];
+	id <SubP> c = [Foo fooC];
+	id <Proto> d;
+	d = c;
+	[d pMethod];
+	[bee catMethodWithFloat:[bee floatMethod]];
+  main(someEnum, (const char **)bee);
+}
+
+// CHECK: {start_line=1 start_col=1 end_line=2 end_col=62} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=3 start_col=1 end_line=6 end_col=1} ObjCInterfaceDecl=Foo:3:1
+// CHECK: {start_line=7 start_col=1 end_line=7 end_col=6} ObjCInstanceMethodDecl=foo:7:1
+// CHECK: {start_line=7 start_col=7 end_line=7 end_col=7} ObjCInterfaceDecl=Foo:3:1
+// CHECK: {start_line=8 start_col=1 end_line=8 end_col=7} ObjCClassMethodDecl=fooC:8:1
+// CHECK: {start_line=8 start_col=8 end_line=10 end_col=1} ObjCInterfaceDecl=Foo:3:1
+// CHECK: {start_line=10 start_col=2 end_line=11 end_col=1} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=12 start_col=1 end_line=16 end_col=1} ObjCInterfaceDecl=Bar:12:1
+// CHECK: {start_line=16 start_col=2 end_line=17 end_col=1} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=18 start_col=1 end_line=18 end_col=24} ObjCCategoryDecl=FooCat:18:1
+// CHECK: {start_line=19 start_col=1 end_line=19 end_col=28} ObjCInstanceMethodDecl=catMethodWithFloat::19:1
+// CHECK: {start_line=19 start_col=29 end_line=19 end_col=33} ParmDecl=fArg:19:36
+// CHECK: {start_line=19 start_col=34 end_line=19 end_col=35} ObjCInstanceMethodDecl=catMethodWithFloat::19:1
+// CHECK: {start_line=19 start_col=36 end_line=19 end_col=39} ParmDecl=fArg:19:36
+// CHECK: {start_line=19 start_col=40 end_line=19 end_col=40} ObjCInstanceMethodDecl=catMethodWithFloat::19:1
+// CHECK: {start_line=19 start_col=41 end_line=19 end_col=41} ObjCCategoryDecl=FooCat:18:1
+// CHECK: {start_line=20 start_col=1 end_line=20 end_col=22} ObjCInstanceMethodDecl=floatMethod:20:1
+// CHECK: {start_line=20 start_col=23 end_line=21 end_col=1} ObjCCategoryDecl=FooCat:18:1
+// CHECK: {start_line=21 start_col=2 end_line=22 end_col=1} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=23 start_col=1 end_line=23 end_col=16} ObjCProtocolDecl=Proto:23:1
+// CHECK: {start_line=24 start_col=1 end_line=24 end_col=10} ObjCInstanceMethodDecl=pMethod:24:1
+// CHECK: {start_line=24 start_col=11 end_line=25 end_col=1} ObjCProtocolDecl=Proto:23:1
+// CHECK: {start_line=25 start_col=2 end_line=26 end_col=1} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=27 start_col=1 end_line=27 end_col=23} ObjCProtocolDecl=SubP:27:1
+// CHECK: {start_line=28 start_col=1 end_line=28 end_col=11} ObjCInstanceMethodDecl=spMethod:28:1
+// CHECK: {start_line=28 start_col=12 end_line=29 end_col=1} ObjCProtocolDecl=SubP:27:1
+// CHECK: {start_line=29 start_col=2 end_line=30 end_col=1} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=31 start_col=1 end_line=33 end_col=4} ObjCInterfaceDecl=Baz:31:1
+// CHECK: {start_line=33 start_col=5 end_line=33 end_col=7} Invalid Cursor => NotImplemented
+// CHECK: {start_line=33 start_col=8 end_line=33 end_col=8} ObjCInterfaceDecl=Baz:31:1
+// CHECK: {start_line=33 start_col=9 end_line=33 end_col=15} Invalid Cursor => NotImplemented
+// CHECK: {start_line=33 start_col=16 end_line=35 end_col=1} ObjCInterfaceDecl=Baz:31:1
+// CHECK: {start_line=36 start_col=1 end_line=36 end_col=20} ObjCInstanceMethodDecl=bazMethod:36:1
+// CHECK: {start_line=36 start_col=21 end_line=38 end_col=1} ObjCInterfaceDecl=Baz:31:1
+// CHECK: {start_line=38 start_col=2 end_line=39 end_col=1} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=40 start_col=1 end_line=41 end_col=2} EnumDecl=:40:1
+// CHECK: {start_line=41 start_col=3 end_line=41 end_col=10} EnumConstantDecl=someEnum:41:3
+// CHECK: {start_line=41 start_col=11 end_line=42 end_col=1} EnumDecl=:40:1
+// CHECK: {start_line=42 start_col=2 end_line=43 end_col=1} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=44 start_col=1 end_line=44 end_col=3} FunctionDecl=main:44:5
+// CHECK: {start_line=44 start_col=4 end_line=44 end_col=4} Invalid Cursor => NoDeclFound
+// CHECK: {start_line=44 start_col=5 end_line=44 end_col=10} FunctionDecl=main:44:5
+// CHECK: {start_line=44 start_col=11 end_line=44 end_col=13} ParmDecl=argc:44:15
+// CHECK: {start_line=44 start_col=14 end_line=44 end_col=14} FunctionDecl=main:44:5
+// CHECK: {start_line=44 start_col=15 end_line=44 end_col=18} ParmDecl=argc:44:15
+// CHECK: {start_line=44 start_col=19 end_line=44 end_col=26} FunctionDecl=main:44:5
+// CHECK: {start_line=44 start_col=27 end_line=44 end_col=30} ParmDecl=argv:44:34
+// CHECK: {start_line=44 start_col=31 end_line=44 end_col=31} FunctionDecl=main:44:5
+// CHECK: {start_line=44 start_col=32 end_line=44 end_col=32} ParmDecl=argv:44:34
+// CHECK: {start_line=44 start_col=33 end_line=44 end_col=33} FunctionDecl=main:44:5
+// CHECK: {start_line=44 start_col=34 end_line=44 end_col=39} ParmDecl=argv:44:34
+// CHECK: {start_line=44 start_col=40 end_line=45 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=45 start_col=2 end_line=45 end_col=4} ObjCClassRef=Baz:45:8
+// CHECK: {start_line=45 start_col=5 end_line=45 end_col=5} FunctionDecl=main:44:5
+// CHECK: {start_line=45 start_col=6 end_line=45 end_col=6} VarDecl=bee:45:8
+// CHECK: {start_line=45 start_col=7 end_line=45 end_col=7} FunctionDecl=main:44:5
+// CHECK: {start_line=45 start_col=8 end_line=45 end_col=10} VarDecl=bee:45:8
+// CHECK: {start_line=45 start_col=11 end_line=46 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=46 start_col=2 end_line=46 end_col=3} TypedefDecl=id:0:0
+// CHECK: {start_line=46 start_col=4 end_line=46 end_col=4} FunctionDecl=main:44:5
+// CHECK: {start_line=46 start_col=5 end_line=46 end_col=8} VarDecl=a:46:5
+// CHECK: {start_line=46 start_col=9 end_line=46 end_col=9} ObjCSelectorRef=foo:7:1
+// CHECK: {start_line=46 start_col=10 end_line=46 end_col=12} VarRef=bee:45:8
+// CHECK: {start_line=46 start_col=13 end_line=46 end_col=17} ObjCSelectorRef=foo:7:1
+// CHECK: {start_line=46 start_col=18 end_line=47 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=47 start_col=2 end_line=47 end_col=3} TypedefDecl=id:0:0
+// CHECK: {start_line=47 start_col=4 end_line=47 end_col=4} FunctionDecl=main:44:5
+// CHECK: {start_line=47 start_col=5 end_line=47 end_col=5} VarDecl=c:47:12
+// CHECK: {start_line=47 start_col=6 end_line=47 end_col=9} ObjCProtocolRef=SubP:47:12
+// CHECK: {start_line=47 start_col=10 end_line=47 end_col=10} VarDecl=c:47:12
+// CHECK: {start_line=47 start_col=11 end_line=47 end_col=11} FunctionDecl=main:44:5
+// CHECK: {start_line=47 start_col=12 end_line=47 end_col=15} VarDecl=c:47:12
+// CHECK: {start_line=47 start_col=16 end_line=47 end_col=25} ObjCSelectorRef=fooC:8:1
+// CHECK: {start_line=47 start_col=26 end_line=48 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=48 start_col=2 end_line=48 end_col=3} TypedefDecl=id:0:0
+// CHECK: {start_line=48 start_col=4 end_line=48 end_col=4} FunctionDecl=main:44:5
+// CHECK: {start_line=48 start_col=5 end_line=48 end_col=5} VarDecl=d:48:13
+// CHECK: {start_line=48 start_col=6 end_line=48 end_col=10} ObjCProtocolRef=Proto:48:13
+// CHECK: {start_line=48 start_col=11 end_line=48 end_col=11} VarDecl=d:48:13
+// CHECK: {start_line=48 start_col=12 end_line=48 end_col=12} FunctionDecl=main:44:5
+// CHECK: {start_line=48 start_col=13 end_line=48 end_col=13} VarDecl=d:48:13
+// CHECK: {start_line=48 start_col=14 end_line=49 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=49 start_col=2 end_line=49 end_col=2} VarRef=d:48:13
+// CHECK: {start_line=49 start_col=3 end_line=49 end_col=5} FunctionDecl=main:44:5
+// CHECK: {start_line=49 start_col=6 end_line=49 end_col=6} VarRef=c:47:12
+// CHECK: {start_line=49 start_col=7 end_line=50 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=50 start_col=2 end_line=50 end_col=2} ObjCSelectorRef=pMethod:24:1
+// CHECK: {start_line=50 start_col=3 end_line=50 end_col=3} VarRef=d:48:13
+// CHECK: {start_line=50 start_col=4 end_line=50 end_col=12} ObjCSelectorRef=pMethod:24:1
+// CHECK: {start_line=50 start_col=13 end_line=51 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=51 start_col=2 end_line=51 end_col=2} ObjCSelectorRef=catMethodWithFloat::19:1
+// CHECK: {start_line=51 start_col=3 end_line=51 end_col=5} VarRef=bee:45:8
+// CHECK: {start_line=51 start_col=6 end_line=51 end_col=25} ObjCSelectorRef=catMethodWithFloat::19:1
+// CHECK: {start_line=51 start_col=26 end_line=51 end_col=26} ObjCSelectorRef=floatMethod:20:1
+// CHECK: {start_line=51 start_col=27 end_line=51 end_col=29} VarRef=bee:45:8
+// CHECK: {start_line=51 start_col=30 end_line=51 end_col=42} ObjCSelectorRef=floatMethod:20:1
+// CHECK: {start_line=51 start_col=43 end_line=51 end_col=43} ObjCSelectorRef=catMethodWithFloat::19:1
+// CHECK: {start_line=51 start_col=44 end_line=52 end_col=2} FunctionDecl=main:44:5
+// CHECK: {start_line=52 start_col=3 end_line=52 end_col=6} FunctionRef=main:44:5
+// CHECK: {start_line=52 start_col=7 end_line=52 end_col=7} FunctionDecl=main:44:5
+// CHECK: {start_line=52 start_col=8 end_line=52 end_col=15} EnumConstantRef=someEnum:41:3
+// CHECK: {start_line=52 start_col=16 end_line=52 end_col=32} FunctionDecl=main:44:5
+// CHECK: {start_line=52 start_col=33 end_line=52 end_col=35} VarRef=bee:45:8
+// CHECK: {start_line=52 start_col=36 end_line=53 end_col=1} FunctionDecl=main:44:5
+// CHECK: {start_line=53 start_col=2 end_line=160 end_col=1} Invalid Cursor => NoDeclFound
+

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=89136&r1=89135&r2=89136&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Tue Nov 17 13:37:36 2009
@@ -27,13 +27,25 @@
 extern char *basename(const char *);
 #endif
 
+static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
+                                      CXTranslationUnit *TU) {
+  
+  *TU = clang_createTranslationUnit(Idx, file);
+  if (!TU) {
+    fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
+    return 0;
+  }  
+  return 1;
+}
+
+
 /******************************************************************************/
 /* Pretty-printing.                                                           */
 /******************************************************************************/
 
 static void PrintCursor(CXCursor Cursor) {
   if (clang_isInvalid(Cursor.kind))
-    printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
+    printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
   else {
     CXDecl DeclReferenced;
     CXString string;
@@ -136,12 +148,8 @@
                           !strcmp(filter, "local") ? 1 : 0, 
                           /* displayDiagnostics */ 1);
   
-  TU = clang_createTranslationUnit(Idx, file);
-  
-  if (!TU) {
-    fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
+  if (!CreateTranslationUnit(Idx, file, &TU))
     return 1;
-  }
   
   /* Perform some simple filtering. */
   if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
@@ -161,6 +169,89 @@
 }
 
 /******************************************************************************/
+/* Logic for testing clang_getCursor().                                       */
+/******************************************************************************/
+
+static void print_cursor_file_scan(CXCursor cursor,
+                                   unsigned start_line, unsigned start_col,
+                                   unsigned end_line, unsigned end_col) {
+  printf("CHECK: {start_line=%d start_col=%d end_line=%d end_col=%d} ",
+         start_line, start_col, end_line, end_col);
+  PrintCursor(cursor);
+  printf("\n");
+}
+
+static int perform_file_scan(const char *ast_file, const char *source_file) {
+  CXIndex Idx;
+  CXTranslationUnit TU;
+  FILE *fp;
+  unsigned line;
+  CXCursor prevCursor;
+  unsigned printed;
+  unsigned start_line, start_col, last_line, last_col;
+  size_t i;
+  
+  if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
+                                /* displayDiagnostics */ 1))) {
+    fprintf(stderr, "Could not create Index\n");
+    return 1;
+  }
+  
+  if (!CreateTranslationUnit(Idx, ast_file, &TU))
+    return 1;
+  
+  if ((fp = fopen(source_file, "r")) == NULL) {
+    fprintf(stderr, "Could not open '%s'\n", source_file);
+    return 1;
+  }
+  
+  line = 0;
+  prevCursor = clang_getNullCursor();
+  printed = 0;
+  start_line = last_line = 1;
+  start_col = last_col = 1;
+  
+  while (!feof(fp)) {
+    size_t len;
+    const char *buf;
+        
+    if ((buf = fgetln(fp, &len)) == NULL)
+      break;
+    
+    ++line;
+    
+    for (i = 0; i < len ; ++i) {
+      CXCursor cursor;
+      cursor = clang_getCursor(TU, source_file, line, i+1);
+
+      if (!clang_equalCursors(cursor, prevCursor) &&
+          prevCursor.kind != CXCursor_InvalidFile) {
+        print_cursor_file_scan(prevCursor, start_line, start_col,
+                               last_line, last_col);
+        printed = 1;
+        start_line = line;
+        start_col = (unsigned) i+1;
+      }
+      else {
+        printed = 0;
+      }
+      
+      prevCursor = cursor;
+      last_line = line;
+      last_col = (unsigned) i+1;
+    }    
+  }
+  
+  if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
+    print_cursor_file_scan(prevCursor, start_line, start_col,
+                           last_line, last_col);
+  }  
+  
+  fclose(fp);
+  return 0;
+}
+
+/******************************************************************************/
 /* Logic for testing clang_codeComplete().                                    */
 /******************************************************************************/
 
@@ -292,6 +383,7 @@
 static void print_usage(void) {
   fprintf(stderr,
     "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
+    "       c-index-test -test-file-scan <AST file> <source file>\n"
     "       c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
     " <symbol filter> options for -test-load-tu:\n%s",
     "   all - load all symbols, including those from PCH\n"
@@ -308,6 +400,8 @@
     return perform_code_completion(argc, argv);
   if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
     return perform_test_load_tu(argv[2], argv[3]);
+  if (argc == 4 && strcmp(argv[1], "-test-file-scan") == 0)
+    return perform_file_scan(argv[2], argv[3]);
 
   print_usage();
   return 1;





More information about the cfe-commits mailing list