[cfe-commits] r85425 - in /cfe/trunk: include/clang-c/Index.h include/clang/Frontend/ASTUnit.h include/clang/Index/Utils.h lib/Index/ResolveLocation.cpp tools/CIndex/CIndex.cpp tools/CIndex/CIndex.exports tools/c-index-test/c-index-test.c

Steve Naroff snaroff at apple.com
Wed Oct 28 13:44:47 PDT 2009


Author: snaroff
Date: Wed Oct 28 15:44:47 2009
New Revision: 85425

URL: http://llvm.org/viewvc/llvm-project?rev=85425&view=rev
Log:
Remove _clang_initCXLookupHint() and _clang_getCursorWithHint(). Related to <rdar://problem/7310688>.

Localize the optimization to ResolveLocationInAST(). The last valid AST location is now stored with ASTUnit. There still isn't optimal, however it's an improvement (with a much cleaner API). Having the client manage an "hint" is error prone and complex.

I wanted to land the major changes before finishing up the optimizations. 

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Frontend/ASTUnit.h
    cfe/trunk/include/clang/Index/Utils.h
    cfe/trunk/lib/Index/ResolveLocation.cpp
    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=85425&r1=85424&r2=85425&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Wed Oct 28 15:44:47 2009
@@ -276,21 +276,6 @@
 CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, 
                          unsigned line, unsigned column);
 
-/**
-   Usage: clang_getCursorWithHint() provides the same functionality as
-   clang_getCursor() except that it takes an option 'hint' argument.
-   The 'hint' is a temporary CXLookupHint object (whose lifetime is managed by 
-   the caller) that should be initialized with clang_initCXLookupHint().
-
-   FIXME: Add a better comment once getCursorWithHint() has more functionality.
- */                         
-typedef CXCursor CXLookupHint;
-CINDEX_LINKAGE CXCursor clang_getCursorWithHint(CXTranslationUnit, const char *source_name, 
-                                 unsigned line, unsigned column, 
-                                 CXLookupHint *hint);
-
-CINDEX_LINKAGE void clang_initCXLookupHint(CXLookupHint *hint);
-
 CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
 CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
 CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);

Modified: cfe/trunk/include/clang/Frontend/ASTUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/ASTUnit.h?rev=85425&r1=85424&r2=85425&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/ASTUnit.h (original)
+++ cfe/trunk/include/clang/Frontend/ASTUnit.h Wed Oct 28 15:44:47 2009
@@ -18,6 +18,7 @@
 #include "llvm/ADT/OwningPtr.h"
 #include "clang/Frontend/TextDiagnosticBuffer.h"
 #include "clang/Basic/FileManager.h"
+#include "clang/Index/ASTLocation.h"
 #include <string>
 
 namespace clang {
@@ -32,6 +33,8 @@
   class ASTContext;
   class Decl;
 
+using namespace idx;
+
 /// \brief Utility class for loading a ASTContext from a PCH file.
 ///
 class ASTUnit {
@@ -50,6 +53,9 @@
   // FIXME: This is temporary; eventually, CIndex will always do this.
   bool                              OnlyLocalDecls;
   
+  // Critical optimization when using clang_getCursor().
+  ASTLocation LastLoc;
+  
   ASTUnit(const ASTUnit&); // DO NOT IMPLEMENT
   ASTUnit &operator=(const ASTUnit &); // DO NOT IMPLEMENT
 
@@ -79,6 +85,9 @@
   
   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
   
+  void setLastASTLocation(ASTLocation ALoc) { LastLoc = ALoc; }
+  ASTLocation getLastASTLocation() const { return LastLoc; }
+  
   /// \brief Create a ASTUnit from a PCH file.
   ///
   /// \param Filename - The PCH file to load.

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

==============================================================================
--- cfe/trunk/include/clang/Index/Utils.h (original)
+++ cfe/trunk/include/clang/Index/Utils.h Wed Oct 28 15:44:47 2009
@@ -18,7 +18,6 @@
 namespace clang {
   class ASTContext;
   class SourceLocation;
-  class Decl;
   
 namespace idx {
   class ASTLocation;
@@ -28,7 +27,7 @@
 /// \returns the resolved ASTLocation or an invalid ASTLocation if the source
 /// location could not be resolved.
 ASTLocation ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc, 
-                                 Decl *RelativeToDecl = 0);
+                                 ASTLocation *LastLoc = 0);
 
 } // end namespace idx
 

Modified: cfe/trunk/lib/Index/ResolveLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/ResolveLocation.cpp?rev=85425&r1=85424&r2=85425&view=diff

==============================================================================
--- cfe/trunk/lib/Index/ResolveLocation.cpp (original)
+++ cfe/trunk/lib/Index/ResolveLocation.cpp Wed Oct 28 15:44:47 2009
@@ -60,11 +60,6 @@
   }
 
   template <typename T>
-  bool ContainsLocation(T Node) {
-    return CheckRange(Node) == ContainsLoc;
-  }
-
-  template <typename T>
   bool isAfterLocation(T Node) {
     return CheckRange(Node) == AfterLoc;
   }
@@ -72,6 +67,11 @@
 public:
   LocResolverBase(ASTContext &ctx, SourceLocation loc)
     : Ctx(ctx), Loc(loc) {}
+    
+  template <typename T>
+  bool ContainsLocation(T Node) {
+    return CheckRange(Node) == ContainsLoc;
+  }
 
 #ifndef NDEBUG
   /// \brief Debugging output.
@@ -543,11 +543,40 @@
 /// \brief Returns the AST node that a source location points to.
 ///
 ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc,
-                                      Decl *RelativeToDecl) {
+                                      ASTLocation *LastLoc) {
   if (Loc.isInvalid())
     return ASTLocation();
 
-  if (RelativeToDecl)
-    return DeclLocResolver(Ctx, Loc).Visit(RelativeToDecl);    
+  if (LastLoc && LastLoc->isValid()) {
+    DeclContext *DC = 0;
+  
+    if (Decl *Dcl = LastLoc->dyn_AsDecl()) {
+      DC = Dcl->getDeclContext();
+    } else if (LastLoc->isStmt()) {
+      Decl *Parent = LastLoc->getParentDecl();
+      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Parent))
+        DC = FD;
+      else { 
+        // This is needed to handle statements within an initializer.
+        // Example:
+        //   void func() { long double fabsf = __builtin_fabsl(__x); }
+        // In this case, the 'parent' of __builtin_fabsl is fabsf.
+        DC = Parent->getDeclContext();
+      }
+    } else { // We have 'N_NamedRef' or 'N_Type'
+      DC = LastLoc->getParentDecl()->getDeclContext();
+    } 
+    assert(DC && "Missing DeclContext");
+    
+    FunctionDecl *FD = dyn_cast<FunctionDecl>(DC);
+    DeclLocResolver DLocResolver(Ctx, Loc);
+    
+    if (FD && FD->isThisDeclarationADefinition() &&
+        DLocResolver.ContainsLocation(FD)) {
+      return DLocResolver.VisitFunctionDecl(FD);
+    }
+    // Fall through and try the slow path...
+    // FIXME: Optimize more cases.
+  }
   return DeclLocResolver(Ctx, Loc).Visit(Ctx.getTranslationUnitDecl());
 }

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Wed Oct 28 15:44:47 2009
@@ -116,6 +116,10 @@
     if (ND->getPCHLevel() > MaxPCHLevel)
       return;
     
+    // Filter any implicit declarations (since the source info will be bogus).
+    if (ND->isImplicit())
+      return;
+      
     CXCursor C = { CK, ND, 0 };
     Callback(TUnit, C, CData);
   }
@@ -721,26 +725,12 @@
 //
 // CXCursor Operations.
 //
-void clang_initCXLookupHint(CXLookupHint *hint) {
-  memset(hint, 0, sizeof(*hint));
-}
-
 CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, 
-                         unsigned line, unsigned column) {
-  return clang_getCursorWithHint(CTUnit, source_name, line, column, NULL);
-}
-  
-CXCursor clang_getCursorWithHint(CXTranslationUnit CTUnit,
-                                 const char *source_name, 
-                                 unsigned line, unsigned column, 
-                                 CXLookupHint *hint)
+                         unsigned line, unsigned column)
 {
   assert(CTUnit && "Passed null CXTranslationUnit");
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
   
-  // FIXME: Make this better.
-  CXDecl RelativeToDecl = hint ? hint->decl : NULL;
-  
   FileManager &FMgr = CXXUnit->getFileManager();
   const FileEntry *File = FMgr.getFile(source_name, 
                                        source_name+strlen(source_name));  
@@ -751,9 +741,13 @@
   SourceLocation SLoc = 
     CXXUnit->getSourceManager().getLocation(File, line, column);
                                                                 
-  ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
-                                      static_cast<NamedDecl *>(RelativeToDecl));
-  
+  ASTLocation LastLoc = CXXUnit->getLastASTLocation();
+
+  ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc, 
+                                          &LastLoc);
+  if (ALoc.isValid())
+    CXXUnit->setLastASTLocation(ALoc);
+    
   Decl *Dcl = ALoc.getParentDecl();
   if (ALoc.isNamedRef())
     Dcl = ALoc.AsNamedRef().ND;

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

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Wed Oct 28 15:44:47 2009
@@ -7,7 +7,6 @@
 _clang_getCursorKind
 _clang_getCursorLine
 _clang_getCursorSource
-_clang_getCursorWithHint
 _clang_getDeclarationName
 _clang_getDeclSpelling
 _clang_getDeclLine
@@ -21,7 +20,6 @@
 _clang_createTranslationUnit
 _clang_createTranslationUnitFromSourceFile
 _clang_disposeTranslationUnit
-_clang_initCXLookupHint
 _clang_isDeclaration
 _clang_isReference
 _clang_isDefinition

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=85425&r1=85424&r2=85425&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Wed Oct 28 15:44:47 2009
@@ -70,7 +70,6 @@
         CXCursor Ref;
 
         while (startBuf < endBuf) {
-          CXLookupHint hint;
           if (*startBuf == '\n') {
             startBuf++;
             curLine++;
@@ -78,11 +77,8 @@
           } else if (*startBuf != '\t')
             curColumn++;
           
-          clang_initCXLookupHint(&hint);
-          hint.decl = Cursor.decl;
-
-          Ref = clang_getCursorWithHint(Unit, clang_getCursorSource(Cursor),
-                                        curLine, curColumn, &hint);
+          Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
+                                curLine, curColumn);
           if (Ref.kind == CXCursor_NoDeclFound) {
             /* Nothing found here; that's fine. */
           } else if (Ref.kind != CXCursor_FunctionDecl) {





More information about the cfe-commits mailing list