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 <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
diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp
index eff602f..fb6dd56 100644
--- a/tools/CIndex/CIndex.cpp
+++ b/tools/CIndex/CIndex.cpp
@@ -655,7 +655,8 @@
// 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 @@
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())
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 83d3d3f..b458216 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -53,7 +53,7 @@
unsigned curLine = startLine, curColumn = startColumn;
CXCursor Ref;
- while (startBuf <= endBuf) {
+ while (startBuf < endBuf) {
if (*startBuf == '\n') {
startBuf++;
curLine++;
@@ -62,7 +62,7 @@
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) {