blob: d21d9c752d7e10558842a4025ab4dba2a55fbf47 [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;
Ted Kremenek97f75312007-08-21 21:42:03 +000056 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000057 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000058 CFGBlock* BreakTargetBlock;
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 Kremenek4db5b452007-08-23 16:51:22 +000068 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000069 ContinueTargetBlock(NULL), BreakTargetBlock(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 Kremenek4db5b452007-08-23 16:51:22 +000082 CFG* buildCFG(Stmt* Statement) {
Ted Kremenek97f75312007-08-21 21:42:03 +000083 if (!Statement) return NULL;
84
Ted Kremenek4db5b452007-08-23 16:51:22 +000085 // Create an empty block that will serve as the exit block for the CFG.
86 // Since this is the first block added to the CFG, it will be implicitly
87 // registered as the exit block.
Ted Kremenek97f75312007-08-21 21:42:03 +000088 Block = createBlock();
Ted Kremenek4db5b452007-08-23 16:51:22 +000089 assert (Block == &cfg->getExit());
Ted Kremenek97f75312007-08-21 21:42:03 +000090
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.
Ted Kremenek4db5b452007-08-23 16:51:22 +0000257 Block->addSuccessor(&cfg->getExit());
Ted Kremenek95e854d2007-08-21 22:06:14 +0000258
Ted Kremenek4db5b452007-08-23 16:51:22 +0000259 // Add the return statement to the block.
Ted Kremenek95e854d2007-08-21 22:06:14 +0000260 Block->appendStmt(R);
261
Ted Kremenek4db5b452007-08-23 16:51:22 +0000262 // Also add the return statement as the terminator.
263 Block->setTerminator(R);
Ted Kremenek95e854d2007-08-21 22:06:14 +0000264
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) {
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000312 // "for" is a control-flow statement. Thus we stop processing the
Ted Kremenekd8313202007-08-22 18:22:34 +0000313 // current block.
Ted Kremenekd8313202007-08-22 18:22:34 +0000314
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000315 CFGBlock* LoopSuccessor = NULL;
Ted Kremenekd8313202007-08-22 18:22:34 +0000316
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000317 if (Block) {
318 FinishBlock(Block);
319 LoopSuccessor = Block;
320 }
321 else LoopSuccessor = Succ;
322
323 // Create the condition block.
Ted Kremenekf511d672007-08-22 21:36:54 +0000324 CFGBlock* ConditionBlock = createBlock(false);
Ted Kremenekf511d672007-08-22 21:36:54 +0000325 ConditionBlock->setTerminator(F);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000326 if (Stmt* C = F->getCond()) ConditionBlock->appendStmt(C);
Ted Kremenekf511d672007-08-22 21:36:54 +0000327
328 // The condition block is the implicit successor for the loop body as
329 // well as any code above the loop.
330 Succ = ConditionBlock;
Ted Kremenekd8313202007-08-22 18:22:34 +0000331
332 // Now create the loop body.
333 {
334 assert (F->getBody());
Ted Kremenekf511d672007-08-22 21:36:54 +0000335
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000336 // Save the current values for Block, Succ, and continue and break targets
Ted Kremenekf511d672007-08-22 21:36:54 +0000337 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000338 save_continue(ContinueTargetBlock),
339 save_break(BreakTargetBlock);
340
Ted Kremenekf511d672007-08-22 21:36:54 +0000341 // All continues within this loop should go to the condition block
342 ContinueTargetBlock = ConditionBlock;
Ted Kremenekd8313202007-08-22 18:22:34 +0000343
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000344 // All breaks should go to the code following the loop.
345 BreakTargetBlock = LoopSuccessor;
Ted Kremenekf308d372007-08-22 21:51:58 +0000346
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000347 // Create a new block to contain the (bottom) of the loop body.
Ted Kremenekd8313202007-08-22 18:22:34 +0000348 Block = createBlock();
349
350 // If we have increment code, insert it at the end of the body block.
351 if (Stmt* I = F->getInc()) Block->appendStmt(I);
352
353 // Now populate the body block, and in the process create new blocks
354 // as we walk the body of the loop.
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000355 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekd8313202007-08-22 18:22:34 +0000356 assert (BodyBlock);
357 FinishBlock(BodyBlock);
358
359 // This new body block is a successor to our condition block.
Ted Kremenekf511d672007-08-22 21:36:54 +0000360 ConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd8313202007-08-22 18:22:34 +0000361 }
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000362
Ted Kremenekd8313202007-08-22 18:22:34 +0000363 // Link up the condition block with the code that follows the loop.
364 // (the false branch).
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000365 ConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd8313202007-08-22 18:22:34 +0000366
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000367 // If the loop contains initialization, create a new block for those
368 // statements. This block can also contain statements that precede
369 // the loop.
370 if (Stmt* I = F->getInit()) {
371 Block = createBlock();
372 Block->appendStmt(I);
373 return Block;
374 }
375 else {
376 // There is no loop initialization. We are thus basically a while
377 // loop. NULL out Block to force lazy block construction.
378 Block = NULL;
379 return ConditionBlock;
380 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000381 }
Ted Kremenekbec06e82007-08-22 21:05:42 +0000382
383 CFGBlock* VisitWhileStmt(WhileStmt* W) {
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000384 // "while" is a control-flow statement. Thus we stop processing the
Ted Kremenekbec06e82007-08-22 21:05:42 +0000385 // current block.
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000386
387 CFGBlock* LoopSuccessor = NULL;
388
389 if (Block) {
390 FinishBlock(Block);
391 LoopSuccessor = Block;
392 }
393 else LoopSuccessor = Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +0000394
395 // Create the condition block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000396 CFGBlock* ConditionBlock = createBlock(false);
397 ConditionBlock->setTerminator(W);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000398 if (Stmt* C = W->getCond()) ConditionBlock->appendStmt(C);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000399
Ted Kremenekf511d672007-08-22 21:36:54 +0000400 // The condition block is the implicit successor for the loop body as
401 // well as any code above the loop.
402 Succ = ConditionBlock;
403
Ted Kremenekbec06e82007-08-22 21:05:42 +0000404 // Process the loop body.
405 {
406 assert (W->getBody());
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000407
408 // Save the current values for Block, Succ, and continue and break targets
Ted Kremenekf511d672007-08-22 21:36:54 +0000409 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Ted Kremenekf308d372007-08-22 21:51:58 +0000410 save_continue(ContinueTargetBlock),
411 save_break(BreakTargetBlock);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000412
Ted Kremenekf511d672007-08-22 21:36:54 +0000413 // All continues within this loop should go to the condition block
414 ContinueTargetBlock = ConditionBlock;
415
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000416 // All breaks should go to the code following the loop.
417 BreakTargetBlock = LoopSuccessor;
Ted Kremenekf308d372007-08-22 21:51:58 +0000418
Ted Kremenekf511d672007-08-22 21:36:54 +0000419 // NULL out Block to force lazy instantiation of blocks for the body.
420 Block = NULL;
421
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000422 // Create the body. The returned block is the entry to the loop body.
Ted Kremenekf511d672007-08-22 21:36:54 +0000423 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekbec06e82007-08-22 21:05:42 +0000424 assert (BodyBlock);
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000425 FinishBlock(BodyBlock);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000426
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000427 // Add the loop body entry as a successor to the condition.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000428 ConditionBlock->addSuccessor(BodyBlock);
429 }
430
Ted Kremenekf308d372007-08-22 21:51:58 +0000431 // Link up the condition block with the code that follows the loop.
432 // (the false branch).
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000433 ConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekbec06e82007-08-22 21:05:42 +0000434
435 // There can be no more statements in the condition block
436 // since we loop back to this block. NULL out Block to force
437 // lazy creation of another block.
438 Block = NULL;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000439
Ted Kremenek0baf7c02007-08-22 22:35:28 +0000440 // Return the condition block, which is the dominating block for the loop.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000441 return ConditionBlock;
442 }
443
Ted Kremenekf511d672007-08-22 21:36:54 +0000444 CFGBlock* VisitContinueStmt(ContinueStmt* C) {
Ted Kremenekf308d372007-08-22 21:51:58 +0000445 // "continue" is a control-flow statement. Thus we stop processing the
Ted Kremenekf511d672007-08-22 21:36:54 +0000446 // current block.
447 if (Block) FinishBlock(Block);
448
449 // Now create a new block that ends with the continue statement.
450 Block = createBlock(false);
451 Block->setTerminator(C);
452
453 // FIXME: We should gracefully handle continues without resolved targets.
454 assert (ContinueTargetBlock);
455
456 Block->addSuccessor(ContinueTargetBlock);
457 return Block;
458 }
459
Ted Kremenekf308d372007-08-22 21:51:58 +0000460 CFGBlock* VisitBreakStmt(BreakStmt* B) {
461 // "break" is a control-flow statement. Thus we stop processing the
462 // current block.
463 if (Block) FinishBlock(Block);
464
465 // Now create a new block that ends with the continue statement.
466 Block = createBlock(false);
467 Block->setTerminator(B);
468
469 // FIXME: We should gracefully handle breaks without resolved targets.
470 assert (BreakTargetBlock);
471
472 Block->addSuccessor(BreakTargetBlock);
473 return Block;
474 }
475
Ted Kremenek97f75312007-08-21 21:42:03 +0000476};
477
Ted Kremenek4db5b452007-08-23 16:51:22 +0000478
479/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
480/// block has no successors or predecessors. If this is the first block
481/// created in the CFG, it is automatically set to be the Entry and Exit
482/// of the CFG.
483CFGBlock* CFG::createBlock(unsigned blockID) {
484 bool first_block = begin() == end();
485
486 // Create the block.
487 Blocks.push_front(CFGBlock(blockID));
488
489 // If this is the first block, set it as the Entry and Exit.
490 if (first_block) Entry = Exit = &front();
491
492 // Return the block.
493 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +0000494}
495
Ted Kremenek4db5b452007-08-23 16:51:22 +0000496/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
497/// CFG is returned to the caller.
498CFG* CFG::buildCFG(Stmt* Statement) {
499 CFGBuilder Builder;
500 return Builder.buildCFG(Statement);
501}
502
503/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +0000504void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
505
Ted Kremenek4db5b452007-08-23 16:51:22 +0000506/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000507void CFG::dump() { print(std::cerr); }
508
Ted Kremenek4db5b452007-08-23 16:51:22 +0000509/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremenek97f75312007-08-21 21:42:03 +0000510void CFG::print(std::ostream& OS) {
Ted Kremenek4db5b452007-08-23 16:51:22 +0000511
512 // Print the Entry block.
Ted Kremenekbec06e82007-08-22 21:05:42 +0000513 if (begin() != end()) {
514 CFGBlock& Entry = getEntry();
515 OS << "\n [ B" << Entry.getBlockID() << " (ENTRY) ]\n";
516 Entry.print(OS);
517 }
518
Ted Kremenek4db5b452007-08-23 16:51:22 +0000519 // Iterate through the CFGBlocks and print them one by one.
Ted Kremenek97f75312007-08-21 21:42:03 +0000520 for (iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
Ted Kremenekbec06e82007-08-22 21:05:42 +0000521 // Skip the entry block, because we already printed it.
Ted Kremenek4db5b452007-08-23 16:51:22 +0000522 if (&(*I) == &getEntry() || &(*I) == &getExit()) continue;
Ted Kremenekbec06e82007-08-22 21:05:42 +0000523
Ted Kremenek4db5b452007-08-23 16:51:22 +0000524 OS << "\n [ B" << I->getBlockID() << " ]\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000525 I->print(OS);
526 }
Ted Kremenek4db5b452007-08-23 16:51:22 +0000527
528 // Print the Exit Block.
529 if (begin() != end()) {
530 CFGBlock& Exit = getExit();
531 OS << "\n [ B" << Exit.getBlockID() << " (EXIT) ]\n";
532 Exit.print(OS);
533 }
534
Ted Kremenek97f75312007-08-21 21:42:03 +0000535 OS << "\n";
536}
537
Ted Kremenekd8313202007-08-22 18:22:34 +0000538
539namespace {
540
541 class CFGBlockTerminatorPrint : public StmtVisitor<CFGBlockTerminatorPrint,
542 void > {
543 std::ostream& OS;
544 public:
545 CFGBlockTerminatorPrint(std::ostream& os) : OS(os) {}
546
547 void VisitIfStmt(IfStmt* I) {
548 OS << "if ";
549 I->getCond()->printPretty(std::cerr);
550 OS << "\n";
551 }
552
553 // Default case.
554 void VisitStmt(Stmt* S) { S->printPretty(OS); }
555
556 void VisitForStmt(ForStmt* F) {
557 OS << "for (" ;
558 if (Stmt* I = F->getInit()) I->printPretty(OS);
559 OS << " ; ";
560 if (Stmt* C = F->getCond()) C->printPretty(OS);
561 OS << " ; ";
562 if (Stmt* I = F->getInc()) I->printPretty(OS);
563 OS << ")\n";
Ted Kremenekbec06e82007-08-22 21:05:42 +0000564 }
565
566 void VisitWhileStmt(WhileStmt* W) {
567 OS << "while " ;
568 if (Stmt* C = W->getCond()) C->printPretty(OS);
569 OS << "\n";
570 }
Ted Kremenekd8313202007-08-22 18:22:34 +0000571 };
572}
573
Ted Kremenek4db5b452007-08-23 16:51:22 +0000574/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek97f75312007-08-21 21:42:03 +0000575void CFGBlock::dump() { print(std::cerr); }
576
Ted Kremenek4db5b452007-08-23 16:51:22 +0000577/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
578/// Generally this will only be called from CFG::print.
Ted Kremenek97f75312007-08-21 21:42:03 +0000579void CFGBlock::print(std::ostream& OS) {
580
581 // Iterate through the statements in the block and print them.
582 OS << " ------------------------\n";
583 unsigned j = 1;
584 for (iterator I = Stmts.begin(), E = Stmts.end() ; I != E ; ++I, ++j ) {
Ted Kremenekc5de2222007-08-21 23:26:17 +0000585 // Print the statement # in the basic block.
586 OS << " " << std::setw(3) << j << ": ";
587
588 // Print the statement/expression.
589 Stmt* S = *I;
590
591 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
592 OS << L->getName() << ": (LABEL)\n";
593 else
594 (*I)->printPretty(OS);
595
596 // Expressions need a newline.
Ted Kremenek97f75312007-08-21 21:42:03 +0000597 if (isa<Expr>(*I)) OS << '\n';
598 }
599 OS << " ------------------------\n";
600
601 // Print the predecessors of this block.
602 OS << " Predecessors (" << pred_size() << "):";
603 unsigned i = 0;
604 for (pred_iterator I = pred_begin(), E = pred_end(); I != E; ++I, ++i ) {
605 if (i == 8 || (i-8) == 0) {
606 OS << "\n ";
607 }
608 OS << " B" << (*I)->getBlockID();
609 }
610
611 // Print the terminator of this block.
612 OS << "\n Terminator: ";
Ted Kremenekd8313202007-08-22 18:22:34 +0000613 if (ControlFlowStmt)
614 CFGBlockTerminatorPrint(OS).Visit(ControlFlowStmt);
615 else
616 OS << "<NULL>\n";
Ted Kremenek97f75312007-08-21 21:42:03 +0000617
618 // Print the successors of this block.
619 OS << " Successors (" << succ_size() << "):";
620 i = 0;
621 for (succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I, ++i ) {
622 if (i == 8 || (i-8) % 10 == 0) {
623 OS << "\n ";
624 }
625 OS << " B" << (*I)->getBlockID();
626 }
627 OS << '\n';
Ted Kremenek4db5b452007-08-23 16:51:22 +0000628}