[cfe-commits] r94078 - in /cfe/trunk: include/clang-c/Index.h test/Index/c-index-api-loadTU-test.m test/Index/cindex-from-source.m tools/CIndex/CIndex.cpp tools/CIndex/CXCursor.cpp tools/CIndex/CXCursor.h

Douglas Gregor dgregor at apple.com
Thu Jan 21 08:28:36 PST 2010


Author: dgregor
Date: Thu Jan 21 10:28:34 2010
New Revision: 94078

URL: http://llvm.org/viewvc/llvm-project?rev=94078&view=rev
Log:
Introduce type references into the C API, capturing references to
typedefs only (for now).

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/test/Index/c-index-api-loadTU-test.m
    cfe/trunk/test/Index/cindex-from-source.m
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CXCursor.cpp
    cfe/trunk/tools/CIndex/CXCursor.h

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

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Jan 21 10:28:34 2010
@@ -128,7 +128,23 @@
  CXCursor_ObjCSuperClassRef             = 40,            
  CXCursor_ObjCProtocolRef               = 41,
  CXCursor_ObjCClassRef                  = 42,
- CXCursor_LastRef                       = 42,
+ /**
+  * \brief A reference to a type declaration.
+  *
+  * A type reference occurs anywhere where a type is named but not
+  * declared. For example, given:
+  *
+  * \code
+  * typedef unsigned size_type;
+  * size_type size;
+  * \endcode
+  *
+  * The typedef is a declaration of size_type (CXCursor_TypedefDecl),
+  * while the type of the variable "size" is referenced. The cursor
+  * referenced by the type of size is the typedef for size_type.
+  */
+ CXCursor_TypeRef                       = 43,
+ CXCursor_LastRef                       = 43,
  
  /* Error conditions */
  CXCursor_FirstInvalid                  = 70,

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=94078&r1=94077&r2=94078&view=diff

==============================================================================
--- cfe/trunk/test/Index/c-index-api-loadTU-test.m (original)
+++ cfe/trunk/test/Index/c-index-api-loadTU-test.m Thu Jan 21 10:28:34 2010
@@ -79,6 +79,7 @@
 // CHECK: c-index-api-loadTU-test.m:45:34: ParmDecl=argv:45:34 (Definition) [Extent=45:34:45:37]
 // CHECK: c-index-api-loadTU-test.m:46:8: VarDecl=bee:46:8 (Definition) [Extent=46:8:46:10]
 // CHECK: c-index-api-loadTU-test.m:47:5: VarDecl=a:47:5 (Definition) [Extent=47:5:47:17]
+// CHECK: c-index-api-loadTU-test.m:47:2: TypeRef=id:0:0 [Extent=47:2:47:3]
 // CHECK: c-index-api-loadTU-test.m:48:12: VarDecl=c:48:12 (Definition) [Extent=48:12:48:25]
 // CHECK: c-index-api-loadTU-test.m:49:13: VarDecl=d:49:13 (Definition) [Extent=49:13:49:13]
 

Modified: cfe/trunk/test/Index/cindex-from-source.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/cindex-from-source.m?rev=94078&r1=94077&r2=94078&view=diff

==============================================================================
--- cfe/trunk/test/Index/cindex-from-source.m (original)
+++ cfe/trunk/test/Index/cindex-from-source.m Thu Jan 21 10:28:34 2010
@@ -4,6 +4,6 @@
 // RUN: FileCheck %s < %t
 // CHECK: cindex-from-source.m:{{.*}}:{{.*}}: StructDecl=s0:{{.*}}:{{.*}}
 // CHECK: cindex-from-source.m:{{.*}}:{{.*}}: VarDecl=g0:{{.*}}:{{.*}}
-
+// CHECK: cindex-from-source.m:9:1: TypeRef=t0:1:13 [Extent=9:1:9:2]
 struct s0 {};
 t0 g0;

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Thu Jan 21 10:28:34 2010
@@ -17,6 +17,7 @@
 
 #include "clang/AST/DeclVisitor.h"
 #include "clang/AST/StmtVisitor.h"
+#include "clang/AST/TypeLocVisitor.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/System/Program.h"
@@ -140,7 +141,9 @@
 namespace {
   
 // Cursor visitor.
-class CursorVisitor : public DeclVisitor<CursorVisitor, bool> {
+class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
+                      public TypeLocVisitor<CursorVisitor, bool>
+{
   ASTUnit *TU;
   CXCursor Parent;
   CXCursorVisitor Visitor;
@@ -152,6 +155,7 @@
   unsigned MaxPCHLevel;
   
   using DeclVisitor<CursorVisitor, bool>::Visit;
+  using TypeLocVisitor<CursorVisitor, bool>::Visit;
   
 public:
   CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData, 
@@ -167,15 +171,19 @@
   bool Visit(CXCursor Cursor);
   bool VisitChildren(CXCursor Parent);
   
+  // Declaration visitors
   bool VisitDeclContext(DeclContext *DC);
-  
   bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
+  bool VisitDeclaratorDecl(DeclaratorDecl *DD);
   bool VisitFunctionDecl(FunctionDecl *ND);
   bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
   bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
   bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
   bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
   bool VisitTagDecl(TagDecl *D);
+
+  // Type visitors
+  bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
 };
   
 } // end anonymous namespace
@@ -279,6 +287,14 @@
   return false;
 }
 
+bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
+  if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
+    if (Visit(TSInfo->getTypeLoc()))
+      return true;
+
+  return false;
+}
+
 bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
   // FIXME: This is wrong. We always want to visit the parameters and
   // the body, if available.
@@ -349,6 +365,10 @@
   return VisitDeclContext(D);
 }
 
+bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
+  return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
+}
+
 CXString CIndexer::createCXString(const char *String, bool DupString){
   CXString Str;
   if (DupString) {
@@ -705,6 +725,15 @@
       assert(OID && "getCursorSpelling(): Missing protocol decl");
       return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
     }
+    case CXCursor_TypeRef: {
+      TypeDecl *Type = getCursorTypeRef(C).first;
+      assert(Type && "Missing type decl");
+
+      return CIndexer::createCXString(
+               getCursorContext(C).getTypeDeclType(Type).getAsString().c_str(),
+                                      true);
+    }
+
     default:
       return CIndexer::createCXString("<not implemented>");
     }
@@ -745,6 +774,7 @@
   case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
   case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
   case CXCursor_ObjCClassRef: return "ObjCClassRef";
+  case CXCursor_TypeRef: return "TypeRef";
   case CXCursor_UnexposedExpr: return "UnexposedExpr";
   case CXCursor_DeclRefExpr: return "DeclRefExpr";
   case CXCursor_MemberRefExpr: return "MemberRefExpr";
@@ -870,6 +900,11 @@
         = getCursorObjCClassRef(C);
       return translateSourceLocation(P.first->getASTContext(), P.second);
     }
+
+    case CXCursor_TypeRef: {       
+      std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
+      return translateSourceLocation(P.first->getASTContext(), P.second);
+    }
       
     default:
       // FIXME: Need a way to enumerate all non-reference cases.
@@ -914,6 +949,11 @@
         
         return translateSourceRange(P.first->getASTContext(), P.second);
       }
+
+      case CXCursor_TypeRef: {       
+        std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
+        return translateSourceRange(P.first->getASTContext(), P.second);
+      }
         
       default:
         // FIXME: Need a way to enumerate all non-reference cases.
@@ -961,6 +1001,9 @@
       
     case CXCursor_ObjCClassRef:      
       return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
+
+    case CXCursor_TypeRef:      
+      return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
       
     default:
       // We would prefer to enumerate all non-reference cursor kinds here.

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

==============================================================================
--- cfe/trunk/tools/CIndex/CXCursor.cpp (original)
+++ cfe/trunk/tools/CIndex/CXCursor.cpp Thu Jan 21 10:28:34 2010
@@ -259,6 +259,21 @@
                                       reinterpret_cast<uintptr_t>(C.data[1])));
 }
 
+CXCursor cxcursor::MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc, 
+                                     ASTUnit *TU) {
+  void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
+  CXCursor C = { CXCursor_TypeRef, { Type, RawLoc, TU } };
+  return C;    
+}
+
+std::pair<TypeDecl *, SourceLocation> 
+cxcursor::getCursorTypeRef(CXCursor C) {
+  assert(C.kind == CXCursor_TypeRef);
+  return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
+           SourceLocation::getFromRawEncoding(
+                                      reinterpret_cast<uintptr_t>(C.data[1])));
+}
+
 Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
   return (Decl *)Cursor.data[0];
 }

Modified: cfe/trunk/tools/CIndex/CXCursor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CXCursor.h?rev=94078&r1=94077&r2=94078&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CXCursor.h (original)
+++ cfe/trunk/tools/CIndex/CXCursor.h Thu Jan 21 10:28:34 2010
@@ -28,6 +28,7 @@
 class ObjCInterfaceDecl;
 class ObjCProtocolDecl;
 class Stmt;
+class TypeDecl;
 
 namespace cxcursor {
   
@@ -63,6 +64,13 @@
 std::pair<ObjCInterfaceDecl *, SourceLocation> 
   getCursorObjCClassRef(CXCursor C);
 
+/// \brief Create a type reference at the given location.
+CXCursor MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc, ASTUnit *TU);
+
+/// \brief Unpack a TypeRef cursor into the class it references
+/// and optionally the location where the reference occurred.
+std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
+
 Decl *getCursorDecl(CXCursor Cursor);
 Expr *getCursorExpr(CXCursor Cursor);
 Stmt *getCursorStmt(CXCursor Cursor);





More information about the cfe-commits mailing list