blob: bab3f4c38c759e96d7df7da1cdbf27e7e8f0171f [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"
15#include "clang/AST/Expr.h"
16#include "llvm/ADT/DenseMap.h"
17
18using namespace clang;
19
20typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
21
22static void BuildParentMap(MapTy& M, Stmt* S) {
23 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I)
24 if (*I) {
25 M[*I] = S;
26 BuildParentMap(M, *I);
27 }
28}
29
30ParentMap::ParentMap(Stmt* S) : Impl(0) {
31 if (S) {
32 MapTy *M = new MapTy();
33 BuildParentMap(*M, S);
34 Impl = M;
35 }
36}
37
38ParentMap::~ParentMap() {
39 delete (MapTy*) Impl;
40}
41
42Stmt* ParentMap::getParent(Stmt* S) const {
43 MapTy* M = (MapTy*) Impl;
44 MapTy::iterator I = M->find(S);
45 return I == M->end() ? 0 : I->second;
46}
47
48bool ParentMap::isSubExpr(Stmt* S) const {
49 if (!isa<Expr>(S))
50 return false;
51
52 Stmt* P = getParent(S);
53 return P ? !isa<CompoundStmt>(P) : false;
54}