blob: eb358281ef2275c2c7921a63761621d63076ef34 [file] [log] [blame]
Ted Kremenek97f75312007-08-21 21:42:03 +00001//===--- 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 Kremenek95e854d2007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenekc5de2222007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek97f75312007-08-21 21:42:03 +000019#include <iostream>
20#include <iomanip>
21#include <algorithm>
22using namespace clang;
23
24namespace {
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 Kremenek95e854d2007-08-21 22:06:14 +000047/// 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///
53class CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000054 CFG* cfg;
55 CFGBlock* Block;
56 CFGBlock* Exit;
57 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000060 unsigned NumBlocks;
61
Ted Kremenekc5de2222007-08-21 23:26:17 +000062 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
63 LabelMapTy LabelMap;
64
Ted Kremenekf5392b72007-08-22 15:40:58 +000065 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000066 BackpatchBlocksTy BackpatchBlocks;
67
Ted Kremenek97f75312007-08-21 21:42:03 +000068public:
Ted Kremenekf511d672007-08-22 21:36:54 +000069 explicit CFGBuilder() : cfg(NULL), Block(NULL), Exit(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000070 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek97f75312007-08-21 21:42:03 +000071 NumBlocks(0) {
72 // Create an empty CFG.
73 cfg = new CFG();
74 }
75
76 ~CFGBuilder() { delete cfg; }
Ted Kremenekd8313202007-08-22 18:22:34 +000077
Ted Kremenekbec06e82007-08-22 21:05:42 +000078 /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
Ted Kremenek97f75312007-08-21 21:42:03 +000079 /// represent an arbitrary statement. Examples include a single expression
80 /// or a function body (compound statement). The ownership of the returned
81 /// CFG is transferred to the caller. If CFG construction fails, this method
82 /// returns NULL.
Ted Kremenekbec06e82007-08-22 21:05:42 +000083 CFG* BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +000084 if (!Statement) return NULL;
85
Ted Kremenekc5de2222007-08-21 23:26:17 +000086 assert (!Exit && "CFGBuilder should only be used to construct one CFG");
Ted Kremenek97f75312007-08-21 21:42:03 +000087
88 // Create the exit block.
89 Block = createBlock();
90 Exit = Block;
91
92 // Visit the statements and create the CFG.
Ted Kremenek95e854d2007-08-21 22:06:14 +000093 if (CFGBlock* B = Visit(Statement)) {
Ted Kremenekd8313202007-08-22 18:22:34 +000094 // Finalize the last constructed block. This usually involves
95 // reversing the order of the statements in the block.
96 FinishBlock(B);
Ted Kremenekbec06e82007-08-22 21:05:42 +000097 cfg->setEntry(B);
Ted Kremenekc5de2222007-08-21 23:26:17 +000098
99 // Backpatch the gotos whose label -> block mappings we didn't know
100 // when we encountered them.
101 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
102 E = BackpatchBlocks.end(); I != E; ++I ) {
103
104 CFGBlock* B = *I;
105 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
106 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
107
108 if (LI == LabelMap.end())
109 return NULL; // No matching label. Bad CFG.
110
111 B->addSuccessor(LI->second);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000112 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000113
Ted Kremenek97f75312007-08-21 21:42:03 +0000114 // NULL out cfg so that repeated calls
115 CFG* t = cfg;
116 cfg = NULL;
117 return t;
118 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000119 else return NULL;
Ted Kremenek97f75312007-08-21 21:42:03 +0000120 }
Ted Kremenek95e854d2007-08-21 22:06:14 +0000121
Ted Kremenek97f75312007-08-21 21:42:03 +0000122 // createBlock - Used to lazily create blocks that are connected
123 // to the current (global) succcessor.
124 CFGBlock* createBlock( bool add_successor = true ) {
125 CFGBlock* B = cfg->createBlock(NumBlocks++);
126 if (add_successor && Succ) B->addSuccessor(Succ);
127 return B;
128 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000129
130 // FinishBlock - When the last statement has been added to the block,
131 // usually we must reverse the statements because they have been inserted
132 // in reverse order. When processing labels, however, there are cases
133 // in the recursion where we may have already reversed the statements
134 // in a block. This method safely tidies up a block: if the block
135 // has a label at the front, it has already been reversed. Otherwise,
136 // we reverse it.
137 void FinishBlock(CFGBlock* B) {
138 assert (B);
139 CFGBlock::iterator I = B->begin();
140 if (I != B->end()) {
141 Stmt* S = *I;
142 if (S->getStmtClass() != Stmt::LabelStmtClass)
143 B->reverseStmts();
144 }
145 }
Ted Kremenek97f75312007-08-21 21:42:03 +0000146
Ted Kremenek95e854d2007-08-21 22:06:14 +0000147 /// Here we handle statements with no branching control flow.
148 CFGBlock* VisitStmt(Stmt* Statement) {
149 // We cannot assume that we are in the middle of a basic block, since
150 // the CFG might only be constructed for this single statement. If
151 // we have no current basic block, just create one lazily.
152 if (!Block) Block = createBlock();
153
154 // Simply add the statement to the current block. We actually
155 // insert statements in reverse order; this order is reversed later
156 // when processing the containing element in the AST.
157 Block->appendStmt(Statement);
Ted Kremenek97f75312007-08-21 21:42:03 +0000158
159 return Block;
160 }
161
Ted Kremenekd8313202007-08-22 18:22:34 +0000162 CFGBlock* VisitNullStmt(NullStmt* Statement) {
163 return Block;
164 }
165
Ted Kremenek95e854d2007-08-21 22:06:14 +0000166 CFGBlock* VisitCompoundStmt(CompoundStmt* C) {
167 // The value returned from this function is the last created CFGBlock
168 // that represents the "entry" point for the translated AST node.
Ted Kremenekd8313202007-08-22 18:22:34 +0000169 CFGBlock* LastBlock;
170
Ted Kremenek95e854d2007-08-21 22:06:14 +0000171 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
Ted Kremenekd8313202007-08-22 18:22:34 +0000172 E = C->body_rend(); I != E; ++I )
Ted Kremenek95e854d2007-08-21 22:06:14 +0000173 // Add the statement to the current block.
Ted Kremenekd8313202007-08-22 18:22:34 +0000174 if (!(LastBlock=Visit(*I)))
175 return NULL;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000176
Ted Kremenekd8313202007-08-22 18:22:34 +0000177 return LastBlock;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000178 }
179
180 CFGBlock* VisitIfStmt(IfStmt* I) {
181
182 // We may see an if statement in the middle of a basic block, or
183 // it may be the first statement we are processing. In either case,
184 // we create a new basic block. First, we create the blocks for
185 // the then...else statements, and then we create the block containing
186 // the if statement. If we were in the middle of a block, we
187 // stop processing that block and reverse its statements. That block
188 // is then the implicit successor for the "then" and "else" clauses.
189
190 // The block we were proccessing is now finished. Make it the
191 // successor block.
192 if (Block) {
193 Succ = Block;
Ted Kremenekd8313202007-08-22 18:22:34 +0000194 FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000195 }
196
197 // Process the false branch. NULL out Block so that the recursive
198 // call to Visit will create a new basic block.
199 // Null out Block so that all successor
200 CFGBlock* ElseBlock = Succ;
201
202 if (Stmt* Else = I->getElse()) {
203 SaveAndRestore<CFGBlock*> sv(Succ);
204
205 // NULL out Block so that the recursive call to Visit will
206 // create a new basic block.
207 Block = NULL;
208 ElseBlock = Visit(Else);
209 if (!ElseBlock) return NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000210 FinishBlock(ElseBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000211 }
212
213 // Process the true branch. NULL out Block so that the recursive
214 // call to Visit will create a new basic block.
215 // Null out Block so that all successor
216 CFGBlock* ThenBlock;
217 {
218 Stmt* Then = I->getThen();
219 assert (Then);
220 SaveAndRestore<CFGBlock*> sv(Succ);
221 Block = NULL;
222 ThenBlock = Visit(Then);
223 if (!ThenBlock) return NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000224 FinishBlock(ThenBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000225 }
226
227 // Now create a new block containing the if statement.
228 Block = createBlock(false);
229
230 // Add the condition as the last statement in the new block.
231 Block->appendStmt(I->getCond());
232
233 // Set the terminator of the new block to the If statement.
234 Block->setTerminator(I);
235
236 // Now add the successors.
237 Block->addSuccessor(ThenBlock);
238 Block->addSuccessor(ElseBlock);
239
240 return Block;
241 }
242
243 CFGBlock* VisitReturnStmt(ReturnStmt* R) {
244 // If we were in the middle of a block we stop processing that block
245 // and reverse its statements.
246 //
247 // NOTE: If a "return" appears in the middle of a block, this means
248 // that the code afterwards is DEAD (unreachable). We still
249 // keep a basic block for that code; a simple "mark-and-sweep"
250 // from the entry block will be able to report such dead
251 // blocks.
Ted Kremenekd8313202007-08-22 18:22:34 +0000252 if (Block) FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000253
254 // Create the new block.
255 Block = createBlock(false);
256
257 // The Exit block is the only successor.
258 Block->addSuccessor(Exit);
259
260 // Add the return expression to the block.
261 Block->appendStmt(R);
262
263 // Add the return statement itself to the block.
264 if (R->getRetValue()) Block->appendStmt(R->getRetValue());
265
266 return Block;
Ted Kremenekc5de2222007-08-21 23:26:17 +0000267 }
268
269 CFGBlock* VisitLabelStmt(LabelStmt* L) {
270 // Get the block of the labeled statement. Add it to our map.
271 CFGBlock* LabelBlock = Visit(L->getSubStmt());
Ted Kremenekd8313202007-08-22 18:22:34 +0000272 assert (LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000273
274 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
275 LabelMap[ L ] = LabelBlock;
276
277 // Labels partition blocks, so this is the end of the basic block
278 // we were processing (the label is the first statement).
Ted Kremenekd8313202007-08-22 18:22:34 +0000279 LabelBlock->appendStmt(L);
280 FinishBlock(LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000281
282 // We set Block to NULL to allow lazy creation of a new block
283 // (if necessary);
284 Block = NULL;
285
286 // This block is now the implicit successor of other blocks.
287 Succ = LabelBlock;
288
289 return LabelBlock;
290 }
291
292 CFGBlock* VisitGotoStmt(GotoStmt* G) {
293 // Goto is a control-flow statement. Thus we stop processing the
294 // current block and create a new one.
Ted Kremenekd8313202007-08-22 18:22:34 +0000295 if (Block) FinishBlock(Block);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000296 Block = createBlock(false);
297 Block->setTerminator(G);
298
299 // If we already know the mapping to the label block add the
300 // successor now.
301 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
302
303 if (I == LabelMap.end())
304 // We will need to backpatch this block later.
305 BackpatchBlocks.push_back(Block);
306 else
307 Block->addSuccessor(I->second);
308
309 return Block;
310 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000311
312 CFGBlock* VisitForStmt(ForStmt* F) {
313 // For is a control-flow statement. Thus we stop processing the
314 // current block.
315 if (Block) FinishBlock(Block);
316
317 // Besides the loop body, we actually create two new blocks:
318 //
319 // The first contains the initialization statement for the loop.
320 //
321 // The second block evaluates the loop condition.
322 //
323 // We create the initialization block last, as that will be the block
324 // we return for the recursion.
325
Ted Kremenekf511d672007-08-22 21:36:54 +0000326 CFGBlock* ConditionBlock = createBlock(false);
327 if (Stmt* C = F->getCond()) ConditionBlock->appendStmt(C);
328 ConditionBlock->setTerminator(F);
329
330 // The condition block is the implicit successor for the loop body as
331 // well as any code above the loop.
332 Succ = ConditionBlock;
Ted Kremenekd8313202007-08-22 18:22:34 +0000333
334 // Now create the loop body.
335 {
336 assert (F->getBody());
Ted Kremenekf511d672007-08-22 21:36:54 +0000337
338 // Save the current values for Block, Succ, and ContinueTargetBlock
339 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Ted Kremenekf308d372007-08-22 21:51:58 +0000340 save_continue(ContinueTargetBlock),
341 save_break(BreakTargetBlock);
Ted Kremenekf511d672007-08-22 21:36:54 +0000342
343 // All continues within this loop should go to the condition block
344 ContinueTargetBlock = ConditionBlock;
Ted Kremenekd8313202007-08-22 18:22:34 +0000345
Ted Kremenekf308d372007-08-22 21:51:58 +0000346 // All breaks should go to the code that follows the loop.
347 BreakTargetBlock = Block;
348
Ted Kremenekd8313202007-08-22 18:22:34 +0000349 // create a new block to contain the body.
350 Block = createBlock();
351
352 // If we have increment code, insert it at the end of the body block.
353 if (Stmt* I = F->getInc()) Block->appendStmt(I);
354
355 // Now populate the body block, and in the process create new blocks
356 // as we walk the body of the loop.
357 CFGBlock* BodyBlock = Visit(F->getBody());
358
359 assert (BodyBlock);
360 FinishBlock(BodyBlock);
361
362 // This new body block is a successor to our condition block.
Ted Kremenekf511d672007-08-22 21:36:54 +0000363 ConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd8313202007-08-22 18:22:34 +0000364 }
365
366 // Link up the condition block with the code that follows the loop.
367 // (the false branch).
Ted Kremenekf511d672007-08-22 21:36:54 +0000368 ConditionBlock->addSuccessor(Block);
Ted Kremenekd8313202007-08-22 18:22:34 +0000369
370 // Now create the block to contain the initialization.
Ted Kremenekd8313202007-08-22 18:22:34 +0000371 Block = createBlock();
372
373 if (Stmt* I = F->getInit()) Block->appendStmt(I);
374 return Block;
375 }
Ted Kremenekbec06e82007-08-22 21:05:42 +0000376
377 CFGBlock* VisitWhileStmt(WhileStmt* W) {
378 // While is a control-flow statement. Thus we stop processing the
379 // current block.
380 if (Block) FinishBlock(Block);
Ted Kremenekf511d672007-08-22 21:36:54 +0000381
382 // Create the condition block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000383 CFGBlock* ConditionBlock = createBlock(false);
384 ConditionBlock->setTerminator(W);
385 if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C);
386
Ted Kremenekf511d672007-08-22 21:36:54 +0000387 // The condition block is the implicit successor for the loop body as
388 // well as any code above the loop.
389 Succ = ConditionBlock;
390
Ted Kremenekbec06e82007-08-22 21:05:42 +0000391 // Process the loop body.
392 {
393 assert (W->getBody());
Ted Kremenekbec06e82007-08-22 21:05:42 +0000394
Ted Kremenekf511d672007-08-22 21:36:54 +0000395 // Save the current values for Block, Succ, and ContinueTargetBlock
396 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Ted Kremenekf308d372007-08-22 21:51:58 +0000397 save_continue(ContinueTargetBlock),
398 save_break(BreakTargetBlock);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000399
Ted Kremenekf511d672007-08-22 21:36:54 +0000400 // All continues within this loop should go to the condition block
401 ContinueTargetBlock = ConditionBlock;
402
Ted Kremenekf308d372007-08-22 21:51:58 +0000403 // All breaks should go to the code that follows the loop.
404 BreakTargetBlock = Block;
405
Ted Kremenekf511d672007-08-22 21:36:54 +0000406 // NULL out Block to force lazy instantiation of blocks for the body.
407 Block = NULL;
408
409 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekbec06e82007-08-22 21:05:42 +0000410 assert (BodyBlock);
411
412 ConditionBlock->addSuccessor(BodyBlock);
413 }
414
Ted Kremenekf308d372007-08-22 21:51:58 +0000415 // Link up the condition block with the code that follows the loop.
416 // (the false branch).
Ted Kremenekbec06e82007-08-22 21:05:42 +0000417 ConditionBlock->addSuccessor(Block);
418
419 // There can be no more statements in the condition block
420 // since we loop back to this block. NULL out Block to force
421 // lazy creation of another block.
422 Block = NULL;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000423
424 return ConditionBlock;
425 }
426
Ted Kremenekf511d672007-08-22 21:36:54 +0000427 CFGBlock* VisitContinueStmt(ContinueStmt* C) {
Ted Kremenekf308d372007-08-22 21:51:58 +0000428 // "continue" is a control-flow statement. Thus we stop processing the
Ted Kremenekf511d672007-08-22 21:36:54 +0000429 // current block.
430 if (Block) FinishBlock(Block);
431
432 // Now create a new block that ends with the continue statement.
433 Block = createBlock(false);
434 Block->setTerminator(C);
435
436 // FIXME: We should gracefully handle continues without resolved targets.
437 assert (ContinueTargetBlock);
438
439 Block->addSuccessor(ContinueTargetBlock);
440 return Block;
441 }
442
Ted Kremenekf308d372007-08-22 21:51:58 +0000443 CFGBlock* VisitBreakStmt(BreakStmt* B) {
444 // "break" is a control-flow statement. Thus we stop processing the
445 // current block.
446 if (Block) FinishBlock(Block);
447
448 // Now create a new block that ends with the continue statement.
449 Block = createBlock(false);
450 Block->setTerminator(B);
451
452 // FIXME: We should gracefully handle breaks without resolved targets.
453 assert (BreakTargetBlock);
454
455 Block->addSuccessor(BreakTargetBlock);
456 return Block;
457 }
458
Ted Kremenek97f75312007-08-21 21:42:03 +0000459};
460
461// BuildCFG - A helper function that builds CFGs from ASTS.
Ted Kremenek95e854d2007-08-21 22:06:14 +0000462CFG* CFG::BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +0000463 CFGBuilder Builder;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000464 return Builder.BuildCFG(Statement);
Ted Kremenek97f75312007-08-21 21:42:03 +0000465}
466
467// reverseStmts - A method that reverses the order of the statements within
468// a CFGBlock.
469void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
470
471// dump - A simple pretty printer of a CFG that outputs to stderr.
472void CFG::dump() { print(std::cerr); }
473
474// print - A simple pretty printer of a CFG that outputs to an ostream.
475void CFG::print(std::ostream& OS) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000476 // First print out the Entry block, which may not be the first block
477 // in our list of blocks
478 if (begin() != end()) {
479 CFGBlock& Entry = getEntry();
480 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
481 Entry.print(OS);
482 }
483
Ted Kremenek97f75312007-08-21 21:42:03 +0000484 // Iterate through the CFGBlocks and print them one by one. Specially
485 // designate the Entry and Exit blocks.
486 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000487 // Skip the entry block, because we already printed it.
488 if (&(*I) == &getEntry())
489 continue;
490
Ted Kremenek97f75312007-08-21 21:42:03 +0000491 OS << "\n [ B" << I->getBlockID();
Ted Kremenekbec06e82007-08-22 21:05:42 +0000492
Ted Kremenekc5de2222007-08-21 23:26:17 +0000493 if (&(*I) == &getExit()) OS << " (EXIT) ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000494 else OS << " ]\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000495
Ted Kremenek97f75312007-08-21 21:42:03 +0000496 I->print(OS);
497 }
498 OS << "\n";
499}
500
Ted Kremenekd8313202007-08-22 18:22:34 +0000501
502namespace {
503
504 class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
505 void > {
506 std::ostream& OS;
507 public:
508 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
509
510 void VisitIfStmt(IfStmt* I) {
511 OS << "if ";
512 I->getCond()->printPretty(std::cerr);
513 OS << "\n";
514 }
515
516 // Default case.
517 void VisitStmt(Stmt* S) { S->printPretty(OS); }
518
519 void VisitForStmt(ForStmt* F) {
520 OS << "for (" ;
521 if (Stmt* I = F->getInit()) I->printPretty(OS);
522 OS << " ; ";
523 if (Stmt* C = F->getCond()) C->printPretty(OS);
524 OS << " ; ";
525 if (Stmt* I = F->getInc()) I->printPretty(OS);
526 OS << ")\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000527 }
528
529 void VisitWhileStmt(WhileStmt* W) {
530 OS << "while " ;
531 if (Stmt* C = W->getCond()) C->printPretty(OS);
532 OS << "\n";
533 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000534 };
535}
536
Ted Kremenek97f75312007-08-21 21:42:03 +0000537// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
538void CFGBlock::dump() { print(std::cerr); }
539
540// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
541// Generally this will only be called from CFG::print.
542void CFGBlock::print(std::ostream& OS) {
543
544 // Iterate through the statements in the block and print them.
545 OS << " ------------------------\n";
546 unsigned j = 1;
547 for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000548 // Print the statement # in the basic block.
549 OS << " " << std::setw(3) << j << ": ";
550
551 // Print the statement/expression.
552 Stmt* S = *I;
553
554 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
555 OS << L->getName() << ": (LABEL)\n";
556 else
557 (*I)->printPretty(OS);
558
559 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000560 if (isa<Expr>(*I)) OS << '\n';
561 }
562 OS << " ------------------------\n";
563
564 // Print the predecessors of this block.
565 OS << " Predecessors (" << pred_size() << "):";
566 unsigned i = 0;
567 for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
568 if (i == 8 || (i-8) == 0) {
569 OS << "\n ";
570 }
571 OS << " B" << (*I)->getBlockID();
572 }
573
574 // Print the terminator of this block.
575 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +0000576 if (ControlFlowStmt)
577 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
578 else
579 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000580
581 // Print the successors of this block.
582 OS << " Successors (" << succ_size() << "):";
583 i = 0;
584 for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
585 if (i == 8 || (i-8) % 10 == 0) {
586 OS << "\n ";
587 }
588 OS << " B" << (*I)->getBlockID();
589 }
590 OS << '\n';
591}