2009-07-07 01:34:47 +04:00
|
|
|
//===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
|
2009-07-06 02:21:28 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2009-07-07 01:34:20 +04:00
|
|
|
// ASTLocation is Decl or a Stmt and its immediate Decl parent.
|
2009-07-06 02:21:28 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 01:34:47 +04:00
|
|
|
#include "clang/Index/ASTLocation.h"
|
2009-07-06 02:21:28 +04:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
using namespace clang;
|
2009-07-07 01:34:47 +04:00
|
|
|
using namespace idx;
|
2009-07-06 02:21:28 +04:00
|
|
|
|
|
|
|
static bool isContainedInStatement(Stmt *Node, Stmt *Parent) {
|
|
|
|
assert(Node && Parent && "Passed null Node or Parent");
|
|
|
|
|
|
|
|
if (Node == Parent)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (Stmt::child_iterator
|
|
|
|
I = Parent->child_begin(), E = Parent->child_end(); I != E; ++I) {
|
|
|
|
if (isContainedInStatement(Node, *I))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-17 11:36:20 +04:00
|
|
|
Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) {
|
2009-07-06 02:21:28 +04:00
|
|
|
assert(D && Node && "Passed null Decl or null Stmt");
|
|
|
|
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
Expr *Init = VD->getInit();
|
|
|
|
if (Init == 0)
|
|
|
|
return 0;
|
|
|
|
return isContainedInStatement(Node, Init) ? D : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (!FD->isThisDeclarationADefinition())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = FD->decls_begin(), E = FD->decls_end(); I != E; ++I) {
|
|
|
|
Decl *Child = FindImmediateParent(*I, Node);
|
|
|
|
if (Child)
|
|
|
|
return Child;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(FD->getBody() && "If not definition we should have exited already");
|
|
|
|
return isContainedInStatement(Node, FD->getBody()) ? D : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-07 01:34:20 +04:00
|
|
|
bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) {
|
2009-07-06 02:21:28 +04:00
|
|
|
assert(D && Node && "Passed null Decl or null Stmt");
|
|
|
|
return D == FindImmediateParent(D, Node);
|
|
|
|
}
|
|
|
|
|
2009-07-07 01:35:20 +04:00
|
|
|
SourceRange ASTLocation::getSourceRange() const {
|
|
|
|
return isDecl() ? getDecl()->getSourceRange() : getStmt()->getSourceRange();
|
|
|
|
}
|
|
|
|
|
2009-07-07 01:34:20 +04:00
|
|
|
void ASTLocation::print(llvm::raw_ostream &OS) {
|
|
|
|
assert(isValid() && "ASTLocation is not valid");
|
2009-07-06 02:21:28 +04:00
|
|
|
|
|
|
|
OS << "[Decl: " << getDecl()->getDeclKindName() << " ";
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(getDecl()))
|
|
|
|
OS << ND->getNameAsString();
|
|
|
|
|
|
|
|
if (getStmt()) {
|
|
|
|
ASTContext &Ctx = getDecl()->getASTContext();
|
|
|
|
OS << " | Stmt: " << getStmt()->getStmtClassName() << " ";
|
|
|
|
getStmt()->printPretty(OS, Ctx, 0, PrintingPolicy(Ctx.getLangOptions()));
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "] <";
|
|
|
|
|
2009-07-07 01:35:20 +04:00
|
|
|
SourceRange Range = getSourceRange();
|
2009-07-06 02:21:28 +04:00
|
|
|
SourceManager &SourceMgr = getDecl()->getASTContext().getSourceManager();
|
|
|
|
Range.getBegin().print(OS, SourceMgr);
|
|
|
|
OS << ", ";
|
|
|
|
Range.getEnd().print(OS, SourceMgr);
|
|
|
|
OS << ">\n";
|
|
|
|
}
|