From 6a6de8b4fc944ca1bfa4e47c516d049a0d627b0e Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Wed, 21 Oct 2009 13:56:23 +0000 Subject: [PATCH] Extend clang_getCursor() to take a 'relativeDecl' argument (so speed up searching). Without a 'relativeDecl', the algorithm is n-squared. For example, running the following command on 'Large.m' takes hours without a 'relatvieDecl'. snaroff% time ../../Debug/bin/c-index-test Large.ast all > Large.out snaroff% cat Large.m #import #import #import 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 | 10 +++++++++- include/clang/Index/Utils.h | 6 ++++-- lib/Index/ResolveLocation.cpp | 5 ++++- tools/CIndex/CIndex.cpp | 6 ++++-- tools/c-index-test/c-index-test.c | 4 ++-- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 4e7f0a0fa9..47cecd1c8c 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -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); diff --git a/include/clang/Index/Utils.h b/include/clang/Index/Utils.h index e78ef8a155..36cf56dea2 100644 --- a/include/clang/Index/Utils.h +++ b/include/clang/Index/Utils.h @@ -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 diff --git a/lib/Index/ResolveLocation.cpp b/lib/Index/ResolveLocation.cpp index b94d48e33d..d3ba8192b2 100644 --- a/lib/Index/ResolveLocation.cpp +++ b/lib/Index/ResolveLocation.cpp @@ -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()); } diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index eff602ff8e..fb6dd5608f 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -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(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(RelativeToDecl)); Decl *Dcl = ALoc.getParentDecl(); if (ALoc.isNamedRef()) diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 83d3d3f313..b458216f70 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -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) { -- 2.40.0