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 | // |
| 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 | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 19 | #include <iostream> |
| 20 | #include <iomanip> |
| 21 | #include <algorithm> |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // SaveAndRestore - A utility class that uses RIIA to save and restore |
| 27 | // the value of a variable. |
| 28 | template<typename T> |
| 29 | struct SaveAndRestore { |
| 30 | SaveAndRestore(T& x) : X(x), old_value(x) {} |
| 31 | ~SaveAndRestore() { X = old_value; } |
| 32 | |
| 33 | T& X; |
| 34 | T old_value; |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | /// CFGBuilder - This class is implements CFG construction from an AST. |
| 39 | /// The builder is stateful: an instance of the builder should be used to only |
| 40 | /// construct a single CFG. |
| 41 | /// |
| 42 | /// Example usage: |
| 43 | /// |
| 44 | /// CFGBuilder builder; |
| 45 | /// CFG* cfg = builder.BuildAST(stmt1); |
| 46 | /// |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 47 | /// CFG construction is done via a recursive walk of an AST. |
| 48 | /// We actually parse the AST in reverse order so that the successor |
| 49 | /// of a basic block is constructed prior to its predecessor. This |
| 50 | /// allows us to nicely capture implicit fall-throughs without extra |
| 51 | /// basic blocks. |
| 52 | /// |
| 53 | class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 54 | CFG* cfg; |
| 55 | CFGBlock* Block; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 56 | CFGBlock* Succ; |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 57 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 58 | CFGBlock* BreakTargetBlock; |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame^] | 59 | CFGBlock* SwitchTerminatedBlock; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 60 | unsigned NumBlocks; |
| 61 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 62 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 63 | LabelMapTy LabelMap; |
| 64 | |
Ted Kremenek | f5392b7 | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 65 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 66 | BackpatchBlocksTy BackpatchBlocks; |
| 67 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 68 | public: |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 69 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL), |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 70 | ContinueTargetBlock(NULL), BreakTargetBlock(NULL), |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame^] | 71 | SwitchTerminatedBlock(NULL), |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 72 | NumBlocks(0) { |
| 73 | // Create an empty CFG. |
| 74 | cfg = new CFG(); |
| 75 | } |
| 76 | |
| 77 | ~CFGBuilder() { delete cfg; } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 79 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 80 | /// represent an arbitrary statement. Examples include a single expression |
| 81 | /// or a function body (compound statement). The ownership of the returned |
| 82 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 83 | /// returns NULL. |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 84 | CFG* buildCFG(Stmt* Statement) { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 85 | if (!Statement) return NULL; |
| 86 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 87 | // Create an empty block that will serve as the exit block for the CFG. |
| 88 | // Since this is the first block added to the CFG, it will be implicitly |
| 89 | // registered as the exit block. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 90 | Block = createBlock(); |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 91 | assert (Block == &cfg->getExit()); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 92 | |
| 93 | // Visit the statements and create the CFG. |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 94 | if (CFGBlock* B = Visit(Statement)) { |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 95 | // Finalize the last constructed block. This usually involves |
| 96 | // reversing the order of the statements in the block. |
| 97 | FinishBlock(B); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 98 | cfg->setEntry(B); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 99 | |
| 100 | // Backpatch the gotos whose label -> block mappings we didn't know |
| 101 | // when we encountered them. |
| 102 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 103 | E = BackpatchBlocks.end(); I != E; ++I ) { |
| 104 | |
| 105 | CFGBlock* B = *I; |
| 106 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 107 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 108 | |
Ted Kremenek | 7372bb2 | 2007-08-23 17:29:58 +0000 | [diff] [blame] | 109 | // If there is no target for the goto, then we are looking at an |
| 110 | // incomplete AST. Handle this by not registering a successor. |
| 111 | if (LI == LabelMap.end()) continue; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 112 | |
| 113 | B->addSuccessor(LI->second); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 114 | } |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 116 | // NULL out cfg so that repeated calls |
| 117 | CFG* t = cfg; |
| 118 | cfg = NULL; |
| 119 | return t; |
| 120 | } |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 121 | else return NULL; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 122 | } |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 124 | // createBlock - Used to lazily create blocks that are connected |
| 125 | // to the current (global) succcessor. |
| 126 | CFGBlock* createBlock( bool add_successor = true ) { |
| 127 | CFGBlock* B = cfg->createBlock(NumBlocks++); |
| 128 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 129 | return B; |
| 130 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 131 | |
| 132 | // FinishBlock - When the last statement has been added to the block, |
| 133 | // usually we must reverse the statements because they have been inserted |
| 134 | // in reverse order. When processing labels, however, there are cases |
| 135 | // in the recursion where we may have already reversed the statements |
| 136 | // in a block. This method safely tidies up a block: if the block |
| 137 | // has a label at the front, it has already been reversed. Otherwise, |
| 138 | // we reverse it. |
| 139 | void FinishBlock(CFGBlock* B) { |
| 140 | assert (B); |
| 141 | CFGBlock::iterator I = B->begin(); |
| 142 | if (I != B->end()) { |
| 143 | Stmt* S = *I; |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame^] | 144 | |
| 145 | if (isa<LabelStmt>(S) || isa<SwitchCase>(S)) |
| 146 | return; |
| 147 | |
| 148 | B->reverseStmts(); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 152 | /// Here we handle statements with no branching control flow. |
| 153 | CFGBlock* VisitStmt(Stmt* Statement) { |
| 154 | // We cannot assume that we are in the middle of a basic block, since |
| 155 | // the CFG might only be constructed for this single statement. If |
| 156 | // we have no current basic block, just create one lazily. |
| 157 | if (!Block) Block = createBlock(); |
| 158 | |
| 159 | // Simply add the statement to the current block. We actually |
| 160 | // insert statements in reverse order; this order is reversed later |
| 161 | // when processing the containing element in the AST. |
| 162 | Block->appendStmt(Statement); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 163 | |
| 164 | return Block; |
| 165 | } |
| 166 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 167 | CFGBlock* VisitNullStmt(NullStmt* Statement) { |
| 168 | return Block; |
| 169 | } |
| 170 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 171 | CFGBlock* VisitCompoundStmt(CompoundStmt* C) { |
| 172 | // The value returned from this function is the last created CFGBlock |
| 173 | // that represents the "entry" point for the translated AST node. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 174 | CFGBlock* LastBlock; |
| 175 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 176 | for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(), |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 177 | E = C->body_rend(); I != E; ++I ) |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 178 | // Add the statement to the current block. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 179 | if (!(LastBlock=Visit(*I))) |
| 180 | return NULL; |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 182 | return LastBlock; |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | CFGBlock* VisitIfStmt(IfStmt* I) { |
| 186 | |
| 187 | // We may see an if statement in the middle of a basic block, or |
| 188 | // it may be the first statement we are processing. In either case, |
| 189 | // we create a new basic block. First, we create the blocks for |
| 190 | // the then...else statements, and then we create the block containing |
| 191 | // the if statement. If we were in the middle of a block, we |
| 192 | // stop processing that block and reverse its statements. That block |
| 193 | // is then the implicit successor for the "then" and "else" clauses. |
| 194 | |
| 195 | // The block we were proccessing is now finished. Make it the |
| 196 | // successor block. |
| 197 | if (Block) { |
| 198 | Succ = Block; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 199 | FinishBlock(Block); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Process the false branch. NULL out Block so that the recursive |
| 203 | // call to Visit will create a new basic block. |
| 204 | // Null out Block so that all successor |
| 205 | CFGBlock* ElseBlock = Succ; |
| 206 | |
| 207 | if (Stmt* Else = I->getElse()) { |
| 208 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 209 | |
| 210 | // NULL out Block so that the recursive call to Visit will |
| 211 | // create a new basic block. |
| 212 | Block = NULL; |
| 213 | ElseBlock = Visit(Else); |
| 214 | if (!ElseBlock) return NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 215 | FinishBlock(ElseBlock); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Process the true branch. NULL out Block so that the recursive |
| 219 | // call to Visit will create a new basic block. |
| 220 | // Null out Block so that all successor |
| 221 | CFGBlock* ThenBlock; |
| 222 | { |
| 223 | Stmt* Then = I->getThen(); |
| 224 | assert (Then); |
| 225 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 226 | Block = NULL; |
| 227 | ThenBlock = Visit(Then); |
| 228 | if (!ThenBlock) return NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 229 | FinishBlock(ThenBlock); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // Now create a new block containing the if statement. |
| 233 | Block = createBlock(false); |
| 234 | |
| 235 | // Add the condition as the last statement in the new block. |
| 236 | Block->appendStmt(I->getCond()); |
| 237 | |
| 238 | // Set the terminator of the new block to the If statement. |
| 239 | Block->setTerminator(I); |
| 240 | |
| 241 | // Now add the successors. |
| 242 | Block->addSuccessor(ThenBlock); |
| 243 | Block->addSuccessor(ElseBlock); |
| 244 | |
| 245 | return Block; |
| 246 | } |
| 247 | |
| 248 | CFGBlock* VisitReturnStmt(ReturnStmt* R) { |
| 249 | // If we were in the middle of a block we stop processing that block |
| 250 | // and reverse its statements. |
| 251 | // |
| 252 | // NOTE: If a "return" appears in the middle of a block, this means |
| 253 | // that the code afterwards is DEAD (unreachable). We still |
| 254 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 255 | // from the entry block will be able to report such dead |
| 256 | // blocks. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 257 | if (Block) FinishBlock(Block); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 258 | |
| 259 | // Create the new block. |
| 260 | Block = createBlock(false); |
| 261 | |
| 262 | // The Exit block is the only successor. |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 263 | Block->addSuccessor(&cfg->getExit()); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 265 | // Add the return statement to the block. |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 266 | Block->appendStmt(R); |
| 267 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 268 | // Also add the return statement as the terminator. |
| 269 | Block->setTerminator(R); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 270 | |
| 271 | return Block; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | CFGBlock* VisitLabelStmt(LabelStmt* L) { |
| 275 | // Get the block of the labeled statement. Add it to our map. |
| 276 | CFGBlock* LabelBlock = Visit(L->getSubStmt()); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 277 | assert (LabelBlock); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 278 | |
| 279 | assert (LabelMap.find(L) == LabelMap.end() && "label already in map"); |
| 280 | LabelMap[ L ] = LabelBlock; |
| 281 | |
| 282 | // Labels partition blocks, so this is the end of the basic block |
| 283 | // we were processing (the label is the first statement). |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 284 | LabelBlock->appendStmt(L); |
| 285 | FinishBlock(LabelBlock); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 286 | |
| 287 | // We set Block to NULL to allow lazy creation of a new block |
| 288 | // (if necessary); |
| 289 | Block = NULL; |
| 290 | |
| 291 | // This block is now the implicit successor of other blocks. |
| 292 | Succ = LabelBlock; |
| 293 | |
| 294 | return LabelBlock; |
| 295 | } |
| 296 | |
| 297 | CFGBlock* VisitGotoStmt(GotoStmt* G) { |
| 298 | // Goto is a control-flow statement. Thus we stop processing the |
| 299 | // current block and create a new one. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 300 | if (Block) FinishBlock(Block); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 301 | Block = createBlock(false); |
| 302 | Block->setTerminator(G); |
| 303 | |
| 304 | // If we already know the mapping to the label block add the |
| 305 | // successor now. |
| 306 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
| 307 | |
| 308 | if (I == LabelMap.end()) |
| 309 | // We will need to backpatch this block later. |
| 310 | BackpatchBlocks.push_back(Block); |
| 311 | else |
| 312 | Block->addSuccessor(I->second); |
| 313 | |
| 314 | return Block; |
| 315 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 316 | |
| 317 | CFGBlock* VisitForStmt(ForStmt* F) { |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 318 | // "for" is a control-flow statement. Thus we stop processing the |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 319 | // current block. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 321 | CFGBlock* LoopSuccessor = NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 322 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 323 | if (Block) { |
| 324 | FinishBlock(Block); |
| 325 | LoopSuccessor = Block; |
| 326 | } |
| 327 | else LoopSuccessor = Succ; |
| 328 | |
| 329 | // Create the condition block. |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 330 | CFGBlock* ConditionBlock = createBlock(false); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 331 | ConditionBlock->setTerminator(F); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 332 | if (Stmt* C = F->getCond()) ConditionBlock->appendStmt(C); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 333 | |
| 334 | // The condition block is the implicit successor for the loop body as |
| 335 | // well as any code above the loop. |
| 336 | Succ = ConditionBlock; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 337 | |
| 338 | // Now create the loop body. |
| 339 | { |
| 340 | assert (F->getBody()); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 341 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 342 | // Save the current values for Block, Succ, and continue and break targets |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 343 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 344 | save_continue(ContinueTargetBlock), |
| 345 | save_break(BreakTargetBlock); |
| 346 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 347 | // All continues within this loop should go to the condition block |
| 348 | ContinueTargetBlock = ConditionBlock; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 350 | // All breaks should go to the code following the loop. |
| 351 | BreakTargetBlock = LoopSuccessor; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 353 | // Create a new block to contain the (bottom) of the loop body. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 354 | Block = createBlock(); |
| 355 | |
| 356 | // If we have increment code, insert it at the end of the body block. |
| 357 | if (Stmt* I = F->getInc()) Block->appendStmt(I); |
| 358 | |
| 359 | // Now populate the body block, and in the process create new blocks |
| 360 | // as we walk the body of the loop. |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 361 | CFGBlock* BodyBlock = Visit(F->getBody()); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 362 | assert (BodyBlock); |
| 363 | FinishBlock(BodyBlock); |
| 364 | |
| 365 | // This new body block is a successor to our condition block. |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 366 | ConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 367 | } |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 369 | // Link up the condition block with the code that follows the loop. |
| 370 | // (the false branch). |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 371 | ConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 373 | // If the loop contains initialization, create a new block for those |
| 374 | // statements. This block can also contain statements that precede |
| 375 | // the loop. |
| 376 | if (Stmt* I = F->getInit()) { |
| 377 | Block = createBlock(); |
| 378 | Block->appendStmt(I); |
| 379 | return Block; |
| 380 | } |
| 381 | else { |
| 382 | // There is no loop initialization. We are thus basically a while |
| 383 | // loop. NULL out Block to force lazy block construction. |
| 384 | Block = NULL; |
| 385 | return ConditionBlock; |
| 386 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 387 | } |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 388 | |
| 389 | CFGBlock* VisitWhileStmt(WhileStmt* W) { |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 390 | // "while" is a control-flow statement. Thus we stop processing the |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 391 | // current block. |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 392 | |
| 393 | CFGBlock* LoopSuccessor = NULL; |
| 394 | |
| 395 | if (Block) { |
| 396 | FinishBlock(Block); |
| 397 | LoopSuccessor = Block; |
| 398 | } |
| 399 | else LoopSuccessor = Succ; |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 400 | |
| 401 | // Create the condition block. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 402 | CFGBlock* ConditionBlock = createBlock(false); |
| 403 | ConditionBlock->setTerminator(W); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 404 | if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 406 | // The condition block is the implicit successor for the loop body as |
| 407 | // well as any code above the loop. |
| 408 | Succ = ConditionBlock; |
| 409 | |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 410 | // Process the loop body. |
| 411 | { |
| 412 | assert (W->getBody()); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 413 | |
| 414 | // Save the current values for Block, Succ, and continue and break targets |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 415 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 416 | save_continue(ContinueTargetBlock), |
| 417 | save_break(BreakTargetBlock); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 419 | // All continues within this loop should go to the condition block |
| 420 | ContinueTargetBlock = ConditionBlock; |
| 421 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 422 | // All breaks should go to the code following the loop. |
| 423 | BreakTargetBlock = LoopSuccessor; |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 424 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 425 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 426 | Block = NULL; |
| 427 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 428 | // Create the body. The returned block is the entry to the loop body. |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 429 | CFGBlock* BodyBlock = Visit(W->getBody()); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 430 | assert (BodyBlock); |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 431 | FinishBlock(BodyBlock); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 432 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 433 | // Add the loop body entry as a successor to the condition. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 434 | ConditionBlock->addSuccessor(BodyBlock); |
| 435 | } |
| 436 | |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 437 | // Link up the condition block with the code that follows the loop. |
| 438 | // (the false branch). |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 439 | ConditionBlock->addSuccessor(LoopSuccessor); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 440 | |
| 441 | // There can be no more statements in the condition block |
| 442 | // since we loop back to this block. NULL out Block to force |
| 443 | // lazy creation of another block. |
| 444 | Block = NULL; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | 0baf7c0 | 2007-08-22 22:35:28 +0000 | [diff] [blame] | 446 | // Return the condition block, which is the dominating block for the loop. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 447 | return ConditionBlock; |
| 448 | } |
| 449 | |
Ted Kremenek | 95cc35c | 2007-08-23 17:15:32 +0000 | [diff] [blame] | 450 | CFGBlock* VisitDoStmt(DoStmt* D) { |
| 451 | // "do...while" is a control-flow statement. Thus we stop processing the |
| 452 | // current block. |
| 453 | |
| 454 | CFGBlock* LoopSuccessor = NULL; |
| 455 | |
| 456 | if (Block) { |
| 457 | FinishBlock(Block); |
| 458 | LoopSuccessor = Block; |
| 459 | } |
| 460 | else LoopSuccessor = Succ; |
| 461 | |
| 462 | // Create the condition block. |
| 463 | CFGBlock* ConditionBlock = createBlock(false); |
| 464 | ConditionBlock->setTerminator(D); |
| 465 | if (Stmt* C = D->getCond()) ConditionBlock->appendStmt(C); |
| 466 | |
| 467 | // The condition block is the implicit successor for the loop body. |
| 468 | Succ = ConditionBlock; |
| 469 | |
| 470 | CFGBlock* BodyBlock = NULL; |
| 471 | // Process the loop body. |
| 472 | { |
| 473 | assert (D->getBody()); |
| 474 | |
| 475 | // Save the current values for Block, Succ, and continue and break targets |
| 476 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 477 | save_continue(ContinueTargetBlock), |
| 478 | save_break(BreakTargetBlock); |
| 479 | |
| 480 | // All continues within this loop should go to the condition block |
| 481 | ContinueTargetBlock = ConditionBlock; |
| 482 | |
| 483 | // All breaks should go to the code following the loop. |
| 484 | BreakTargetBlock = LoopSuccessor; |
| 485 | |
| 486 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 487 | Block = NULL; |
| 488 | |
| 489 | // Create the body. The returned block is the entry to the loop body. |
| 490 | BodyBlock = Visit(D->getBody()); |
| 491 | assert (BodyBlock); |
| 492 | FinishBlock(BodyBlock); |
| 493 | |
| 494 | // Add the loop body entry as a successor to the condition. |
| 495 | ConditionBlock->addSuccessor(BodyBlock); |
| 496 | } |
| 497 | |
| 498 | // Link up the condition block with the code that follows the loop. |
| 499 | // (the false branch). |
| 500 | ConditionBlock->addSuccessor(LoopSuccessor); |
| 501 | |
| 502 | // There can be no more statements in the condition block |
| 503 | // since we loop back to this block. NULL out Block to force |
| 504 | // lazy creation of another block. |
| 505 | Block = NULL; |
| 506 | |
| 507 | // Return the loop body, which is the dominating block for the loop. |
| 508 | return BodyBlock; |
| 509 | } |
| 510 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 511 | CFGBlock* VisitContinueStmt(ContinueStmt* C) { |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 512 | // "continue" is a control-flow statement. Thus we stop processing the |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 513 | // current block. |
| 514 | if (Block) FinishBlock(Block); |
| 515 | |
| 516 | // Now create a new block that ends with the continue statement. |
| 517 | Block = createBlock(false); |
| 518 | Block->setTerminator(C); |
| 519 | |
Ted Kremenek | 7372bb2 | 2007-08-23 17:29:58 +0000 | [diff] [blame] | 520 | // If there is no target for the continue, then we are looking at an |
| 521 | // incomplete AST. Handle this by not registering a successor. |
| 522 | if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 523 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame] | 524 | return Block; |
| 525 | } |
| 526 | |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 527 | CFGBlock* VisitBreakStmt(BreakStmt* B) { |
| 528 | // "break" is a control-flow statement. Thus we stop processing the |
| 529 | // current block. |
| 530 | if (Block) FinishBlock(Block); |
| 531 | |
| 532 | // Now create a new block that ends with the continue statement. |
| 533 | Block = createBlock(false); |
| 534 | Block->setTerminator(B); |
| 535 | |
Ted Kremenek | 7372bb2 | 2007-08-23 17:29:58 +0000 | [diff] [blame] | 536 | // If there is no target for the break, then we are looking at an |
| 537 | // incomplete AST. Handle this by not registering a successor. |
| 538 | if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock); |
| 539 | |
Ted Kremenek | f308d37 | 2007-08-22 21:51:58 +0000 | [diff] [blame] | 540 | return Block; |
| 541 | } |
| 542 | |
Ted Kremenek | e809ebf | 2007-08-23 18:43:24 +0000 | [diff] [blame^] | 543 | CFGBlock* VisitSwitchStmt(SwitchStmt* S) { |
| 544 | // "switch" is a control-flow statement. Thus we stop processing the |
| 545 | // current block. |
| 546 | CFGBlock* SwitchSuccessor = NULL; |
| 547 | |
| 548 | if (Block) { |
| 549 | FinishBlock(Block); |
| 550 | SwitchSuccessor = Block; |
| 551 | } |
| 552 | else SwitchSuccessor = Succ; |
| 553 | |
| 554 | // Save the current "switch" context. |
| 555 | SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock), |
| 556 | save_break(BreakTargetBlock); |
| 557 | |
| 558 | // Create a new block that will contain the switch statement. |
| 559 | SwitchTerminatedBlock = createBlock(false); |
| 560 | |
| 561 | // Add the terminator and condition in the switch block. |
| 562 | assert (S->getCond() && "switch condition must be non-NULL"); |
| 563 | SwitchTerminatedBlock->appendStmt(S->getCond()); |
| 564 | SwitchTerminatedBlock->setTerminator(S); |
| 565 | |
| 566 | |
| 567 | // Now process the switch body. The code after the switch is the implicit |
| 568 | // successor. |
| 569 | Succ = SwitchSuccessor; |
| 570 | BreakTargetBlock = SwitchSuccessor; |
| 571 | |
| 572 | assert (S->getBody() && "switch must contain a non-NULL body"); |
| 573 | Block = NULL; |
| 574 | |
| 575 | // When visiting the body, the case statements should automatically get |
| 576 | // linked up to the switch. We also don't keep a pointer to the body, |
| 577 | // since all control-flow from the switch goes to case/default statements. |
| 578 | Visit(S->getBody()); |
| 579 | |
| 580 | Block = SwitchTerminatedBlock; |
| 581 | return SwitchTerminatedBlock; |
| 582 | } |
| 583 | |
| 584 | CFGBlock* VisitSwitchCase(SwitchCase* S) { |
| 585 | // A SwitchCase is either a "default" or "case" statement. We handle |
| 586 | // both in the same way. They are essentially labels, so they are the |
| 587 | // first statement in a block. |
| 588 | CFGBlock* CaseBlock = Visit(S->getSubStmt()); |
| 589 | assert (CaseBlock); |
| 590 | |
| 591 | // Cases/Default statements parition block, so this is the top of |
| 592 | // the basic block we were processing (the case/default is the first stmt). |
| 593 | CaseBlock->appendStmt(S); |
| 594 | FinishBlock(CaseBlock); |
| 595 | |
| 596 | // Add this block to the list of successors for the block with the |
| 597 | // switch statement. |
| 598 | if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock); |
| 599 | |
| 600 | // We set Block to NULL to allow lazy creation of a new block (if necessary) |
| 601 | Block = NULL; |
| 602 | |
| 603 | // This block is now the implicit successor of other blocks. |
| 604 | Succ = CaseBlock; |
| 605 | |
| 606 | return CaseBlock; |
| 607 | } |
| 608 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 609 | }; |
| 610 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 611 | |
| 612 | /// createBlock - Constructs and adds a new CFGBlock to the CFG. The |
| 613 | /// block has no successors or predecessors. If this is the first block |
| 614 | /// created in the CFG, it is automatically set to be the Entry and Exit |
| 615 | /// of the CFG. |
| 616 | CFGBlock* CFG::createBlock(unsigned blockID) { |
| 617 | bool first_block = begin() == end(); |
| 618 | |
| 619 | // Create the block. |
| 620 | Blocks.push_front(CFGBlock(blockID)); |
| 621 | |
| 622 | // If this is the first block, set it as the Entry and Exit. |
| 623 | if (first_block) Entry = Exit = &front(); |
| 624 | |
| 625 | // Return the block. |
| 626 | return &front(); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 629 | /// buildCFG - Constructs a CFG from an AST. Ownership of the returned |
| 630 | /// CFG is returned to the caller. |
| 631 | CFG* CFG::buildCFG(Stmt* Statement) { |
| 632 | CFGBuilder Builder; |
| 633 | return Builder.buildCFG(Statement); |
| 634 | } |
| 635 | |
| 636 | /// reverseStmts - Reverses the orders of statements within a CFGBlock. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 637 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 638 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 639 | /// dump - A simple pretty printer of a CFG that outputs to stderr. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 640 | void CFG::dump() { print(std::cerr); } |
| 641 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 642 | /// print - A simple pretty printer of a CFG that outputs to an ostream. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 643 | void CFG::print(std::ostream& OS) { |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 644 | |
| 645 | // Print the Entry block. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 646 | if (begin() != end()) { |
| 647 | CFGBlock& Entry = getEntry(); |
| 648 | OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n"; |
| 649 | Entry.print(OS); |
| 650 | } |
| 651 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 652 | // Iterate through the CFGBlocks and print them one by one. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 653 | for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 654 | // Skip the entry block, because we already printed it. |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 655 | if (&(*I) == &getEntry() || &(*I) == &getExit()) continue; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 656 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 657 | OS << "\n [ B" << I->getBlockID() << " ]\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 658 | I->print(OS); |
| 659 | } |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 660 | |
| 661 | // Print the Exit Block. |
| 662 | if (begin() != end()) { |
| 663 | CFGBlock& Exit = getExit(); |
| 664 | OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n"; |
| 665 | Exit.print(OS); |
| 666 | } |
| 667 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 668 | OS << "\n"; |
| 669 | } |
| 670 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 671 | |
| 672 | namespace { |
| 673 | |
| 674 | class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint, |
| 675 | void > { |
| 676 | std::ostream& OS; |
| 677 | public: |
| 678 | CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {} |
| 679 | |
| 680 | void VisitIfStmt(IfStmt* I) { |
| 681 | OS << "if "; |
| 682 | I->getCond()->printPretty(std::cerr); |
| 683 | OS << "\n"; |
| 684 | } |
| 685 | |
| 686 | // Default case. |
| 687 | void VisitStmt(Stmt* S) { S->printPretty(OS); } |
| 688 | |
| 689 | void VisitForStmt(ForStmt* F) { |
| 690 | OS << "for (" ; |
| 691 | if (Stmt* I = F->getInit()) I->printPretty(OS); |
| 692 | OS << " ; "; |
| 693 | if (Stmt* C = F->getCond()) C->printPretty(OS); |
| 694 | OS << " ; "; |
| 695 | if (Stmt* I = F->getInc()) I->printPretty(OS); |
| 696 | OS << ")\n"; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void VisitWhileStmt(WhileStmt* W) { |
| 700 | OS << "while " ; |
| 701 | if (Stmt* C = W->getCond()) C->printPretty(OS); |
| 702 | OS << "\n"; |
Ted Kremenek | 95cc35c | 2007-08-23 17:15:32 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void VisitDoStmt(DoStmt* D) { |
| 706 | OS << "do ... while "; |
| 707 | if (Stmt* C = D->getCond()) C->printPretty(OS); |
| 708 | OS << "\n"; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 709 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 710 | }; |
| 711 | } |
| 712 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 713 | /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 714 | void CFGBlock::dump() { print(std::cerr); } |
| 715 | |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 716 | /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 717 | /// Generally this will only be called from CFG::print. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 718 | void CFGBlock::print(std::ostream& OS) { |
| 719 | |
| 720 | // Iterate through the statements in the block and print them. |
| 721 | OS << " ------------------------\n"; |
| 722 | unsigned j = 1; |
| 723 | for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) { |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 724 | // Print the statement # in the basic block. |
| 725 | OS << " " << std::setw(3) << j << ": "; |
| 726 | |
| 727 | // Print the statement/expression. |
| 728 | Stmt* S = *I; |
| 729 | |
| 730 | if (LabelStmt* L = dyn_cast<LabelStmt>(S)) |
| 731 | OS << L->getName() << ": (LABEL)\n"; |
| 732 | else |
| 733 | (*I)->printPretty(OS); |
| 734 | |
| 735 | // Expressions need a newline. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 736 | if (isa<Expr>(*I)) OS << '\n'; |
| 737 | } |
| 738 | OS << " ------------------------\n"; |
| 739 | |
| 740 | // Print the predecessors of this block. |
| 741 | OS << " Predecessors (" << pred_size() << "):"; |
| 742 | unsigned i = 0; |
| 743 | for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) { |
| 744 | if (i == 8 || (i-8) == 0) { |
| 745 | OS << "\n "; |
| 746 | } |
| 747 | OS << " B" << (*I)->getBlockID(); |
| 748 | } |
| 749 | |
| 750 | // Print the terminator of this block. |
| 751 | OS << "\n Terminator: "; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 752 | if (ControlFlowStmt) |
| 753 | CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt); |
| 754 | else |
| 755 | OS << "<NULL>\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 756 | |
| 757 | // Print the successors of this block. |
| 758 | OS << " Successors (" << succ_size() << "):"; |
| 759 | i = 0; |
| 760 | for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) { |
| 761 | if (i == 8 || (i-8) % 10 == 0) { |
| 762 | OS << "\n "; |
| 763 | } |
| 764 | OS << " B" << (*I)->getBlockID(); |
| 765 | } |
| 766 | OS << '\n'; |
Ted Kremenek | 4db5b45 | 2007-08-23 16:51:22 +0000 | [diff] [blame] | 767 | } |