blob: 9d87daa0bfd8613e149136dcd90040c5c3122d06 [file] [log] [blame]
Ted Kremenekf8e32cf2008-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 Dunbaracc5f3e2008-08-11 06:23:49 +000015#include "clang/AST/Decl.h"
Ted Kremenekf8e32cf2008-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) {
24 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
25 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);
35 Impl = M;
36 }
37}
38
39ParentMap::~ParentMap() {
40 delete (MapTy*) Impl;
41}
42
43Stmt* ParentMap::getParent(Stmt* S) const {
44 MapTy* M = (MapTy*) Impl;
45 MapTy::iterator I = M->find(S);
46 return I == M->end() ? 0 : I->second;
47}
Ted Kremenekb930d7a2009-04-01 06:52:48 +000048
Ted Kremenekb1b9f682009-05-11 19:49:27 +000049Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
50 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
51 return S;
52}
53
Ted Kremenekb930d7a2009-04-01 06:52:48 +000054bool ParentMap::isConsumedExpr(Expr* E) const {
55 Stmt *P = getParent(E);
56 Stmt *DirectChild = E;
57
58 // Ignore parents that are parentheses or casts.
Ted Kremenekade9eca2009-05-05 22:16:12 +000059 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P))) {
Ted Kremenekb930d7a2009-04-01 06:52:48 +000060 DirectChild = P;
61 P = getParent(P);
62 }
63
64 if (!P)
65 return false;
66
67 switch (P->getStmtClass()) {
68 default:
69 return isa<Expr>(P);
70 case Stmt::DeclStmtClass:
71 return true;
72 case Stmt::BinaryOperatorClass: {
73 BinaryOperator *BE = cast<BinaryOperator>(P);
Ted Kremenek24ae89a2009-04-09 05:34:31 +000074 // If it is a comma, only the right side is consumed.
Ted Kremeneke42ac982009-04-08 18:49:36 +000075 // If it isn't a comma, both sides are consumed.
Ted Kremenek24ae89a2009-04-09 05:34:31 +000076 return BE->getOpcode()!=BinaryOperator::Comma ||DirectChild==BE->getRHS();
Ted Kremenekb930d7a2009-04-01 06:52:48 +000077 }
78 case Stmt::ForStmtClass:
79 return DirectChild == cast<ForStmt>(P)->getCond();
80 case Stmt::WhileStmtClass:
81 return DirectChild == cast<WhileStmt>(P)->getCond();
82 case Stmt::DoStmtClass:
83 return DirectChild == cast<DoStmt>(P)->getCond();
84 case Stmt::IfStmtClass:
85 return DirectChild == cast<IfStmt>(P)->getCond();
86 case Stmt::IndirectGotoStmtClass:
87 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
88 case Stmt::SwitchStmtClass:
89 return DirectChild == cast<SwitchStmt>(P)->getCond();
90 case Stmt::ReturnStmtClass:
91 return true;
92 }
93}
94