2008-06-21 05:40:36 +08:00
|
|
|
//===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ParentMap class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ParentMap.h"
|
2008-08-11 14:23:49 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2008-06-21 05:40:36 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
|
|
|
|
|
|
|
|
static void BuildParentMap(MapTy& M, Stmt* S) {
|
2011-02-13 12:07:26 +08:00
|
|
|
for (Stmt::child_range I = S->children(); I; ++I)
|
2008-06-21 05:40:36 +08:00
|
|
|
if (*I) {
|
2012-08-07 05:28:11 +08:00
|
|
|
// Prefer the first time we see this statement in the traversal.
|
|
|
|
// This is important for PseudoObjectExprs.
|
|
|
|
Stmt *&Parent = M[*I];
|
|
|
|
if (!Parent) {
|
|
|
|
Parent = S;
|
|
|
|
BuildParentMap(M, *I);
|
|
|
|
}
|
2008-06-21 05:40:36 +08:00
|
|
|
}
|
2012-02-19 06:02:57 +08:00
|
|
|
|
|
|
|
// Also include the source expr tree of an OpaqueValueExpr in the map.
|
2012-08-07 05:28:11 +08:00
|
|
|
if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(S)) {
|
|
|
|
M[OVE->getSourceExpr()] = S;
|
2012-02-19 06:02:57 +08:00
|
|
|
BuildParentMap(M, OVE->getSourceExpr());
|
2012-08-07 05:28:11 +08:00
|
|
|
}
|
2008-06-21 05:40:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ParentMap::ParentMap(Stmt* S) : Impl(0) {
|
|
|
|
if (S) {
|
|
|
|
MapTy *M = new MapTy();
|
|
|
|
BuildParentMap(*M, S);
|
2009-09-09 23:08:12 +08:00
|
|
|
Impl = M;
|
2008-06-21 05:40:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ParentMap::~ParentMap() {
|
|
|
|
delete (MapTy*) Impl;
|
|
|
|
}
|
|
|
|
|
2010-11-16 04:54:24 +08:00
|
|
|
void ParentMap::addStmt(Stmt* S) {
|
|
|
|
if (S) {
|
|
|
|
BuildParentMap(*(MapTy*) Impl, S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-21 05:40:36 +08:00
|
|
|
Stmt* ParentMap::getParent(Stmt* S) const {
|
|
|
|
MapTy* M = (MapTy*) Impl;
|
|
|
|
MapTy::iterator I = M->find(S);
|
|
|
|
return I == M->end() ? 0 : I->second;
|
|
|
|
}
|
2009-04-01 14:52:48 +08:00
|
|
|
|
2009-05-12 03:49:27 +08:00
|
|
|
Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
|
|
|
|
do { S = getParent(S); } while (S && isa<ParenExpr>(S));
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2011-02-12 08:17:19 +08:00
|
|
|
Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
|
|
|
|
do {
|
|
|
|
S = getParent(S);
|
|
|
|
}
|
|
|
|
while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
|
|
|
|
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2011-07-27 13:28:18 +08:00
|
|
|
Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
|
|
|
|
do {
|
|
|
|
S = getParent(S);
|
|
|
|
} while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
|
|
|
|
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
|
|
|
|
Stmt *Paren = 0;
|
|
|
|
while (isa<ParenExpr>(S)) {
|
|
|
|
Paren = S;
|
|
|
|
S = getParent(S);
|
|
|
|
};
|
|
|
|
return Paren;
|
|
|
|
}
|
|
|
|
|
2009-04-01 14:52:48 +08:00
|
|
|
bool ParentMap::isConsumedExpr(Expr* E) const {
|
|
|
|
Stmt *P = getParent(E);
|
|
|
|
Stmt *DirectChild = E;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-01 14:52:48 +08:00
|
|
|
// Ignore parents that are parentheses or casts.
|
2009-05-06 06:16:12 +08:00
|
|
|
while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
|
2009-04-01 14:52:48 +08:00
|
|
|
DirectChild = P;
|
|
|
|
P = getParent(P);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-01 14:52:48 +08:00
|
|
|
if (!P)
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-01 14:52:48 +08:00
|
|
|
switch (P->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
return isa<Expr>(P);
|
|
|
|
case Stmt::DeclStmtClass:
|
|
|
|
return true;
|
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
BinaryOperator *BE = cast<BinaryOperator>(P);
|
2009-04-09 13:34:31 +08:00
|
|
|
// If it is a comma, only the right side is consumed.
|
2009-04-09 02:49:36 +08:00
|
|
|
// If it isn't a comma, both sides are consumed.
|
2010-08-25 19:45:40 +08:00
|
|
|
return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
|
2009-04-01 14:52:48 +08:00
|
|
|
}
|
|
|
|
case Stmt::ForStmtClass:
|
|
|
|
return DirectChild == cast<ForStmt>(P)->getCond();
|
|
|
|
case Stmt::WhileStmtClass:
|
2009-09-09 23:08:12 +08:00
|
|
|
return DirectChild == cast<WhileStmt>(P)->getCond();
|
2009-04-01 14:52:48 +08:00
|
|
|
case Stmt::DoStmtClass:
|
|
|
|
return DirectChild == cast<DoStmt>(P)->getCond();
|
|
|
|
case Stmt::IfStmtClass:
|
|
|
|
return DirectChild == cast<IfStmt>(P)->getCond();
|
|
|
|
case Stmt::IndirectGotoStmtClass:
|
|
|
|
return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
|
|
|
|
case Stmt::SwitchStmtClass:
|
|
|
|
return DirectChild == cast<SwitchStmt>(P)->getCond();
|
|
|
|
case Stmt::ReturnStmtClass:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|