1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_POSTORDERCFGVIEW_H |
14 | #define LLVM_CLANG_ANALYSIS_ANALYSES_POSTORDERCFGVIEW_H |
15 | |
16 | #include "clang/Analysis/AnalysisDeclContext.h" |
17 | #include "clang/Analysis/CFG.h" |
18 | #include "clang/Basic/LLVM.h" |
19 | #include "llvm/ADT/BitVector.h" |
20 | #include "llvm/ADT/DenseMap.h" |
21 | #include "llvm/ADT/None.h" |
22 | #include "llvm/ADT/PostOrderIterator.h" |
23 | #include <utility> |
24 | #include <vector> |
25 | |
26 | namespace clang { |
27 | |
28 | class PostOrderCFGView : public ManagedAnalysis { |
29 | virtual void anchor(); |
30 | |
31 | public: |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | class CFGBlockSet { |
39 | llvm::BitVector VisitedBlockIDs; |
40 | |
41 | public: |
42 | |
43 | |
44 | struct iterator { using value_type = const CFGBlock *; }; |
45 | |
46 | CFGBlockSet() = default; |
47 | CFGBlockSet(const CFG *G) : VisitedBlockIDs(G->getNumBlockIDs(), false) {} |
48 | |
49 | |
50 | |
51 | std::pair<llvm::NoneType, bool> insert(const CFGBlock *Block) { |
52 | |
53 | |
54 | |
55 | |
56 | if (!Block) |
57 | return std::make_pair(None, false); |
58 | if (VisitedBlockIDs.test(Block->getBlockID())) |
59 | return std::make_pair(None, false); |
60 | VisitedBlockIDs.set(Block->getBlockID()); |
61 | return std::make_pair(None, true); |
62 | } |
63 | |
64 | |
65 | |
66 | |
67 | bool alreadySet(const CFGBlock *Block) { |
68 | return VisitedBlockIDs.test(Block->getBlockID()); |
69 | } |
70 | }; |
71 | |
72 | private: |
73 | using po_iterator = llvm::po_iterator<const CFG *, CFGBlockSet, true>; |
74 | std::vector<const CFGBlock *> Blocks; |
75 | |
76 | using BlockOrderTy = llvm::DenseMap<const CFGBlock *, unsigned>; |
77 | BlockOrderTy BlockOrder; |
78 | |
79 | public: |
80 | friend struct BlockOrderCompare; |
81 | |
82 | using iterator = std::vector<const CFGBlock *>::reverse_iterator; |
83 | using const_iterator = std::vector<const CFGBlock *>::const_reverse_iterator; |
84 | |
85 | PostOrderCFGView(const CFG *cfg); |
86 | |
87 | iterator begin() { return Blocks.rbegin(); } |
88 | iterator end() { return Blocks.rend(); } |
89 | |
90 | const_iterator begin() const { return Blocks.rbegin(); } |
91 | const_iterator end() const { return Blocks.rend(); } |
92 | |
93 | bool empty() const { return begin() == end(); } |
94 | |
95 | struct BlockOrderCompare { |
96 | const PostOrderCFGView &POV; |
97 | |
98 | public: |
99 | BlockOrderCompare(const PostOrderCFGView &pov) : POV(pov) {} |
100 | |
101 | bool operator()(const CFGBlock *b1, const CFGBlock *b2) const; |
102 | }; |
103 | |
104 | BlockOrderCompare getComparator() const { |
105 | return BlockOrderCompare(*this); |
106 | } |
107 | |
108 | |
109 | static const void *getTag(); |
110 | |
111 | static PostOrderCFGView *create(AnalysisDeclContext &analysisContext); |
112 | }; |
113 | |
114 | } |
115 | |
116 | #endif |
117 | |