[cfe-commits] r93954 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/CIndex/CXCursor.cpp

Douglas Gregor dgregor at apple.com
Tue Jan 19 16:23:15 PST 2010


Author: dgregor
Date: Tue Jan 19 18:23:15 2010
New Revision: 93954

URL: http://llvm.org/viewvc/llvm-project?rev=93954&view=rev
Log:
Introduce a special cursor kind for the translation unit, to serve as
the root of the conceptual cursor hierarchy (just like we do with
declarations). This will be used when we get to unify
clang_loadTranslationUnit() and clang_loadDeclaration() into something
more generally useful.


Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.cpp
    cfe/trunk/tools/CIndex/CIndex.exports
    cfe/trunk/tools/CIndex/CXCursor.cpp

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

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Jan 19 18:23:15 2010
@@ -168,7 +168,15 @@
   * reported.
   */
  CXCursor_UnexposedStmt                 = 200,
- CXCursor_LastStmt                      = 200
+ CXCursor_LastStmt                      = 200,
+
+ /**
+  * \brief Cursor that represents the translation unit itself.
+  *
+  * The translation unit cursor exists primarily to act as the root
+  * cursor for traversing the contents of a translation unit.
+  */
+ CXCursor_TranslationUnit               = 300
 };
 
 /**
@@ -380,6 +388,14 @@
 
 CINDEX_LINKAGE void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
 
+/**
+ * \brief Retrieve the cursor that represents the given translation unit.
+ *
+ * The translation unit cursor can be used to start traversing the
+ * various declarations within the given translation unit.
+ */
+CINDEX_LINKAGE CXCursor clang_getTranslationUnitCursor(CXTranslationUnit);
+
 /*
  * CXFile Operations.
  */
@@ -492,7 +508,7 @@
 CINDEX_LINKAGE unsigned clang_isExpression(enum CXCursorKind);
 CINDEX_LINKAGE unsigned clang_isStatement(enum CXCursorKind);
 CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
-
+CINDEX_LINKAGE unsigned clang_isTranslationUnit(enum CXCursorKind);
 CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
 
 CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Tue Jan 19 18:23:15 2010
@@ -565,6 +565,11 @@
                                   true);
 }
 
+CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
+  CXCursor Result = { CXCursor_TranslationUnit, { TU, 0, 0 } };
+  return Result;
+}
+
 void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
                                CXTranslationUnitIterator callback,
                                CXClientData CData) {
@@ -803,6 +808,9 @@
 extern "C" {
 CXString clang_getCursorSpelling(CXCursor C) {
   assert(getCursorDecl(C) && "CXCursor has null decl");
+  if (clang_isTranslationUnit(C.kind))
+    return clang_getTranslationUnitSpelling(C.data[0]);
+
   if (clang_isReference(C.kind)) {
     switch (C.kind) {
     case CXCursor_ObjCSuperClassRef: {
@@ -867,6 +875,7 @@
   case CXCursor_InvalidFile: return "InvalidFile";
   case CXCursor_NoDeclFound: return "NoDeclFound";
   case CXCursor_NotImplemented: return "NotImplemented";
+  case CXCursor_TranslationUnit: return "TranslationUnit";
   }
   
   llvm_unreachable("Unhandled CXCursorKind");
@@ -947,6 +956,10 @@
   return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
 }
 
+unsigned clang_isTranslationUnit(enum CXCursorKind K) {
+  return K == CXCursor_TranslationUnit;
+}
+
 CXCursorKind clang_getCursorKind(CXCursor C) {
   return C.kind;
 }

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Tue Jan 19 18:23:15 2010
@@ -39,6 +39,7 @@
 _clang_getNumCompletionChunks
 _clang_getRangeEnd
 _clang_getRangeStart
+_clang_getTranslationUnitCursor
 _clang_getTranslationUnitSpelling
 _clang_isCursorDefinition
 _clang_isDeclaration
@@ -46,6 +47,7 @@
 _clang_isInvalid
 _clang_isReference
 _clang_isStatement
+_clang_isTranslationUnit
 _clang_loadDeclaration
 _clang_loadTranslationUnit
 _clang_setUseExternalASTGeneration

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

==============================================================================
--- cfe/trunk/tools/CIndex/CXCursor.cpp (original)
+++ cfe/trunk/tools/CIndex/CXCursor.cpp Tue Jan 19 18:23:15 2010
@@ -14,6 +14,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "CXCursor.h"
+#include "clang/Frontend/ASTUnit.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/Expr.h"
@@ -320,6 +321,10 @@
   case CXCursor_UnexposedStmt:
     return static_cast<Decl *>(Cursor.data[0])->getASTContext();
 
+  case CXCursor_TranslationUnit: {
+    ASTUnit *CXXUnit = static_cast<ASTUnit *>(Cursor.data[0]);
+    return CXXUnit->getASTContext();
+  }
   }
   
   llvm_unreachable("No context available");





More information about the cfe-commits mailing list