[cfe-commits] r80787 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp tools/c-index-test/c-index-test.c

Steve Naroff snaroff at apple.com
Wed Sep 2 06:28:55 PDT 2009


Author: snaroff
Date: Wed Sep  2 08:28:54 2009
New Revision: 80787

URL: http://llvm.org/viewvc/llvm-project?rev=80787&view=rev
Log:
Flesh out CXCursorKind...

- More declaration types (distinguish between struct/union/class, instance/class methods).
- Add definition types (class, category, function, instance/class method, etc.).

Add client data to clang_loadDeclaration() and implement.


Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    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=80787&r1=80786&r2=80787&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Wed Sep  2 08:28:54 2009
@@ -35,35 +35,47 @@
 
 typedef void *CXDecl;    /* A specific declaration within a translation unit. */
 
-/* Cursors represent declarations and references (provides line/column info). */
+/* Cursors represent declarations, definitions, and references. */
 enum CXCursorKind {
  CXCursor_Invalid                       = 0,
  
  /* Declarations */
  CXCursor_FirstDecl                     = 1,
- CXCursor_TypedefDecl                   = 1,
- CXCursor_EnumDecl                      = 2,
- CXCursor_EnumConstantDecl              = 3,
- CXCursor_RecordDecl                    = 4, /* struct/union/class */
- CXCursor_FieldDecl                     = 5,
- CXCursor_FunctionDecl                  = 6,
- CXCursor_VarDecl                       = 7,
- CXCursor_ParmDecl                      = 8,
- CXCursor_ObjCInterfaceDecl             = 9,
- CXCursor_ObjCCategoryDecl              = 10,
- CXCursor_ObjCProtocolDecl              = 11,
- CXCursor_ObjCPropertyDecl              = 12,
- CXCursor_ObjCIvarDecl                  = 13,
- CXCursor_ObjCMethodDecl                = 14,
- CXCursor_LastDecl                      = 14,
+ CXCursor_TypedefDecl                   = 2,
+ CXCursor_StructDecl                    = 3, 
+ CXCursor_UnionDecl                     = 4,
+ CXCursor_ClassDecl                     = 5,
+ CXCursor_EnumDecl                      = 6,
+ CXCursor_FieldDecl                     = 7,
+ CXCursor_EnumConstantDecl              = 8,
+ CXCursor_FunctionDecl                  = 9,
+ CXCursor_VarDecl                       = 10,
+ CXCursor_ParmDecl                      = 11,
+ CXCursor_ObjCInterfaceDecl             = 12,
+ CXCursor_ObjCCategoryDecl              = 13,
+ CXCursor_ObjCProtocolDecl              = 14,
+ CXCursor_ObjCPropertyDecl              = 15,
+ CXCursor_ObjCIvarDecl                  = 16,
+ CXCursor_ObjCInstanceMethodDecl        = 17,
+ CXCursor_ObjCClassMethodDecl           = 18,
+ CXCursor_LastDecl                      = 18,
  
+ /* Definitions */
+ CXCursor_FirstDefn                     = 32,
+ CXCursor_FunctionDefn                  = 32,
+ CXCursor_ObjCClassDefn                 = 33,
+ CXCursor_ObjCCategoryDefn              = 34,
+ CXCursor_ObjCInstanceMethodDefn        = 35,
+ CXCursor_ObjCClassMethodDefn           = 36,
+ CXCursor_LastDefn                      = 36,
+   
  /* References */
- CXCursor_FirstRef                      = 19,
- CXCursor_ObjCClassRef                  = 19,            
- CXCursor_ObjCProtocolRef               = 20,
- CXCursor_ObjCMessageRef                = 21,
- CXCursor_ObjCSelectorRef               = 22,
- CXCursor_LastRef                       = 23
+ CXCursor_FirstRef                      = 40,
+ CXCursor_ObjCClassRef                  = 41,            
+ CXCursor_ObjCProtocolRef               = 42,
+ CXCursor_ObjCMessageRef                = 43,
+ CXCursor_ObjCSelectorRef               = 44,
+ CXCursor_LastRef                       = 44
 };
 
 /* A cursor into the CXTranslationUnit. */
@@ -131,9 +143,9 @@
      }
    }
 */
-typedef void (*CXDeclIterator)(CXTranslationUnit, CXDecl, void *clientData);
+typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
 
-void clang_loadDeclaration(CXDecl, CXDeclIterator);
+void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
 
 /*
  * CXEntity Operations.

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Wed Sep  2 08:28:54 2009
@@ -15,6 +15,7 @@
 #include "clang/Index/Program.h"
 #include "clang/Index/Indexer.h"
 #include "clang/AST/DeclVisitor.h"
+#include "clang/AST/Decl.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Frontend/ASTUnit.h"
@@ -51,10 +52,24 @@
     Call(CXCursor_TypedefDecl, ND); 
   }
   void VisitTagDecl(TagDecl *ND) {
-    Call(ND->isEnum() ? CXCursor_EnumDecl : CXCursor_RecordDecl, ND);
+    switch (ND->getTagKind()) {
+      case TagDecl::TK_struct:
+        Call(CXCursor_StructDecl, ND);
+        break;
+      case TagDecl::TK_class:
+        Call(CXCursor_ClassDecl, ND);
+        break;
+      case TagDecl::TK_union:
+        Call(CXCursor_UnionDecl, ND);
+        break;
+      case TagDecl::TK_enum:
+        Call(CXCursor_EnumDecl, ND);
+        break;
+    }
   }
   void VisitFunctionDecl(FunctionDecl *ND) {
-    Call(CXCursor_FunctionDecl, ND);
+    Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
+                                            : CXCursor_FunctionDecl, ND);
   }
   void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
     Call(CXCursor_ObjCInterfaceDecl, ND);
@@ -65,21 +80,65 @@
   void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
     Call(CXCursor_ObjCProtocolDecl, ND);
   }
+  void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
+    Call(CXCursor_ObjCClassDefn, ND);
+  }
+  void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
+    Call(CXCursor_ObjCCategoryDefn, ND);
+  }
 };
 
-// Top-level declaration visitor.
-class TLDeclVisitor : public DeclVisitor<TLDeclVisitor> {
+// Declaration visitor.
+class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
+  CXDecl CDecl;
+  CXDeclIterator Callback;
+  CXClientData CData;
+  
+  void Call(enum CXCursorKind CK, NamedDecl *ND) {
+    CXCursor C = { CK, ND };
+    Callback(CDecl, C, CData);
+  }
 public:
+  CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) : 
+    CDecl(C), Callback(cback), CData(D) {}
+    
+  void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
+    VisitDeclContext(dyn_cast<DeclContext>(D));
+  }
+  void VisitTagDecl(TagDecl *D) {
+    VisitDeclContext(dyn_cast<DeclContext>(D));
+  }
+  void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
+    VisitDeclContext(dyn_cast<DeclContext>(D));
+  }
+  void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
+    VisitDeclContext(dyn_cast<DeclContext>(D));
+  }
   void VisitDeclContext(DeclContext *DC) {
     for (DeclContext::decl_iterator
            I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
       Visit(*I);
   }
   void VisitEnumConstantDecl(EnumConstantDecl *ND) {
+    Call(CXCursor_EnumConstantDecl, ND);
   }
   void VisitFieldDecl(FieldDecl *ND) {
+    Call(CXCursor_FieldDecl, ND);
+  }
+  void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
+    Call(CXCursor_ObjCPropertyDecl, ND);
   }
   void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
+    Call(CXCursor_ObjCIvarDecl, ND);
+  }
+  void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
+    if (ND->getBody()) {
+      Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
+                                  : CXCursor_ObjCClassMethodDefn, ND);
+      // FIXME: load body.
+    } else
+      Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
+                                  : CXCursor_ObjCClassMethodDecl, ND);
   }
 };
 
@@ -105,9 +164,9 @@
 }
 
 
-void clang_loadTranslationUnit(
-  CXTranslationUnit CTUnit, CXTranslationUnitIterator callback,
-  CXClientData CData)
+void clang_loadTranslationUnit(CXTranslationUnit CTUnit, 
+                               CXTranslationUnitIterator callback,
+                               CXClientData CData)
 {
   assert(CTUnit && "Passed null CXTranslationUnit");
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
@@ -117,8 +176,14 @@
   DVisit.Visit(Ctx.getTranslationUnitDecl());
 }
 
-void clang_loadDeclaration(CXDecl, CXDeclIterator)
+void clang_loadDeclaration(CXDecl Dcl, 
+                           CXDeclIterator callback, 
+                           CXClientData CData)
 {
+  assert(Dcl && "Passed null CXDecl");
+  
+  CDeclVisitor DVisit(Dcl, callback, CData);
+  DVisit.Visit(static_cast<Decl *>(Dcl));
 }
 
 // Some notes on CXEntity:
@@ -167,9 +232,13 @@
 {
   assert(AnonDecl && "Passed null CXDecl");
   NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
+  
+  if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
+    return OMD->getSelector().getAsString().c_str();
+  }    
   if (ND->getIdentifier())
     return ND->getIdentifier()->getName();
-  else
+  else 
     return "";
 }
 const char *clang_getKindSpelling(enum CXCursorKind Kind)
@@ -179,7 +248,9 @@
    case CXCursor_TypedefDecl: return "TypedefDecl";
    case CXCursor_EnumDecl: return "EnumDecl";
    case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
-   case CXCursor_RecordDecl: return "RecordDecl";
+   case CXCursor_StructDecl: return "StructDecl";
+   case CXCursor_UnionDecl: return "UnionDecl";
+   case CXCursor_ClassDecl: return "ClassDecl";
    case CXCursor_FieldDecl: return "FieldDecl";
    case CXCursor_VarDecl: return "VarDecl";
    case CXCursor_ParmDecl: return "ParmDecl";
@@ -188,7 +259,13 @@
    case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
    case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
    case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
-   case CXCursor_ObjCMethodDecl: return "ObjCMethodDecl";
+   case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
+   case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
+   case CXCursor_FunctionDefn: return "FunctionDefn";
+   case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
+   case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
+   case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
+   case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
    default: return "<not implemented>";
   }
 }

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=80787&r1=80786&r2=80787&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Wed Sep  2 08:28:54 2009
@@ -3,16 +3,28 @@
 #include "clang-c/Index.h"
 #include <stdio.h>
 
-static void PrintDecls(CXTranslationUnit Unit, CXCursor Cursor,
-                       CXClientData Filter) {
-  if (clang_isDeclaration(Cursor.kind)) {
-    if (Cursor.kind == *(enum CXCursorKind *)Filter) {
-      printf("%s => %s", clang_getKindSpelling(Cursor.kind),
-                         clang_getDeclSpelling(Cursor.decl));
-      printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor),
-                              clang_getCursorLine(Cursor),
-                              clang_getCursorColumn(Cursor));
-    }
+static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) 
+{
+  if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
+    printf("%s => %s", clang_getKindSpelling(Cursor.kind),
+                       clang_getDeclSpelling(Cursor.decl));
+    printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor),
+                            clang_getCursorLine(Cursor),
+                            clang_getCursorColumn(Cursor));
+  }
+}
+static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
+                                   CXClientData Filter) 
+{
+  if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
+    printf("%s => %s", clang_getKindSpelling(Cursor.kind),
+                       clang_getDeclSpelling(Cursor.decl));
+    printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor),
+                            clang_getCursorLine(Cursor),
+                            clang_getCursorColumn(Cursor));
+
+    enum CXCursorKind filterData = CXCursor_FieldDecl;
+    clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
   }
 }
 
@@ -23,8 +35,7 @@
   CXIndex Idx = clang_createIndex();
   CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
   
-  /* Use client data to only print ObjC interfaces */
-  enum CXCursorKind filterData = CXCursor_ObjCInterfaceDecl;
-  clang_loadTranslationUnit(TU, PrintDecls, &filterData);
+  enum CXCursorKind filterData = CXCursor_StructDecl;
+  clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
   return 1;
 }





More information about the cfe-commits mailing list