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