CFGStmtMap.cpp revision 249423
1212795Sdim//===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===// 2212795Sdim// 3212795Sdim// The LLVM Compiler Infrastructure 4212795Sdim// 5212795Sdim// This file is distributed under the University of Illinois Open Source 6212795Sdim// License. See LICENSE.TXT for details. 7212795Sdim// 8212795Sdim//===----------------------------------------------------------------------===// 9212795Sdim// 10212795Sdim// This file defines the CFGStmtMap class, which defines a mapping from 11212795Sdim// Stmt* to CFGBlock* 12212795Sdim// 13212795Sdim//===----------------------------------------------------------------------===// 14212795Sdim 15212795Sdim#include "llvm/ADT/DenseMap.h" 16212795Sdim#include "clang/AST/ParentMap.h" 17212795Sdim#include "clang/Analysis/CFG.h" 18212795Sdim#include "clang/Analysis/CFGStmtMap.h" 19212795Sdim 20212795Sdimusing namespace clang; 21212795Sdim 22226633Sdimtypedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap; 23212795Sdimstatic SMap *AsMap(void *m) { return (SMap*) m; } 24212795Sdim 25212795SdimCFGStmtMap::~CFGStmtMap() { delete AsMap(M); } 26212795Sdim 27212795SdimCFGBlock *CFGStmtMap::getBlock(Stmt *S) { 28212795Sdim SMap *SM = AsMap(M); 29212795Sdim Stmt *X = S; 30212795Sdim 31212795Sdim // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors 32212795Sdim // is in the map. 33212795Sdim while (X) { 34212795Sdim SMap::iterator I = SM->find(X); 35212795Sdim if (I != SM->end()) { 36212795Sdim CFGBlock *B = I->second; 37212795Sdim // Memoize this lookup. 38212795Sdim if (X != S) 39212795Sdim (*SM)[X] = B; 40212795Sdim return B; 41212795Sdim } 42212795Sdim 43212795Sdim X = PM->getParentIgnoreParens(X); 44212795Sdim } 45212795Sdim 46212795Sdim return 0; 47212795Sdim} 48212795Sdim 49212795Sdimstatic void Accumulate(SMap &SM, CFGBlock *B) { 50212795Sdim // First walk the block-level expressions. 51212795Sdim for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; ++I) { 52212795Sdim const CFGElement &CE = *I; 53249423Sdim Optional<CFGStmt> CS = CE.getAs<CFGStmt>(); 54221345Sdim if (!CS) 55218893Sdim continue; 56218893Sdim 57221345Sdim CFGBlock *&Entry = SM[CS->getStmt()]; 58218893Sdim // If 'Entry' is already initialized (e.g., a terminator was already), 59218893Sdim // skip. 60218893Sdim if (Entry) 61218893Sdim continue; 62212795Sdim 63218893Sdim Entry = B; 64218893Sdim 65212795Sdim } 66212795Sdim 67212795Sdim // Look at the label of the block. 68212795Sdim if (Stmt *Label = B->getLabel()) 69212795Sdim SM[Label] = B; 70212795Sdim 71212795Sdim // Finally, look at the terminator. If the terminator was already added 72212795Sdim // because it is a block-level expression in another block, overwrite 73212795Sdim // that mapping. 74212795Sdim if (Stmt *Term = B->getTerminator()) 75212795Sdim SM[Term] = B; 76212795Sdim} 77212795Sdim 78212795SdimCFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) { 79212795Sdim if (!C || !PM) 80212795Sdim return 0; 81212795Sdim 82212795Sdim SMap *SM = new SMap(); 83212795Sdim 84212795Sdim // Walk all blocks, accumulating the block-level expressions, labels, 85212795Sdim // and terminators. 86212795Sdim for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I) 87212795Sdim Accumulate(*SM, *I); 88212795Sdim 89212795Sdim return new CFGStmtMap(PM, SM); 90212795Sdim} 91212795Sdim 92