blob: befd84020d059a43818ad3f887022171503acedb [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//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek97f75312007-08-21 21:42:03 +00007//
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 Kremenek08176a52007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Ted Kremenekc5de2222007-08-21 23:26:17 +000019#include "llvm/ADT/DenseMap.h"
Ted Kremenek0edd3a92007-08-28 19:26:49 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000021#include "llvm/Support/GraphWriter.h"
Ted Kremenek56c939e2007-12-17 19:35:20 +000022#include "llvm/Support/Streams.h"
Ted Kremenek98cee3a2008-01-08 18:15:10 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek5ee98a72008-01-11 00:40:29 +000024#include <set>
Ted Kremenek97f75312007-08-21 21:42:03 +000025#include <iomanip>
26#include <algorithm>
Ted Kremenekb3bb91b2007-08-29 21:56:09 +000027#include <sstream>
28
Ted Kremenek5ee98a72008-01-11 00:40:29 +000029
Ted Kremenek97f75312007-08-21 21:42:03 +000030using namespace clang;
31
32namespace {
33
Ted Kremenekd6e50602007-08-23 21:26:19 +000034// SaveAndRestore - A utility class that uses RIIA to save and restore
35// the value of a variable.
36template<typename T>
Ted Kremenek98cee3a2008-01-08 18:15:10 +000037struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenekd6e50602007-08-23 21:26:19 +000038 SaveAndRestore(T& x) : X(x), old_value(x) {}
39 ~SaveAndRestore() { X = old_value; }
Ted Kremenek44db7872007-08-30 18:13:31 +000040 T get() { return old_value; }
41
Ted Kremenekd6e50602007-08-23 21:26:19 +000042 T& X;
43 T old_value;
44};
Ted Kremenek97f75312007-08-21 21:42:03 +000045
46/// CFGBuilder - This class is implements CFG construction from an AST.
47/// The builder is stateful: an instance of the builder should be used to only
48/// construct a single CFG.
49///
50/// Example usage:
51///
52/// CFGBuilder builder;
53/// CFG* cfg = builder.BuildAST(stmt1);
54///
Ted Kremenek95e854d2007-08-21 22:06:14 +000055/// CFG construction is done via a recursive walk of an AST.
56/// We actually parse the AST in reverse order so that the successor
57/// of a basic block is constructed prior to its predecessor. This
58/// allows us to nicely capture implicit fall-throughs without extra
59/// basic blocks.
60///
Ted Kremenek98cee3a2008-01-08 18:15:10 +000061class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenek97f75312007-08-21 21:42:03 +000062 CFG* cfg;
63 CFGBlock* Block;
Ted Kremenek97f75312007-08-21 21:42:03 +000064 CFGBlock* Succ;
Ted Kremenekf511d672007-08-22 21:36:54 +000065 CFGBlock* ContinueTargetBlock;
Ted Kremenekf308d372007-08-22 21:51:58 +000066 CFGBlock* BreakTargetBlock;
Ted Kremeneke809ebf2007-08-23 18:43:24 +000067 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek97f75312007-08-21 21:42:03 +000068
Ted Kremenek0edd3a92007-08-28 19:26:49 +000069 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenekc5de2222007-08-21 23:26:17 +000070 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
71 LabelMapTy LabelMap;
72
Ted Kremenek0edd3a92007-08-28 19:26:49 +000073 // A list of blocks that end with a "goto" that must be backpatched to
74 // their resolved targets upon completion of CFG construction.
Ted Kremenekf5392b72007-08-22 15:40:58 +000075 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenekc5de2222007-08-21 23:26:17 +000076 BackpatchBlocksTy BackpatchBlocks;
77
Ted Kremenek0edd3a92007-08-28 19:26:49 +000078 // A list of labels whose address has been taken (for indirect gotos).
79 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
80 LabelSetTy AddressTakenLabels;
81
Ted Kremenek97f75312007-08-21 21:42:03 +000082public:
Ted Kremenek4db5b452007-08-23 16:51:22 +000083 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenekf308d372007-08-22 21:51:58 +000084 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenek14594572007-09-05 20:02:05 +000085 SwitchTerminatedBlock(NULL) {
Ted Kremenek97f75312007-08-21 21:42:03 +000086 // Create an empty CFG.
87 cfg = new CFG();
88 }
89
90 ~CFGBuilder() { delete cfg; }
Ted Kremenek97f75312007-08-21 21:42:03 +000091
Ted Kremenek73543912007-08-23 21:42:29 +000092 // buildCFG - Used by external clients to construct the CFG.
93 CFG* buildCFG(Stmt* Statement);
Ted Kremenek95e854d2007-08-21 22:06:14 +000094
Ted Kremenek73543912007-08-23 21:42:29 +000095 // Visitors to walk an AST and construct the CFG. Called by
96 // buildCFG. Do not call directly!
Ted Kremenekd8313202007-08-22 18:22:34 +000097
Ted Kremenek73543912007-08-23 21:42:29 +000098 CFGBlock* VisitStmt(Stmt* Statement);
99 CFGBlock* VisitNullStmt(NullStmt* Statement);
100 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
101 CFGBlock* VisitIfStmt(IfStmt* I);
102 CFGBlock* VisitReturnStmt(ReturnStmt* R);
103 CFGBlock* VisitLabelStmt(LabelStmt* L);
104 CFGBlock* VisitGotoStmt(GotoStmt* G);
105 CFGBlock* VisitForStmt(ForStmt* F);
106 CFGBlock* VisitWhileStmt(WhileStmt* W);
107 CFGBlock* VisitDoStmt(DoStmt* D);
108 CFGBlock* VisitContinueStmt(ContinueStmt* C);
109 CFGBlock* VisitBreakStmt(BreakStmt* B);
110 CFGBlock* VisitSwitchStmt(SwitchStmt* S);
111 CFGBlock* VisitSwitchCase(SwitchCase* S);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000112 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek97f75312007-08-21 21:42:03 +0000113
Ted Kremenek73543912007-08-23 21:42:29 +0000114private:
115 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000116 CFGBlock* addStmt(Stmt* S);
117 CFGBlock* WalkAST(Stmt* S, bool AlwaysAddStmt);
118 CFGBlock* WalkAST_VisitChildren(Stmt* S);
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000119 CFGBlock* WalkAST_VisitDeclSubExprs(StmtIterator& I);
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000120 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* S);
Ted Kremenekd11620d2007-09-11 21:29:43 +0000121 CFGBlock* WalkAST_VisitCallExpr(CallExpr* C);
Ted Kremenek73543912007-08-23 21:42:29 +0000122 void FinishBlock(CFGBlock* B);
Ted Kremenekd8313202007-08-22 18:22:34 +0000123
Ted Kremenek97f75312007-08-21 21:42:03 +0000124};
Ted Kremenek73543912007-08-23 21:42:29 +0000125
126/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
127/// represent an arbitrary statement. Examples include a single expression
128/// or a function body (compound statement). The ownership of the returned
129/// CFG is transferred to the caller. If CFG construction fails, this method
130/// returns NULL.
131CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000132 assert (cfg);
Ted Kremenek73543912007-08-23 21:42:29 +0000133 if (!Statement) return NULL;
134
135 // Create an empty block that will serve as the exit block for the CFG.
136 // Since this is the first block added to the CFG, it will be implicitly
137 // registered as the exit block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000138 Succ = createBlock();
139 assert (Succ == &cfg->getExit());
140 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenek73543912007-08-23 21:42:29 +0000141
142 // Visit the statements and create the CFG.
143 if (CFGBlock* B = Visit(Statement)) {
144 // Finalize the last constructed block. This usually involves
145 // reversing the order of the statements in the block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000146 if (Block) FinishBlock(B);
Ted Kremenek73543912007-08-23 21:42:29 +0000147
148 // Backpatch the gotos whose label -> block mappings we didn't know
149 // when we encountered them.
150 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
151 E = BackpatchBlocks.end(); I != E; ++I ) {
152
153 CFGBlock* B = *I;
154 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
155 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
156
157 // If there is no target for the goto, then we are looking at an
158 // incomplete AST. Handle this by not registering a successor.
159 if (LI == LabelMap.end()) continue;
160
161 B->addSuccessor(LI->second);
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000162 }
Ted Kremenek73543912007-08-23 21:42:29 +0000163
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000164 // Add successors to the Indirect Goto Dispatch block (if we have one).
165 if (CFGBlock* B = cfg->getIndirectGotoBlock())
166 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
167 E = AddressTakenLabels.end(); I != E; ++I ) {
168
169 // Lookup the target block.
170 LabelMapTy::iterator LI = LabelMap.find(*I);
171
172 // If there is no target block that contains label, then we are looking
173 // at an incomplete AST. Handle this by not registering a successor.
174 if (LI == LabelMap.end()) continue;
175
176 B->addSuccessor(LI->second);
177 }
Ted Kremenek680fcb82007-09-26 21:23:31 +0000178
Ted Kremenek844cb4d2007-09-17 16:18:02 +0000179 Succ = B;
Ted Kremenek680fcb82007-09-26 21:23:31 +0000180 }
181
182 // Create an empty entry block that has no predecessors.
183 cfg->setEntry(createBlock());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000184
Ted Kremenek680fcb82007-09-26 21:23:31 +0000185 // NULL out cfg so that repeated calls to the builder will fail and that
186 // the ownership of the constructed CFG is passed to the caller.
187 CFG* t = cfg;
188 cfg = NULL;
189 return t;
Ted Kremenek73543912007-08-23 21:42:29 +0000190}
191
192/// createBlock - Used to lazily create blocks that are connected
193/// to the current (global) succcessor.
194CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek14594572007-09-05 20:02:05 +0000195 CFGBlock* B = cfg->createBlock();
Ted Kremenek73543912007-08-23 21:42:29 +0000196 if (add_successor && Succ) B->addSuccessor(Succ);
197 return B;
198}
199
200/// FinishBlock - When the last statement has been added to the block,
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000201/// we must reverse the statements because they have been inserted
202/// in reverse order.
Ted Kremenek73543912007-08-23 21:42:29 +0000203void CFGBuilder::FinishBlock(CFGBlock* B) {
204 assert (B);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000205 B->reverseStmts();
Ted Kremenek73543912007-08-23 21:42:29 +0000206}
207
Ted Kremenek65cfa562007-08-27 21:27:44 +0000208/// addStmt - Used to add statements/expressions to the current CFGBlock
209/// "Block". This method calls WalkAST on the passed statement to see if it
210/// contains any short-circuit expressions. If so, it recursively creates
211/// the necessary blocks for such expressions. It returns the "topmost" block
212/// of the created blocks, or the original value of "Block" when this method
213/// was called if no additional blocks are created.
214CFGBlock* CFGBuilder::addStmt(Stmt* S) {
Ted Kremenek390b9762007-08-30 18:39:40 +0000215 if (!Block) Block = createBlock();
Ted Kremenek65cfa562007-08-27 21:27:44 +0000216 return WalkAST(S,true);
217}
218
219/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremeneke822b622007-08-28 18:14:37 +0000220/// add extra blocks for ternary operators, &&, and ||. We also
221/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek65cfa562007-08-27 21:27:44 +0000222CFGBlock* CFGBuilder::WalkAST(Stmt* S, bool AlwaysAddStmt = false) {
223 switch (S->getStmtClass()) {
224 case Stmt::ConditionalOperatorClass: {
225 ConditionalOperator* C = cast<ConditionalOperator>(S);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000226
227 // Create the confluence block that will "merge" the results
228 // of the ternary expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000229 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
230 ConfluenceBlock->appendStmt(C);
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000231 FinishBlock(ConfluenceBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000232
233 // Create a block for the LHS expression if there is an LHS expression.
234 // A GCC extension allows LHS to be NULL, causing the condition to
235 // be the value that is returned instead.
236 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000237 Succ = ConfluenceBlock;
238 Block = NULL;
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000239 CFGBlock* LHSBlock = NULL;
240 if (C->getLHS()) {
241 LHSBlock = Visit(C->getLHS());
242 FinishBlock(LHSBlock);
243 Block = NULL;
244 }
Ted Kremenek65cfa562007-08-27 21:27:44 +0000245
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000246 // Create the block for the RHS expression.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000247 Succ = ConfluenceBlock;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000248 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000249 FinishBlock(RHSBlock);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000250
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000251 // Create the block that will contain the condition.
Ted Kremenek65cfa562007-08-27 21:27:44 +0000252 Block = createBlock(false);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000253
254 if (LHSBlock)
255 Block->addSuccessor(LHSBlock);
256 else {
257 // If we have no LHS expression, add the ConfluenceBlock as a direct
258 // successor for the block containing the condition. Moreover,
259 // we need to reverse the order of the predecessors in the
260 // ConfluenceBlock because the RHSBlock will have been added to
261 // the succcessors already, and we want the first predecessor to the
262 // the block containing the expression for the case when the ternary
263 // expression evaluates to true.
264 Block->addSuccessor(ConfluenceBlock);
265 assert (ConfluenceBlock->pred_size() == 2);
266 std::reverse(ConfluenceBlock->pred_begin(),
267 ConfluenceBlock->pred_end());
268 }
269
Ted Kremenek65cfa562007-08-27 21:27:44 +0000270 Block->addSuccessor(RHSBlock);
Ted Kremenekc0980cd2007-11-26 18:20:26 +0000271
Ted Kremenek65cfa562007-08-27 21:27:44 +0000272 Block->setTerminator(C);
273 return addStmt(C->getCond());
274 }
Ted Kremenek7f788422007-08-31 17:03:41 +0000275
276 case Stmt::ChooseExprClass: {
277 ChooseExpr* C = cast<ChooseExpr>(S);
278
279 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
280 ConfluenceBlock->appendStmt(C);
281 FinishBlock(ConfluenceBlock);
282
283 Succ = ConfluenceBlock;
284 Block = NULL;
285 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000286 FinishBlock(LHSBlock);
287
Ted Kremenek7f788422007-08-31 17:03:41 +0000288 Succ = ConfluenceBlock;
289 Block = NULL;
290 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekd11620d2007-09-11 21:29:43 +0000291 FinishBlock(RHSBlock);
Ted Kremenek7f788422007-08-31 17:03:41 +0000292
293 Block = createBlock(false);
294 Block->addSuccessor(LHSBlock);
295 Block->addSuccessor(RHSBlock);
296 Block->setTerminator(C);
297 return addStmt(C->getCond());
298 }
Ted Kremenek666a6af2007-08-28 16:18:58 +0000299
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000300 case Stmt::DeclStmtClass: {
301 ScopedDecl* D = cast<DeclStmt>(S)->getDecl();
302 Block->appendStmt(S);
303
304 StmtIterator I(D);
305 return WalkAST_VisitDeclSubExprs(I);
306 }
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000307
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000308 case Stmt::AddrLabelExprClass: {
309 AddrLabelExpr* A = cast<AddrLabelExpr>(S);
310 AddressTakenLabels.insert(A->getLabel());
311
312 if (AlwaysAddStmt) Block->appendStmt(S);
313 return Block;
314 }
Ted Kremenekd11620d2007-09-11 21:29:43 +0000315
316 case Stmt::CallExprClass:
317 return WalkAST_VisitCallExpr(cast<CallExpr>(S));
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000318
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000319 case Stmt::StmtExprClass:
320 return WalkAST_VisitStmtExpr(cast<StmtExpr>(S));
Ted Kremeneke822b622007-08-28 18:14:37 +0000321
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000322 case Stmt::UnaryOperatorClass: {
323 UnaryOperator* U = cast<UnaryOperator>(S);
324
325 // sizeof(expressions). For such expressions,
326 // the subexpression is not really evaluated, so
327 // we don't care about control-flow within the sizeof.
328 if (U->getOpcode() == UnaryOperator::SizeOf) {
329 Block->appendStmt(S);
330 return Block;
331 }
332
333 break;
334 }
335
Ted Kremenekcfaae762007-08-27 21:54:41 +0000336 case Stmt::BinaryOperatorClass: {
337 BinaryOperator* B = cast<BinaryOperator>(S);
338
339 if (B->isLogicalOp()) { // && or ||
340 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
341 ConfluenceBlock->appendStmt(B);
342 FinishBlock(ConfluenceBlock);
343
344 // create the block evaluating the LHS
345 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekb2348522007-12-21 19:49:00 +0000346 LHSBlock->setTerminator(B);
Ted Kremenekcfaae762007-08-27 21:54:41 +0000347
348 // create the block evaluating the RHS
349 Succ = ConfluenceBlock;
350 Block = NULL;
351 CFGBlock* RHSBlock = Visit(B->getRHS());
Ted Kremenekb2348522007-12-21 19:49:00 +0000352
353 // Now link the LHSBlock with RHSBlock.
354 if (B->getOpcode() == BinaryOperator::LOr) {
355 LHSBlock->addSuccessor(ConfluenceBlock);
356 LHSBlock->addSuccessor(RHSBlock);
357 }
358 else {
359 assert (B->getOpcode() == BinaryOperator::LAnd);
360 LHSBlock->addSuccessor(RHSBlock);
361 LHSBlock->addSuccessor(ConfluenceBlock);
362 }
Ted Kremenekcfaae762007-08-27 21:54:41 +0000363
364 // Generate the blocks for evaluating the LHS.
365 Block = LHSBlock;
366 return addStmt(B->getLHS());
Ted Kremeneke822b622007-08-28 18:14:37 +0000367 }
368 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
369 Block->appendStmt(B);
370 addStmt(B->getRHS());
371 return addStmt(B->getLHS());
Ted Kremenek3a819822007-10-01 19:33:33 +0000372 }
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000373
374 break;
Ted Kremenekcfaae762007-08-27 21:54:41 +0000375 }
376
Ted Kremenek65cfa562007-08-27 21:27:44 +0000377 default:
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000378 break;
Ted Kremenek65cfa562007-08-27 21:27:44 +0000379 };
Ted Kremenekd1a80f72007-12-13 22:44:18 +0000380
381 if (AlwaysAddStmt) Block->appendStmt(S);
382 return WalkAST_VisitChildren(S);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000383}
384
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000385/// WalkAST_VisitDeclSubExprs - Utility method to handle Decls contained in
386/// DeclStmts. Because the initialization code (and sometimes the
387/// the type declarations) for DeclStmts can contain arbitrary expressions,
388/// we must linearize declarations to handle arbitrary control-flow induced by
389/// those expressions.
390CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExprs(StmtIterator& I) {
Ted Kremenekf4e35622007-11-18 20:06:01 +0000391 if (I == StmtIterator())
392 return Block;
393
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000394 Stmt* S = *I;
395 ++I;
Ted Kremenekf4e35622007-11-18 20:06:01 +0000396 WalkAST_VisitDeclSubExprs(I);
Ted Kremenek7c6c0fd2007-10-30 21:48:34 +0000397
398 Block = addStmt(S);
Ted Kremeneke822b622007-08-28 18:14:37 +0000399 return Block;
400}
401
Ted Kremenek65cfa562007-08-27 21:27:44 +0000402/// WalkAST_VisitChildren - Utility method to call WalkAST on the
403/// children of a Stmt.
Ted Kremenekcfaae762007-08-27 21:54:41 +0000404CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* S) {
Ted Kremenek65cfa562007-08-27 21:27:44 +0000405 CFGBlock* B = Block;
406 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
407 I != E; ++I)
Ted Kremenek680fcb82007-09-26 21:23:31 +0000408 if (*I) B = WalkAST(*I);
Ted Kremenek65cfa562007-08-27 21:27:44 +0000409
410 return B;
411}
412
Ted Kremenek6fca3e02007-08-28 18:30:10 +0000413/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
414/// expressions (a GCC extension).
415CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* S) {
416 Block->appendStmt(S);
417 return VisitCompoundStmt(S->getSubStmt());
418}
419
Ted Kremenekd11620d2007-09-11 21:29:43 +0000420/// WalkAST_VisitCallExpr - Utility method to handle function calls that
421/// are nested in expressions. The idea is that each function call should
422/// appear as a distinct statement in the CFGBlock.
423CFGBlock* CFGBuilder::WalkAST_VisitCallExpr(CallExpr* C) {
424 Block->appendStmt(C);
425 return WalkAST_VisitChildren(C);
426}
427
Ted Kremenek73543912007-08-23 21:42:29 +0000428/// VisitStmt - Handle statements with no branching control flow.
429CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
430 // We cannot assume that we are in the middle of a basic block, since
431 // the CFG might only be constructed for this single statement. If
432 // we have no current basic block, just create one lazily.
433 if (!Block) Block = createBlock();
434
435 // Simply add the statement to the current block. We actually
436 // insert statements in reverse order; this order is reversed later
437 // when processing the containing element in the AST.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000438 addStmt(Statement);
439
Ted Kremenek73543912007-08-23 21:42:29 +0000440 return Block;
441}
442
443CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
444 return Block;
445}
446
447CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
448 // The value returned from this function is the last created CFGBlock
449 // that represents the "entry" point for the translated AST node.
Chris Lattner265c8172007-09-27 15:15:46 +0000450 CFGBlock* LastBlock = 0;
Ted Kremenek73543912007-08-23 21:42:29 +0000451
452 for (CompoundStmt::reverse_body_iterator I = C->body_rbegin(),
453 E = C->body_rend(); I != E; ++I )
454 // Add the statement to the current block.
455 if (!(LastBlock=Visit(*I)))
456 return NULL;
457
458 return LastBlock;
459}
460
461CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
462 // We may see an if statement in the middle of a basic block, or
463 // it may be the first statement we are processing. In either case,
464 // we create a new basic block. First, we create the blocks for
465 // the then...else statements, and then we create the block containing
466 // the if statement. If we were in the middle of a block, we
467 // stop processing that block and reverse its statements. That block
468 // is then the implicit successor for the "then" and "else" clauses.
469
470 // The block we were proccessing is now finished. Make it the
471 // successor block.
472 if (Block) {
473 Succ = Block;
474 FinishBlock(Block);
475 }
476
477 // Process the false branch. NULL out Block so that the recursive
478 // call to Visit will create a new basic block.
479 // Null out Block so that all successor
480 CFGBlock* ElseBlock = Succ;
481
482 if (Stmt* Else = I->getElse()) {
483 SaveAndRestore<CFGBlock*> sv(Succ);
484
485 // NULL out Block so that the recursive call to Visit will
486 // create a new basic block.
487 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000488 ElseBlock = Visit(Else);
489
490 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
491 ElseBlock = sv.get();
492 else if (Block)
493 FinishBlock(ElseBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000494 }
495
496 // Process the true branch. NULL out Block so that the recursive
497 // call to Visit will create a new basic block.
498 // Null out Block so that all successor
499 CFGBlock* ThenBlock;
500 {
501 Stmt* Then = I->getThen();
502 assert (Then);
503 SaveAndRestore<CFGBlock*> sv(Succ);
504 Block = NULL;
Ted Kremenek44db7872007-08-30 18:13:31 +0000505 ThenBlock = Visit(Then);
506
507 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
508 ThenBlock = sv.get();
509 else if (Block)
510 FinishBlock(ThenBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000511 }
512
513 // Now create a new block containing the if statement.
514 Block = createBlock(false);
Ted Kremenek73543912007-08-23 21:42:29 +0000515
516 // Set the terminator of the new block to the If statement.
517 Block->setTerminator(I);
518
519 // Now add the successors.
520 Block->addSuccessor(ThenBlock);
521 Block->addSuccessor(ElseBlock);
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000522
523 // Add the condition as the last statement in the new block. This
524 // may create new blocks as the condition may contain control-flow. Any
525 // newly created blocks will be pointed to be "Block".
526 return addStmt(I->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000527}
Ted Kremenekd11620d2007-09-11 21:29:43 +0000528
Ted Kremenek73543912007-08-23 21:42:29 +0000529
530CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
531 // If we were in the middle of a block we stop processing that block
532 // and reverse its statements.
533 //
534 // NOTE: If a "return" appears in the middle of a block, this means
535 // that the code afterwards is DEAD (unreachable). We still
536 // keep a basic block for that code; a simple "mark-and-sweep"
537 // from the entry block will be able to report such dead
538 // blocks.
539 if (Block) FinishBlock(Block);
540
541 // Create the new block.
542 Block = createBlock(false);
543
544 // The Exit block is the only successor.
545 Block->addSuccessor(&cfg->getExit());
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000546
547 // Add the return statement to the block. This may create new blocks
548 // if R contains control-flow (short-circuit operations).
549 return addStmt(R);
Ted Kremenek73543912007-08-23 21:42:29 +0000550}
551
552CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
553 // Get the block of the labeled statement. Add it to our map.
554 CFGBlock* LabelBlock = Visit(L->getSubStmt());
Ted Kremenek9b0d1b62007-08-30 18:20:57 +0000555
556 if (!LabelBlock) // This can happen when the body is empty, i.e.
557 LabelBlock=createBlock(); // scopes that only contains NullStmts.
558
Ted Kremenek73543912007-08-23 21:42:29 +0000559 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
560 LabelMap[ L ] = LabelBlock;
561
562 // Labels partition blocks, so this is the end of the basic block
Ted Kremenekec055e12007-08-29 23:20:49 +0000563 // we were processing (L is the block's label). Because this is
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000564 // label (and we have already processed the substatement) there is no
565 // extra control-flow to worry about.
Ted Kremenekec055e12007-08-29 23:20:49 +0000566 LabelBlock->setLabel(L);
Ted Kremenek73543912007-08-23 21:42:29 +0000567 FinishBlock(LabelBlock);
568
569 // We set Block to NULL to allow lazy creation of a new block
570 // (if necessary);
571 Block = NULL;
572
573 // This block is now the implicit successor of other blocks.
574 Succ = LabelBlock;
575
576 return LabelBlock;
577}
578
579CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
580 // Goto is a control-flow statement. Thus we stop processing the
581 // current block and create a new one.
582 if (Block) FinishBlock(Block);
583 Block = createBlock(false);
584 Block->setTerminator(G);
585
586 // If we already know the mapping to the label block add the
587 // successor now.
588 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
589
590 if (I == LabelMap.end())
591 // We will need to backpatch this block later.
592 BackpatchBlocks.push_back(Block);
593 else
594 Block->addSuccessor(I->second);
595
596 return Block;
597}
598
599CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
600 // "for" is a control-flow statement. Thus we stop processing the
601 // current block.
602
603 CFGBlock* LoopSuccessor = NULL;
604
605 if (Block) {
606 FinishBlock(Block);
607 LoopSuccessor = Block;
608 }
609 else LoopSuccessor = Succ;
610
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000611 // Because of short-circuit evaluation, the condition of the loop
612 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
613 // blocks that evaluate the condition.
614 CFGBlock* ExitConditionBlock = createBlock(false);
615 CFGBlock* EntryConditionBlock = ExitConditionBlock;
616
617 // Set the terminator for the "exit" condition block.
618 ExitConditionBlock->setTerminator(F);
619
620 // Now add the actual condition to the condition block. Because the
621 // condition itself may contain control-flow, new blocks may be created.
622 if (Stmt* C = F->getCond()) {
623 Block = ExitConditionBlock;
624 EntryConditionBlock = addStmt(C);
625 if (Block) FinishBlock(EntryConditionBlock);
626 }
Ted Kremenek73543912007-08-23 21:42:29 +0000627
628 // The condition block is the implicit successor for the loop body as
629 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000630 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000631
632 // Now create the loop body.
633 {
634 assert (F->getBody());
635
636 // Save the current values for Block, Succ, and continue and break targets
637 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
638 save_continue(ContinueTargetBlock),
639 save_break(BreakTargetBlock);
640
641 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000642 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000643
644 // All breaks should go to the code following the loop.
645 BreakTargetBlock = LoopSuccessor;
646
Ted Kremenek390b9762007-08-30 18:39:40 +0000647 // Create a new block to contain the (bottom) of the loop body.
648 Block = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000649
650 // If we have increment code, insert it at the end of the body block.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000651 if (Stmt* I = F->getInc()) Block = addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000652
653 // Now populate the body block, and in the process create new blocks
654 // as we walk the body of the loop.
655 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000656
657 if (!BodyBlock)
658 BodyBlock = ExitConditionBlock; // can happen for "for (...;...; ) ;"
659 else if (Block)
660 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000661
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000662 // This new body block is a successor to our "exit" condition block.
663 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000664 }
665
666 // Link up the condition block with the code that follows the loop.
667 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000668 ExitConditionBlock->addSuccessor(LoopSuccessor);
669
Ted Kremenek73543912007-08-23 21:42:29 +0000670 // If the loop contains initialization, create a new block for those
671 // statements. This block can also contain statements that precede
672 // the loop.
673 if (Stmt* I = F->getInit()) {
674 Block = createBlock();
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000675 return addStmt(I);
Ted Kremenek73543912007-08-23 21:42:29 +0000676 }
677 else {
678 // There is no loop initialization. We are thus basically a while
679 // loop. NULL out Block to force lazy block construction.
680 Block = NULL;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000681 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000682 }
683}
684
685CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
686 // "while" is a control-flow statement. Thus we stop processing the
687 // current block.
688
689 CFGBlock* LoopSuccessor = NULL;
690
691 if (Block) {
692 FinishBlock(Block);
693 LoopSuccessor = Block;
694 }
695 else LoopSuccessor = Succ;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000696
697 // Because of short-circuit evaluation, the condition of the loop
698 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
699 // blocks that evaluate the condition.
700 CFGBlock* ExitConditionBlock = createBlock(false);
701 CFGBlock* EntryConditionBlock = ExitConditionBlock;
702
703 // Set the terminator for the "exit" condition block.
704 ExitConditionBlock->setTerminator(W);
705
706 // Now add the actual condition to the condition block. Because the
707 // condition itself may contain control-flow, new blocks may be created.
708 // Thus we update "Succ" after adding the condition.
709 if (Stmt* C = W->getCond()) {
710 Block = ExitConditionBlock;
711 EntryConditionBlock = addStmt(C);
712 if (Block) FinishBlock(EntryConditionBlock);
713 }
Ted Kremenek73543912007-08-23 21:42:29 +0000714
715 // The condition block is the implicit successor for the loop body as
716 // well as any code above the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000717 Succ = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000718
719 // Process the loop body.
720 {
721 assert (W->getBody());
722
723 // Save the current values for Block, Succ, and continue and break targets
724 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
725 save_continue(ContinueTargetBlock),
726 save_break(BreakTargetBlock);
727
728 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000729 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000730
731 // All breaks should go to the code following the loop.
732 BreakTargetBlock = LoopSuccessor;
733
734 // NULL out Block to force lazy instantiation of blocks for the body.
735 Block = NULL;
736
737 // Create the body. The returned block is the entry to the loop body.
738 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenek390b9762007-08-30 18:39:40 +0000739
740 if (!BodyBlock)
741 BodyBlock = ExitConditionBlock; // can happen for "while(...) ;"
742 else if (Block)
743 FinishBlock(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000744
745 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000746 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000747 }
748
749 // Link up the condition block with the code that follows the loop.
750 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000751 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000752
753 // There can be no more statements in the condition block
754 // since we loop back to this block. NULL out Block to force
755 // lazy creation of another block.
756 Block = NULL;
757
758 // Return the condition block, which is the dominating block for the loop.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000759 return EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000760}
761
762CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
763 // "do...while" is a control-flow statement. Thus we stop processing the
764 // current block.
765
766 CFGBlock* LoopSuccessor = NULL;
767
768 if (Block) {
769 FinishBlock(Block);
770 LoopSuccessor = Block;
771 }
772 else LoopSuccessor = Succ;
773
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000774 // Because of short-circuit evaluation, the condition of the loop
775 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
776 // blocks that evaluate the condition.
777 CFGBlock* ExitConditionBlock = createBlock(false);
778 CFGBlock* EntryConditionBlock = ExitConditionBlock;
779
780 // Set the terminator for the "exit" condition block.
781 ExitConditionBlock->setTerminator(D);
Ted Kremenek73543912007-08-23 21:42:29 +0000782
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000783 // Now add the actual condition to the condition block. Because the
784 // condition itself may contain control-flow, new blocks may be created.
785 if (Stmt* C = D->getCond()) {
786 Block = ExitConditionBlock;
787 EntryConditionBlock = addStmt(C);
788 if (Block) FinishBlock(EntryConditionBlock);
789 }
Ted Kremenek73543912007-08-23 21:42:29 +0000790
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000791 // The condition block is the implicit successor for the loop body as
792 // well as any code above the loop.
793 Succ = EntryConditionBlock;
794
795
Ted Kremenek73543912007-08-23 21:42:29 +0000796 // Process the loop body.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000797 CFGBlock* BodyBlock = NULL;
Ted Kremenek73543912007-08-23 21:42:29 +0000798 {
799 assert (D->getBody());
800
801 // Save the current values for Block, Succ, and continue and break targets
802 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
803 save_continue(ContinueTargetBlock),
804 save_break(BreakTargetBlock);
805
806 // All continues within this loop should go to the condition block
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000807 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenek73543912007-08-23 21:42:29 +0000808
809 // All breaks should go to the code following the loop.
810 BreakTargetBlock = LoopSuccessor;
811
812 // NULL out Block to force lazy instantiation of blocks for the body.
813 Block = NULL;
814
815 // Create the body. The returned block is the entry to the loop body.
816 BodyBlock = Visit(D->getBody());
Ted Kremenek73543912007-08-23 21:42:29 +0000817
Ted Kremenek390b9762007-08-30 18:39:40 +0000818 if (!BodyBlock)
819 BodyBlock = ExitConditionBlock; // can happen for "do ; while(...)"
820 else if (Block)
821 FinishBlock(BodyBlock);
822
Ted Kremenek73543912007-08-23 21:42:29 +0000823 // Add the loop body entry as a successor to the condition.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000824 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenek73543912007-08-23 21:42:29 +0000825 }
826
827 // Link up the condition block with the code that follows the loop.
828 // (the false branch).
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000829 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenek73543912007-08-23 21:42:29 +0000830
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000831 // There can be no more statements in the body block(s)
832 // since we loop back to the body. NULL out Block to force
Ted Kremenek73543912007-08-23 21:42:29 +0000833 // lazy creation of another block.
834 Block = NULL;
835
836 // Return the loop body, which is the dominating block for the loop.
837 return BodyBlock;
838}
839
840CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
841 // "continue" is a control-flow statement. Thus we stop processing the
842 // current block.
843 if (Block) FinishBlock(Block);
844
845 // Now create a new block that ends with the continue statement.
846 Block = createBlock(false);
847 Block->setTerminator(C);
848
849 // If there is no target for the continue, then we are looking at an
850 // incomplete AST. Handle this by not registering a successor.
851 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
852
853 return Block;
854}
855
856CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
857 // "break" is a control-flow statement. Thus we stop processing the
858 // current block.
859 if (Block) FinishBlock(Block);
860
861 // Now create a new block that ends with the continue statement.
862 Block = createBlock(false);
863 Block->setTerminator(B);
864
865 // If there is no target for the break, then we are looking at an
866 // incomplete AST. Handle this by not registering a successor.
867 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
868
869 return Block;
870}
871
872CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* S) {
873 // "switch" is a control-flow statement. Thus we stop processing the
874 // current block.
875 CFGBlock* SwitchSuccessor = NULL;
876
877 if (Block) {
878 FinishBlock(Block);
879 SwitchSuccessor = Block;
880 }
881 else SwitchSuccessor = Succ;
882
883 // Save the current "switch" context.
884 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
885 save_break(BreakTargetBlock);
886
887 // Create a new block that will contain the switch statement.
888 SwitchTerminatedBlock = createBlock(false);
889
Ted Kremenek73543912007-08-23 21:42:29 +0000890 // Now process the switch body. The code after the switch is the implicit
891 // successor.
892 Succ = SwitchSuccessor;
893 BreakTargetBlock = SwitchSuccessor;
Ted Kremenek73543912007-08-23 21:42:29 +0000894
895 // When visiting the body, the case statements should automatically get
896 // linked up to the switch. We also don't keep a pointer to the body,
897 // since all control-flow from the switch goes to case/default statements.
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000898 assert (S->getBody() && "switch must contain a non-NULL body");
899 Block = NULL;
900 CFGBlock *BodyBlock = Visit(S->getBody());
901 if (Block) FinishBlock(BodyBlock);
902
903 // Add the terminator and condition in the switch block.
904 SwitchTerminatedBlock->setTerminator(S);
905 assert (S->getCond() && "switch condition must be non-NULL");
Ted Kremenek73543912007-08-23 21:42:29 +0000906 Block = SwitchTerminatedBlock;
Ted Kremenekcfee50c2007-08-27 19:46:09 +0000907 return addStmt(S->getCond());
Ted Kremenek73543912007-08-23 21:42:29 +0000908}
909
910CFGBlock* CFGBuilder::VisitSwitchCase(SwitchCase* S) {
911 // A SwitchCase is either a "default" or "case" statement. We handle
912 // both in the same way. They are essentially labels, so they are the
913 // first statement in a block.
Ted Kremenek44659d82007-08-30 18:48:11 +0000914
915 if (S->getSubStmt()) Visit(S->getSubStmt());
916 CFGBlock* CaseBlock = Block;
917 if (!CaseBlock) CaseBlock = createBlock();
918
Ted Kremenekec055e12007-08-29 23:20:49 +0000919 // Cases/Default statements partition block, so this is the top of
920 // the basic block we were processing (the case/default is the label).
921 CaseBlock->setLabel(S);
Ted Kremenek73543912007-08-23 21:42:29 +0000922 FinishBlock(CaseBlock);
923
924 // Add this block to the list of successors for the block with the
925 // switch statement.
926 if (SwitchTerminatedBlock) SwitchTerminatedBlock->addSuccessor(CaseBlock);
927
928 // We set Block to NULL to allow lazy creation of a new block (if necessary)
929 Block = NULL;
930
931 // This block is now the implicit successor of other blocks.
932 Succ = CaseBlock;
933
934 return CaseBlock;
935}
936
Ted Kremenek0edd3a92007-08-28 19:26:49 +0000937CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
938 // Lazily create the indirect-goto dispatch block if there isn't one
939 // already.
940 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
941
942 if (!IBlock) {
943 IBlock = createBlock(false);
944 cfg->setIndirectGotoBlock(IBlock);
945 }
946
947 // IndirectGoto is a control-flow statement. Thus we stop processing the
948 // current block and create a new one.
949 if (Block) FinishBlock(Block);
950 Block = createBlock(false);
951 Block->setTerminator(I);
952 Block->addSuccessor(IBlock);
953 return addStmt(I->getTarget());
954}
955
Ted Kremenek73543912007-08-23 21:42:29 +0000956
Ted Kremenekd6e50602007-08-23 21:26:19 +0000957} // end anonymous namespace
Ted Kremenek4db5b452007-08-23 16:51:22 +0000958
959/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
960/// block has no successors or predecessors. If this is the first block
961/// created in the CFG, it is automatically set to be the Entry and Exit
962/// of the CFG.
Ted Kremenek14594572007-09-05 20:02:05 +0000963CFGBlock* CFG::createBlock() {
Ted Kremenek4db5b452007-08-23 16:51:22 +0000964 bool first_block = begin() == end();
965
966 // Create the block.
Ted Kremenek14594572007-09-05 20:02:05 +0000967 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek4db5b452007-08-23 16:51:22 +0000968
969 // If this is the first block, set it as the Entry and Exit.
970 if (first_block) Entry = Exit = &front();
971
972 // Return the block.
973 return &front();
Ted Kremenek97f75312007-08-21 21:42:03 +0000974}
975
Ted Kremenek4db5b452007-08-23 16:51:22 +0000976/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
977/// CFG is returned to the caller.
978CFG* CFG::buildCFG(Stmt* Statement) {
979 CFGBuilder Builder;
980 return Builder.buildCFG(Statement);
981}
982
983/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenek97f75312007-08-21 21:42:03 +0000984void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
985
Ted Kremenek3a819822007-10-01 19:33:33 +0000986//===----------------------------------------------------------------------===//
987// CFG: Queries for BlkExprs.
988//===----------------------------------------------------------------------===//
Ted Kremenekb3bb91b2007-08-29 21:56:09 +0000989
Ted Kremenek3a819822007-10-01 19:33:33 +0000990namespace {
991 typedef llvm::DenseMap<const Expr*,unsigned> BlkExprMapTy;
992}
993
994static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
995 BlkExprMapTy* M = new BlkExprMapTy();
996
997 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
998 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek962212d2007-12-10 23:58:39 +0000999 if (const Expr* E = dyn_cast<Expr>(*BI)) {
1000 unsigned x = M->size();
1001 (*M)[E] = x;
1002 }
Ted Kremenek3a819822007-10-01 19:33:33 +00001003
1004 return M;
1005}
1006
1007bool CFG::isBlkExpr(const Stmt* S) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +00001008 assert (S != NULL);
Ted Kremenek3a819822007-10-01 19:33:33 +00001009 if (const Expr* E = dyn_cast<Expr>(S)) return getBlkExprNum(E);
1010 else return true; // Statements are by default "block-level expressions."
1011}
1012
1013CFG::BlkExprNumTy CFG::getBlkExprNum(const Expr* E) {
Ted Kremenek8ce772b2007-10-01 20:33:52 +00001014 assert(E != NULL);
Ted Kremenek3a819822007-10-01 19:33:33 +00001015 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1016
1017 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
1018 BlkExprMapTy::iterator I = M->find(E);
1019
1020 if (I == M->end()) return CFG::BlkExprNumTy();
1021 else return CFG::BlkExprNumTy(I->second);
1022}
1023
1024unsigned CFG::getNumBlkExprs() {
1025 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1026 return M->size();
1027 else {
1028 // We assume callers interested in the number of BlkExprs will want
1029 // the map constructed if it doesn't already exist.
1030 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1031 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1032 }
1033}
1034
Ted Kremenek5ee98a72008-01-11 00:40:29 +00001035typedef std::set<std::pair<CFGBlock*,CFGBlock*> > BlkEdgeSetTy;
1036
1037const std::pair<CFGBlock*,CFGBlock*>*
1038CFG::getBlockEdgeImpl(const CFGBlock* B1, const CFGBlock* B2) {
1039
1040 BlkEdgeSetTy*& p = reinterpret_cast<BlkEdgeSetTy*&>(BlkEdgeSet);
1041 if (!p) p = new BlkEdgeSetTy();
1042
1043 return &*(p->insert(std::make_pair(const_cast<CFGBlock*>(B1),
1044 const_cast<CFGBlock*>(B2))).first);
1045}
1046
Ted Kremenek3a819822007-10-01 19:33:33 +00001047CFG::~CFG() {
1048 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
Ted Kremenek5ee98a72008-01-11 00:40:29 +00001049 delete reinterpret_cast<BlkEdgeSetTy*>(BlkEdgeSet);
Ted Kremenek3a819822007-10-01 19:33:33 +00001050}
1051
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001052//===----------------------------------------------------------------------===//
1053// CFG pretty printing
1054//===----------------------------------------------------------------------===//
1055
Ted Kremenekd8313202007-08-22 18:22:34 +00001056namespace {
1057
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001058class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek86afc042007-08-31 22:26:13 +00001059
Ted Kremenek08176a52007-08-31 21:30:12 +00001060 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1061 StmtMapTy StmtMap;
1062 signed CurrentBlock;
1063 unsigned CurrentStmt;
Ted Kremenek86afc042007-08-31 22:26:13 +00001064
Ted Kremenek73543912007-08-23 21:42:29 +00001065public:
Ted Kremenek86afc042007-08-31 22:26:13 +00001066
Ted Kremenek08176a52007-08-31 21:30:12 +00001067 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1068 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1069 unsigned j = 1;
1070 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1071 BI != BEnd; ++BI, ++j )
1072 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1073 }
1074 }
1075
1076 virtual ~StmtPrinterHelper() {}
1077
1078 void setBlockID(signed i) { CurrentBlock = i; }
1079 void setStmtID(unsigned i) { CurrentStmt = i; }
1080
Ted Kremenek86afc042007-08-31 22:26:13 +00001081 virtual bool handledStmt(Stmt* S, std::ostream& OS) {
1082
1083 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek08176a52007-08-31 21:30:12 +00001084
1085 if (I == StmtMap.end())
1086 return false;
1087
1088 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1089 && I->second.second == CurrentStmt)
1090 return false;
1091
Ted Kremenek86afc042007-08-31 22:26:13 +00001092 OS << "[B" << I->second.first << "." << I->second.second << "]";
1093 return true;
Ted Kremenek08176a52007-08-31 21:30:12 +00001094 }
1095};
1096
Ted Kremenek98cee3a2008-01-08 18:15:10 +00001097class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1098 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1099
Ted Kremenek08176a52007-08-31 21:30:12 +00001100 std::ostream& OS;
1101 StmtPrinterHelper* Helper;
1102public:
1103 CFGBlockTerminatorPrint(std::ostream& os, StmtPrinterHelper* helper)
1104 : OS(os), Helper(helper) {}
Ted Kremenek73543912007-08-23 21:42:29 +00001105
1106 void VisitIfStmt(IfStmt* I) {
1107 OS << "if ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001108 I->getCond()->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001109 OS << "\n";
1110 }
1111
1112 // Default case.
Ted Kremenek621e1592007-08-31 21:49:40 +00001113 void VisitStmt(Stmt* S) { S->printPretty(OS); }
Ted Kremenek73543912007-08-23 21:42:29 +00001114
1115 void VisitForStmt(ForStmt* F) {
1116 OS << "for (" ;
Ted Kremenek23a1d662007-08-30 21:28:02 +00001117 if (F->getInit()) OS << "...";
1118 OS << "; ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001119 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek23a1d662007-08-30 21:28:02 +00001120 OS << "; ";
1121 if (F->getInc()) OS << "...";
Ted Kremenek73543912007-08-23 21:42:29 +00001122 OS << ")\n";
1123 }
1124
1125 void VisitWhileStmt(WhileStmt* W) {
1126 OS << "while " ;
Ted Kremenek08176a52007-08-31 21:30:12 +00001127 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenek73543912007-08-23 21:42:29 +00001128 OS << "\n";
1129 }
1130
1131 void VisitDoStmt(DoStmt* D) {
1132 OS << "do ... while ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001133 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001134 OS << '\n';
1135 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001136
Ted Kremenek65cfa562007-08-27 21:27:44 +00001137 void VisitSwitchStmt(SwitchStmt* S) {
1138 OS << "switch ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001139 S->getCond()->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001140 OS << '\n';
1141 }
1142
Ted Kremenek621e1592007-08-31 21:49:40 +00001143 void VisitConditionalOperator(ConditionalOperator* C) {
1144 C->getCond()->printPretty(OS,Helper);
1145 OS << " ? ... : ...\n";
1146 }
1147
Ted Kremenek2025cc92007-08-31 22:29:13 +00001148 void VisitChooseExpr(ChooseExpr* C) {
1149 OS << "__builtin_choose_expr( ";
1150 C->getCond()->printPretty(OS,Helper);
1151 OS << " )\n";
1152 }
1153
Ted Kremenek86afc042007-08-31 22:26:13 +00001154 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1155 OS << "goto *";
1156 I->getTarget()->printPretty(OS,Helper);
1157 OS << '\n';
1158 }
1159
Ted Kremenek621e1592007-08-31 21:49:40 +00001160 void VisitBinaryOperator(BinaryOperator* B) {
1161 if (!B->isLogicalOp()) {
1162 VisitExpr(B);
1163 return;
1164 }
1165
1166 B->getLHS()->printPretty(OS,Helper);
1167
1168 switch (B->getOpcode()) {
1169 case BinaryOperator::LOr:
1170 OS << " || ...\n";
1171 return;
1172 case BinaryOperator::LAnd:
1173 OS << " && ...\n";
1174 return;
1175 default:
1176 assert(false && "Invalid logical operator.");
1177 }
1178 }
1179
Ted Kremenekcfaae762007-08-27 21:54:41 +00001180 void VisitExpr(Expr* E) {
Ted Kremenek08176a52007-08-31 21:30:12 +00001181 E->printPretty(OS,Helper);
Ted Kremenek65cfa562007-08-27 21:27:44 +00001182 OS << '\n';
Ted Kremenekcfaae762007-08-27 21:54:41 +00001183 }
Ted Kremenek73543912007-08-23 21:42:29 +00001184};
Ted Kremenek08176a52007-08-31 21:30:12 +00001185
1186
Ted Kremenek86afc042007-08-31 22:26:13 +00001187void print_stmt(std::ostream&OS, StmtPrinterHelper* Helper, Stmt* S) {
1188 if (Helper) {
1189 // special printing for statement-expressions.
1190 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
1191 CompoundStmt* Sub = SE->getSubStmt();
1192
1193 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001194 OS << "({ ... ; ";
Ted Kremenek256a2592007-10-29 20:41:04 +00001195 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek16e3b9a2007-08-31 22:47:06 +00001196 OS << " })\n";
Ted Kremenek86afc042007-08-31 22:26:13 +00001197 return;
1198 }
1199 }
1200
1201 // special printing for comma expressions.
1202 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
1203 if (B->getOpcode() == BinaryOperator::Comma) {
1204 OS << "... , ";
1205 Helper->handledStmt(B->getRHS(),OS);
1206 OS << '\n';
1207 return;
1208 }
1209 }
1210 }
1211
1212 S->printPretty(OS, Helper);
1213
1214 // Expressions need a newline.
1215 if (isa<Expr>(S)) OS << '\n';
1216}
1217
Ted Kremenek08176a52007-08-31 21:30:12 +00001218void print_block(std::ostream& OS, const CFG* cfg, const CFGBlock& B,
1219 StmtPrinterHelper* Helper, bool print_edges) {
1220
1221 if (Helper) Helper->setBlockID(B.getBlockID());
1222
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001223 // Print the header.
Ted Kremenek08176a52007-08-31 21:30:12 +00001224 OS << "\n [ B" << B.getBlockID();
1225
1226 if (&B == &cfg->getEntry())
1227 OS << " (ENTRY) ]\n";
1228 else if (&B == &cfg->getExit())
1229 OS << " (EXIT) ]\n";
1230 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001231 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek08176a52007-08-31 21:30:12 +00001232 else
1233 OS << " ]\n";
1234
Ted Kremenekec055e12007-08-29 23:20:49 +00001235 // Print the label of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001236 if (Stmt* S = const_cast<Stmt*>(B.getLabel())) {
1237
1238 if (print_edges)
1239 OS << " ";
1240
Ted Kremenekec055e12007-08-29 23:20:49 +00001241 if (LabelStmt* L = dyn_cast<LabelStmt>(S))
1242 OS << L->getName();
1243 else if (CaseStmt* C = dyn_cast<CaseStmt>(S)) {
1244 OS << "case ";
1245 C->getLHS()->printPretty(OS);
1246 if (C->getRHS()) {
1247 OS << " ... ";
1248 C->getRHS()->printPretty(OS);
1249 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001250 }
Chris Lattner1501e122007-09-16 19:11:53 +00001251 else if (isa<DefaultStmt>(S))
Ted Kremenekec055e12007-08-29 23:20:49 +00001252 OS << "default";
Ted Kremenek08176a52007-08-31 21:30:12 +00001253 else
1254 assert(false && "Invalid label statement in CFGBlock.");
1255
Ted Kremenekec055e12007-08-29 23:20:49 +00001256 OS << ":\n";
1257 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001258
Ted Kremenek97f75312007-08-21 21:42:03 +00001259 // Iterate through the statements in the block and print them.
Ted Kremenek97f75312007-08-21 21:42:03 +00001260 unsigned j = 1;
Ted Kremenek08176a52007-08-31 21:30:12 +00001261
1262 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1263 I != E ; ++I, ++j ) {
1264
Ted Kremenekec055e12007-08-29 23:20:49 +00001265 // Print the statement # in the basic block and the statement itself.
Ted Kremenek08176a52007-08-31 21:30:12 +00001266 if (print_edges)
1267 OS << " ";
1268
1269 OS << std::setw(3) << j << ": ";
1270
1271 if (Helper)
1272 Helper->setStmtID(j);
Ted Kremenek86afc042007-08-31 22:26:13 +00001273
1274 print_stmt(OS,Helper,*I);
Ted Kremenek97f75312007-08-21 21:42:03 +00001275 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001276
Ted Kremenekec055e12007-08-29 23:20:49 +00001277 // Print the terminator of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001278 if (B.getTerminator()) {
1279 if (print_edges)
1280 OS << " ";
1281
Ted Kremenekec055e12007-08-29 23:20:49 +00001282 OS << " T: ";
Ted Kremenek08176a52007-08-31 21:30:12 +00001283
1284 if (Helper) Helper->setBlockID(-1);
1285
1286 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1287 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek97f75312007-08-21 21:42:03 +00001288 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001289
Ted Kremenekec055e12007-08-29 23:20:49 +00001290 if (print_edges) {
1291 // Print the predecessors of this block.
Ted Kremenek08176a52007-08-31 21:30:12 +00001292 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenekec055e12007-08-29 23:20:49 +00001293 unsigned i = 0;
Ted Kremenekec055e12007-08-29 23:20:49 +00001294
Ted Kremenek08176a52007-08-31 21:30:12 +00001295 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1296 I != E; ++I, ++i) {
1297
1298 if (i == 8 || (i-8) == 0)
1299 OS << "\n ";
1300
Ted Kremenekec055e12007-08-29 23:20:49 +00001301 OS << " B" << (*I)->getBlockID();
1302 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001303
1304 OS << '\n';
1305
1306 // Print the successors of this block.
1307 OS << " Successors (" << B.succ_size() << "):";
1308 i = 0;
1309
1310 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1311 I != E; ++I, ++i) {
1312
1313 if (i == 8 || (i-8) % 10 == 0)
1314 OS << "\n ";
1315
1316 OS << " B" << (*I)->getBlockID();
1317 }
1318
Ted Kremenekec055e12007-08-29 23:20:49 +00001319 OS << '\n';
Ted Kremenek97f75312007-08-21 21:42:03 +00001320 }
Ted Kremenek08176a52007-08-31 21:30:12 +00001321}
1322
1323} // end anonymous namespace
1324
1325/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremenek56c939e2007-12-17 19:35:20 +00001326void CFG::dump() const { print(*llvm::cerr.stream()); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001327
1328/// print - A simple pretty printer of a CFG that outputs to an ostream.
1329void CFG::print(std::ostream& OS) const {
1330
1331 StmtPrinterHelper Helper(this);
1332
1333 // Print the entry block.
1334 print_block(OS, this, getEntry(), &Helper, true);
1335
1336 // Iterate through the CFGBlocks and print them one by one.
1337 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1338 // Skip the entry block, because we already printed it.
1339 if (&(*I) == &getEntry() || &(*I) == &getExit())
1340 continue;
1341
1342 print_block(OS, this, *I, &Helper, true);
1343 }
1344
1345 // Print the exit block.
1346 print_block(OS, this, getExit(), &Helper, true);
1347}
1348
1349/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremenek56c939e2007-12-17 19:35:20 +00001350void CFGBlock::dump(const CFG* cfg) const { print(*llvm::cerr.stream(), cfg); }
Ted Kremenek08176a52007-08-31 21:30:12 +00001351
1352/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1353/// Generally this will only be called from CFG::print.
1354void CFGBlock::print(std::ostream& OS, const CFG* cfg) const {
1355 StmtPrinterHelper Helper(cfg);
1356 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek4db5b452007-08-23 16:51:22 +00001357}
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001358
1359//===----------------------------------------------------------------------===//
1360// CFG Graphviz Visualization
1361//===----------------------------------------------------------------------===//
1362
Ted Kremenek08176a52007-08-31 21:30:12 +00001363
1364#ifndef NDEBUG
Chris Lattner26002172007-09-17 06:16:32 +00001365static StmtPrinterHelper* GraphHelper;
Ted Kremenek08176a52007-08-31 21:30:12 +00001366#endif
1367
1368void CFG::viewCFG() const {
1369#ifndef NDEBUG
1370 StmtPrinterHelper H(this);
1371 GraphHelper = &H;
1372 llvm::ViewGraph(this,"CFG");
1373 GraphHelper = NULL;
1374#else
1375 std::cerr << "CFG::viewCFG is only available in debug builds on "
Hartmut Kaiser284bff92007-09-17 12:29:55 +00001376 << "systems with Graphviz or gv!\n";
Ted Kremenek08176a52007-08-31 21:30:12 +00001377#endif
1378}
1379
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001380namespace llvm {
1381template<>
1382struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1383 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1384
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001385#ifndef NDEBUG
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001386 std::ostringstream Out;
Ted Kremenek08176a52007-08-31 21:30:12 +00001387 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001388 std::string OutStr = Out.str();
1389
1390 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1391
1392 // Process string output to make it nicer...
1393 for (unsigned i = 0; i != OutStr.length(); ++i)
1394 if (OutStr[i] == '\n') { // Left justify
1395 OutStr[i] = '\\';
1396 OutStr.insert(OutStr.begin()+i+1, 'l');
1397 }
1398
1399 return OutStr;
Hartmut Kaiser752a0052007-09-16 00:28:28 +00001400#else
1401 return "";
1402#endif
Ted Kremenekb3bb91b2007-08-29 21:56:09 +00001403 }
1404};
1405} // end namespace llvm