Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 1 | //===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the CFGStmtMap class, which defines a mapping from |
| 10 | // Stmt* to CFGBlock* |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include "clang/AST/ParentMap.h" |
| 16 | #include "clang/Analysis/CFG.h" |
| 17 | #include "clang/Analysis/CFGStmtMap.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
Ted Kremenek | adfb445 | 2011-08-23 23:05:04 +0000 | [diff] [blame] | 21 | typedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap; |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 22 | static SMap *AsMap(void *m) { return (SMap*) m; } |
| 23 | |
| 24 | CFGStmtMap::~CFGStmtMap() { delete AsMap(M); } |
| 25 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 26 | CFGBlock *CFGStmtMap::getBlock(Stmt *S) { |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 27 | SMap *SM = AsMap(M); |
| 28 | Stmt *X = S; |
| 29 | |
| 30 | // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors |
| 31 | // is in the map. |
| 32 | while (X) { |
Tom Care | ec389da | 2010-08-11 23:36:58 +0000 | [diff] [blame] | 33 | SMap::iterator I = SM->find(X); |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 34 | if (I != SM->end()) { |
| 35 | CFGBlock *B = I->second; |
| 36 | // Memoize this lookup. |
| 37 | if (X != S) |
| 38 | (*SM)[X] = B; |
| 39 | return B; |
| 40 | } |
| 41 | |
Ted Kremenek | 8a297e9 | 2010-08-05 00:03:46 +0000 | [diff] [blame] | 42 | X = PM->getParentIgnoreParens(X); |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 43 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 44 | |
| 45 | return nullptr; |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 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; |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 52 | Optional<CFGStmt> CS = CE.getAs<CFGStmt>(); |
Ted Kremenek | 96a7a59 | 2011-03-01 03:15:10 +0000 | [diff] [blame] | 53 | if (!CS) |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 54 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 55 | |
David Blaikie | 00be69a | 2013-02-23 00:29:34 +0000 | [diff] [blame] | 56 | CFGBlock *&Entry = SM[CS->getStmt()]; |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 57 | // If 'Entry' is already initialized (e.g., a terminator was already), |
| 58 | // skip. |
| 59 | if (Entry) |
| 60 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 61 | |
Zhongxing Xu | 2cd7a78 | 2010-09-16 01:25:47 +0000 | [diff] [blame] | 62 | Entry = B; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 64 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 66 | // Look at the label of the block. |
| 67 | if (Stmt *Label = B->getLabel()) |
| 68 | SM[Label] = B; |
| 69 | |
| 70 | // Finally, look at the terminator. If the terminator was already added |
| 71 | // because it is a block-level expression in another block, overwrite |
| 72 | // that mapping. |
Artem Dergachev | 4e53032 | 2019-05-24 01:34:22 +0000 | [diff] [blame] | 73 | if (Stmt *Term = B->getTerminatorStmt()) |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 74 | SM[Term] = B; |
| 75 | } |
| 76 | |
| 77 | CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) { |
| 78 | if (!C || !PM) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 79 | return nullptr; |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 80 | |
| 81 | SMap *SM = new SMap(); |
| 82 | |
| 83 | // Walk all blocks, accumulating the block-level expressions, labels, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 84 | // and terminators. |
Ted Kremenek | 0950212 | 2010-08-04 18:23:15 +0000 | [diff] [blame] | 85 | for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I) |
| 86 | Accumulate(*SM, *I); |
| 87 | |
| 88 | return new CFGStmtMap(PM, SM); |
| 89 | } |
| 90 | |