2009-06-26 02:22:41 +08:00
|
|
|
//===--- ResolveLocation.cpp - Source location resolver ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This defines the ResolveLocationInAST function, which resolves a
|
2009-07-07 05:35:02 +08:00
|
|
|
// source location into a ASTLocation.
|
2009-06-26 02:22:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 05:35:02 +08:00
|
|
|
#include "clang/Index/Utils.h"
|
2009-07-07 05:34:47 +08:00
|
|
|
#include "clang/Index/ASTLocation.h"
|
2009-06-26 02:22:41 +08:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
using namespace clang;
|
2009-07-07 05:34:47 +08:00
|
|
|
using namespace idx;
|
2009-06-26 02:22:41 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// \brief Base for the LocResolver classes. Mostly does source range checking.
|
|
|
|
class VISIBILITY_HIDDEN LocResolverBase {
|
|
|
|
protected:
|
|
|
|
ASTContext &Ctx;
|
|
|
|
SourceLocation Loc;
|
2009-07-07 08:53:31 +08:00
|
|
|
|
|
|
|
enum RangePos {
|
|
|
|
BeforeLoc,
|
|
|
|
ContainsLoc,
|
|
|
|
AfterLoc
|
|
|
|
};
|
|
|
|
|
|
|
|
RangePos CheckRange(SourceRange Range);
|
|
|
|
RangePos CheckRange(Decl *D) { return CheckRange(D->getSourceRange()); }
|
|
|
|
RangePos CheckRange(Stmt *Node) { return CheckRange(Node->getSourceRange()); }
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-10 11:41:26 +08:00
|
|
|
template <typename T>
|
|
|
|
bool isBeforeLocation(T *Node) {
|
|
|
|
return CheckRange(Node) == BeforeLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool ContainsLocation(T *Node) {
|
|
|
|
return CheckRange(Node) == ContainsLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool isAfterLocation(T *Node) {
|
|
|
|
return CheckRange(Node) == AfterLoc;
|
|
|
|
}
|
|
|
|
|
2009-06-26 02:22:41 +08:00
|
|
|
public:
|
|
|
|
LocResolverBase(ASTContext &ctx, SourceLocation loc)
|
2009-07-07 08:53:31 +08:00
|
|
|
: Ctx(ctx), Loc(loc) {}
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-17 09:20:03 +08:00
|
|
|
#ifndef NDEBUG
|
2009-06-26 02:22:41 +08:00
|
|
|
/// \brief Debugging output.
|
|
|
|
void print(Decl *D);
|
|
|
|
/// \brief Debugging output.
|
|
|
|
void print(Stmt *Node);
|
2009-07-17 09:20:03 +08:00
|
|
|
#endif
|
2009-06-26 02:22:41 +08:00
|
|
|
};
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
/// \brief Searches a statement for the ASTLocation that corresponds to a source
|
2009-06-26 02:22:41 +08:00
|
|
|
/// location.
|
|
|
|
class VISIBILITY_HIDDEN StmtLocResolver : public LocResolverBase,
|
2009-07-07 08:53:31 +08:00
|
|
|
public StmtVisitor<StmtLocResolver,
|
|
|
|
ASTLocation > {
|
2009-07-18 08:33:52 +08:00
|
|
|
Decl * const Parent;
|
2009-07-07 08:53:31 +08:00
|
|
|
|
2009-06-26 02:22:41 +08:00
|
|
|
public:
|
2009-07-07 08:53:31 +08:00
|
|
|
StmtLocResolver(ASTContext &ctx, SourceLocation loc, Decl *parent)
|
|
|
|
: LocResolverBase(ctx, loc), Parent(parent) {}
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation VisitDeclStmt(DeclStmt *Node);
|
|
|
|
ASTLocation VisitStmt(Stmt *Node);
|
2009-06-26 02:22:41 +08:00
|
|
|
};
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
/// \brief Searches a declaration for the ASTLocation that corresponds to a
|
|
|
|
/// source location.
|
2009-06-26 02:22:41 +08:00
|
|
|
class VISIBILITY_HIDDEN DeclLocResolver : public LocResolverBase,
|
2009-07-07 08:53:31 +08:00
|
|
|
public DeclVisitor<DeclLocResolver,
|
|
|
|
ASTLocation > {
|
2009-06-26 02:22:41 +08:00
|
|
|
public:
|
|
|
|
DeclLocResolver(ASTContext &ctx, SourceLocation loc)
|
|
|
|
: LocResolverBase(ctx, loc) {}
|
|
|
|
|
2009-07-14 11:18:17 +08:00
|
|
|
ASTLocation VisitDeclContext(DeclContext *DC);
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation VisitTranslationUnitDecl(TranslationUnitDecl *TU);
|
|
|
|
ASTLocation VisitVarDecl(VarDecl *D);
|
|
|
|
ASTLocation VisitFunctionDecl(FunctionDecl *D);
|
2009-07-18 08:33:52 +08:00
|
|
|
ASTLocation VisitObjCMethodDecl(ObjCMethodDecl *D);
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation VisitDecl(Decl *D);
|
2009-06-26 02:22:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation StmtLocResolver::VisitDeclStmt(DeclStmt *Node) {
|
2009-07-10 11:41:26 +08:00
|
|
|
assert(ContainsLocation(Node) &&
|
|
|
|
"Should visit only after verifying that loc is in range");
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
// Search all declarations of this DeclStmt.
|
2009-06-26 02:22:41 +08:00
|
|
|
for (DeclStmt::decl_iterator
|
|
|
|
I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I) {
|
2009-07-07 08:53:31 +08:00
|
|
|
RangePos RP = CheckRange(*I);
|
|
|
|
if (RP == AfterLoc)
|
|
|
|
break;
|
|
|
|
if (RP == ContainsLoc)
|
|
|
|
return DeclLocResolver(Ctx, Loc).Visit(*I);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
2009-07-07 08:53:31 +08:00
|
|
|
|
|
|
|
return ASTLocation(Parent, Node);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation StmtLocResolver::VisitStmt(Stmt *Node) {
|
2009-07-10 11:41:26 +08:00
|
|
|
assert(ContainsLocation(Node) &&
|
|
|
|
"Should visit only after verifying that loc is in range");
|
2009-07-07 08:53:31 +08:00
|
|
|
|
2009-06-26 02:22:41 +08:00
|
|
|
// Search the child statements.
|
|
|
|
for (Stmt::child_iterator
|
|
|
|
I = Node->child_begin(), E = Node->child_end(); I != E; ++I) {
|
2009-07-07 08:53:31 +08:00
|
|
|
RangePos RP = CheckRange(*I);
|
|
|
|
if (RP == AfterLoc)
|
|
|
|
break;
|
|
|
|
if (RP == ContainsLoc)
|
|
|
|
return Visit(*I);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
2009-07-07 08:53:31 +08:00
|
|
|
|
|
|
|
return ASTLocation(Parent, Node);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 11:18:17 +08:00
|
|
|
ASTLocation DeclLocResolver::VisitDeclContext(DeclContext *DC) {
|
2009-06-26 02:22:41 +08:00
|
|
|
for (DeclContext::decl_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
|
2009-07-07 08:53:31 +08:00
|
|
|
RangePos RP = CheckRange(*I);
|
|
|
|
if (RP == AfterLoc)
|
|
|
|
break;
|
|
|
|
if (RP == ContainsLoc)
|
|
|
|
return Visit(*I);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-14 11:18:17 +08:00
|
|
|
return ASTLocation(cast<Decl>(DC));
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTLocation DeclLocResolver::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
|
|
|
|
ASTLocation ASTLoc = VisitDeclContext(TU);
|
|
|
|
if (ASTLoc.getDecl() == TU)
|
|
|
|
return ASTLocation();
|
|
|
|
return ASTLoc;
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation DeclLocResolver::VisitFunctionDecl(FunctionDecl *D) {
|
2009-07-10 11:41:26 +08:00
|
|
|
assert(ContainsLocation(D) &&
|
|
|
|
"Should visit only after verifying that loc is in range");
|
2009-06-26 02:22:41 +08:00
|
|
|
|
|
|
|
// First, search through the parameters of the function.
|
|
|
|
for (FunctionDecl::param_iterator
|
|
|
|
I = D->param_begin(), E = D->param_end(); I != E; ++I) {
|
2009-07-07 08:53:31 +08:00
|
|
|
RangePos RP = CheckRange(*I);
|
|
|
|
if (RP == AfterLoc)
|
|
|
|
return ASTLocation(D);
|
|
|
|
if (RP == ContainsLoc)
|
|
|
|
return Visit(*I);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
// We didn't find the location in the parameters and we didn't get passed it.
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-06 06:21:17 +08:00
|
|
|
if (!D->isThisDeclarationADefinition())
|
2009-07-07 08:53:31 +08:00
|
|
|
return ASTLocation(D);
|
2009-07-06 06:21:17 +08:00
|
|
|
|
2009-06-26 02:22:41 +08:00
|
|
|
// Second, search through the declarations that are part of the function.
|
|
|
|
// If we find he location there, we won't have to search through its body.
|
2009-07-06 06:21:46 +08:00
|
|
|
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = D->decls_begin(), E = D->decls_end(); I != E; ++I) {
|
|
|
|
if (isa<ParmVarDecl>(*I))
|
|
|
|
continue; // We already searched through the parameters.
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
RangePos RP = CheckRange(*I);
|
|
|
|
if (RP == AfterLoc)
|
|
|
|
break;
|
|
|
|
if (RP == ContainsLoc)
|
|
|
|
return Visit(*I);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
2009-07-06 06:21:46 +08:00
|
|
|
|
2009-06-26 02:22:41 +08:00
|
|
|
// We didn't find a declaration that corresponds to the source location.
|
|
|
|
|
|
|
|
// Finally, search through the body of the function.
|
2009-07-07 08:53:31 +08:00
|
|
|
Stmt *Body = D->getBody();
|
|
|
|
assert(Body && "Expected definition");
|
2009-07-10 11:41:26 +08:00
|
|
|
assert(!isBeforeLocation(Body) &&
|
|
|
|
"This function is supposed to contain the loc");
|
|
|
|
if (isAfterLocation(Body))
|
2009-07-07 08:53:31 +08:00
|
|
|
return ASTLocation(D);
|
|
|
|
|
|
|
|
// The body contains the location.
|
2009-07-10 11:41:26 +08:00
|
|
|
assert(ContainsLocation(Body));
|
2009-07-07 08:53:31 +08:00
|
|
|
return StmtLocResolver(Ctx, Loc, D).Visit(Body);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation DeclLocResolver::VisitVarDecl(VarDecl *D) {
|
2009-07-10 11:41:26 +08:00
|
|
|
assert(ContainsLocation(D) &&
|
|
|
|
"Should visit only after verifying that loc is in range");
|
2009-07-07 08:53:31 +08:00
|
|
|
|
2009-06-26 02:22:41 +08:00
|
|
|
// Check whether the location points to the init expression.
|
2009-07-07 08:53:31 +08:00
|
|
|
Expr *Init = D->getInit();
|
2009-07-10 11:41:26 +08:00
|
|
|
if (Init && ContainsLocation(Init))
|
2009-07-07 08:53:31 +08:00
|
|
|
return StmtLocResolver(Ctx, Loc, D).Visit(Init);
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
return ASTLocation(D);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-18 08:33:52 +08:00
|
|
|
ASTLocation DeclLocResolver::VisitObjCMethodDecl(ObjCMethodDecl *D) {
|
|
|
|
assert(ContainsLocation(D) &&
|
|
|
|
"Should visit only after verifying that loc is in range");
|
|
|
|
|
|
|
|
// First, search through the parameters of the method.
|
|
|
|
for (ObjCMethodDecl::param_iterator
|
|
|
|
I = D->param_begin(), E = D->param_end(); I != E; ++I) {
|
|
|
|
RangePos RP = CheckRange(*I);
|
|
|
|
if (RP == AfterLoc)
|
|
|
|
return ASTLocation(D);
|
|
|
|
if (RP == ContainsLoc)
|
|
|
|
return Visit(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find the location in the parameters and we didn't get passed it.
|
|
|
|
|
|
|
|
if (!D->getBody())
|
|
|
|
return ASTLocation(D);
|
|
|
|
|
|
|
|
// Second, search through the declarations that are part of the method.
|
|
|
|
// If we find he location there, we won't have to search through its body.
|
|
|
|
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = D->decls_begin(), E = D->decls_end(); I != E; ++I) {
|
|
|
|
if (isa<ParmVarDecl>(*I))
|
|
|
|
continue; // We already searched through the parameters.
|
|
|
|
|
|
|
|
RangePos RP = CheckRange(*I);
|
|
|
|
if (RP == AfterLoc)
|
|
|
|
break;
|
|
|
|
if (RP == ContainsLoc)
|
|
|
|
return Visit(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find a declaration that corresponds to the source location.
|
|
|
|
|
|
|
|
// Finally, search through the body of the method.
|
|
|
|
Stmt *Body = D->getBody();
|
|
|
|
assert(Body && "Expected definition");
|
|
|
|
assert(!isBeforeLocation(Body) &&
|
|
|
|
"This method is supposed to contain the loc");
|
|
|
|
if (isAfterLocation(Body))
|
|
|
|
return ASTLocation(D);
|
|
|
|
|
|
|
|
// The body contains the location.
|
|
|
|
assert(ContainsLocation(Body));
|
|
|
|
return StmtLocResolver(Ctx, Loc, D).Visit(Body);
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
ASTLocation DeclLocResolver::VisitDecl(Decl *D) {
|
2009-07-10 11:41:26 +08:00
|
|
|
assert(ContainsLocation(D) &&
|
|
|
|
"Should visit only after verifying that loc is in range");
|
2009-07-18 08:33:40 +08:00
|
|
|
if (DeclContext *DC = dyn_cast<DeclContext>(D))
|
|
|
|
return VisitDeclContext(DC);
|
2009-07-07 08:53:31 +08:00
|
|
|
return ASTLocation(D);
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
LocResolverBase::RangePos LocResolverBase::CheckRange(SourceRange Range) {
|
2009-06-26 02:22:41 +08:00
|
|
|
if (!Range.isValid())
|
2009-07-07 08:53:31 +08:00
|
|
|
return BeforeLoc; // Keep looking.
|
|
|
|
|
|
|
|
// Update the end source range to cover the full length of the token
|
|
|
|
// positioned at the end of the source range.
|
|
|
|
//
|
|
|
|
// e.g.,
|
|
|
|
// int foo
|
|
|
|
// ^ ^
|
|
|
|
//
|
|
|
|
// will be updated to
|
|
|
|
// int foo
|
|
|
|
// ^ ^
|
|
|
|
unsigned TokSize = Lexer::MeasureTokenLength(Range.getEnd(),
|
|
|
|
Ctx.getSourceManager(),
|
|
|
|
Ctx.getLangOptions());
|
|
|
|
Range.setEnd(Range.getEnd().getFileLocWithOffset(TokSize-1));
|
2009-06-26 02:22:41 +08:00
|
|
|
|
|
|
|
SourceManager &SourceMgr = Ctx.getSourceManager();
|
|
|
|
if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), Loc))
|
2009-07-07 08:53:31 +08:00
|
|
|
return BeforeLoc;
|
2009-06-26 02:22:41 +08:00
|
|
|
|
|
|
|
if (SourceMgr.isBeforeInTranslationUnit(Loc, Range.getBegin()))
|
2009-07-07 08:53:31 +08:00
|
|
|
return AfterLoc;
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
return ContainsLoc;
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|
|
|
|
|
2009-07-17 09:20:03 +08:00
|
|
|
#ifndef NDEBUG
|
2009-06-26 02:22:41 +08:00
|
|
|
void LocResolverBase::print(Decl *D) {
|
|
|
|
llvm::raw_ostream &OS = llvm::outs();
|
2009-07-17 09:20:03 +08:00
|
|
|
OS << "#### DECL " << D->getDeclKindName() << " ####\n";
|
2009-06-30 10:35:04 +08:00
|
|
|
D->print(OS);
|
2009-06-26 02:22:41 +08:00
|
|
|
OS << " <";
|
|
|
|
D->getLocStart().print(OS, Ctx.getSourceManager());
|
|
|
|
OS << " > - <";
|
|
|
|
D->getLocEnd().print(OS, Ctx.getSourceManager());
|
|
|
|
OS << ">\n\n";
|
|
|
|
OS.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocResolverBase::print(Stmt *Node) {
|
|
|
|
llvm::raw_ostream &OS = llvm::outs();
|
2009-07-17 09:20:03 +08:00
|
|
|
OS << "#### STMT " << Node->getStmtClassName() << " ####\n";
|
2009-06-30 09:26:17 +08:00
|
|
|
Node->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
|
2009-06-26 02:22:41 +08:00
|
|
|
OS << " <";
|
|
|
|
Node->getLocStart().print(OS, Ctx.getSourceManager());
|
|
|
|
OS << " > - <";
|
|
|
|
Node->getLocEnd().print(OS, Ctx.getSourceManager());
|
|
|
|
OS << ">\n\n";
|
|
|
|
OS.flush();
|
|
|
|
}
|
2009-07-17 09:20:03 +08:00
|
|
|
#endif
|
2009-06-26 02:22:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
/// \brief Returns the AST node that a source location points to.
|
|
|
|
///
|
2009-07-07 05:35:02 +08:00
|
|
|
ASTLocation idx::ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc) {
|
2009-06-26 02:22:41 +08:00
|
|
|
if (Loc.isInvalid())
|
2009-07-07 05:34:20 +08:00
|
|
|
return ASTLocation();
|
2009-06-26 02:22:41 +08:00
|
|
|
|
2009-07-07 08:53:31 +08:00
|
|
|
return DeclLocResolver(Ctx, Loc).Visit(Ctx.getTranslationUnitDecl());
|
2009-06-26 02:22:41 +08:00
|
|
|
}
|