]> granicus.if.org Git - clang/commitdiff
Extend clang_getCursor() to take a 'relativeDecl' argument (so speed up searching...
authorSteve Naroff <snaroff@apple.com>
Wed, 21 Oct 2009 13:56:23 +0000 (13:56 +0000)
committerSteve Naroff <snaroff@apple.com>
Wed, 21 Oct 2009 13:56:23 +0000 (13:56 +0000)
snaroff% time ../../Debug/bin/c-index-test Large.ast all > Large.out
snaroff% cat Large.m
#import <Cocoa/Cocoa.h>
#import <QuickTime/QuickTime.h>
#import <OpenGL/OpenGL.h>

With a 'relativeDecl', it takes <30 seconds:-)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84760 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang-c/Index.h
include/clang/Index/Utils.h
lib/Index/ResolveLocation.cpp
tools/CIndex/CIndex.cpp
tools/c-index-test/c-index-test.c

index 4e7f0a0fa94f24d1a77c4601b5ac85a5295e8a4c..47cecd1c8cc67fd544a3485d0761d0b31999c30c 100644 (file)
@@ -240,8 +240,16 @@ const char *clang_getDeclSource(CXDecl);
 /*
  * CXCursor Operations.
  */
+/**
+   Usage: clang_getCursor() will translate a source/line/column position
+   into an AST cursor (to derive semantic information from the source code).
+   If 'RelativeToDecl' is NULL, the entire translation unit will be searched.
+   Note that searching the entire translation unit can be slow.
+   Otherwise, the "search" for the AST cursor will start at 'RelativeToDecl'.
+ */
 CXCursor clang_getCursor(CXTranslationUnit, const char *source_name, 
-                         unsigned line, unsigned column);
+                         unsigned line, unsigned column, 
+                         CXDecl RelativeToDecl);
 
 enum CXCursorKind clang_getCursorKind(CXCursor);
 unsigned clang_isDeclaration(enum CXCursorKind);
index e78ef8a155630a26aa1a975351ff1cbf9fd981db..36cf56dea203eda60441e8968231c38e86f3efa0 100644 (file)
@@ -18,7 +18,8 @@
 namespace clang {
   class ASTContext;
   class SourceLocation;
-
+  class Decl;
+  
 namespace idx {
   class ASTLocation;
 
@@ -26,7 +27,8 @@ namespace idx {
 ///
 /// \returns the resolved ASTLocation or an invalid ASTLocation if the source
 /// location could not be resolved.
-ASTLocation ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc);
+ASTLocation ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc, 
+                                 Decl *RelativeToDecl = 0);
 
 } // end namespace idx
 
index b94d48e33d6f263601529de3d83d121fbcfcd6c6..d3ba8192b22e53876d397e3191fa0be758460d88 100644 (file)
@@ -497,9 +497,12 @@ void LocResolverBase::print(Stmt *Node) {
 
 /// \brief Returns the AST node that a source location points to.
 ///
-ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc) {
+ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc,
+                                      Decl *RelativeToDecl) {
   if (Loc.isInvalid())
     return ASTLocation();
 
+  if (RelativeToDecl)
+    return DeclLocResolver(Ctx, Loc).Visit(RelativeToDecl);    
   return DeclLocResolver(Ctx, Loc).Visit(Ctx.getTranslationUnitDecl());
 }
index eff602ff8e84b6620429040d37d99ac6624a6587..fb6dd5608fe788533af164ffe0d1e53dbe1289c5 100644 (file)
@@ -655,7 +655,8 @@ static enum CXCursorKind TranslateKind(Decl *D) {
 // CXCursor Operations.
 //
 CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name, 
-                         unsigned line, unsigned column)
+                         unsigned line, unsigned column, 
+                         CXDecl RelativeToDecl)
 {
   assert(CTUnit && "Passed null CXTranslationUnit");
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
@@ -670,7 +671,8 @@ CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
   SourceLocation SLoc = 
     CXXUnit->getSourceManager().getLocation(File, line, column);
                                                                 
-  ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
+  ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
+                                      static_cast<NamedDecl *>(RelativeToDecl));
   
   Decl *Dcl = ALoc.getParentDecl();
   if (ALoc.isNamedRef())
index 83d3d3f31381afa0f097b87ed8c344e7868a482c..b458216f70d6391dd25076ea61c4624316aed12d 100644 (file)
@@ -53,7 +53,7 @@ static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
         unsigned curLine = startLine, curColumn = startColumn;
         CXCursor Ref;
 
-        while (startBuf <= endBuf) {
+        while (startBuf < endBuf) {
           if (*startBuf == '\n') {
             startBuf++;
             curLine++;
@@ -62,7 +62,7 @@ static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
             curColumn++;
 
           Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
-                                curLine, curColumn);
+                                curLine, curColumn, Cursor.decl);
           if (Ref.kind == CXCursor_NoDeclFound) {
             /* Nothing found here; that's fine. */
           } else if (Ref.kind != CXCursor_FunctionDecl) {