/src/llvm-project/clang/lib/Analysis/CFGStmtMap.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CFGStmtMap.h - Map from Stmt* to CFGBlock* -----------*- C++ -*-===// |
2 | | // |
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 |
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 | | #include <optional> |
19 | | |
20 | | using namespace clang; |
21 | | |
22 | | typedef llvm::DenseMap<const Stmt*, CFGBlock*> SMap; |
23 | 0 | static SMap *AsMap(void *m) { return (SMap*) m; } |
24 | | |
25 | 0 | CFGStmtMap::~CFGStmtMap() { delete AsMap(M); } |
26 | | |
27 | 0 | CFGBlock *CFGStmtMap::getBlock(Stmt *S) { |
28 | 0 | SMap *SM = AsMap(M); |
29 | 0 | Stmt *X = S; |
30 | | |
31 | | // If 'S' isn't in the map, walk the ParentMap to see if one of its ancestors |
32 | | // is in the map. |
33 | 0 | while (X) { |
34 | 0 | SMap::iterator I = SM->find(X); |
35 | 0 | if (I != SM->end()) { |
36 | 0 | CFGBlock *B = I->second; |
37 | | // Memoize this lookup. |
38 | 0 | if (X != S) |
39 | 0 | (*SM)[X] = B; |
40 | 0 | return B; |
41 | 0 | } |
42 | | |
43 | 0 | X = PM->getParentIgnoreParens(X); |
44 | 0 | } |
45 | | |
46 | 0 | return nullptr; |
47 | 0 | } |
48 | | |
49 | 0 | static void Accumulate(SMap &SM, CFGBlock *B) { |
50 | | // First walk the block-level expressions. |
51 | 0 | for (CFGBlock::iterator I = B->begin(), E = B->end(); I != E; ++I) { |
52 | 0 | const CFGElement &CE = *I; |
53 | 0 | std::optional<CFGStmt> CS = CE.getAs<CFGStmt>(); |
54 | 0 | if (!CS) |
55 | 0 | continue; |
56 | | |
57 | 0 | CFGBlock *&Entry = SM[CS->getStmt()]; |
58 | | // If 'Entry' is already initialized (e.g., a terminator was already), |
59 | | // skip. |
60 | 0 | if (Entry) |
61 | 0 | continue; |
62 | | |
63 | 0 | Entry = B; |
64 | |
|
65 | 0 | } |
66 | | |
67 | | // Look at the label of the block. |
68 | 0 | if (Stmt *Label = B->getLabel()) |
69 | 0 | SM[Label] = B; |
70 | | |
71 | | // Finally, look at the terminator. If the terminator was already added |
72 | | // because it is a block-level expression in another block, overwrite |
73 | | // that mapping. |
74 | 0 | if (Stmt *Term = B->getTerminatorStmt()) |
75 | 0 | SM[Term] = B; |
76 | 0 | } |
77 | | |
78 | 0 | CFGStmtMap *CFGStmtMap::Build(CFG *C, ParentMap *PM) { |
79 | 0 | if (!C || !PM) |
80 | 0 | return nullptr; |
81 | | |
82 | 0 | SMap *SM = new SMap(); |
83 | | |
84 | | // Walk all blocks, accumulating the block-level expressions, labels, |
85 | | // and terminators. |
86 | 0 | for (CFG::iterator I = C->begin(), E = C->end(); I != E; ++I) |
87 | 0 | Accumulate(*SM, *I); |
88 | |
|
89 | 0 | return new CFGStmtMap(PM, SM); |
90 | 0 | } |
91 | | |