Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1 | //===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the CFG and CFGBuilder classes for representing and |
| 11 | // building Control-Flow Graphs (CFGs) from ASTs. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/CFG.h" |
| 16 | #include "clang/AST/Expr.h" |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/PrettyPrinter.h" |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 21 | #include "llvm/Support/GraphWriter.h" |
Ted Kremenek | 7e3a89d | 2007-12-17 19:35:20 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
Ted Kremenek | 6fa9b88 | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 24 | #include <llvm/Support/Allocator.h> |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 25 | #include <iomanip> |
| 26 | #include <algorithm> |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 27 | #include <sstream> |
Ted Kremenek | 83c01da | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
| 31 | namespace { |
| 32 | |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 33 | // SaveAndRestore - A utility class that uses RIIA to save and restore |
| 34 | // the value of a variable. |
| 35 | template<typename T> |
Ted Kremenek | 6fa9b88 | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 36 | struct VISIBILITY_HIDDEN SaveAndRestore { |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 37 | SaveAndRestore(T& x) : X(x), old_value(x) {} |
| 38 | ~SaveAndRestore() { X = old_value; } |
Ted Kremenek | b6f7b72 | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 39 | T get() { return old_value; } |
| 40 | |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 41 | T& X; |
| 42 | T old_value; |
| 43 | }; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 44 | |
| 45 | /// CFGBuilder - This class is implements CFG construction from an AST. |
| 46 | /// The builder is stateful: an instance of the builder should be used to only |
| 47 | /// construct a single CFG. |
| 48 | /// |
| 49 | /// Example usage: |
| 50 | /// |
| 51 | /// CFGBuilder builder; |
| 52 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 53 | /// |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 54 | /// CFG construction is done via a recursive walk of an AST. |
| 55 | /// We actually parse the AST in reverse order so that the successor |
| 56 | /// of a basic block is constructed prior to its predecessor. This |
| 57 | /// allows us to nicely capture implicit fall-throughs without extra |
| 58 | /// basic blocks. |
| 59 | /// |
Ted Kremenek | 6fa9b88 | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 60 | class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> { |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 61 | CFG* cfg; |
| 62 | CFGBlock* Block; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 63 | CFGBlock* Succ; |
Ted Kremenek | bf15b27 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 64 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | 8a29471 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 65 | CFGBlock* BreakTargetBlock; |
Ted Kremenek | b5c13b0 | 2007-08-23 18:43:24 +0000 | [diff] [blame] | 66 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 67 | CFGBlock* DefaultCaseBlock; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 69 | // LabelMap records the mapping from Label expressions to their blocks. |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 70 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 71 | LabelMapTy LabelMap; |
| 72 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 73 | // A list of blocks that end with a "goto" that must be backpatched to |
| 74 | // their resolved targets upon completion of CFG construction. |
Ted Kremenek | 4a2b8a1 | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 75 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | 0cebe3e | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 76 | BackpatchBlocksTy BackpatchBlocks; |
| 77 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 78 | // A list of labels whose address has been taken (for indirect gotos). |
| 79 | typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy; |
| 80 | LabelSetTy AddressTakenLabels; |
| 81 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 82 | public: |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 83 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL), |
Ted Kremenek | 8a29471 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 84 | ContinueTargetBlock(NULL), BreakTargetBlock(NULL), |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 85 | SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) { |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 86 | // Create an empty CFG. |
| 87 | cfg = new CFG(); |
| 88 | } |
| 89 | |
| 90 | ~CFGBuilder() { delete cfg; } |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 92 | // buildCFG - Used by external clients to construct the CFG. |
| 93 | CFG* buildCFG(Stmt* Statement); |
Ted Kremenek | c310e93 | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 95 | // Visitors to walk an AST and construct the CFG. Called by |
| 96 | // buildCFG. Do not call directly! |
Ted Kremenek | e8ee26b | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 98 | CFGBlock* VisitStmt(Stmt* Statement); |
| 99 | CFGBlock* VisitNullStmt(NullStmt* Statement); |
| 100 | CFGBlock* VisitCompoundStmt(CompoundStmt* C); |
| 101 | CFGBlock* VisitIfStmt(IfStmt* I); |
| 102 | CFGBlock* VisitReturnStmt(ReturnStmt* R); |
| 103 | CFGBlock* VisitLabelStmt(LabelStmt* L); |
| 104 | CFGBlock* VisitGotoStmt(GotoStmt* G); |
| 105 | CFGBlock* VisitForStmt(ForStmt* F); |
| 106 | CFGBlock* VisitWhileStmt(WhileStmt* W); |
| 107 | CFGBlock* VisitDoStmt(DoStmt* D); |
| 108 | CFGBlock* VisitContinueStmt(ContinueStmt* C); |
| 109 | CFGBlock* VisitBreakStmt(BreakStmt* B); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 110 | CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator); |
| 111 | CFGBlock* VisitCaseStmt(CaseStmt* Terminator); |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 112 | CFGBlock* VisitDefaultStmt(DefaultStmt* D); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 113 | CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 115 | // FIXME: Add support for ObjC-specific control-flow structures. |
| 116 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 117 | // NYS == Not Yet Supported |
| 118 | CFGBlock* NYS() { |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 119 | badCFG = true; |
| 120 | return Block; |
| 121 | } |
| 122 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 123 | CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S){ return NYS();} |
| 124 | CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S) { return NYS(); } |
| 125 | CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { return NYS(); } |
| 126 | CFGBlock* VisitObjCAtFinallyStmt(ObjCAtFinallyStmt* S) { return NYS(); } |
| 127 | CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { return NYS(); } |
| 128 | |
| 129 | CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){ |
| 130 | return NYS(); |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 133 | private: |
| 134 | CFGBlock* createBlock(bool add_successor = true); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 135 | CFGBlock* addStmt(Stmt* Terminator); |
| 136 | CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt); |
| 137 | CFGBlock* WalkAST_VisitChildren(Stmt* Terminator); |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 138 | CFGBlock* WalkAST_VisitDeclSubExprs(StmtIterator& I); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 139 | CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 140 | void FinishBlock(CFGBlock* B); |
Ted Kremenek | e8ee26b | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 142 | bool badCFG; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 143 | }; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 144 | |
| 145 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
| 146 | /// represent an arbitrary statement. Examples include a single expression |
| 147 | /// or a function body (compound statement). The ownership of the returned |
| 148 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 149 | /// returns NULL. |
| 150 | CFG* CFGBuilder::buildCFG(Stmt* Statement) { |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 151 | assert (cfg); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 152 | if (!Statement) return NULL; |
| 153 | |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 154 | badCFG = false; |
| 155 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 156 | // Create an empty block that will serve as the exit block for the CFG. |
| 157 | // Since this is the first block added to the CFG, it will be implicitly |
| 158 | // registered as the exit block. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 159 | Succ = createBlock(); |
| 160 | assert (Succ == &cfg->getExit()); |
| 161 | Block = NULL; // the EXIT block is empty. Create all other blocks lazily. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 162 | |
| 163 | // Visit the statements and create the CFG. |
Ted Kremenek | 0d99ecf | 2008-02-27 17:33:02 +0000 | [diff] [blame] | 164 | CFGBlock* B = Visit(Statement); |
| 165 | if (!B) B = Succ; |
| 166 | |
| 167 | if (B) { |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 168 | // Finalize the last constructed block. This usually involves |
| 169 | // reversing the order of the statements in the block. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 170 | if (Block) FinishBlock(B); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 171 | |
| 172 | // Backpatch the gotos whose label -> block mappings we didn't know |
| 173 | // when we encountered them. |
| 174 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 175 | E = BackpatchBlocks.end(); I != E; ++I ) { |
| 176 | |
| 177 | CFGBlock* B = *I; |
| 178 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 179 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 180 | |
| 181 | // If there is no target for the goto, then we are looking at an |
| 182 | // incomplete AST. Handle this by not registering a successor. |
| 183 | if (LI == LabelMap.end()) continue; |
| 184 | |
| 185 | B->addSuccessor(LI->second); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 186 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 188 | // Add successors to the Indirect Goto Dispatch block (if we have one). |
| 189 | if (CFGBlock* B = cfg->getIndirectGotoBlock()) |
| 190 | for (LabelSetTy::iterator I = AddressTakenLabels.begin(), |
| 191 | E = AddressTakenLabels.end(); I != E; ++I ) { |
| 192 | |
| 193 | // Lookup the target block. |
| 194 | LabelMapTy::iterator LI = LabelMap.find(*I); |
| 195 | |
| 196 | // If there is no target block that contains label, then we are looking |
| 197 | // at an incomplete AST. Handle this by not registering a successor. |
| 198 | if (LI == LabelMap.end()) continue; |
| 199 | |
| 200 | B->addSuccessor(LI->second); |
| 201 | } |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 94b3316 | 2007-09-17 16:18:02 +0000 | [diff] [blame] | 203 | Succ = B; |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Create an empty entry block that has no predecessors. |
| 207 | cfg->setEntry(createBlock()); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 4102af9 | 2008-03-13 03:04:22 +0000 | [diff] [blame] | 209 | if (badCFG) { |
| 210 | delete cfg; |
| 211 | cfg = NULL; |
| 212 | return NULL; |
| 213 | } |
| 214 | |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 215 | // NULL out cfg so that repeated calls to the builder will fail and that |
| 216 | // the ownership of the constructed CFG is passed to the caller. |
| 217 | CFG* t = cfg; |
| 218 | cfg = NULL; |
| 219 | return t; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /// createBlock - Used to lazily create blocks that are connected |
| 223 | /// to the current (global) succcessor. |
| 224 | CFGBlock* CFGBuilder::createBlock(bool add_successor) { |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 225 | CFGBlock* B = cfg->createBlock(); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 226 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 227 | return B; |
| 228 | } |
| 229 | |
| 230 | /// FinishBlock - When the last statement has been added to the block, |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 231 | /// we must reverse the statements because they have been inserted |
| 232 | /// in reverse order. |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 233 | void CFGBuilder::FinishBlock(CFGBlock* B) { |
| 234 | assert (B); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 235 | B->reverseStmts(); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 238 | /// addStmt - Used to add statements/expressions to the current CFGBlock |
| 239 | /// "Block". This method calls WalkAST on the passed statement to see if it |
| 240 | /// contains any short-circuit expressions. If so, it recursively creates |
| 241 | /// the necessary blocks for such expressions. It returns the "topmost" block |
| 242 | /// of the created blocks, or the original value of "Block" when this method |
| 243 | /// was called if no additional blocks are created. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 244 | CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) { |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 245 | if (!Block) Block = createBlock(); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 246 | return WalkAST(Terminator,true); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /// WalkAST - Used by addStmt to walk the subtree of a statement and |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 250 | /// add extra blocks for ternary operators, &&, and ||. We also |
| 251 | /// process "," and DeclStmts (which may contain nested control-flow). |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 252 | CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) { |
| 253 | switch (Terminator->getStmtClass()) { |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 254 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 255 | ConditionalOperator* C = cast<ConditionalOperator>(Terminator); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 256 | |
| 257 | // Create the confluence block that will "merge" the results |
| 258 | // of the ternary expression. |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 259 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 260 | ConfluenceBlock->appendStmt(C); |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame] | 261 | FinishBlock(ConfluenceBlock); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 262 | |
| 263 | // Create a block for the LHS expression if there is an LHS expression. |
| 264 | // A GCC extension allows LHS to be NULL, causing the condition to |
| 265 | // be the value that is returned instead. |
| 266 | // e.g: x ?: y is shorthand for: x ? x : y; |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 267 | Succ = ConfluenceBlock; |
| 268 | Block = NULL; |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 269 | CFGBlock* LHSBlock = NULL; |
| 270 | if (C->getLHS()) { |
| 271 | LHSBlock = Visit(C->getLHS()); |
| 272 | FinishBlock(LHSBlock); |
| 273 | Block = NULL; |
| 274 | } |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 276 | // Create the block for the RHS expression. |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 277 | Succ = ConfluenceBlock; |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 278 | CFGBlock* RHSBlock = Visit(C->getRHS()); |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 279 | FinishBlock(RHSBlock); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 281 | // Create the block that will contain the condition. |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 282 | Block = createBlock(false); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 283 | |
| 284 | if (LHSBlock) |
| 285 | Block->addSuccessor(LHSBlock); |
| 286 | else { |
| 287 | // If we have no LHS expression, add the ConfluenceBlock as a direct |
| 288 | // successor for the block containing the condition. Moreover, |
| 289 | // we need to reverse the order of the predecessors in the |
| 290 | // ConfluenceBlock because the RHSBlock will have been added to |
| 291 | // the succcessors already, and we want the first predecessor to the |
| 292 | // the block containing the expression for the case when the ternary |
| 293 | // expression evaluates to true. |
| 294 | Block->addSuccessor(ConfluenceBlock); |
| 295 | assert (ConfluenceBlock->pred_size() == 2); |
| 296 | std::reverse(ConfluenceBlock->pred_begin(), |
| 297 | ConfluenceBlock->pred_end()); |
| 298 | } |
| 299 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 300 | Block->addSuccessor(RHSBlock); |
Ted Kremenek | ecc04c9 | 2007-11-26 18:20:26 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 302 | Block->setTerminator(C); |
| 303 | return addStmt(C->getCond()); |
| 304 | } |
Ted Kremenek | 49a436d | 2007-08-31 17:03:41 +0000 | [diff] [blame] | 305 | |
| 306 | case Stmt::ChooseExprClass: { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 307 | ChooseExpr* C = cast<ChooseExpr>(Terminator); |
Ted Kremenek | 49a436d | 2007-08-31 17:03:41 +0000 | [diff] [blame] | 308 | |
| 309 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 310 | ConfluenceBlock->appendStmt(C); |
| 311 | FinishBlock(ConfluenceBlock); |
| 312 | |
| 313 | Succ = ConfluenceBlock; |
| 314 | Block = NULL; |
| 315 | CFGBlock* LHSBlock = Visit(C->getLHS()); |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 316 | FinishBlock(LHSBlock); |
| 317 | |
Ted Kremenek | 49a436d | 2007-08-31 17:03:41 +0000 | [diff] [blame] | 318 | Succ = ConfluenceBlock; |
| 319 | Block = NULL; |
| 320 | CFGBlock* RHSBlock = Visit(C->getRHS()); |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 321 | FinishBlock(RHSBlock); |
Ted Kremenek | 49a436d | 2007-08-31 17:03:41 +0000 | [diff] [blame] | 322 | |
| 323 | Block = createBlock(false); |
| 324 | Block->addSuccessor(LHSBlock); |
| 325 | Block->addSuccessor(RHSBlock); |
| 326 | Block->setTerminator(C); |
| 327 | return addStmt(C->getCond()); |
| 328 | } |
Ted Kremenek | 7926f7c | 2007-08-28 16:18:58 +0000 | [diff] [blame] | 329 | |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 330 | case Stmt::DeclStmtClass: { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 331 | ScopedDecl* D = cast<DeclStmt>(Terminator)->getDecl(); |
| 332 | Block->appendStmt(Terminator); |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 333 | |
| 334 | StmtIterator I(D); |
| 335 | return WalkAST_VisitDeclSubExprs(I); |
| 336 | } |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 338 | case Stmt::AddrLabelExprClass: { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 339 | AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 340 | AddressTakenLabels.insert(A->getLabel()); |
| 341 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 342 | if (AlwaysAddStmt) Block->appendStmt(Terminator); |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 343 | return Block; |
| 344 | } |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 346 | case Stmt::StmtExprClass: |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 347 | return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator)); |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame] | 349 | case Stmt::UnaryOperatorClass: { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 350 | UnaryOperator* U = cast<UnaryOperator>(Terminator); |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame] | 351 | |
| 352 | // sizeof(expressions). For such expressions, |
| 353 | // the subexpression is not really evaluated, so |
| 354 | // we don't care about control-flow within the sizeof. |
| 355 | if (U->getOpcode() == UnaryOperator::SizeOf) { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 356 | Block->appendStmt(Terminator); |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame] | 357 | return Block; |
| 358 | } |
| 359 | |
| 360 | break; |
| 361 | } |
| 362 | |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 363 | case Stmt::BinaryOperatorClass: { |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 364 | BinaryOperator* B = cast<BinaryOperator>(Terminator); |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 365 | |
| 366 | if (B->isLogicalOp()) { // && or || |
| 367 | CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock(); |
| 368 | ConfluenceBlock->appendStmt(B); |
| 369 | FinishBlock(ConfluenceBlock); |
| 370 | |
| 371 | // create the block evaluating the LHS |
| 372 | CFGBlock* LHSBlock = createBlock(false); |
Ted Kremenek | afe5433 | 2007-12-21 19:49:00 +0000 | [diff] [blame] | 373 | LHSBlock->setTerminator(B); |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 374 | |
| 375 | // create the block evaluating the RHS |
| 376 | Succ = ConfluenceBlock; |
| 377 | Block = NULL; |
| 378 | CFGBlock* RHSBlock = Visit(B->getRHS()); |
Ted Kremenek | afe5433 | 2007-12-21 19:49:00 +0000 | [diff] [blame] | 379 | |
| 380 | // Now link the LHSBlock with RHSBlock. |
| 381 | if (B->getOpcode() == BinaryOperator::LOr) { |
| 382 | LHSBlock->addSuccessor(ConfluenceBlock); |
| 383 | LHSBlock->addSuccessor(RHSBlock); |
| 384 | } |
| 385 | else { |
| 386 | assert (B->getOpcode() == BinaryOperator::LAnd); |
| 387 | LHSBlock->addSuccessor(RHSBlock); |
| 388 | LHSBlock->addSuccessor(ConfluenceBlock); |
| 389 | } |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 390 | |
| 391 | // Generate the blocks for evaluating the LHS. |
| 392 | Block = LHSBlock; |
| 393 | return addStmt(B->getLHS()); |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 394 | } |
| 395 | else if (B->getOpcode() == BinaryOperator::Comma) { // , |
| 396 | Block->appendStmt(B); |
| 397 | addStmt(B->getRHS()); |
| 398 | return addStmt(B->getLHS()); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 399 | } |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame] | 400 | |
| 401 | break; |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 402 | } |
Ted Kremenek | f4e15fc | 2008-02-26 02:37:08 +0000 | [diff] [blame] | 403 | |
| 404 | case Stmt::ParenExprClass: |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 405 | return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt); |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 406 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 407 | default: |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame] | 408 | break; |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 409 | }; |
Ted Kremenek | a651e0e | 2007-12-13 22:44:18 +0000 | [diff] [blame] | 410 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 411 | if (AlwaysAddStmt) Block->appendStmt(Terminator); |
| 412 | return WalkAST_VisitChildren(Terminator); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 415 | /// WalkAST_VisitDeclSubExprs - Utility method to handle Decls contained in |
| 416 | /// DeclStmts. Because the initialization code (and sometimes the |
| 417 | /// the type declarations) for DeclStmts can contain arbitrary expressions, |
| 418 | /// we must linearize declarations to handle arbitrary control-flow induced by |
| 419 | /// those expressions. |
| 420 | CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExprs(StmtIterator& I) { |
Ted Kremenek | d660322 | 2007-11-18 20:06:01 +0000 | [diff] [blame] | 421 | if (I == StmtIterator()) |
| 422 | return Block; |
| 423 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 424 | Stmt* Terminator = *I; |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 425 | ++I; |
Ted Kremenek | d660322 | 2007-11-18 20:06:01 +0000 | [diff] [blame] | 426 | WalkAST_VisitDeclSubExprs(I); |
Ted Kremenek | ae2a98c | 2008-02-29 22:32:24 +0000 | [diff] [blame] | 427 | |
| 428 | // Optimization: Don't create separate block-level statements for literals. |
Ted Kremenek | 8f54c1f | 2007-10-30 21:48:34 +0000 | [diff] [blame] | 429 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 430 | switch (Terminator->getStmtClass()) { |
Ted Kremenek | ae2a98c | 2008-02-29 22:32:24 +0000 | [diff] [blame] | 431 | case Stmt::IntegerLiteralClass: |
| 432 | case Stmt::CharacterLiteralClass: |
| 433 | case Stmt::StringLiteralClass: |
| 434 | break; |
| 435 | |
| 436 | // All other cases. |
| 437 | |
| 438 | default: |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 439 | Block = addStmt(Terminator); |
Ted Kremenek | ae2a98c | 2008-02-29 22:32:24 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Ted Kremenek | b49e1aa | 2007-08-28 18:14:37 +0000 | [diff] [blame] | 442 | return Block; |
| 443 | } |
| 444 | |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 445 | /// WalkAST_VisitChildren - Utility method to call WalkAST on the |
| 446 | /// children of a Stmt. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 447 | CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) { |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 448 | CFGBlock* B = Block; |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 449 | for (Stmt::child_iterator I = Terminator->child_begin(), E = Terminator->child_end() ; |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 450 | I != E; ++I) |
Ted Kremenek | 322f58d | 2007-09-26 21:23:31 +0000 | [diff] [blame] | 451 | if (*I) B = WalkAST(*I); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 452 | |
| 453 | return B; |
| 454 | } |
| 455 | |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 456 | /// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement |
| 457 | /// expressions (a GCC extension). |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 458 | CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) { |
| 459 | Block->appendStmt(Terminator); |
| 460 | return VisitCompoundStmt(Terminator->getSubStmt()); |
Ted Kremenek | 15c27a8 | 2007-08-28 18:30:10 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 463 | /// VisitStmt - Handle statements with no branching control flow. |
| 464 | CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) { |
| 465 | // We cannot assume that we are in the middle of a basic block, since |
| 466 | // the CFG might only be constructed for this single statement. If |
| 467 | // we have no current basic block, just create one lazily. |
| 468 | if (!Block) Block = createBlock(); |
| 469 | |
| 470 | // Simply add the statement to the current block. We actually |
| 471 | // insert statements in reverse order; this order is reversed later |
| 472 | // when processing the containing element in the AST. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 473 | addStmt(Statement); |
| 474 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 475 | return Block; |
| 476 | } |
| 477 | |
| 478 | CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) { |
| 479 | return Block; |
| 480 | } |
| 481 | |
| 482 | CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) { |
Ted Kremenek | a716d7a | 2008-03-17 17:19:44 +0000 | [diff] [blame] | 483 | |
| 484 | CFGBlock* LastBlock = NULL; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | d34066c | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 486 | for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend(); |
| 487 | I != E; ++I ) { |
Ted Kremenek | a716d7a | 2008-03-17 17:19:44 +0000 | [diff] [blame] | 488 | LastBlock = Visit(*I); |
Ted Kremenek | d34066c | 2008-02-26 00:22:58 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Ted Kremenek | a716d7a | 2008-03-17 17:19:44 +0000 | [diff] [blame] | 491 | return LastBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) { |
| 495 | // We may see an if statement in the middle of a basic block, or |
| 496 | // it may be the first statement we are processing. In either case, |
| 497 | // we create a new basic block. First, we create the blocks for |
| 498 | // the then...else statements, and then we create the block containing |
| 499 | // the if statement. If we were in the middle of a block, we |
| 500 | // stop processing that block and reverse its statements. That block |
| 501 | // is then the implicit successor for the "then" and "else" clauses. |
| 502 | |
| 503 | // The block we were proccessing is now finished. Make it the |
| 504 | // successor block. |
| 505 | if (Block) { |
| 506 | Succ = Block; |
| 507 | FinishBlock(Block); |
| 508 | } |
| 509 | |
| 510 | // Process the false branch. NULL out Block so that the recursive |
| 511 | // call to Visit will create a new basic block. |
| 512 | // Null out Block so that all successor |
| 513 | CFGBlock* ElseBlock = Succ; |
| 514 | |
| 515 | if (Stmt* Else = I->getElse()) { |
| 516 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 517 | |
| 518 | // NULL out Block so that the recursive call to Visit will |
| 519 | // create a new basic block. |
| 520 | Block = NULL; |
Ted Kremenek | b6f7b72 | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 521 | ElseBlock = Visit(Else); |
| 522 | |
| 523 | if (!ElseBlock) // Can occur when the Else body has all NullStmts. |
| 524 | ElseBlock = sv.get(); |
| 525 | else if (Block) |
| 526 | FinishBlock(ElseBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Process the true branch. NULL out Block so that the recursive |
| 530 | // call to Visit will create a new basic block. |
| 531 | // Null out Block so that all successor |
| 532 | CFGBlock* ThenBlock; |
| 533 | { |
| 534 | Stmt* Then = I->getThen(); |
| 535 | assert (Then); |
| 536 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 537 | Block = NULL; |
Ted Kremenek | b6f7b72 | 2007-08-30 18:13:31 +0000 | [diff] [blame] | 538 | ThenBlock = Visit(Then); |
| 539 | |
| 540 | if (!ThenBlock) // Can occur when the Then body has all NullStmts. |
| 541 | ThenBlock = sv.get(); |
| 542 | else if (Block) |
| 543 | FinishBlock(ThenBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // Now create a new block containing the if statement. |
| 547 | Block = createBlock(false); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 548 | |
| 549 | // Set the terminator of the new block to the If statement. |
| 550 | Block->setTerminator(I); |
| 551 | |
| 552 | // Now add the successors. |
| 553 | Block->addSuccessor(ThenBlock); |
| 554 | Block->addSuccessor(ElseBlock); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 555 | |
| 556 | // Add the condition as the last statement in the new block. This |
| 557 | // may create new blocks as the condition may contain control-flow. Any |
| 558 | // newly created blocks will be pointed to be "Block". |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 559 | return addStmt(I->getCond()->IgnoreParens()); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 560 | } |
Ted Kremenek | f50ec10 | 2007-09-11 21:29:43 +0000 | [diff] [blame] | 561 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 562 | |
| 563 | CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) { |
| 564 | // If we were in the middle of a block we stop processing that block |
| 565 | // and reverse its statements. |
| 566 | // |
| 567 | // NOTE: If a "return" appears in the middle of a block, this means |
| 568 | // that the code afterwards is DEAD (unreachable). We still |
| 569 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 570 | // from the entry block will be able to report such dead |
| 571 | // blocks. |
| 572 | if (Block) FinishBlock(Block); |
| 573 | |
| 574 | // Create the new block. |
| 575 | Block = createBlock(false); |
| 576 | |
| 577 | // The Exit block is the only successor. |
| 578 | Block->addSuccessor(&cfg->getExit()); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 579 | |
| 580 | // Add the return statement to the block. This may create new blocks |
| 581 | // if R contains control-flow (short-circuit operations). |
| 582 | return addStmt(R); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) { |
| 586 | // Get the block of the labeled statement. Add it to our map. |
Ted Kremenek | 2677ea8 | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 587 | Visit(L->getSubStmt()); |
| 588 | CFGBlock* LabelBlock = Block; |
Ted Kremenek | 16e4dc8 | 2007-08-30 18:20:57 +0000 | [diff] [blame] | 589 | |
| 590 | if (!LabelBlock) // This can happen when the body is empty, i.e. |
| 591 | LabelBlock=createBlock(); // scopes that only contains NullStmts. |
| 592 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 593 | assert (LabelMap.find(L) == LabelMap.end() && "label already in map"); |
| 594 | LabelMap[ L ] = LabelBlock; |
| 595 | |
| 596 | // Labels partition blocks, so this is the end of the basic block |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 597 | // we were processing (L is the block's label). Because this is |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 598 | // label (and we have already processed the substatement) there is no |
| 599 | // extra control-flow to worry about. |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 600 | LabelBlock->setLabel(L); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 601 | FinishBlock(LabelBlock); |
| 602 | |
| 603 | // We set Block to NULL to allow lazy creation of a new block |
| 604 | // (if necessary); |
| 605 | Block = NULL; |
| 606 | |
| 607 | // This block is now the implicit successor of other blocks. |
| 608 | Succ = LabelBlock; |
| 609 | |
| 610 | return LabelBlock; |
| 611 | } |
| 612 | |
| 613 | CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) { |
| 614 | // Goto is a control-flow statement. Thus we stop processing the |
| 615 | // current block and create a new one. |
| 616 | if (Block) FinishBlock(Block); |
| 617 | Block = createBlock(false); |
| 618 | Block->setTerminator(G); |
| 619 | |
| 620 | // If we already know the mapping to the label block add the |
| 621 | // successor now. |
| 622 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
| 623 | |
| 624 | if (I == LabelMap.end()) |
| 625 | // We will need to backpatch this block later. |
| 626 | BackpatchBlocks.push_back(Block); |
| 627 | else |
| 628 | Block->addSuccessor(I->second); |
| 629 | |
| 630 | return Block; |
| 631 | } |
| 632 | |
| 633 | CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) { |
| 634 | // "for" is a control-flow statement. Thus we stop processing the |
| 635 | // current block. |
| 636 | |
| 637 | CFGBlock* LoopSuccessor = NULL; |
| 638 | |
| 639 | if (Block) { |
| 640 | FinishBlock(Block); |
| 641 | LoopSuccessor = Block; |
| 642 | } |
| 643 | else LoopSuccessor = Succ; |
| 644 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 645 | // Because of short-circuit evaluation, the condition of the loop |
| 646 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 647 | // blocks that evaluate the condition. |
| 648 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 649 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 650 | |
| 651 | // Set the terminator for the "exit" condition block. |
| 652 | ExitConditionBlock->setTerminator(F); |
| 653 | |
| 654 | // Now add the actual condition to the condition block. Because the |
| 655 | // condition itself may contain control-flow, new blocks may be created. |
| 656 | if (Stmt* C = F->getCond()) { |
| 657 | Block = ExitConditionBlock; |
| 658 | EntryConditionBlock = addStmt(C); |
| 659 | if (Block) FinishBlock(EntryConditionBlock); |
| 660 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 661 | |
| 662 | // The condition block is the implicit successor for the loop body as |
| 663 | // well as any code above the loop. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 664 | Succ = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 665 | |
| 666 | // Now create the loop body. |
| 667 | { |
| 668 | assert (F->getBody()); |
| 669 | |
| 670 | // Save the current values for Block, Succ, and continue and break targets |
| 671 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 672 | save_continue(ContinueTargetBlock), |
| 673 | save_break(BreakTargetBlock); |
| 674 | |
| 675 | // All continues within this loop should go to the condition block |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 676 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 677 | |
| 678 | // All breaks should go to the code following the loop. |
| 679 | BreakTargetBlock = LoopSuccessor; |
| 680 | |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 681 | // Create a new block to contain the (bottom) of the loop body. |
| 682 | Block = NULL; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 683 | |
| 684 | // If we have increment code, insert it at the end of the body block. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 685 | if (Stmt* I = F->getInc()) Block = addStmt(I); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 686 | |
| 687 | // Now populate the body block, and in the process create new blocks |
| 688 | // as we walk the body of the loop. |
| 689 | CFGBlock* BodyBlock = Visit(F->getBody()); |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 690 | |
| 691 | if (!BodyBlock) |
Ted Kremenek | a9d996d | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 692 | BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;" |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 693 | else if (Block) |
| 694 | FinishBlock(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 695 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 696 | // This new body block is a successor to our "exit" condition block. |
| 697 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | // Link up the condition block with the code that follows the loop. |
| 701 | // (the false branch). |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 702 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
| 703 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 704 | // If the loop contains initialization, create a new block for those |
| 705 | // statements. This block can also contain statements that precede |
| 706 | // the loop. |
| 707 | if (Stmt* I = F->getInit()) { |
| 708 | Block = createBlock(); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 709 | return addStmt(I); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 710 | } |
| 711 | else { |
| 712 | // There is no loop initialization. We are thus basically a while |
| 713 | // loop. NULL out Block to force lazy block construction. |
| 714 | Block = NULL; |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 715 | Succ = EntryConditionBlock; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 716 | return EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
| 720 | CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) { |
| 721 | // "while" is a control-flow statement. Thus we stop processing the |
| 722 | // current block. |
| 723 | |
| 724 | CFGBlock* LoopSuccessor = NULL; |
| 725 | |
| 726 | if (Block) { |
| 727 | FinishBlock(Block); |
| 728 | LoopSuccessor = Block; |
| 729 | } |
| 730 | else LoopSuccessor = Succ; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 731 | |
| 732 | // Because of short-circuit evaluation, the condition of the loop |
| 733 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 734 | // blocks that evaluate the condition. |
| 735 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 736 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 737 | |
| 738 | // Set the terminator for the "exit" condition block. |
| 739 | ExitConditionBlock->setTerminator(W); |
| 740 | |
| 741 | // Now add the actual condition to the condition block. Because the |
| 742 | // condition itself may contain control-flow, new blocks may be created. |
| 743 | // Thus we update "Succ" after adding the condition. |
| 744 | if (Stmt* C = W->getCond()) { |
| 745 | Block = ExitConditionBlock; |
| 746 | EntryConditionBlock = addStmt(C); |
Ted Kremenek | a9d996d | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 747 | assert (Block == EntryConditionBlock); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 748 | if (Block) FinishBlock(EntryConditionBlock); |
| 749 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 750 | |
| 751 | // The condition block is the implicit successor for the loop body as |
| 752 | // well as any code above the loop. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 753 | Succ = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 754 | |
| 755 | // Process the loop body. |
| 756 | { |
| 757 | assert (W->getBody()); |
| 758 | |
| 759 | // Save the current values for Block, Succ, and continue and break targets |
| 760 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 761 | save_continue(ContinueTargetBlock), |
| 762 | save_break(BreakTargetBlock); |
| 763 | |
| 764 | // All continues within this loop should go to the condition block |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 765 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 766 | |
| 767 | // All breaks should go to the code following the loop. |
| 768 | BreakTargetBlock = LoopSuccessor; |
| 769 | |
| 770 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 771 | Block = NULL; |
| 772 | |
| 773 | // Create the body. The returned block is the entry to the loop body. |
| 774 | CFGBlock* BodyBlock = Visit(W->getBody()); |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 775 | |
| 776 | if (!BodyBlock) |
Ted Kremenek | a9d996d | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 777 | BodyBlock = EntryConditionBlock; // can happen for "while(...) ;" |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 778 | else if (Block) |
| 779 | FinishBlock(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 780 | |
| 781 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 782 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | // Link up the condition block with the code that follows the loop. |
| 786 | // (the false branch). |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 787 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 788 | |
| 789 | // There can be no more statements in the condition block |
| 790 | // since we loop back to this block. NULL out Block to force |
| 791 | // lazy creation of another block. |
| 792 | Block = NULL; |
| 793 | |
| 794 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 795 | Succ = EntryConditionBlock; |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 796 | return EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) { |
| 800 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 801 | // current block. |
| 802 | |
| 803 | CFGBlock* LoopSuccessor = NULL; |
| 804 | |
| 805 | if (Block) { |
| 806 | FinishBlock(Block); |
| 807 | LoopSuccessor = Block; |
| 808 | } |
| 809 | else LoopSuccessor = Succ; |
| 810 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 811 | // Because of short-circuit evaluation, the condition of the loop |
| 812 | // can span multiple basic blocks. Thus we need the "Entry" and "Exit" |
| 813 | // blocks that evaluate the condition. |
| 814 | CFGBlock* ExitConditionBlock = createBlock(false); |
| 815 | CFGBlock* EntryConditionBlock = ExitConditionBlock; |
| 816 | |
| 817 | // Set the terminator for the "exit" condition block. |
| 818 | ExitConditionBlock->setTerminator(D); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 819 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 820 | // Now add the actual condition to the condition block. Because the |
| 821 | // condition itself may contain control-flow, new blocks may be created. |
| 822 | if (Stmt* C = D->getCond()) { |
| 823 | Block = ExitConditionBlock; |
| 824 | EntryConditionBlock = addStmt(C); |
| 825 | if (Block) FinishBlock(EntryConditionBlock); |
| 826 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 827 | |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 828 | // The condition block is the implicit successor for the loop body. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 829 | Succ = EntryConditionBlock; |
| 830 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 831 | // Process the loop body. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 832 | CFGBlock* BodyBlock = NULL; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 833 | { |
| 834 | assert (D->getBody()); |
| 835 | |
| 836 | // Save the current values for Block, Succ, and continue and break targets |
| 837 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 838 | save_continue(ContinueTargetBlock), |
| 839 | save_break(BreakTargetBlock); |
| 840 | |
| 841 | // All continues within this loop should go to the condition block |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 842 | ContinueTargetBlock = EntryConditionBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 843 | |
| 844 | // All breaks should go to the code following the loop. |
| 845 | BreakTargetBlock = LoopSuccessor; |
| 846 | |
| 847 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 848 | Block = NULL; |
| 849 | |
| 850 | // Create the body. The returned block is the entry to the loop body. |
| 851 | BodyBlock = Visit(D->getBody()); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 852 | |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 853 | if (!BodyBlock) |
Ted Kremenek | a9d996d | 2008-02-27 00:28:17 +0000 | [diff] [blame] | 854 | BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)" |
Ted Kremenek | af603f7 | 2007-08-30 18:39:40 +0000 | [diff] [blame] | 855 | else if (Block) |
| 856 | FinishBlock(BodyBlock); |
| 857 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 858 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 859 | ExitConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | // Link up the condition block with the code that follows the loop. |
| 863 | // (the false branch). |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 864 | ExitConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 865 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 866 | // There can be no more statements in the body block(s) |
| 867 | // since we loop back to the body. NULL out Block to force |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 868 | // lazy creation of another block. |
| 869 | Block = NULL; |
| 870 | |
| 871 | // Return the loop body, which is the dominating block for the loop. |
Ted Kremenek | 5482713 | 2008-02-27 07:20:00 +0000 | [diff] [blame] | 872 | Succ = BodyBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 873 | return BodyBlock; |
| 874 | } |
| 875 | |
| 876 | CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) { |
| 877 | // "continue" is a control-flow statement. Thus we stop processing the |
| 878 | // current block. |
| 879 | if (Block) FinishBlock(Block); |
| 880 | |
| 881 | // Now create a new block that ends with the continue statement. |
| 882 | Block = createBlock(false); |
| 883 | Block->setTerminator(C); |
| 884 | |
| 885 | // If there is no target for the continue, then we are looking at an |
| 886 | // incomplete AST. Handle this by not registering a successor. |
| 887 | if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock); |
| 888 | |
| 889 | return Block; |
| 890 | } |
| 891 | |
| 892 | CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) { |
| 893 | // "break" is a control-flow statement. Thus we stop processing the |
| 894 | // current block. |
| 895 | if (Block) FinishBlock(Block); |
| 896 | |
| 897 | // Now create a new block that ends with the continue statement. |
| 898 | Block = createBlock(false); |
| 899 | Block->setTerminator(B); |
| 900 | |
| 901 | // If there is no target for the break, then we are looking at an |
| 902 | // incomplete AST. Handle this by not registering a successor. |
| 903 | if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock); |
| 904 | |
| 905 | return Block; |
| 906 | } |
| 907 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 908 | CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) { |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 909 | // "switch" is a control-flow statement. Thus we stop processing the |
| 910 | // current block. |
| 911 | CFGBlock* SwitchSuccessor = NULL; |
| 912 | |
| 913 | if (Block) { |
| 914 | FinishBlock(Block); |
| 915 | SwitchSuccessor = Block; |
| 916 | } |
| 917 | else SwitchSuccessor = Succ; |
| 918 | |
| 919 | // Save the current "switch" context. |
| 920 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 921 | save_break(BreakTargetBlock), |
| 922 | save_default(DefaultCaseBlock); |
| 923 | |
| 924 | // Set the "default" case to be the block after the switch statement. |
| 925 | // If the switch statement contains a "default:", this value will |
| 926 | // be overwritten with the block for that code. |
| 927 | DefaultCaseBlock = SwitchSuccessor; |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 928 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 929 | // Create a new block that will contain the switch statement. |
| 930 | SwitchTerminatedBlock = createBlock(false); |
| 931 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 932 | // Now process the switch body. The code after the switch is the implicit |
| 933 | // successor. |
| 934 | Succ = SwitchSuccessor; |
| 935 | BreakTargetBlock = SwitchSuccessor; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 936 | |
| 937 | // When visiting the body, the case statements should automatically get |
| 938 | // linked up to the switch. We also don't keep a pointer to the body, |
| 939 | // since all control-flow from the switch goes to case/default statements. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 940 | assert (Terminator->getBody() && "switch must contain a non-NULL body"); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 941 | Block = NULL; |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 942 | CFGBlock *BodyBlock = Visit(Terminator->getBody()); |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 943 | if (Block) FinishBlock(BodyBlock); |
| 944 | |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 945 | // If we have no "default:" case, the default transition is to the |
| 946 | // code following the switch body. |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 947 | SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock); |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 948 | |
Ted Kremenek | 49af7cb | 2007-08-27 19:46:09 +0000 | [diff] [blame] | 949 | // Add the terminator and condition in the switch block. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 950 | SwitchTerminatedBlock->setTerminator(Terminator); |
| 951 | assert (Terminator->getCond() && "switch condition must be non-NULL"); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 952 | Block = SwitchTerminatedBlock; |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 953 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 954 | return addStmt(Terminator->getCond()); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 957 | CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) { |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 958 | // CaseStmts are essentially labels, so they are the |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 959 | // first statement in a block. |
Ted Kremenek | 29ccaa1 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 960 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 961 | if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt()); |
Ted Kremenek | 29ccaa1 | 2007-08-30 18:48:11 +0000 | [diff] [blame] | 962 | CFGBlock* CaseBlock = Block; |
| 963 | if (!CaseBlock) CaseBlock = createBlock(); |
| 964 | |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 965 | // Cases statements partition blocks, so this is the top of |
| 966 | // the basic block we were processing (the "case XXX:" is the label). |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 967 | CaseBlock->setLabel(Terminator); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 968 | FinishBlock(CaseBlock); |
| 969 | |
| 970 | // Add this block to the list of successors for the block with the |
| 971 | // switch statement. |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 972 | assert (SwitchTerminatedBlock); |
| 973 | SwitchTerminatedBlock->addSuccessor(CaseBlock); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 974 | |
| 975 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 976 | Block = NULL; |
| 977 | |
| 978 | // This block is now the implicit successor of other blocks. |
| 979 | Succ = CaseBlock; |
| 980 | |
Ted Kremenek | 2677ea8 | 2008-03-15 07:45:02 +0000 | [diff] [blame] | 981 | return CaseBlock; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 982 | } |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 984 | CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) { |
| 985 | if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt()); |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 986 | DefaultCaseBlock = Block; |
| 987 | if (!DefaultCaseBlock) DefaultCaseBlock = createBlock(); |
| 988 | |
| 989 | // Default statements partition blocks, so this is the top of |
| 990 | // the basic block we were processing (the "default:" is the label). |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 991 | DefaultCaseBlock->setLabel(Terminator); |
Ted Kremenek | eef5a9a | 2008-02-13 22:05:39 +0000 | [diff] [blame] | 992 | FinishBlock(DefaultCaseBlock); |
| 993 | |
| 994 | // Unlike case statements, we don't add the default block to the |
| 995 | // successors for the switch statement immediately. This is done |
| 996 | // when we finish processing the switch statement. This allows for |
| 997 | // the default case (including a fall-through to the code after the |
| 998 | // switch statement) to always be the last successor of a switch-terminated |
| 999 | // block. |
| 1000 | |
| 1001 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 1002 | Block = NULL; |
| 1003 | |
| 1004 | // This block is now the implicit successor of other blocks. |
| 1005 | Succ = DefaultCaseBlock; |
| 1006 | |
| 1007 | return DefaultCaseBlock; |
Ted Kremenek | 295222c | 2008-02-13 21:46:34 +0000 | [diff] [blame] | 1008 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1009 | |
Ted Kremenek | 19bb356 | 2007-08-28 19:26:49 +0000 | [diff] [blame] | 1010 | CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 1011 | // Lazily create the indirect-goto dispatch block if there isn't one |
| 1012 | // already. |
| 1013 | CFGBlock* IBlock = cfg->getIndirectGotoBlock(); |
| 1014 | |
| 1015 | if (!IBlock) { |
| 1016 | IBlock = createBlock(false); |
| 1017 | cfg->setIndirectGotoBlock(IBlock); |
| 1018 | } |
| 1019 | |
| 1020 | // IndirectGoto is a control-flow statement. Thus we stop processing the |
| 1021 | // current block and create a new one. |
| 1022 | if (Block) FinishBlock(Block); |
| 1023 | Block = createBlock(false); |
| 1024 | Block->setTerminator(I); |
| 1025 | Block->addSuccessor(IBlock); |
| 1026 | return addStmt(I->getTarget()); |
| 1027 | } |
| 1028 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1029 | |
Ted Kremenek | befef2f | 2007-08-23 21:26:19 +0000 | [diff] [blame] | 1030 | } // end anonymous namespace |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1031 | |
| 1032 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The |
| 1033 | /// block has no successors or predecessors. If this is the first block |
| 1034 | /// created in the CFG, it is automatically set to be the Entry and Exit |
| 1035 | /// of the CFG. |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 1036 | CFGBlock* CFG::createBlock() { |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1037 | bool first_block = begin() == end(); |
| 1038 | |
| 1039 | // Create the block. |
Ted Kremenek | 9438252 | 2007-09-05 20:02:05 +0000 | [diff] [blame] | 1040 | Blocks.push_front(CFGBlock(NumBlockIDs++)); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1041 | |
| 1042 | // If this is the first block, set it as the Entry and Exit. |
| 1043 | if (first_block) Entry = Exit = &front(); |
| 1044 | |
| 1045 | // Return the block. |
| 1046 | return &front(); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1049 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 1050 | /// CFG is returned to the caller. |
| 1051 | CFG* CFG::buildCFG(Stmt* Statement) { |
| 1052 | CFGBuilder Builder; |
| 1053 | return Builder.buildCFG(Statement); |
| 1054 | } |
| 1055 | |
| 1056 | /// reverseStmts - Reverses the orders of statements within a CFGBlock. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1057 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 1058 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1059 | //===----------------------------------------------------------------------===// |
| 1060 | // CFG: Queries for BlkExprs. |
| 1061 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1062 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1063 | namespace { |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1064 | typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy; |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1067 | static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) { |
| 1068 | if (!Terminator) |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1069 | return; |
| 1070 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1071 | for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1072 | if (!*I) continue; |
| 1073 | |
| 1074 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I)) |
| 1075 | if (B->isAssignmentOp()) Set.insert(B); |
| 1076 | |
| 1077 | FindSubExprAssignments(*I, Set); |
| 1078 | } |
| 1079 | } |
| 1080 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1081 | static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) { |
| 1082 | BlkExprMapTy* M = new BlkExprMapTy(); |
| 1083 | |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1084 | // Look for assignments that are used as subexpressions. These are the |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1085 | // only assignments that we want to *possibly* register as a block-level |
| 1086 | // expression. Basically, if an assignment occurs both in a subexpression |
| 1087 | // and at the block-level, it is a block-level expression. |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1088 | llvm::SmallPtrSet<Expr*,50> SubExprAssignments; |
| 1089 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1090 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) |
| 1091 | for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1092 | FindSubExprAssignments(*BI, SubExprAssignments); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1093 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1094 | for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) { |
| 1095 | |
| 1096 | // Iterate over the statements again on identify the Expr* and Stmt* at |
| 1097 | // the block-level that are block-level expressions. |
| 1098 | |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1099 | for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI) |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1100 | if (Expr* Exp = dyn_cast<Expr>(*BI)) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1101 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1102 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1103 | // Assignment expressions that are not nested within another |
| 1104 | // expression are really "statements" whose value is never |
| 1105 | // used by another expression. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1106 | if (B->isAssignmentOp() && !SubExprAssignments.count(Exp)) |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1107 | continue; |
| 1108 | } |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1109 | else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1110 | // Special handling for statement expressions. The last statement |
| 1111 | // in the statement expression is also a block-level expr. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1112 | const CompoundStmt* C = Terminator->getSubStmt(); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1113 | if (!C->body_empty()) { |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1114 | unsigned x = M->size(); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1115 | (*M)[C->body_back()] = x; |
| 1116 | } |
| 1117 | } |
Ted Kremenek | e2dcd78 | 2008-01-25 23:22:27 +0000 | [diff] [blame] | 1118 | |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1119 | unsigned x = M->size(); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1120 | (*M)[Exp] = x; |
Ted Kremenek | 33d4aab | 2008-01-26 00:03:27 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1123 | // Look at terminators. The condition is a block-level expression. |
| 1124 | |
| 1125 | Expr* Exp = I->getTerminatorCondition(); |
| 1126 | |
| 1127 | if (Exp && M->find(Exp) == M->end()) { |
| 1128 | unsigned x = M->size(); |
| 1129 | (*M)[Exp] = x; |
| 1130 | } |
| 1131 | } |
| 1132 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1133 | return M; |
| 1134 | } |
| 1135 | |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1136 | CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) { |
| 1137 | assert(S != NULL); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1138 | if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); } |
| 1139 | |
| 1140 | BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap); |
Ted Kremenek | 8694674 | 2008-01-17 20:48:37 +0000 | [diff] [blame] | 1141 | BlkExprMapTy::iterator I = M->find(S); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (I == M->end()) return CFG::BlkExprNumTy(); |
| 1144 | else return CFG::BlkExprNumTy(I->second); |
| 1145 | } |
| 1146 | |
| 1147 | unsigned CFG::getNumBlkExprs() { |
| 1148 | if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap)) |
| 1149 | return M->size(); |
| 1150 | else { |
| 1151 | // We assume callers interested in the number of BlkExprs will want |
| 1152 | // the map constructed if it doesn't already exist. |
| 1153 | BlkExprMap = (void*) PopulateBlkExprMap(*this); |
| 1154 | return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size(); |
| 1155 | } |
| 1156 | } |
| 1157 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1158 | //===----------------------------------------------------------------------===// |
| 1159 | // Internal Block-Edge Set; used for modeling persistent <CFGBlock*,CFGBlock*> |
| 1160 | // pairs for use with ProgramPoint. |
| 1161 | //===----------------------------------------------------------------------===// |
| 1162 | |
| 1163 | typedef std::pair<CFGBlock*,CFGBlock*> BPairTy; |
| 1164 | |
| 1165 | namespace llvm { |
| 1166 | template<> struct FoldingSetTrait<BPairTy*> { |
| 1167 | static void Profile(const BPairTy* X, FoldingSetNodeID& profile) { |
| 1168 | profile.AddPointer(X->first); |
| 1169 | profile.AddPointer(X->second); |
| 1170 | } |
| 1171 | }; |
| 1172 | } |
| 1173 | |
| 1174 | typedef llvm::FoldingSetNodeWrapper<BPairTy*> PersistPairTy; |
| 1175 | typedef llvm::FoldingSet<PersistPairTy> BlkEdgeSetTy; |
Ted Kremenek | 83c01da | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 1176 | |
| 1177 | const std::pair<CFGBlock*,CFGBlock*>* |
| 1178 | CFG::getBlockEdgeImpl(const CFGBlock* B1, const CFGBlock* B2) { |
| 1179 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1180 | llvm::BumpPtrAllocator*& Alloc = |
| 1181 | reinterpret_cast<llvm::BumpPtrAllocator*&>(Allocator); |
Ted Kremenek | 83c01da | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 1182 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1183 | if (!Alloc) |
| 1184 | Alloc = new llvm::BumpPtrAllocator(); |
| 1185 | |
| 1186 | BlkEdgeSetTy*& p = reinterpret_cast<BlkEdgeSetTy*&>(BlkEdgeSet); |
| 1187 | |
| 1188 | if (!p) |
| 1189 | p = new BlkEdgeSetTy(); |
| 1190 | |
| 1191 | // Profile the edges. |
| 1192 | llvm::FoldingSetNodeID profile; |
| 1193 | void* InsertPos; |
| 1194 | |
| 1195 | profile.AddPointer(B1); |
| 1196 | profile.AddPointer(B2); |
| 1197 | |
| 1198 | PersistPairTy* V = p->FindNodeOrInsertPos(profile, InsertPos); |
| 1199 | |
| 1200 | if (!V) { |
| 1201 | assert (llvm::AlignOf<BPairTy>::Alignment_LessEqual_8Bytes); |
| 1202 | |
| 1203 | // Allocate the pair, forcing an 8-byte alignment. |
| 1204 | BPairTy* pair = (BPairTy*) Alloc->Allocate(sizeof(*pair), 8); |
| 1205 | |
| 1206 | new (pair) BPairTy(const_cast<CFGBlock*>(B1), |
| 1207 | const_cast<CFGBlock*>(B2)); |
| 1208 | |
| 1209 | // Allocate the meta data to store the pair in the FoldingSet. |
| 1210 | PersistPairTy* ppair = (PersistPairTy*) Alloc->Allocate<PersistPairTy>(); |
| 1211 | new (ppair) PersistPairTy(pair); |
| 1212 | |
| 1213 | p->InsertNode(ppair, InsertPos); |
| 1214 | |
| 1215 | return pair; |
| 1216 | } |
| 1217 | |
| 1218 | return V->getValue(); |
Ted Kremenek | 83c01da | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1221 | //===----------------------------------------------------------------------===// |
| 1222 | // Cleanup: CFG dstor. |
| 1223 | //===----------------------------------------------------------------------===// |
| 1224 | |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1225 | CFG::~CFG() { |
| 1226 | delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap); |
Ted Kremenek | 83c01da | 2008-01-11 00:40:29 +0000 | [diff] [blame] | 1227 | delete reinterpret_cast<BlkEdgeSetTy*>(BlkEdgeSet); |
Ted Kremenek | 274f433 | 2008-04-28 18:00:46 +0000 | [diff] [blame] | 1228 | delete reinterpret_cast<llvm::BumpPtrAllocator*> (Allocator); |
Ted Kremenek | 63f5887 | 2007-10-01 19:33:33 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1231 | //===----------------------------------------------------------------------===// |
| 1232 | // CFG pretty printing |
| 1233 | //===----------------------------------------------------------------------===// |
| 1234 | |
Ted Kremenek | e8ee26b | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 1235 | namespace { |
| 1236 | |
Ted Kremenek | 6fa9b88 | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 1237 | class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper { |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1238 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1239 | typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy; |
| 1240 | StmtMapTy StmtMap; |
| 1241 | signed CurrentBlock; |
| 1242 | unsigned CurrentStmt; |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1243 | |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1244 | public: |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1245 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1246 | StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) { |
| 1247 | for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) { |
| 1248 | unsigned j = 1; |
| 1249 | for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ; |
| 1250 | BI != BEnd; ++BI, ++j ) |
| 1251 | StmtMap[*BI] = std::make_pair(I->getBlockID(),j); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | virtual ~StmtPrinterHelper() {} |
| 1256 | |
| 1257 | void setBlockID(signed i) { CurrentBlock = i; } |
| 1258 | void setStmtID(unsigned i) { CurrentStmt = i; } |
| 1259 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1260 | virtual bool handledStmt(Stmt* Terminator, std::ostream& OS) { |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1261 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1262 | StmtMapTy::iterator I = StmtMap.find(Terminator); |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1263 | |
| 1264 | if (I == StmtMap.end()) |
| 1265 | return false; |
| 1266 | |
| 1267 | if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock |
| 1268 | && I->second.second == CurrentStmt) |
| 1269 | return false; |
| 1270 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1271 | OS << "[B" << I->second.first << "." << I->second.second << "]"; |
| 1272 | return true; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1273 | } |
| 1274 | }; |
| 1275 | |
Ted Kremenek | 6fa9b88 | 2008-01-08 18:15:10 +0000 | [diff] [blame] | 1276 | class VISIBILITY_HIDDEN CFGBlockTerminatorPrint |
| 1277 | : public StmtVisitor<CFGBlockTerminatorPrint,void> { |
| 1278 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1279 | std::ostream& OS; |
| 1280 | StmtPrinterHelper* Helper; |
| 1281 | public: |
| 1282 | CFGBlockTerminatorPrint(std::ostream& os, StmtPrinterHelper* helper) |
| 1283 | : OS(os), Helper(helper) {} |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1284 | |
| 1285 | void VisitIfStmt(IfStmt* I) { |
| 1286 | OS << "if "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1287 | I->getCond()->printPretty(OS,Helper); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | // Default case. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1291 | void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1292 | |
| 1293 | void VisitForStmt(ForStmt* F) { |
| 1294 | OS << "for (" ; |
Ted Kremenek | 535bb20 | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1295 | if (F->getInit()) OS << "..."; |
| 1296 | OS << "; "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1297 | if (Stmt* C = F->getCond()) C->printPretty(OS,Helper); |
Ted Kremenek | 535bb20 | 2007-08-30 21:28:02 +0000 | [diff] [blame] | 1298 | OS << "; "; |
| 1299 | if (F->getInc()) OS << "..."; |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1300 | OS << ")"; |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | void VisitWhileStmt(WhileStmt* W) { |
| 1304 | OS << "while " ; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1305 | if (Stmt* C = W->getCond()) C->printPretty(OS,Helper); |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | void VisitDoStmt(DoStmt* D) { |
| 1309 | OS << "do ... while "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1310 | if (Stmt* C = D->getCond()) C->printPretty(OS,Helper); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1311 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1312 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1313 | void VisitSwitchStmt(SwitchStmt* Terminator) { |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1314 | OS << "switch "; |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1315 | Terminator->getCond()->printPretty(OS,Helper); |
Ted Kremenek | 9da2fb7 | 2007-08-27 21:27:44 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1318 | void VisitConditionalOperator(ConditionalOperator* C) { |
| 1319 | C->getCond()->printPretty(OS,Helper); |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1320 | OS << " ? ... : ..."; |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Ted Kremenek | aeddbf6 | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 1323 | void VisitChooseExpr(ChooseExpr* C) { |
| 1324 | OS << "__builtin_choose_expr( "; |
| 1325 | C->getCond()->printPretty(OS,Helper); |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1326 | OS << " )"; |
Ted Kremenek | aeddbf6 | 2007-08-31 22:29:13 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1329 | void VisitIndirectGotoStmt(IndirectGotoStmt* I) { |
| 1330 | OS << "goto *"; |
| 1331 | I->getTarget()->printPretty(OS,Helper); |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1334 | void VisitBinaryOperator(BinaryOperator* B) { |
| 1335 | if (!B->isLogicalOp()) { |
| 1336 | VisitExpr(B); |
| 1337 | return; |
| 1338 | } |
| 1339 | |
| 1340 | B->getLHS()->printPretty(OS,Helper); |
| 1341 | |
| 1342 | switch (B->getOpcode()) { |
| 1343 | case BinaryOperator::LOr: |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1344 | OS << " || ..."; |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | case BinaryOperator::LAnd: |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1347 | OS << " && ..."; |
Ted Kremenek | 805e9a8 | 2007-08-31 21:49:40 +0000 | [diff] [blame] | 1348 | return; |
| 1349 | default: |
| 1350 | assert(false && "Invalid logical operator."); |
| 1351 | } |
| 1352 | } |
| 1353 | |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 1354 | void VisitExpr(Expr* E) { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1355 | E->printPretty(OS,Helper); |
Ted Kremenek | 0b1d9b7 | 2007-08-27 21:54:41 +0000 | [diff] [blame] | 1356 | } |
Ted Kremenek | d4fdee3 | 2007-08-23 21:42:29 +0000 | [diff] [blame] | 1357 | }; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1358 | |
| 1359 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1360 | void print_stmt(std::ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) { |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1361 | if (Helper) { |
| 1362 | // special printing for statement-expressions. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1363 | if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) { |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1364 | CompoundStmt* Sub = SE->getSubStmt(); |
| 1365 | |
| 1366 | if (Sub->child_begin() != Sub->child_end()) { |
Ted Kremenek | 60266e8 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1367 | OS << "({ ... ; "; |
Ted Kremenek | 7a9d9d7 | 2007-10-29 20:41:04 +0000 | [diff] [blame] | 1368 | Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS); |
Ted Kremenek | 60266e8 | 2007-08-31 22:47:06 +0000 | [diff] [blame] | 1369 | OS << " })\n"; |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1370 | return; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | // special printing for comma expressions. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1375 | if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) { |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1376 | if (B->getOpcode() == BinaryOperator::Comma) { |
| 1377 | OS << "... , "; |
| 1378 | Helper->handledStmt(B->getRHS(),OS); |
| 1379 | OS << '\n'; |
| 1380 | return; |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1385 | Terminator->printPretty(OS, Helper); |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Expressions need a newline. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1388 | if (isa<Expr>(Terminator)) OS << '\n'; |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1391 | void print_block(std::ostream& OS, const CFG* cfg, const CFGBlock& B, |
| 1392 | StmtPrinterHelper* Helper, bool print_edges) { |
| 1393 | |
| 1394 | if (Helper) Helper->setBlockID(B.getBlockID()); |
| 1395 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1396 | // Print the header. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1397 | OS << "\n [ B" << B.getBlockID(); |
| 1398 | |
| 1399 | if (&B == &cfg->getEntry()) |
| 1400 | OS << " (ENTRY) ]\n"; |
| 1401 | else if (&B == &cfg->getExit()) |
| 1402 | OS << " (EXIT) ]\n"; |
| 1403 | else if (&B == cfg->getIndirectGotoBlock()) |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1404 | OS << " (INDIRECT GOTO DISPATCH) ]\n"; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1405 | else |
| 1406 | OS << " ]\n"; |
| 1407 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1408 | // Print the label of this block. |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1409 | if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) { |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1410 | |
| 1411 | if (print_edges) |
| 1412 | OS << " "; |
| 1413 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1414 | if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator)) |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1415 | OS << L->getName(); |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1416 | else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) { |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1417 | OS << "case "; |
| 1418 | C->getLHS()->printPretty(OS); |
| 1419 | if (C->getRHS()) { |
| 1420 | OS << " ... "; |
| 1421 | C->getRHS()->printPretty(OS); |
| 1422 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1423 | } |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1424 | else if (isa<DefaultStmt>(Terminator)) |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1425 | OS << "default"; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1426 | else |
| 1427 | assert(false && "Invalid label statement in CFGBlock."); |
| 1428 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1429 | OS << ":\n"; |
| 1430 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1431 | |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1432 | // Iterate through the statements in the block and print them. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1433 | unsigned j = 1; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1434 | |
| 1435 | for (CFGBlock::const_iterator I = B.begin(), E = B.end() ; |
| 1436 | I != E ; ++I, ++j ) { |
| 1437 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1438 | // Print the statement # in the basic block and the statement itself. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1439 | if (print_edges) |
| 1440 | OS << " "; |
| 1441 | |
| 1442 | OS << std::setw(3) << j << ": "; |
| 1443 | |
| 1444 | if (Helper) |
| 1445 | Helper->setStmtID(j); |
Ted Kremenek | 1c29bba | 2007-08-31 22:26:13 +0000 | [diff] [blame] | 1446 | |
| 1447 | print_stmt(OS,Helper,*I); |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1448 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1449 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1450 | // Print the terminator of this block. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1451 | if (B.getTerminator()) { |
| 1452 | if (print_edges) |
| 1453 | OS << " "; |
| 1454 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1455 | OS << " T: "; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1456 | |
| 1457 | if (Helper) Helper->setBlockID(-1); |
| 1458 | |
| 1459 | CFGBlockTerminatorPrint TPrinter(OS,Helper); |
| 1460 | TPrinter.Visit(const_cast<Stmt*>(B.getTerminator())); |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1461 | OS << '\n'; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1462 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1463 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1464 | if (print_edges) { |
| 1465 | // Print the predecessors of this block. |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1466 | OS << " Predecessors (" << B.pred_size() << "):"; |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1467 | unsigned i = 0; |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1468 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1469 | for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); |
| 1470 | I != E; ++I, ++i) { |
| 1471 | |
| 1472 | if (i == 8 || (i-8) == 0) |
| 1473 | OS << "\n "; |
| 1474 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1475 | OS << " B" << (*I)->getBlockID(); |
| 1476 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1477 | |
| 1478 | OS << '\n'; |
| 1479 | |
| 1480 | // Print the successors of this block. |
| 1481 | OS << " Successors (" << B.succ_size() << "):"; |
| 1482 | i = 0; |
| 1483 | |
| 1484 | for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); |
| 1485 | I != E; ++I, ++i) { |
| 1486 | |
| 1487 | if (i == 8 || (i-8) % 10 == 0) |
| 1488 | OS << "\n "; |
| 1489 | |
| 1490 | OS << " B" << (*I)->getBlockID(); |
| 1491 | } |
| 1492 | |
Ted Kremenek | 9cffe73 | 2007-08-29 23:20:49 +0000 | [diff] [blame] | 1493 | OS << '\n'; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 1494 | } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | } // end anonymous namespace |
| 1498 | |
| 1499 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Ted Kremenek | 7e3a89d | 2007-12-17 19:35:20 +0000 | [diff] [blame] | 1500 | void CFG::dump() const { print(*llvm::cerr.stream()); } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1501 | |
| 1502 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
| 1503 | void CFG::print(std::ostream& OS) const { |
| 1504 | |
| 1505 | StmtPrinterHelper Helper(this); |
| 1506 | |
| 1507 | // Print the entry block. |
| 1508 | print_block(OS, this, getEntry(), &Helper, true); |
| 1509 | |
| 1510 | // Iterate through the CFGBlocks and print them one by one. |
| 1511 | for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
| 1512 | // Skip the entry block, because we already printed it. |
| 1513 | if (&(*I) == &getEntry() || &(*I) == &getExit()) |
| 1514 | continue; |
| 1515 | |
| 1516 | print_block(OS, this, *I, &Helper, true); |
| 1517 | } |
| 1518 | |
| 1519 | // Print the exit block. |
| 1520 | print_block(OS, this, getExit(), &Helper, true); |
| 1521 | } |
| 1522 | |
| 1523 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Ted Kremenek | 7e3a89d | 2007-12-17 19:35:20 +0000 | [diff] [blame] | 1524 | void CFGBlock::dump(const CFG* cfg) const { print(*llvm::cerr.stream(), cfg); } |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1525 | |
| 1526 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 1527 | /// Generally this will only be called from CFG::print. |
| 1528 | void CFGBlock::print(std::ostream& OS, const CFG* cfg) const { |
| 1529 | StmtPrinterHelper Helper(cfg); |
| 1530 | print_block(OS, cfg, *this, &Helper, true); |
Ted Kremenek | 026473c | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 1531 | } |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1532 | |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1533 | /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. |
| 1534 | void CFGBlock::printTerminator(std::ostream& OS) const { |
| 1535 | CFGBlockTerminatorPrint TPrinter(OS,NULL); |
| 1536 | TPrinter.Visit(const_cast<Stmt*>(getTerminator())); |
| 1537 | } |
| 1538 | |
Ted Kremenek | 411cdee | 2008-04-16 21:10:48 +0000 | [diff] [blame] | 1539 | Expr* CFGBlock::getTerminatorCondition() { |
| 1540 | |
| 1541 | if (!Terminator) |
| 1542 | return NULL; |
| 1543 | |
| 1544 | Expr* E = NULL; |
| 1545 | |
| 1546 | switch (Terminator->getStmtClass()) { |
| 1547 | default: |
| 1548 | break; |
| 1549 | |
| 1550 | case Stmt::ForStmtClass: |
| 1551 | E = cast<ForStmt>(Terminator)->getCond(); |
| 1552 | break; |
| 1553 | |
| 1554 | case Stmt::WhileStmtClass: |
| 1555 | E = cast<WhileStmt>(Terminator)->getCond(); |
| 1556 | break; |
| 1557 | |
| 1558 | case Stmt::DoStmtClass: |
| 1559 | E = cast<DoStmt>(Terminator)->getCond(); |
| 1560 | break; |
| 1561 | |
| 1562 | case Stmt::IfStmtClass: |
| 1563 | E = cast<IfStmt>(Terminator)->getCond(); |
| 1564 | break; |
| 1565 | |
| 1566 | case Stmt::ChooseExprClass: |
| 1567 | E = cast<ChooseExpr>(Terminator)->getCond(); |
| 1568 | break; |
| 1569 | |
| 1570 | case Stmt::IndirectGotoStmtClass: |
| 1571 | E = cast<IndirectGotoStmt>(Terminator)->getTarget(); |
| 1572 | break; |
| 1573 | |
| 1574 | case Stmt::SwitchStmtClass: |
| 1575 | E = cast<SwitchStmt>(Terminator)->getCond(); |
| 1576 | break; |
| 1577 | |
| 1578 | case Stmt::ConditionalOperatorClass: |
| 1579 | E = cast<ConditionalOperator>(Terminator)->getCond(); |
| 1580 | break; |
| 1581 | |
| 1582 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 1583 | E = cast<BinaryOperator>(Terminator)->getLHS(); |
| 1584 | break; |
| 1585 | } |
| 1586 | |
| 1587 | return E ? E->IgnoreParens() : NULL; |
| 1588 | } |
| 1589 | |
Ted Kremenek | a292585 | 2008-01-30 23:02:42 +0000 | [diff] [blame] | 1590 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1591 | //===----------------------------------------------------------------------===// |
| 1592 | // CFG Graphviz Visualization |
| 1593 | //===----------------------------------------------------------------------===// |
| 1594 | |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1595 | |
| 1596 | #ifndef NDEBUG |
Chris Lattner | 0012351 | 2007-09-17 06:16:32 +0000 | [diff] [blame] | 1597 | static StmtPrinterHelper* GraphHelper; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1598 | #endif |
| 1599 | |
| 1600 | void CFG::viewCFG() const { |
| 1601 | #ifndef NDEBUG |
| 1602 | StmtPrinterHelper H(this); |
| 1603 | GraphHelper = &H; |
| 1604 | llvm::ViewGraph(this,"CFG"); |
| 1605 | GraphHelper = NULL; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1606 | #endif |
| 1607 | } |
| 1608 | |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1609 | namespace llvm { |
| 1610 | template<> |
| 1611 | struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { |
| 1612 | static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) { |
| 1613 | |
Hartmut Kaiser | bd250b4 | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 1614 | #ifndef NDEBUG |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1615 | std::ostringstream Out; |
Ted Kremenek | 42a509f | 2007-08-31 21:30:12 +0000 | [diff] [blame] | 1616 | print_block(Out,Graph, *Node, GraphHelper, false); |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1617 | std::string OutStr = Out.str(); |
| 1618 | |
| 1619 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 1620 | |
| 1621 | // Process string output to make it nicer... |
| 1622 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 1623 | if (OutStr[i] == '\n') { // Left justify |
| 1624 | OutStr[i] = '\\'; |
| 1625 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 1626 | } |
| 1627 | |
| 1628 | return OutStr; |
Hartmut Kaiser | bd250b4 | 2007-09-16 00:28:28 +0000 | [diff] [blame] | 1629 | #else |
| 1630 | return ""; |
| 1631 | #endif |
Ted Kremenek | 7dba860 | 2007-08-29 21:56:09 +0000 | [diff] [blame] | 1632 | } |
| 1633 | }; |
| 1634 | } // end namespace llvm |