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