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