1 //===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the CFGStmtMap class, which defines a mapping from
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/DenseMap.h"
15 #include "clang/AST/ParentMap.h"
16 #include "clang/Analysis/CFG.h"
17 #include "clang/Analysis/CFGStmtMap.h"
19 using namespace clang;
21 typedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap;
22 static SMap *AsMap(void *m) { return (SMap*) m; }
24 CFGStmtMap::~CFGStmtMap() { delete AsMap(M); }
26 CFGBlock *CFGStmtMap::getBlock(Stmt *S) {
30 // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors
33 SMap::iterator I = SM->find(X);
35 CFGBlock *B = I->second;
36 // Memoize this lookup.
42 X = PM->getParentIgnoreParens(X);
48 static void Accumulate(SMap &SM, CFGBlock *B) {
49 // First walk the block-level expressions.
50 for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; ++I) {
51 const CFGElement &CE = *I;
52 Optional<CFGStmt> CS = CE.getAs<CFGStmt>();
56 CFGBlock *&Entry = SM[CS->getStmt()];
57 // If 'Entry' is already initialized (e.g., a terminator was already),
66 // Look at the label of the block.
67 if (Stmt *Label = B->getLabel())
70 // Finally, look at the terminator. If the terminator was already added
71 // because it is a block-level expression in another block, overwrite
73 if (Stmt *Term = B->getTerminatorStmt())
77 CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) {
81 SMap *SM = new SMap();
83 // Walk all blocks, accumulating the block-level expressions, labels,
85 for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I)
88 return new CFGStmtMap(PM, SM);