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; |
| 56 | CFGBlock* Exit; |
| 57 | CFGBlock* Succ; |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 58 | CFGBlock* ContinueTargetBlock; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 59 | unsigned NumBlocks; |
| 60 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 61 | typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy; |
| 62 | LabelMapTy LabelMap; |
| 63 | |
Ted Kremenek | f5392b7 | 2007-08-22 15:40:58 +0000 | [diff] [blame] | 64 | typedef std::vector<CFGBlock*> BackpatchBlocksTy; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 65 | BackpatchBlocksTy BackpatchBlocks; |
| 66 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 67 | public: |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 68 | explicit CFGBuilder() : cfg(NULL), Block(NULL), Exit(NULL), Succ(NULL), |
| 69 | ContinueTargetBlock(NULL), |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 70 | NumBlocks(0) { |
| 71 | // Create an empty CFG. |
| 72 | cfg = new CFG(); |
| 73 | } |
| 74 | |
| 75 | ~CFGBuilder() { delete cfg; } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 77 | /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 78 | /// represent an arbitrary statement. Examples include a single expression |
| 79 | /// or a function body (compound statement). The ownership of the returned |
| 80 | /// CFG is transferred to the caller. If CFG construction fails, this method |
| 81 | /// returns NULL. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 82 | CFG* BuildCFG(Stmt* Statement) { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 83 | if (!Statement) return NULL; |
| 84 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 85 | assert (!Exit && "CFGBuilder should only be used to construct one CFG"); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 86 | |
| 87 | // Create the exit block. |
| 88 | Block = createBlock(); |
| 89 | Exit = Block; |
| 90 | |
| 91 | // Visit the statements and create the CFG. |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 92 | if (CFGBlock* B = Visit(Statement)) { |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 93 | // Finalize the last constructed block. This usually involves |
| 94 | // reversing the order of the statements in the block. |
| 95 | FinishBlock(B); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 96 | cfg->setEntry(B); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 97 | |
| 98 | // Backpatch the gotos whose label -> block mappings we didn't know |
| 99 | // when we encountered them. |
| 100 | for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(), |
| 101 | E = BackpatchBlocks.end(); I != E; ++I ) { |
| 102 | |
| 103 | CFGBlock* B = *I; |
| 104 | GotoStmt* G = cast<GotoStmt>(B->getTerminator()); |
| 105 | LabelMapTy::iterator LI = LabelMap.find(G->getLabel()); |
| 106 | |
| 107 | if (LI == LabelMap.end()) |
| 108 | return NULL; // No matching label. Bad CFG. |
| 109 | |
| 110 | B->addSuccessor(LI->second); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 111 | } |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 113 | // NULL out cfg so that repeated calls |
| 114 | CFG* t = cfg; |
| 115 | cfg = NULL; |
| 116 | return t; |
| 117 | } |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 118 | else return NULL; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 119 | } |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 121 | // createBlock - Used to lazily create blocks that are connected |
| 122 | // to the current (global) succcessor. |
| 123 | CFGBlock* createBlock( bool add_successor = true ) { |
| 124 | CFGBlock* B = cfg->createBlock(NumBlocks++); |
| 125 | if (add_successor && Succ) B->addSuccessor(Succ); |
| 126 | return B; |
| 127 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 128 | |
| 129 | // FinishBlock - When the last statement has been added to the block, |
| 130 | // usually we must reverse the statements because they have been inserted |
| 131 | // in reverse order. When processing labels, however, there are cases |
| 132 | // in the recursion where we may have already reversed the statements |
| 133 | // in a block. This method safely tidies up a block: if the block |
| 134 | // has a label at the front, it has already been reversed. Otherwise, |
| 135 | // we reverse it. |
| 136 | void FinishBlock(CFGBlock* B) { |
| 137 | assert (B); |
| 138 | CFGBlock::iterator I = B->begin(); |
| 139 | if (I != B->end()) { |
| 140 | Stmt* S = *I; |
| 141 | if (S->getStmtClass() != Stmt::LabelStmtClass) |
| 142 | B->reverseStmts(); |
| 143 | } |
| 144 | } |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 146 | /// Here we handle statements with no branching control flow. |
| 147 | CFGBlock* VisitStmt(Stmt* Statement) { |
| 148 | // We cannot assume that we are in the middle of a basic block, since |
| 149 | // the CFG might only be constructed for this single statement. If |
| 150 | // we have no current basic block, just create one lazily. |
| 151 | if (!Block) Block = createBlock(); |
| 152 | |
| 153 | // Simply add the statement to the current block. We actually |
| 154 | // insert statements in reverse order; this order is reversed later |
| 155 | // when processing the containing element in the AST. |
| 156 | Block->appendStmt(Statement); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 157 | |
| 158 | return Block; |
| 159 | } |
| 160 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 161 | CFGBlock* VisitNullStmt(NullStmt* Statement) { |
| 162 | return Block; |
| 163 | } |
| 164 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 165 | CFGBlock* VisitCompoundStmt(CompoundStmt* C) { |
| 166 | // The value returned from this function is the last created CFGBlock |
| 167 | // that represents the "entry" point for the translated AST node. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 168 | CFGBlock* LastBlock; |
| 169 | |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 170 | for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(), |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 171 | E = C->body_rend(); I != E; ++I ) |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 172 | // Add the statement to the current block. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 173 | if (!(LastBlock=Visit(*I))) |
| 174 | return NULL; |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 176 | return LastBlock; |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | CFGBlock* VisitIfStmt(IfStmt* I) { |
| 180 | |
| 181 | // We may see an if statement in the middle of a basic block, or |
| 182 | // it may be the first statement we are processing. In either case, |
| 183 | // we create a new basic block. First, we create the blocks for |
| 184 | // the then...else statements, and then we create the block containing |
| 185 | // the if statement. If we were in the middle of a block, we |
| 186 | // stop processing that block and reverse its statements. That block |
| 187 | // is then the implicit successor for the "then" and "else" clauses. |
| 188 | |
| 189 | // The block we were proccessing is now finished. Make it the |
| 190 | // successor block. |
| 191 | if (Block) { |
| 192 | Succ = Block; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 193 | FinishBlock(Block); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Process the false branch. NULL out Block so that the recursive |
| 197 | // call to Visit will create a new basic block. |
| 198 | // Null out Block so that all successor |
| 199 | CFGBlock* ElseBlock = Succ; |
| 200 | |
| 201 | if (Stmt* Else = I->getElse()) { |
| 202 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 203 | |
| 204 | // NULL out Block so that the recursive call to Visit will |
| 205 | // create a new basic block. |
| 206 | Block = NULL; |
| 207 | ElseBlock = Visit(Else); |
| 208 | if (!ElseBlock) return NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 209 | FinishBlock(ElseBlock); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Process the true branch. NULL out Block so that the recursive |
| 213 | // call to Visit will create a new basic block. |
| 214 | // Null out Block so that all successor |
| 215 | CFGBlock* ThenBlock; |
| 216 | { |
| 217 | Stmt* Then = I->getThen(); |
| 218 | assert (Then); |
| 219 | SaveAndRestore<CFGBlock*> sv(Succ); |
| 220 | Block = NULL; |
| 221 | ThenBlock = Visit(Then); |
| 222 | if (!ThenBlock) return NULL; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 223 | FinishBlock(ThenBlock); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | // Now create a new block containing the if statement. |
| 227 | Block = createBlock(false); |
| 228 | |
| 229 | // Add the condition as the last statement in the new block. |
| 230 | Block->appendStmt(I->getCond()); |
| 231 | |
| 232 | // Set the terminator of the new block to the If statement. |
| 233 | Block->setTerminator(I); |
| 234 | |
| 235 | // Now add the successors. |
| 236 | Block->addSuccessor(ThenBlock); |
| 237 | Block->addSuccessor(ElseBlock); |
| 238 | |
| 239 | return Block; |
| 240 | } |
| 241 | |
| 242 | CFGBlock* VisitReturnStmt(ReturnStmt* R) { |
| 243 | // If we were in the middle of a block we stop processing that block |
| 244 | // and reverse its statements. |
| 245 | // |
| 246 | // NOTE: If a "return" appears in the middle of a block, this means |
| 247 | // that the code afterwards is DEAD (unreachable). We still |
| 248 | // keep a basic block for that code; a simple "mark-and-sweep" |
| 249 | // from the entry block will be able to report such dead |
| 250 | // blocks. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 251 | if (Block) FinishBlock(Block); |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 252 | |
| 253 | // Create the new block. |
| 254 | Block = createBlock(false); |
| 255 | |
| 256 | // The Exit block is the only successor. |
| 257 | Block->addSuccessor(Exit); |
| 258 | |
| 259 | // Add the return expression to the block. |
| 260 | Block->appendStmt(R); |
| 261 | |
| 262 | // Add the return statement itself to the block. |
| 263 | if (R->getRetValue()) Block->appendStmt(R->getRetValue()); |
| 264 | |
| 265 | return Block; |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | CFGBlock* VisitLabelStmt(LabelStmt* L) { |
| 269 | // Get the block of the labeled statement. Add it to our map. |
| 270 | CFGBlock* LabelBlock = Visit(L->getSubStmt()); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 271 | assert (LabelBlock); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 272 | |
| 273 | assert (LabelMap.find(L) == LabelMap.end() && "label already in map"); |
| 274 | LabelMap[ L ] = LabelBlock; |
| 275 | |
| 276 | // Labels partition blocks, so this is the end of the basic block |
| 277 | // we were processing (the label is the first statement). |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 278 | LabelBlock->appendStmt(L); |
| 279 | FinishBlock(LabelBlock); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 280 | |
| 281 | // We set Block to NULL to allow lazy creation of a new block |
| 282 | // (if necessary); |
| 283 | Block = NULL; |
| 284 | |
| 285 | // This block is now the implicit successor of other blocks. |
| 286 | Succ = LabelBlock; |
| 287 | |
| 288 | return LabelBlock; |
| 289 | } |
| 290 | |
| 291 | CFGBlock* VisitGotoStmt(GotoStmt* G) { |
| 292 | // Goto is a control-flow statement. Thus we stop processing the |
| 293 | // current block and create a new one. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 294 | if (Block) FinishBlock(Block); |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 295 | Block = createBlock(false); |
| 296 | Block->setTerminator(G); |
| 297 | |
| 298 | // If we already know the mapping to the label block add the |
| 299 | // successor now. |
| 300 | LabelMapTy::iterator I = LabelMap.find(G->getLabel()); |
| 301 | |
| 302 | if (I == LabelMap.end()) |
| 303 | // We will need to backpatch this block later. |
| 304 | BackpatchBlocks.push_back(Block); |
| 305 | else |
| 306 | Block->addSuccessor(I->second); |
| 307 | |
| 308 | return Block; |
| 309 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 310 | |
| 311 | CFGBlock* VisitForStmt(ForStmt* F) { |
| 312 | // For is a control-flow statement. Thus we stop processing the |
| 313 | // current block. |
| 314 | if (Block) FinishBlock(Block); |
| 315 | |
| 316 | // Besides the loop body, we actually create two new blocks: |
| 317 | // |
| 318 | // The first contains the initialization statement for the loop. |
| 319 | // |
| 320 | // The second block evaluates the loop condition. |
| 321 | // |
| 322 | // We create the initialization block last, as that will be the block |
| 323 | // we return for the recursion. |
| 324 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 325 | CFGBlock* ConditionBlock = createBlock(false); |
| 326 | if (Stmt* C = F->getCond()) ConditionBlock->appendStmt(C); |
| 327 | ConditionBlock->setTerminator(F); |
| 328 | |
| 329 | // The condition block is the implicit successor for the loop body as |
| 330 | // well as any code above the loop. |
| 331 | Succ = ConditionBlock; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 332 | |
| 333 | // Now create the loop body. |
| 334 | { |
| 335 | assert (F->getBody()); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 336 | |
| 337 | // Save the current values for Block, Succ, and ContinueTargetBlock |
| 338 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 339 | save_continue(ContinueTargetBlock); |
| 340 | |
| 341 | // All continues within this loop should go to the condition block |
| 342 | ContinueTargetBlock = ConditionBlock; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 343 | |
| 344 | // create a new block to contain the body. |
| 345 | Block = createBlock(); |
| 346 | |
| 347 | // If we have increment code, insert it at the end of the body block. |
| 348 | if (Stmt* I = F->getInc()) Block->appendStmt(I); |
| 349 | |
| 350 | // Now populate the body block, and in the process create new blocks |
| 351 | // as we walk the body of the loop. |
| 352 | CFGBlock* BodyBlock = Visit(F->getBody()); |
| 353 | |
| 354 | assert (BodyBlock); |
| 355 | FinishBlock(BodyBlock); |
| 356 | |
| 357 | // This new body block is a successor to our condition block. |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 358 | ConditionBlock->addSuccessor(BodyBlock); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Link up the condition block with the code that follows the loop. |
| 362 | // (the false branch). |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 363 | ConditionBlock->addSuccessor(Block); |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 364 | |
| 365 | // Now create the block to contain the initialization. |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 366 | Block = createBlock(); |
| 367 | |
| 368 | if (Stmt* I = F->getInit()) Block->appendStmt(I); |
| 369 | return Block; |
| 370 | } |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 371 | |
| 372 | CFGBlock* VisitWhileStmt(WhileStmt* W) { |
| 373 | // While is a control-flow statement. Thus we stop processing the |
| 374 | // current block. |
| 375 | if (Block) FinishBlock(Block); |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 376 | |
| 377 | // Create the condition block. |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 378 | CFGBlock* ConditionBlock = createBlock(false); |
| 379 | ConditionBlock->setTerminator(W); |
| 380 | if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C); |
| 381 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 382 | // The condition block is the implicit successor for the loop body as |
| 383 | // well as any code above the loop. |
| 384 | Succ = ConditionBlock; |
| 385 | |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 386 | // Process the loop body. |
| 387 | { |
| 388 | assert (W->getBody()); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 389 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 390 | // Save the current values for Block, Succ, and ContinueTargetBlock |
| 391 | SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ), |
| 392 | save_continue(ContinueTargetBlock); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 394 | // All continues within this loop should go to the condition block |
| 395 | ContinueTargetBlock = ConditionBlock; |
| 396 | |
| 397 | // NULL out Block to force lazy instantiation of blocks for the body. |
| 398 | Block = NULL; |
| 399 | |
| 400 | CFGBlock* BodyBlock = Visit(W->getBody()); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 401 | assert (BodyBlock); |
| 402 | |
| 403 | ConditionBlock->addSuccessor(BodyBlock); |
| 404 | } |
| 405 | |
| 406 | ConditionBlock->addSuccessor(Block); |
| 407 | |
| 408 | // There can be no more statements in the condition block |
| 409 | // since we loop back to this block. NULL out Block to force |
| 410 | // lazy creation of another block. |
| 411 | Block = NULL; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 412 | |
| 413 | return ConditionBlock; |
| 414 | } |
| 415 | |
Ted Kremenek | f511d67 | 2007-08-22 21:36:54 +0000 | [diff] [blame^] | 416 | CFGBlock* VisitContinueStmt(ContinueStmt* C) { |
| 417 | // While is a control-flow statement. Thus we stop processing the |
| 418 | // current block. |
| 419 | if (Block) FinishBlock(Block); |
| 420 | |
| 421 | // Now create a new block that ends with the continue statement. |
| 422 | Block = createBlock(false); |
| 423 | Block->setTerminator(C); |
| 424 | |
| 425 | // FIXME: We should gracefully handle continues without resolved targets. |
| 426 | assert (ContinueTargetBlock); |
| 427 | |
| 428 | Block->addSuccessor(ContinueTargetBlock); |
| 429 | return Block; |
| 430 | } |
| 431 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | // BuildCFG - A helper function that builds CFGs from ASTS. |
Ted Kremenek | 95e854d | 2007-08-21 22:06:14 +0000 | [diff] [blame] | 435 | CFG* CFG::BuildCFG(Stmt* Statement) { |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 436 | CFGBuilder Builder; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 437 | return Builder.BuildCFG(Statement); |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | // reverseStmts - A method that reverses the order of the statements within |
| 441 | // a CFGBlock. |
| 442 | void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); } |
| 443 | |
| 444 | // dump - A simple pretty printer of a CFG that outputs to stderr. |
| 445 | void CFG::dump() { print(std::cerr); } |
| 446 | |
| 447 | // print - A simple pretty printer of a CFG that outputs to an ostream. |
| 448 | void CFG::print(std::ostream& OS) { |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 449 | // First print out the Entry block, which may not be the first block |
| 450 | // in our list of blocks |
| 451 | if (begin() != end()) { |
| 452 | CFGBlock& Entry = getEntry(); |
| 453 | OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n"; |
| 454 | Entry.print(OS); |
| 455 | } |
| 456 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 457 | // Iterate through the CFGBlocks and print them one by one. Specially |
| 458 | // designate the Entry and Exit blocks. |
| 459 | for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 460 | // Skip the entry block, because we already printed it. |
| 461 | if (&(*I) == &getEntry()) |
| 462 | continue; |
| 463 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 464 | OS << "\n [ B" << I->getBlockID(); |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 465 | |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 466 | if (&(*I) == &getExit()) OS << " (EXIT) ]\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 467 | else OS << " ]\n"; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 469 | I->print(OS); |
| 470 | } |
| 471 | OS << "\n"; |
| 472 | } |
| 473 | |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 474 | |
| 475 | namespace { |
| 476 | |
| 477 | class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint, |
| 478 | void > { |
| 479 | std::ostream& OS; |
| 480 | public: |
| 481 | CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {} |
| 482 | |
| 483 | void VisitIfStmt(IfStmt* I) { |
| 484 | OS << "if "; |
| 485 | I->getCond()->printPretty(std::cerr); |
| 486 | OS << "\n"; |
| 487 | } |
| 488 | |
| 489 | // Default case. |
| 490 | void VisitStmt(Stmt* S) { S->printPretty(OS); } |
| 491 | |
| 492 | void VisitForStmt(ForStmt* F) { |
| 493 | OS << "for (" ; |
| 494 | if (Stmt* I = F->getInit()) I->printPretty(OS); |
| 495 | OS << " ; "; |
| 496 | if (Stmt* C = F->getCond()) C->printPretty(OS); |
| 497 | OS << " ; "; |
| 498 | if (Stmt* I = F->getInc()) I->printPretty(OS); |
| 499 | OS << ")\n"; |
Ted Kremenek | bec06e8 | 2007-08-22 21:05:42 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void VisitWhileStmt(WhileStmt* W) { |
| 503 | OS << "while " ; |
| 504 | if (Stmt* C = W->getCond()) C->printPretty(OS); |
| 505 | OS << "\n"; |
| 506 | } |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 507 | }; |
| 508 | } |
| 509 | |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 510 | // dump - A simply pretty printer of a CFGBlock that outputs to stderr. |
| 511 | void CFGBlock::dump() { print(std::cerr); } |
| 512 | |
| 513 | // print - A simple pretty printer of a CFGBlock that outputs to an ostream. |
| 514 | // Generally this will only be called from CFG::print. |
| 515 | void CFGBlock::print(std::ostream& OS) { |
| 516 | |
| 517 | // Iterate through the statements in the block and print them. |
| 518 | OS << " ------------------------\n"; |
| 519 | unsigned j = 1; |
| 520 | for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) { |
Ted Kremenek | c5de222 | 2007-08-21 23:26:17 +0000 | [diff] [blame] | 521 | // Print the statement # in the basic block. |
| 522 | OS << " " << std::setw(3) << j << ": "; |
| 523 | |
| 524 | // Print the statement/expression. |
| 525 | Stmt* S = *I; |
| 526 | |
| 527 | if (LabelStmt* L = dyn_cast<LabelStmt>(S)) |
| 528 | OS << L->getName() << ": (LABEL)\n"; |
| 529 | else |
| 530 | (*I)->printPretty(OS); |
| 531 | |
| 532 | // Expressions need a newline. |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 533 | if (isa<Expr>(*I)) OS << '\n'; |
| 534 | } |
| 535 | OS << " ------------------------\n"; |
| 536 | |
| 537 | // Print the predecessors of this block. |
| 538 | OS << " Predecessors (" << pred_size() << "):"; |
| 539 | unsigned i = 0; |
| 540 | for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) { |
| 541 | if (i == 8 || (i-8) == 0) { |
| 542 | OS << "\n "; |
| 543 | } |
| 544 | OS << " B" << (*I)->getBlockID(); |
| 545 | } |
| 546 | |
| 547 | // Print the terminator of this block. |
| 548 | OS << "\n Terminator: "; |
Ted Kremenek | d831320 | 2007-08-22 18:22:34 +0000 | [diff] [blame] | 549 | if (ControlFlowStmt) |
| 550 | CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt); |
| 551 | else |
| 552 | OS << "<NULL>\n"; |
Ted Kremenek | 97f7531 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 553 | |
| 554 | // Print the successors of this block. |
| 555 | OS << " Successors (" << succ_size() << "):"; |
| 556 | i = 0; |
| 557 | for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) { |
| 558 | if (i == 8 || (i-8) % 10 == 0) { |
| 559 | OS << "\n "; |
| 560 | } |
| 561 | OS << " B" << (*I)->getBlockID(); |
| 562 | } |
| 563 | OS << '\n'; |
| 564 | } |