blob: b7b2005e9f84d48dbc0e57e447cb6e1885e6c735 [file] [log] [blame]
Ted Kremenek99ecdd12008-06-20 21:40:36 +00001//===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ParentMap class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ParentMap.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000015#include "clang/AST/Decl.h"
Ted Kremenek99ecdd12008-06-20 21:40:36 +000016#include "clang/AST/Expr.h"
17#include "llvm/ADT/DenseMap.h"
18
19using namespace clang;
20
21typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
22
23static void BuildParentMap(MapTy& M, Stmt* S) {
John McCall8322c3a2011-02-13 04:07:26 +000024 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenek99ecdd12008-06-20 21:40:36 +000025 if (*I) {
26 M[*I] = S;
27 BuildParentMap(M, *I);
28 }
29}
30
31ParentMap::ParentMap(Stmt* S) : Impl(0) {
32 if (S) {
33 MapTy *M = new MapTy();
34 BuildParentMap(*M, S);
Mike Stump11289f42009-09-09 15:08:12 +000035 Impl = M;
Ted Kremenek99ecdd12008-06-20 21:40:36 +000036 }
37}
38
39ParentMap::~ParentMap() {
40 delete (MapTy*) Impl;
41}
42
Ted Kremenek43f50692010-11-15 20:54:24 +000043void ParentMap::addStmt(Stmt* S) {
44 if (S) {
45 BuildParentMap(*(MapTy*) Impl, S);
46 }
47}
48
Ted Kremenek99ecdd12008-06-20 21:40:36 +000049Stmt* ParentMap::getParent(Stmt* S) const {
50 MapTy* M = (MapTy*) Impl;
51 MapTy::iterator I = M->find(S);
52 return I == M->end() ? 0 : I->second;
53}
Ted Kremenek8b0dba32009-04-01 06:52:48 +000054
Ted Kremenekd4dacb02009-05-11 19:49:27 +000055Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
56 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
57 return S;
58}
59
Ted Kremenekb1c392a2011-02-12 00:17:19 +000060Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
61 do {
62 S = getParent(S);
63 }
64 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
65
66 return S;
67}
68
John McCall31168b02011-06-15 23:02:42 +000069Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
70 Stmt *Paren = 0;
71 while (isa<ParenExpr>(S)) {
72 Paren = S;
73 S = getParent(S);
74 };
75 return Paren;
76}
77
Ted Kremenek8b0dba32009-04-01 06:52:48 +000078bool ParentMap::isConsumedExpr(Expr* E) const {
79 Stmt *P = getParent(E);
80 Stmt *DirectChild = E;
Mike Stump11289f42009-09-09 15:08:12 +000081
Ted Kremenek8b0dba32009-04-01 06:52:48 +000082 // Ignore parents that are parentheses or casts.
Ted Kremenek90a61452009-05-05 22:16:12 +000083 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
Ted Kremenek8b0dba32009-04-01 06:52:48 +000084 DirectChild = P;
85 P = getParent(P);
86 }
Mike Stump11289f42009-09-09 15:08:12 +000087
Ted Kremenek8b0dba32009-04-01 06:52:48 +000088 if (!P)
89 return false;
Mike Stump11289f42009-09-09 15:08:12 +000090
Ted Kremenek8b0dba32009-04-01 06:52:48 +000091 switch (P->getStmtClass()) {
92 default:
93 return isa<Expr>(P);
94 case Stmt::DeclStmtClass:
95 return true;
96 case Stmt::BinaryOperatorClass: {
97 BinaryOperator *BE = cast<BinaryOperator>(P);
Ted Kremenek042befd2009-04-09 05:34:31 +000098 // If it is a comma, only the right side is consumed.
Ted Kremenekd43aaad2009-04-08 18:49:36 +000099 // If it isn't a comma, both sides are consumed.
John McCalle3027922010-08-25 11:45:40 +0000100 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000101 }
102 case Stmt::ForStmtClass:
103 return DirectChild == cast<ForStmt>(P)->getCond();
104 case Stmt::WhileStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000105 return DirectChild == cast<WhileStmt>(P)->getCond();
Ted Kremenek8b0dba32009-04-01 06:52:48 +0000106 case Stmt::DoStmtClass:
107 return DirectChild == cast<DoStmt>(P)->getCond();
108 case Stmt::IfStmtClass:
109 return DirectChild == cast<IfStmt>(P)->getCond();
110 case Stmt::IndirectGotoStmtClass:
111 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
112 case Stmt::SwitchStmtClass:
113 return DirectChild == cast<SwitchStmt>(P)->getCond();
114 case Stmt::ReturnStmtClass:
115 return true;
116 }
117}
118