1212795Sdim//===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===// 2212795Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6212795Sdim// 7212795Sdim//===----------------------------------------------------------------------===// 8212795Sdim// 9212795Sdim// This file defines the CFGStmtMap class, which defines a mapping from 10212795Sdim// Stmt* to CFGBlock* 11212795Sdim// 12212795Sdim//===----------------------------------------------------------------------===// 13212795Sdim 14212795Sdim#include "llvm/ADT/DenseMap.h" 15212795Sdim#include "clang/AST/ParentMap.h" 16212795Sdim#include "clang/Analysis/CFG.h" 17212795Sdim#include "clang/Analysis/CFGStmtMap.h" 18212795Sdim 19212795Sdimusing namespace clang; 20212795Sdim 21226633Sdimtypedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap; 22212795Sdimstatic SMap *AsMap(void *m) { return (SMap*) m; } 23212795Sdim 24212795SdimCFGStmtMap::~CFGStmtMap() { delete AsMap(M); } 25212795Sdim 26341825SdimCFGBlock *CFGStmtMap::getBlock(Stmt *S) { 27212795Sdim SMap *SM = AsMap(M); 28212795Sdim Stmt *X = S; 29212795Sdim 30212795Sdim // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors 31212795Sdim // is in the map. 32212795Sdim while (X) { 33212795Sdim SMap::iterator I = SM->find(X); 34212795Sdim if (I != SM->end()) { 35212795Sdim CFGBlock *B = I->second; 36212795Sdim // Memoize this lookup. 37212795Sdim if (X != S) 38212795Sdim (*SM)[X] = B; 39212795Sdim return B; 40212795Sdim } 41212795Sdim 42212795Sdim X = PM->getParentIgnoreParens(X); 43212795Sdim } 44276479Sdim 45276479Sdim return nullptr; 46212795Sdim} 47212795Sdim 48212795Sdimstatic void Accumulate(SMap &SM, CFGBlock *B) { 49212795Sdim // First walk the block-level expressions. 50212795Sdim for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; ++I) { 51212795Sdim const CFGElement &CE = *I; 52249423Sdim Optional<CFGStmt> CS = CE.getAs<CFGStmt>(); 53221345Sdim if (!CS) 54218893Sdim continue; 55341825Sdim 56221345Sdim CFGBlock *&Entry = SM[CS->getStmt()]; 57218893Sdim // If 'Entry' is already initialized (e.g., a terminator was already), 58218893Sdim // skip. 59218893Sdim if (Entry) 60218893Sdim continue; 61341825Sdim 62218893Sdim Entry = B; 63341825Sdim 64212795Sdim } 65341825Sdim 66212795Sdim // Look at the label of the block. 67212795Sdim if (Stmt *Label = B->getLabel()) 68212795Sdim SM[Label] = B; 69212795Sdim 70212795Sdim // Finally, look at the terminator. If the terminator was already added 71212795Sdim // because it is a block-level expression in another block, overwrite 72212795Sdim // that mapping. 73353358Sdim if (Stmt *Term = B->getTerminatorStmt()) 74212795Sdim SM[Term] = B; 75212795Sdim} 76212795Sdim 77212795SdimCFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) { 78212795Sdim if (!C || !PM) 79276479Sdim return nullptr; 80212795Sdim 81212795Sdim SMap *SM = new SMap(); 82212795Sdim 83212795Sdim // Walk all blocks, accumulating the block-level expressions, labels, 84341825Sdim // and terminators. 85212795Sdim for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I) 86212795Sdim Accumulate(*SM, *I); 87212795Sdim 88212795Sdim return new CFGStmtMap(PM, SM); 89212795Sdim} 90212795Sdim 91