2009-07-07 05:34:47 +08:00
|
|
|
//===--- ASTLocation.cpp - A <Decl, Stmt> pair ------------------*- C++ -*-===//
|
2009-07-06 06:21:28 +08: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 05:34:20 +08:00
|
|
|
// ASTLocation is Decl or a Stmt and its immediate Decl parent.
|
2009-07-06 06:21:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-07 05:34:47 +08:00
|
|
|
#include "clang/Index/ASTLocation.h"
|
2009-07-06 06:21:28 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-07-18 08:33:46 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-07-06 06:21:28 +08:00
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
2009-07-21 08:05:38 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2009-07-06 06:21:28 +08:00
|
|
|
using namespace clang;
|
2009-07-07 05:34:47 +08:00
|
|
|
using namespace idx;
|
2009-07-06 06:21:28 +08:00
|
|
|
|
2009-07-19 05:17:58 +08:00
|
|
|
static Decl *getDeclFromExpr(Stmt *E) {
|
|
|
|
if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
|
|
|
|
return RefExpr->getDecl();
|
|
|
|
if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
|
|
|
|
return ME->getMemberDecl();
|
2009-07-21 08:05:38 +08:00
|
|
|
if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
|
|
|
|
return RE->getDecl();
|
|
|
|
|
2009-07-19 05:17:58 +08:00
|
|
|
if (CallExpr *CE = dyn_cast<CallExpr>(E))
|
|
|
|
return getDeclFromExpr(CE->getCallee());
|
|
|
|
if (CastExpr *CE = dyn_cast<CastExpr>(E))
|
|
|
|
return getDeclFromExpr(CE->getSubExpr());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Decl *ASTLocation::getReferencedDecl() {
|
|
|
|
if (isInvalid())
|
|
|
|
return 0;
|
|
|
|
if (isDecl())
|
|
|
|
return getDecl();
|
|
|
|
|
|
|
|
assert(getStmt());
|
|
|
|
return getDeclFromExpr(getStmt());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-06 06:21:28 +08: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) {
|
2009-07-20 16:28:49 +08:00
|
|
|
if (*I)
|
|
|
|
if (isContainedInStatement(Node, *I))
|
|
|
|
return true;
|
2009-07-06 06:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-17 15:36:20 +08:00
|
|
|
Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) {
|
2009-07-06 06:21:28 +08: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;
|
|
|
|
}
|
2009-07-18 08:33:46 +08:00
|
|
|
|
2009-07-06 06:21:28 +08:00
|
|
|
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;
|
|
|
|
}
|
2009-07-18 08:33:46 +08:00
|
|
|
|
2009-07-06 06:21:28 +08:00
|
|
|
assert(FD->getBody() && "If not definition we should have exited already");
|
|
|
|
return isContainedInStatement(Node, FD->getBody()) ? D : 0;
|
|
|
|
}
|
2009-07-18 08:33:46 +08:00
|
|
|
|
|
|
|
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
|
|
|
|
if (!MD->getBody())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = MD->decls_begin(), E = MD->decls_end(); I != E; ++I) {
|
|
|
|
Decl *Child = FindImmediateParent(*I, Node);
|
|
|
|
if (Child)
|
|
|
|
return Child;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(MD->getBody() && "If not definition we should have exited already");
|
|
|
|
return isContainedInStatement(Node, MD->getBody()) ? D : 0;
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:40:48 +08:00
|
|
|
if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = BD->decls_begin(), E = BD->decls_end(); I != E; ++I) {
|
|
|
|
Decl *Child = FindImmediateParent(*I, Node);
|
|
|
|
if (Child)
|
|
|
|
return Child;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(BD->getBody() && "BlockDecl without body ?");
|
|
|
|
return isContainedInStatement(Node, BD->getBody()) ? D : 0;
|
|
|
|
}
|
|
|
|
|
2009-07-06 06:21:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-07 05:34:20 +08:00
|
|
|
bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) {
|
2009-07-06 06:21:28 +08:00
|
|
|
assert(D && Node && "Passed null Decl or null Stmt");
|
|
|
|
return D == FindImmediateParent(D, Node);
|
|
|
|
}
|
|
|
|
|
2009-07-07 05:35:20 +08:00
|
|
|
SourceRange ASTLocation::getSourceRange() const {
|
2009-07-19 05:17:43 +08:00
|
|
|
if (isInvalid())
|
|
|
|
return SourceRange();
|
2009-07-07 05:35:20 +08:00
|
|
|
return isDecl() ? getDecl()->getSourceRange() : getStmt()->getSourceRange();
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:39:35 +08:00
|
|
|
void ASTLocation::print(llvm::raw_ostream &OS) const {
|
2009-07-19 05:17:43 +08:00
|
|
|
if (isInvalid()) {
|
|
|
|
OS << "<< Invalid ASTLocation >>\n";
|
|
|
|
return;
|
|
|
|
}
|
2009-07-06 06:21:28 +08:00
|
|
|
|
|
|
|
OS << "[Decl: " << getDecl()->getDeclKindName() << " ";
|
2009-07-30 07:39:35 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(getDecl()))
|
2009-07-06 06:21:28 +08:00
|
|
|
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 05:35:20 +08:00
|
|
|
SourceRange Range = getSourceRange();
|
2009-07-06 06:21:28 +08:00
|
|
|
SourceManager &SourceMgr = getDecl()->getASTContext().getSourceManager();
|
|
|
|
Range.getBegin().print(OS, SourceMgr);
|
|
|
|
OS << ", ";
|
|
|
|
Range.getEnd().print(OS, SourceMgr);
|
|
|
|
OS << ">\n";
|
|
|
|
}
|