[cfe-commits] r127776 - in /cfe/trunk: test/Index/load-exprs.c tools/libclang/CIndex.cpp

Douglas Gregor dgregor at apple.com
Wed Mar 16 16:23:30 PDT 2011


Author: dgregor
Date: Wed Mar 16 18:23:30 2011
New Revision: 127776

URL: http://llvm.org/viewvc/llvm-project?rev=127776&view=rev
Log:
When libclang visits a translation unit via clang_visitChildren(),
walk the preprocessing record *before* walking the declarations, so
they we pretend that we actually respect the phases of translation.

We still walk the preprocessing record after the declarations when
performing token annotation or finding the cursor at a location, since
those routines depend on those semantics.

Fixes <rdar://problem/9137195>.

Modified:
    cfe/trunk/test/Index/load-exprs.c
    cfe/trunk/tools/libclang/CIndex.cpp

Modified: cfe/trunk/test/Index/load-exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/load-exprs.c?rev=127776&r1=127775&r2=127776&view=diff
==============================================================================
--- cfe/trunk/test/Index/load-exprs.c (original)
+++ cfe/trunk/test/Index/load-exprs.c Wed Mar 16 18:23:30 2011
@@ -32,7 +32,7 @@
 }
 
 // RUN: c-index-test -test-load-source all %s -fblocks | FileCheck %s
-
+// CHECK: macro definition=__clang__
 // CHECK: load-exprs.c:1:13: TypedefDecl=T:1:13 (Definition) Extent=[1:1 - 1:14]
 // CHECK: load-exprs.c:2:8: StructDecl=X:2:8 (Definition) Extent=[2:1 - 2:23]
 // CHECK: load-exprs.c:2:16: FieldDecl=a:2:16 (Definition) Extent=[2:12 - 2:17]

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=127776&r1=127775&r2=127776&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Wed Mar 16 18:23:30 2011
@@ -188,6 +188,10 @@
   // be suppressed.
   unsigned MaxPCHLevel;
 
+  /// \brief Whether we should visit the preprocessing record entries last, 
+  /// after visiting other declarations.
+  bool VisitPreprocessorLast;
+  
   /// \brief When valid, a source range to which the cursor should restrict
   /// its search.
   SourceRange RegionOfInterest;
@@ -235,11 +239,12 @@
   CursorVisitor(CXTranslationUnit TU, CXCursorVisitor Visitor,
                 CXClientData ClientData,
                 unsigned MaxPCHLevel,
+                bool VisitPreprocessorLast,
                 SourceRange RegionOfInterest = SourceRange())
     : TU(TU), AU(static_cast<ASTUnit*>(TU->TUData)),
       Visitor(Visitor), ClientData(ClientData),
-      MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest), 
-      DI_current(0)
+      MaxPCHLevel(MaxPCHLevel), VisitPreprocessorLast(VisitPreprocessorLast),
+      RegionOfInterest(RegionOfInterest), DI_current(0)
   {
     Parent.kind = CXCursor_NoDeclFound;
     Parent.data[0] = 0;
@@ -501,46 +506,54 @@
   if (clang_isTranslationUnit(Cursor.kind)) {
     CXTranslationUnit tu = getCursorTU(Cursor);
     ASTUnit *CXXUnit = static_cast<ASTUnit*>(tu->TUData);
-    if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
-        RegionOfInterest.isInvalid()) {
-      for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
-                                    TLEnd = CXXUnit->top_level_end();
-           TL != TLEnd; ++TL) {
-        if (Visit(MakeCXCursor(*TL, tu), true))
+    
+    int VisitOrder[2] = { VisitPreprocessorLast, !VisitPreprocessorLast };
+    for (unsigned I = 0; I != 2; ++I) {
+      if (VisitOrder[I]) {
+        if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
+            RegionOfInterest.isInvalid()) {
+          for (ASTUnit::top_level_iterator TL = CXXUnit->top_level_begin(),
+                                        TLEnd = CXXUnit->top_level_end();
+               TL != TLEnd; ++TL) {
+            if (Visit(MakeCXCursor(*TL, tu), true))
+              return true;
+          }
+        } else if (VisitDeclContext(
+                                CXXUnit->getASTContext().getTranslationUnitDecl()))
           return true;
+        continue;
       }
-    } else if (VisitDeclContext(
-                            CXXUnit->getASTContext().getTranslationUnitDecl()))
-      return true;
 
-    // Walk the preprocessing record.
-    if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
-      // FIXME: Once we have the ability to deserialize a preprocessing record,
-      // do so.
-      PreprocessingRecord::iterator E, EEnd;
-      for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
-        if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
-          if (Visit(MakeMacroInstantiationCursor(MI, tu)))
-            return true;
-          
-          continue;
-        }
-        
-        if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
-          if (Visit(MakeMacroDefinitionCursor(MD, tu)))
-            return true;
+      // Walk the preprocessing record.
+      if (CXXUnit->getPreprocessor().getPreprocessingRecord()) {
+        // FIXME: Once we have the ability to deserialize a preprocessing record,
+        // do so.
+        PreprocessingRecord::iterator E, EEnd;
+        for (llvm::tie(E, EEnd) = getPreprocessedEntities(); E != EEnd; ++E) {
+          if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
+            if (Visit(MakeMacroInstantiationCursor(MI, tu)))
+              return true;
+            
+            continue;
+          }
           
-          continue;
-        }
-        
-        if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
-          if (Visit(MakeInclusionDirectiveCursor(ID, tu)))
-            return true;
+          if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
+            if (Visit(MakeMacroDefinitionCursor(MD, tu)))
+              return true;
+            
+            continue;
+          }
           
-          continue;
+          if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
+            if (Visit(MakeInclusionDirectiveCursor(ID, tu)))
+              return true;
+            
+            continue;
+          }
         }
       }
     }
+    
     return false;
   }
 
@@ -2860,7 +2873,8 @@
                              CXCursorVisitor visitor,
                              CXClientData client_data) {
   CursorVisitor CursorVis(getCursorTU(parent), visitor, client_data, 
-                          getCursorASTUnit(parent)->getMaxPCHLevel());
+                          getCursorASTUnit(parent)->getMaxPCHLevel(),
+                          false);
   return CursorVis.VisitChildren(parent);
 }
 
@@ -3316,7 +3330,7 @@
     // the region of interest, rather than starting from the translation unit.
     CXCursor Parent = clang_getTranslationUnitCursor(TU);
     CursorVisitor CursorVis(TU, GetCursorVisitor, &Result,
-                            Decl::MaxPCHLevel, SourceLocation(SLoc));
+                            Decl::MaxPCHLevel, true, SourceLocation(SLoc));
     CursorVis.VisitChildren(Parent);
   }
   
@@ -4283,7 +4297,7 @@
       NumTokens(numTokens), TokIdx(0), PreprocessingTokIdx(0),
       AnnotateVis(tu,
                   AnnotateTokensVisitor, this,
-                  Decl::MaxPCHLevel, RegionOfInterest),
+                  Decl::MaxPCHLevel, true, RegionOfInterest),
       SrcMgr(static_cast<ASTUnit*>(tu->TUData)->getSourceManager()),
       HasContextSensitiveKeywords(false) { }
 





More information about the cfe-commits mailing list