Add more const-goodness to ASTLocation.

llvm-svn: 83087
This commit is contained in:
Argyrios Kyrtzidis 2009-09-29 19:39:53 +00:00
parent d71a03b164
commit 81aba1fe53
3 changed files with 20 additions and 21 deletions

View File

@ -37,22 +37,21 @@ namespace idx {
/// like the declaration context, ASTContext, etc.
///
class ASTLocation {
Decl *D;
Stmt *Stm;
const Decl *D;
const Stmt *Stm;
public:
ASTLocation() : D(0), Stm(0) {}
explicit ASTLocation(const Decl *d, const Stmt *stm = 0)
: D(const_cast<Decl*>(d)), Stm(const_cast<Stmt*>(stm)) {
explicit ASTLocation(const Decl *d, const Stmt *stm = 0) : D(d), Stm(stm) {
assert((Stm == 0 || isImmediateParent(D, Stm)) &&
"The Decl is not the immediate parent of the Stmt.");
}
const Decl *getDecl() const { return D; }
const Stmt *getStmt() const { return Stm; }
Decl *getDecl() { return D; }
Stmt *getStmt() { return Stm; }
Decl *getDecl() { return const_cast<Decl*>(D); }
Stmt *getStmt() { return const_cast<Stmt*>(Stm); }
bool isValid() const { return D != 0; }
bool isInvalid() const { return !isValid(); }
@ -72,8 +71,8 @@ public:
SourceRange getSourceRange() const;
/// \brief Checks that D is the immediate Decl parent of Node.
static bool isImmediateParent(Decl *D, Stmt *Node);
static Decl *FindImmediateParent(Decl *D, Stmt *Node);
static bool isImmediateParent(const Decl *D, const Stmt *Node);
static const Decl *FindImmediateParent(const Decl *D, const Stmt *Node);
friend bool operator==(const ASTLocation &L, const ASTLocation &R) {
return L.D == R.D && L.Stm == R.Stm;

View File

@ -52,7 +52,7 @@ void CGBuilder::VisitCallExpr(CallExpr *CE) {
Entity Ent = Entity::get(CalleeDecl, G.getProgram());
CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent);
Decl *Parent = ASTLocation::FindImmediateParent(FD, CE);
const Decl *Parent = ASTLocation::FindImmediateParent(FD, CE);
CallerNode->addCallee(ASTLocation(Parent, CE), CalleeNode);
}

View File

@ -47,13 +47,13 @@ Decl *ASTLocation::getReferencedDecl() {
}
static bool isContainedInStatement(Stmt *Node, Stmt *Parent) {
static bool isContainedInStatement(const Stmt *Node, const Stmt *Parent) {
assert(Node && Parent && "Passed null Node or Parent");
if (Node == Parent)
return true;
for (Stmt::child_iterator
for (Stmt::const_child_iterator
I = Parent->child_begin(), E = Parent->child_end(); I != E; ++I) {
if (*I)
if (isContainedInStatement(Node, *I))
@ -63,23 +63,23 @@ static bool isContainedInStatement(Stmt *Node, Stmt *Parent) {
return false;
}
Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) {
const Decl *ASTLocation::FindImmediateParent(const Decl *D, const Stmt *Node) {
assert(D && Node && "Passed null Decl or null Stmt");
if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
Expr *Init = VD->getInit();
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
const Expr *Init = VD->getInit();
if (Init == 0)
return 0;
return isContainedInStatement(Node, Init) ? D : 0;
}
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
if (const 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);
const Decl *Child = FindImmediateParent(*I, Node);
if (Child)
return Child;
}
@ -88,13 +88,13 @@ Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) {
return isContainedInStatement(Node, FD->getBody()) ? D : 0;
}
if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
if (const 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);
const Decl *Child = FindImmediateParent(*I, Node);
if (Child)
return Child;
}
@ -103,10 +103,10 @@ Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) {
return isContainedInStatement(Node, MD->getBody()) ? D : 0;
}
if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
if (const 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);
const Decl *Child = FindImmediateParent(*I, Node);
if (Child)
return Child;
}
@ -118,7 +118,7 @@ Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) {
return 0;
}
bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) {
bool ASTLocation::isImmediateParent(const Decl *D, const Stmt *Node) {
assert(D && Node && "Passed null Decl or null Stmt");
return D == FindImmediateParent(D, Node);
}