blob: 98d9c8f8f865f2cc3c6ead3fbc888f437524823d [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 Kremenek97f75312007-08-21 21:42:03 +000059 unsigned NumBlocks;
60
Ted Kremenekc5de2222007-08-21 23:26:17 +000061 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
62 LabelMapTy LabelMap;
63
Ted Kremenekf5392b72007-08-22 15:40:58 +000064 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000065 BackpatchBlocksTy BackpatchBlocks;
66
Ted Kremenek97f75312007-08-21 21:42:03 +000067public:
Ted Kremenekf511d672007-08-22 21:36:54 +000068 explicit CFGBuilder() : cfg(NULL), Block(NULL), Exit(NULL), Succ(NULL),
69 ContinueTargetBlock(NULL),
Ted Kremenek97f75312007-08-21 21:42:03 +000070 NumBlocks(0) {
71 // Create an empty CFG.
72 cfg = new CFG();
73 }
74
75 ~CFGBuilder() { delete cfg; }
Ted Kremenekd8313202007-08-22 18:22:34 +000076
Ted Kremenekbec06e82007-08-22 21:05:42 +000077 /// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
Ted Kremenek97f75312007-08-21 21:42:03 +000078 /// 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 Kremenekbec06e82007-08-22 21:05:42 +000082 CFG* BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +000083 if (!Statement) return NULL;
84
Ted Kremenekc5de2222007-08-21 23:26:17 +000085 assert (!Exit && "CFGBuilder should only be used to construct one CFG");
Ted Kremenek97f75312007-08-21 21:42:03 +000086
87 // Create the exit block.
88 Block = createBlock();
89 Exit = Block;
90
91 // Visit the statements and create the CFG.
Ted Kremenek95e854d2007-08-21 22:06:14 +000092 if (CFGBlock* B = Visit(Statement)) {
Ted Kremenekd8313202007-08-22 18:22:34 +000093 // Finalize the last constructed block. This usually involves
94 // reversing the order of the statements in the block.
95 FinishBlock(B);
Ted Kremenekbec06e82007-08-22 21:05:42 +000096 cfg->setEntry(B);
Ted Kremenekc5de2222007-08-21 23:26:17 +000097
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 Kremenekbec06e82007-08-22 21:05:42 +0000111 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000112
Ted Kremenek97f75312007-08-21 21:42:03 +0000113 // NULL out cfg so that repeated calls
114 CFG* t = cfg;
115 cfg = NULL;
116 return t;
117 }
Ted Kremenekc5de2222007-08-21 23:26:17 +0000118 else return NULL;
Ted Kremenek97f75312007-08-21 21:42:03 +0000119 }
Ted Kremenek95e854d2007-08-21 22:06:14 +0000120
Ted Kremenek97f75312007-08-21 21:42:03 +0000121 // 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 Kremenekd8313202007-08-22 18:22:34 +0000128
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 Kremenek97f75312007-08-21 21:42:03 +0000145
Ted Kremenek95e854d2007-08-21 22:06:14 +0000146 /// 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 Kremenek97f75312007-08-21 21:42:03 +0000157
158 return Block;
159 }
160
Ted Kremenekd8313202007-08-22 18:22:34 +0000161 CFGBlock* VisitNullStmt(NullStmt* Statement) {
162 return Block;
163 }
164
Ted Kremenek95e854d2007-08-21 22:06:14 +0000165 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 Kremenekd8313202007-08-22 18:22:34 +0000168 CFGBlock* LastBlock;
169
Ted Kremenek95e854d2007-08-21 22:06:14 +0000170 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
Ted Kremenekd8313202007-08-22 18:22:34 +0000171 E = C->body_rend(); I != E; ++I )
Ted Kremenek95e854d2007-08-21 22:06:14 +0000172 // Add the statement to the current block.
Ted Kremenekd8313202007-08-22 18:22:34 +0000173 if (!(LastBlock=Visit(*I)))
174 return NULL;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000175
Ted Kremenekd8313202007-08-22 18:22:34 +0000176 return LastBlock;
Ted Kremenek95e854d2007-08-21 22:06:14 +0000177 }
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 Kremenekd8313202007-08-22 18:22:34 +0000193 FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000194 }
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 Kremenekd8313202007-08-22 18:22:34 +0000209 FinishBlock(ElseBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000210 }
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 Kremenekd8313202007-08-22 18:22:34 +0000223 FinishBlock(ThenBlock);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000224 }
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 Kremenekd8313202007-08-22 18:22:34 +0000251 if (Block) FinishBlock(Block);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000252
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 Kremenekc5de2222007-08-21 23:26:17 +0000266 }
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 Kremenekd8313202007-08-22 18:22:34 +0000271 assert (LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000272
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 Kremenekd8313202007-08-22 18:22:34 +0000278 LabelBlock->appendStmt(L);
279 FinishBlock(LabelBlock);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000280
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 Kremenekd8313202007-08-22 18:22:34 +0000294 if (Block) FinishBlock(Block);
Ted Kremenekc5de2222007-08-21 23:26:17 +0000295 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 Kremenekd8313202007-08-22 18:22:34 +0000310
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 Kremenekf511d672007-08-22 21:36:54 +0000325 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 Kremenekd8313202007-08-22 18:22:34 +0000332
333 // Now create the loop body.
334 {
335 assert (F->getBody());
Ted Kremenekf511d672007-08-22 21:36:54 +0000336
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 Kremenekd8313202007-08-22 18:22:34 +0000343
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 Kremenekf511d672007-08-22 21:36:54 +0000358 ConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd8313202007-08-22 18:22:34 +0000359 }
360
361 // Link up the condition block with the code that follows the loop.
362 // (the false branch).
Ted Kremenekf511d672007-08-22 21:36:54 +0000363 ConditionBlock->addSuccessor(Block);
Ted Kremenekd8313202007-08-22 18:22:34 +0000364
365 // Now create the block to contain the initialization.
Ted Kremenekd8313202007-08-22 18:22:34 +0000366 Block = createBlock();
367
368 if (Stmt* I = F->getInit()) Block->appendStmt(I);
369 return Block;
370 }
Ted Kremenekbec06e82007-08-22 21:05:42 +0000371
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 Kremenekf511d672007-08-22 21:36:54 +0000376
377 // Create the condition block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000378 CFGBlock* ConditionBlock = createBlock(false);
379 ConditionBlock->setTerminator(W);
380 if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C);
381
Ted Kremenekf511d672007-08-22 21:36:54 +0000382 // 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 Kremenekbec06e82007-08-22 21:05:42 +0000386 // Process the loop body.
387 {
388 assert (W->getBody());
Ted Kremenekbec06e82007-08-22 21:05:42 +0000389
Ted Kremenekf511d672007-08-22 21:36:54 +0000390 // Save the current values for Block, Succ, and ContinueTargetBlock
391 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
392 save_continue(ContinueTargetBlock);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000393
Ted Kremenekf511d672007-08-22 21:36:54 +0000394 // 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 Kremenekbec06e82007-08-22 21:05:42 +0000401 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 Kremenekbec06e82007-08-22 21:05:42 +0000412
413 return ConditionBlock;
414 }
415
Ted Kremenekf511d672007-08-22 21:36:54 +0000416 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 Kremenek97f75312007-08-21 21:42:03 +0000432};
433
434// BuildCFG - A helper function that builds CFGs from ASTS.
Ted Kremenek95e854d2007-08-21 22:06:14 +0000435CFG* CFG::BuildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +0000436 CFGBuilder Builder;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000437 return Builder.BuildCFG(Statement);
Ted Kremenek97f75312007-08-21 21:42:03 +0000438}
439
440// reverseStmts - A method that reverses the order of the statements within
441// a CFGBlock.
442void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
443
444// dump - A simple pretty printer of a CFG that outputs to stderr.
445void CFG::dump() { print(std::cerr); }
446
447// print - A simple pretty printer of a CFG that outputs to an ostream.
448void CFG::print(std::ostream& OS) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000449 // 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 Kremenek97f75312007-08-21 21:42:03 +0000457 // 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 Kremenekbec06e82007-08-22 21:05:42 +0000460 // Skip the entry block, because we already printed it.
461 if (&(*I) == &getEntry())
462 continue;
463
Ted Kremenek97f75312007-08-21 21:42:03 +0000464 OS << "\n [ B" << I->getBlockID();
Ted Kremenekbec06e82007-08-22 21:05:42 +0000465
Ted Kremenekc5de2222007-08-21 23:26:17 +0000466 if (&(*I) == &getExit()) OS << " (EXIT) ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000467 else OS << " ]\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000468
Ted Kremenek97f75312007-08-21 21:42:03 +0000469 I->print(OS);
470 }
471 OS << "\n";
472}
473
Ted Kremenekd8313202007-08-22 18:22:34 +0000474
475namespace {
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 Kremenekbec06e82007-08-22 21:05:42 +0000500 }
501
502 void VisitWhileStmt(WhileStmt* W) {
503 OS << "while " ;
504 if (Stmt* C = W->getCond()) C->printPretty(OS);
505 OS << "\n";
506 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000507 };
508}
509
Ted Kremenek97f75312007-08-21 21:42:03 +0000510// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
511void 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.
515void 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 Kremenekc5de2222007-08-21 23:26:17 +0000521 // 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 Kremenek97f75312007-08-21 21:42:03 +0000533 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 Kremenekd8313202007-08-22 18:22:34 +0000549 if (ControlFlowStmt)
550 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
551 else
552 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000553
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}