[cfe-commits] r84760 - in /cfe/trunk: 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
Steve Naroff
snaroff at apple.com
Wed Oct 21 06:56:23 PDT 2009
Author: snaroff
Date: Wed Oct 21 08:56:23 2009
New Revision: 84760
URL: http://llvm.org/viewvc/llvm-project?rev=84760&view=rev
Log:
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:-)
Modified:
cfe/trunk/include/clang-c/Index.h
cfe/trunk/include/clang/Index/Utils.h
cfe/trunk/lib/Index/ResolveLocation.cpp
cfe/trunk/tools/CIndex/CIndex.cpp
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=84760&r1=84759&r2=84760&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Wed Oct 21 08:56:23 2009
@@ -240,8 +240,16 @@
/*
* 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);
Modified: cfe/trunk/include/clang/Index/Utils.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/Utils.h?rev=84760&r1=84759&r2=84760&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/Utils.h (original)
+++ cfe/trunk/include/clang/Index/Utils.h Wed Oct 21 08:56:23 2009
@@ -18,7 +18,8 @@
namespace clang {
class ASTContext;
class SourceLocation;
-
+ class Decl;
+
namespace idx {
class ASTLocation;
@@ -26,7 +27,8 @@
///
/// \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
Modified: cfe/trunk/lib/Index/ResolveLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/ResolveLocation.cpp?rev=84760&r1=84759&r2=84760&view=diff
==============================================================================
--- cfe/trunk/lib/Index/ResolveLocation.cpp (original)
+++ cfe/trunk/lib/Index/ResolveLocation.cpp Wed Oct 21 08:56:23 2009
@@ -497,9 +497,12 @@
/// \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());
}
Modified: cfe/trunk/tools/CIndex/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.cpp?rev=84760&r1=84759&r2=84760&view=diff
==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndex.cpp Wed Oct 21 08:56:23 2009
@@ -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())
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=84760&r1=84759&r2=84760&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 21 08:56:23 2009
@@ -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) {
More information about the cfe-commits
mailing list