[cfe-commits] r93830 - in /cfe/trunk: include/clang-c/Index.h test/Index/TestClassDecl.m test/Index/TestClassForwardDecl.m test/Index/c-index-api-loadTU-test.m tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/c-index-test/c-index-test.c

Douglas Gregor dgregor at apple.com
Mon Jan 18 17:20:05 PST 2010


Author: dgregor
Date: Mon Jan 18 19:20:04 2010
New Revision: 93830

URL: http://llvm.org/viewvc/llvm-project?rev=93830&view=rev
Log:
Introduce clang_getCursorReferenced, to get a cursor pointing at the
entity that a particular cursor references.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/test/Index/TestClassDecl.m
    cfe/trunk/test/Index/TestClassForwardDecl.m
    cfe/trunk/test/Index/c-index-api-loadTU-test.m
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=93830&r1=93829&r2=93830&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Mon Jan 18 19:20:04 2010
@@ -334,10 +334,10 @@
 CINDEX_LINKAGE CXCursor clang_getCursorFromDecl(CXDecl);
 CINDEX_LINKAGE CXEntity clang_getEntityFromDecl(CXIndex, CXDecl);
 CINDEX_LINKAGE CXString clang_getDeclSpelling(CXDecl);
-CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl);
-CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl);
+CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl); /* deprecate */
+CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl); /* deprecate */
 CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl); /* deprecate */
-CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl);
+CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl); /* deprecate */
 
 /**
  * \brief Identifies a specific source location given its file, line, and 
@@ -418,6 +418,18 @@
  * entity was actually used).
  */
 CINDEX_LINKAGE CXSourceRange clang_getCursorExtent(CXCursor);
+
+/** \brief For a cursor that is a reference, retrieve a cursor representing the
+ * entity that it references.
+ *
+ * Reference cursors refer to other entities in the AST. For example, an
+ * Objective-C superclass reference cursor refers to an Objective-C class.
+ * This function produces the cursor for the Objective-C class from the 
+ * cursor for the superclass reference. If the input cursor is a declaration or
+ * definition, it returns that declaration or definition unchanged.
+ * Othewise, returns the NULL cursor.
+ */
+CINDEX_LINKAGE CXCursor clang_getCursorReferenced(CXCursor);
   
 /* for debug/testing */
 CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind); 

Modified: cfe/trunk/test/Index/TestClassDecl.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/TestClassDecl.m?rev=93830&r1=93829&r2=93830&view=diff

==============================================================================
--- cfe/trunk/test/Index/TestClassDecl.m (original)
+++ cfe/trunk/test/Index/TestClassDecl.m Mon Jan 18 19:20:04 2010
@@ -32,7 +32,7 @@
 // CHECK-scan: {start_line=13 start_col=24 end_line=16 end_col=1} FunctionDefn=function:13:6
 // CHECK-scan: {start_line=16 start_col=2 end_line=38 end_col=1} Invalid Cursor => NoDeclFound
 
-// CHECK-load: TestClassDecl.m:10:12: ObjCInterfaceDecl=Foo:10:1 [Extent=10:1:11:4]
+// CHECK-load: TestClassDecl.m:10:12: ObjCInterfaceDecl=Foo:10:12 [Extent=10:1:11:4]
 // CHECK-load: TestClassDecl.m:13:6: FunctionDefn=function:13:6 [Extent=13:6:16:1]
 // CHECK-load: TestClassDecl.m:13:21: ParmDecl=arg:13:21 [Extent=13:21:13:23]
 

Modified: cfe/trunk/test/Index/TestClassForwardDecl.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/TestClassForwardDecl.m?rev=93830&r1=93829&r2=93830&view=diff

==============================================================================
--- cfe/trunk/test/Index/TestClassForwardDecl.m (original)
+++ cfe/trunk/test/Index/TestClassForwardDecl.m Mon Jan 18 19:20:04 2010
@@ -14,12 +14,12 @@
 
 // CHECK-scan: {start_line=1 start_col=1 end_line=7 end_col=1} Invalid Cursor => NoDeclFound
 // CHECK-scan: {start_line=8 start_col=1 end_line=8 end_col=7} Invalid Cursor => NotImplemented
-// CHECK-scan: {start_line=8 start_col=8 end_line=8 end_col=10} ObjCClassRef=Foo:8:1
+// CHECK-scan: {start_line=8 start_col=8 end_line=8 end_col=10} ObjCClassRef=Foo:8:8
 // CHECK-scan: {start_line=8 start_col=11 end_line=9 end_col=1} Invalid Cursor => NoDeclFound
 // CHECK-scan: {start_line=10 start_col=1 end_line=10 end_col=4} FunctionDefn=function:10:6
 // CHECK-scan: {start_line=10 start_col=5 end_line=10 end_col=5} Invalid Cursor => NoDeclFound
 // CHECK-scan: {start_line=10 start_col=6 end_line=10 end_col=14} FunctionDefn=function:10:6
-// CHECK-scan: {start_line=10 start_col=15 end_line=10 end_col=17} ObjCClassRef=Foo:8:1
+// CHECK-scan: {start_line=10 start_col=15 end_line=10 end_col=17} ObjCClassRef=Foo:8:8
 // CHECK-scan: {start_line=10 start_col=18 end_line=10 end_col=18} FunctionDefn=function:10:6
 // CHECK-scan: {start_line=10 start_col=19 end_line=10 end_col=19} ParmDecl=arg:10:21
 // CHECK-scan: {start_line=10 start_col=20 end_line=10 end_col=20} FunctionDefn=function:10:6

Modified: cfe/trunk/test/Index/c-index-api-loadTU-test.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/c-index-api-loadTU-test.m?rev=93830&r1=93829&r2=93830&view=diff

==============================================================================
--- cfe/trunk/test/Index/c-index-api-loadTU-test.m (original)
+++ cfe/trunk/test/Index/c-index-api-loadTU-test.m Mon Jan 18 19:20:04 2010
@@ -53,13 +53,13 @@
   main(someEnum, (const char **)bee);
 }
 
-// CHECK: c-index-api-loadTU-test.m:4:12: ObjCInterfaceDecl=Foo:4:1 [Extent=4:1:11:4]
+// CHECK: c-index-api-loadTU-test.m:4:12: ObjCInterfaceDecl=Foo:4:12 [Extent=4:1:11:4]
 // CHECK: c-index-api-loadTU-test.m:8:1: ObjCInstanceMethodDecl=foo:8:1 [Extent=8:1:8:6]
 // CHECK: c-index-api-loadTU-test.m:9:1: ObjCClassMethodDecl=fooC:9:1 [Extent=9:1:9:7]
-// CHECK: c-index-api-loadTU-test.m:13:12: ObjCInterfaceDecl=Bar:13:1 [Extent=13:1:17:4]
-// CHECK: c-index-api-loadTU-test.m:13:18: ObjCSuperClassRef=Foo:4:1 [Extent=13:18:13:20]
+// CHECK: c-index-api-loadTU-test.m:13:12: ObjCInterfaceDecl=Bar:13:12 [Extent=13:1:17:4]
+// CHECK: c-index-api-loadTU-test.m:13:18: ObjCSuperClassRef=Foo:4:12 [Extent=13:18:13:20]
 // CHECK: c-index-api-loadTU-test.m:19:12: ObjCCategoryDecl=FooCat:19:12 [Extent=19:1:22:4]
-// CHECK: c-index-api-loadTU-test.m:19:12: ObjCClassRef=Foo:4:1 [Extent=19:12:19:14]
+// CHECK: c-index-api-loadTU-test.m:19:12: ObjCClassRef=Foo:4:12 [Extent=19:12:19:14]
 // CHECK: c-index-api-loadTU-test.m:20:1: ObjCInstanceMethodDecl=catMethodWithFloat::20:1 [Extent=20:1:20:40]
 // CHECK: c-index-api-loadTU-test.m:21:1: ObjCInstanceMethodDecl=floatMethod:21:1 [Extent=21:1:21:22]
 // CHECK: c-index-api-loadTU-test.m:24:1: ObjCProtocolDecl=Proto:24:1 [Extent=24:1:26:4]
@@ -67,8 +67,8 @@
 // CHECK: c-index-api-loadTU-test.m:28:1: ObjCProtocolDecl=SubP:28:1 [Extent=28:1:30:4]
 // CHECK: c-index-api-loadTU-test.m:28:17: ObjCProtocolRef=Proto:24:1 [Extent=28:17:28:21]
 // CHECK: c-index-api-loadTU-test.m:29:1: ObjCInstanceMethodDecl=spMethod:29:1 [Extent=29:1:29:11]
-// CHECK: c-index-api-loadTU-test.m:32:12: ObjCInterfaceDecl=Baz:32:1 [Extent=32:1:39:4]
-// CHECK: c-index-api-loadTU-test.m:32:18: ObjCSuperClassRef=Bar:13:1 [Extent=32:18:32:20]
+// CHECK: c-index-api-loadTU-test.m:32:12: ObjCInterfaceDecl=Baz:32:12 [Extent=32:1:39:4]
+// CHECK: c-index-api-loadTU-test.m:32:18: ObjCSuperClassRef=Bar:13:12 [Extent=32:18:32:20]
 // CHECK: c-index-api-loadTU-test.m:32:23: ObjCProtocolRef=SubP:28:1 [Extent=32:23:32:26]
 // CHECK: c-index-api-loadTU-test.m:34:9: ObjCIvarDecl=_anIVar:34:9 [Extent=34:9:34:15]
 // CHECK: c-index-api-loadTU-test.m:37:1: ObjCInstanceMethodDecl=bazMethod:37:1 [Extent=37:1:37:20]

Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=93830&r1=93829&r2=93830&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Mon Jan 18 19:20:04 2010
@@ -1006,7 +1006,46 @@
   Decl *D = getCursorDecl(C);
   return translateSourceRange(D->getASTContext(), D->getSourceRange());
 }
+
+CXCursor clang_getCursorReferenced(CXCursor C) {
+  if (clang_isDeclaration(C.kind) || clang_isDefinition(C.kind))
+    return C;
+  
+  if (!clang_isReference(C.kind))
+    return clang_getNullCursor();
+  
+  switch (C.kind) {
+    case CXCursor_ObjCSuperClassRef:
+      return MakeCXCursor(getCursorObjCSuperClassRef(C).first);
+      
+    case CXCursor_ObjCProtocolRef: {       
+      return MakeCXCursor(getCursorObjCProtocolRef(C).first);
+      
+    case CXCursor_ObjCClassRef:      
+      return MakeCXCursor(getCursorObjCClassRef(C).first);
+      
+    case CXCursor_ObjCSelectorRef:
+    case CXCursor_ObjCIvarRef:
+    case CXCursor_VarRef:
+    case CXCursor_FunctionRef:
+    case CXCursor_EnumConstantRef:
+    case CXCursor_MemberRef: {
+      Decl *D = getDeclFromExpr(getCursorExpr(C));
+      if (D)
+        return MakeCXCursor(D);
+      break;
+    }
+      
+    default:
+      // We would prefer to enumerate all non-reference cursor kinds here.
+      llvm_unreachable("Unhandled reference cursor kind");
+      break;
+    }
+  }
   
+  return clang_getNullCursor();
+}
+
 void clang_getDefinitionSpellingAndExtent(CXCursor C,
                                           const char **startBuf,
                                           const char **endBuf,

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=93830&r1=93829&r2=93830&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Mon Jan 18 19:20:04 2010
@@ -18,6 +18,7 @@
 _clang_getCursorKind
 _clang_getCursorKindSpelling
 _clang_getCursorLocation
+_clang_getCursorReferenced
 _clang_getCursorSpelling
 _clang_getCursorUSR
 _clang_getDeclColumn

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=93830&r1=93829&r2=93830&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Jan 18 19:20:04 2010
@@ -47,16 +47,18 @@
   if (clang_isInvalid(Cursor.kind))
     printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
   else {
-    CXDecl DeclReferenced;
     CXString string;
+    CXCursor Referenced;
     string = clang_getCursorSpelling(Cursor);
     printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
                       clang_getCString(string));
     clang_disposeString(string);
-    DeclReferenced = clang_getCursorDecl(Cursor);
-    if (DeclReferenced)
-      printf(":%d:%d", clang_getDeclLine(DeclReferenced),
-                       clang_getDeclColumn(DeclReferenced));
+    
+    Referenced = clang_getCursorReferenced(Cursor);
+    if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
+      CXSourceLocation Loc = clang_getCursorLocation(Referenced);
+      printf(":%d:%d", Loc.line, Loc.column);
+    }
   }
 }
 





More information about the cfe-commits mailing list