blob: 2eea6492cd0f58dea6576ff3081363ffa1e2de04 [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremenekfddd5182007-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"
Ted Kremenekc310e932007-08-21 22:06:14 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000017#include "clang/AST/PrettyPrinter.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000018#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000019#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek7dba8602007-08-29 21:56:09 +000020#include "llvm/Support/GraphWriter.h"
Ted Kremenek7e3a89d2007-12-17 19:35:20 +000021#include "llvm/Support/Streams.h"
Ted Kremenek6fa9b882008-01-08 18:15:10 +000022#include "llvm/Support/Compiler.h"
Ted Kremenek274f4332008-04-28 18:00:46 +000023#include <llvm/Support/Allocator.h>
Ted Kremeneka95d3752008-09-13 05:16:45 +000024#include <llvm/Support/Format.h>
Ted Kremenekfddd5182007-08-21 21:42:03 +000025#include <iomanip>
26#include <algorithm>
Ted Kremenek7dba8602007-08-29 21:56:09 +000027#include <sstream>
Ted Kremenek83c01da2008-01-11 00:40:29 +000028
Ted Kremenekfddd5182007-08-21 21:42:03 +000029using namespace clang;
30
31namespace {
32
Ted Kremenekbefef2f2007-08-23 21:26:19 +000033// SaveAndRestore - A utility class that uses RIIA to save and restore
34// the value of a variable.
35template<typename T>
Ted Kremenek6fa9b882008-01-08 18:15:10 +000036struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenekbefef2f2007-08-23 21:26:19 +000037 SaveAndRestore(T& x) : X(x), old_value(x) {}
38 ~SaveAndRestore() { X = old_value; }
Ted Kremenekb6f7b722007-08-30 18:13:31 +000039 T get() { return old_value; }
40
Ted Kremenekbefef2f2007-08-23 21:26:19 +000041 T& X;
42 T old_value;
43};
Ted Kremenekfddd5182007-08-21 21:42:03 +000044
Ted Kremenekc7eb9032008-08-06 23:20:50 +000045static SourceLocation GetEndLoc(ScopedDecl* D) {
46 if (VarDecl* VD = dyn_cast<VarDecl>(D))
47 if (Expr* Ex = VD->getInit())
48 return Ex->getSourceRange().getEnd();
49
50 return D->getLocation();
51}
52
53class VISIBILITY_HIDDEN UnaryDeclStmt : public DeclStmt {
54 Stmt* Ex;
55public:
56 UnaryDeclStmt(ScopedDecl* D)
57 : DeclStmt(D, D->getLocation(), GetEndLoc(D)), Ex(0) {
58 if (VarDecl* VD = dyn_cast<VarDecl>(D))
59 Ex = VD->getInit();
60 }
61
62 virtual ~UnaryDeclStmt() {}
63 virtual void Destroy(ASTContext& Ctx) { assert(false && "Do not call"); }
64
65 virtual child_iterator child_begin() {
66 return Ex ? &Ex : 0;
67 }
68 virtual child_iterator child_end() {
69 return Ex ? &Ex + 1 : 0;
70 }
71 virtual decl_iterator decl_begin() {
Ted Kremenek53061c82008-10-06 20:56:19 +000072 return TheDecl;
Ted Kremenekc7eb9032008-08-06 23:20:50 +000073 }
74 virtual decl_iterator decl_end() {
Ted Kremenek53061c82008-10-06 20:56:19 +000075 return TheDecl ? TheDecl->getNextDeclarator() : 0;
Ted Kremenekc7eb9032008-08-06 23:20:50 +000076 }
77};
78
Ted Kremeneka34ea072008-08-04 22:51:42 +000079/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000080/// The builder is stateful: an instance of the builder should be used to only
81/// construct a single CFG.
82///
83/// Example usage:
84///
85/// CFGBuilder builder;
86/// CFG* cfg = builder.BuildAST(stmt1);
87///
Ted Kremenekc310e932007-08-21 22:06:14 +000088/// CFG construction is done via a recursive walk of an AST.
89/// We actually parse the AST in reverse order so that the successor
90/// of a basic block is constructed prior to its predecessor. This
91/// allows us to nicely capture implicit fall-throughs without extra
92/// basic blocks.
93///
Ted Kremenek6fa9b882008-01-08 18:15:10 +000094class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenekfddd5182007-08-21 21:42:03 +000095 CFG* cfg;
96 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000097 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000098 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000099 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000100 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000101 CFGBlock* DefaultCaseBlock;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000102
Ted Kremenek19bb3562007-08-28 19:26:49 +0000103 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000104 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
105 LabelMapTy LabelMap;
106
Ted Kremenek19bb3562007-08-28 19:26:49 +0000107 // A list of blocks that end with a "goto" that must be backpatched to
108 // their resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +0000109 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000110 BackpatchBlocksTy BackpatchBlocks;
111
Ted Kremenek19bb3562007-08-28 19:26:49 +0000112 // A list of labels whose address has been taken (for indirect gotos).
113 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
114 LabelSetTy AddressTakenLabels;
115
Ted Kremenekfddd5182007-08-21 21:42:03 +0000116public:
Ted Kremenek026473c2007-08-23 16:51:22 +0000117 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +0000118 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000119 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenekfddd5182007-08-21 21:42:03 +0000120 // Create an empty CFG.
121 cfg = new CFG();
122 }
123
124 ~CFGBuilder() { delete cfg; }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000125
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000126 // buildCFG - Used by external clients to construct the CFG.
127 CFG* buildCFG(Stmt* Statement);
Ted Kremenekc310e932007-08-21 22:06:14 +0000128
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000129 // Visitors to walk an AST and construct the CFG. Called by
130 // buildCFG. Do not call directly!
Ted Kremeneke8ee26b2007-08-22 18:22:34 +0000131
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000132 CFGBlock* VisitStmt(Stmt* Statement);
133 CFGBlock* VisitNullStmt(NullStmt* Statement);
134 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
135 CFGBlock* VisitIfStmt(IfStmt* I);
136 CFGBlock* VisitReturnStmt(ReturnStmt* R);
137 CFGBlock* VisitLabelStmt(LabelStmt* L);
138 CFGBlock* VisitGotoStmt(GotoStmt* G);
139 CFGBlock* VisitForStmt(ForStmt* F);
140 CFGBlock* VisitWhileStmt(WhileStmt* W);
141 CFGBlock* VisitDoStmt(DoStmt* D);
142 CFGBlock* VisitContinueStmt(ContinueStmt* C);
143 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000144 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
145 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek295222c2008-02-13 21:46:34 +0000146 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000147 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000148
Ted Kremenek4102af92008-03-13 03:04:22 +0000149 // FIXME: Add support for ObjC-specific control-flow structures.
150
Ted Kremenek274f4332008-04-28 18:00:46 +0000151 // NYS == Not Yet Supported
152 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000153 badCFG = true;
154 return Block;
155 }
156
Ted Kremenek274f4332008-04-28 18:00:46 +0000157 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S){ return NYS();}
158 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S) { return NYS(); }
159 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { return NYS(); }
160 CFGBlock* VisitObjCAtFinallyStmt(ObjCAtFinallyStmt* S) { return NYS(); }
161 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { return NYS(); }
162
163 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
164 return NYS();
Ted Kremenek4102af92008-03-13 03:04:22 +0000165 }
166
Ted Kremenek00c0a302008-09-26 18:17:07 +0000167 // Blocks.
168 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
169 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
170
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000171private:
172 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000173 CFGBlock* addStmt(Stmt* Terminator);
174 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
175 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000176 CFGBlock* WalkAST_VisitDeclSubExpr(ScopedDecl* D);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000177 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000178 void FinishBlock(CFGBlock* B);
Ted Kremeneke8ee26b2007-08-22 18:22:34 +0000179
Ted Kremenek4102af92008-03-13 03:04:22 +0000180 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000181};
Ted Kremenek610a09e2008-09-26 22:58:57 +0000182
183static VariableArrayType* FindVA(Type* t) {
184 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
185 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
186 if (vat->getSizeExpr())
187 return vat;
188
189 t = vt->getElementType().getTypePtr();
190 }
191
192 return 0;
193}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000194
195/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
196/// represent an arbitrary statement. Examples include a single expression
197/// or a function body (compound statement). The ownership of the returned
198/// CFG is transferred to the caller. If CFG construction fails, this method
199/// returns NULL.
200CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek19bb3562007-08-28 19:26:49 +0000201 assert (cfg);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000202 if (!Statement) return NULL;
203
Ted Kremenek4102af92008-03-13 03:04:22 +0000204 badCFG = false;
205
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000206 // Create an empty block that will serve as the exit block for the CFG.
207 // Since this is the first block added to the CFG, it will be implicitly
208 // registered as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000209 Succ = createBlock();
210 assert (Succ == &cfg->getExit());
211 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000212
213 // Visit the statements and create the CFG.
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000214 CFGBlock* B = Visit(Statement);
215 if (!B) B = Succ;
216
217 if (B) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000218 // Finalize the last constructed block. This usually involves
219 // reversing the order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000220 if (Block) FinishBlock(B);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000221
222 // Backpatch the gotos whose label -> block mappings we didn't know
223 // when we encountered them.
224 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
225 E = BackpatchBlocks.end(); I != E; ++I ) {
226
227 CFGBlock* B = *I;
228 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
229 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
230
231 // If there is no target for the goto, then we are looking at an
232 // incomplete AST. Handle this by not registering a successor.
233 if (LI == LabelMap.end()) continue;
234
235 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000236 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000237
Ted Kremenek19bb3562007-08-28 19:26:49 +0000238 // Add successors to the Indirect Goto Dispatch block (if we have one).
239 if (CFGBlock* B = cfg->getIndirectGotoBlock())
240 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
241 E = AddressTakenLabels.end(); I != E; ++I ) {
242
243 // Lookup the target block.
244 LabelMapTy::iterator LI = LabelMap.find(*I);
245
246 // If there is no target block that contains label, then we are looking
247 // at an incomplete AST. Handle this by not registering a successor.
248 if (LI == LabelMap.end()) continue;
249
250 B->addSuccessor(LI->second);
251 }
Ted Kremenek322f58d2007-09-26 21:23:31 +0000252
Ted Kremenek94b33162007-09-17 16:18:02 +0000253 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000254 }
255
256 // Create an empty entry block that has no predecessors.
257 cfg->setEntry(createBlock());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000258
Ted Kremenek4102af92008-03-13 03:04:22 +0000259 if (badCFG) {
260 delete cfg;
261 cfg = NULL;
262 return NULL;
263 }
264
Ted Kremenek322f58d2007-09-26 21:23:31 +0000265 // NULL out cfg so that repeated calls to the builder will fail and that
266 // the ownership of the constructed CFG is passed to the caller.
267 CFG* t = cfg;
268 cfg = NULL;
269 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000270}
271
272/// createBlock - Used to lazily create blocks that are connected
273/// to the current (global) succcessor.
274CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000275 CFGBlock* B = cfg->createBlock();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000276 if (add_successor && Succ) B->addSuccessor(Succ);
277 return B;
278}
279
280/// FinishBlock - When the last statement has been added to the block,
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000281/// we must reverse the statements because they have been inserted
282/// in reverse order.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000283void CFGBuilder::FinishBlock(CFGBlock* B) {
284 assert (B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000285 B->reverseStmts();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000286}
287
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000288/// addStmt - Used to add statements/expressions to the current CFGBlock
289/// "Block". This method calls WalkAST on the passed statement to see if it
290/// contains any short-circuit expressions. If so, it recursively creates
291/// the necessary blocks for such expressions. It returns the "topmost" block
292/// of the created blocks, or the original value of "Block" when this method
293/// was called if no additional blocks are created.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000294CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenekaf603f72007-08-30 18:39:40 +0000295 if (!Block) Block = createBlock();
Ted Kremenek411cdee2008-04-16 21:10:48 +0000296 return WalkAST(Terminator,true);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000297}
298
299/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000300/// add extra blocks for ternary operators, &&, and ||. We also
301/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000302CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
303 switch (Terminator->getStmtClass()) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000304 case Stmt::ConditionalOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000305 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000306
307 // Create the confluence block that will "merge" the results
308 // of the ternary expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000309 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
310 ConfluenceBlock->appendStmt(C);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000311 FinishBlock(ConfluenceBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000312
313 // Create a block for the LHS expression if there is an LHS expression.
314 // A GCC extension allows LHS to be NULL, causing the condition to
315 // be the value that is returned instead.
316 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000317 Succ = ConfluenceBlock;
318 Block = NULL;
Ted Kremenekecc04c92007-11-26 18:20:26 +0000319 CFGBlock* LHSBlock = NULL;
320 if (C->getLHS()) {
321 LHSBlock = Visit(C->getLHS());
322 FinishBlock(LHSBlock);
323 Block = NULL;
324 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000325
Ted Kremenekecc04c92007-11-26 18:20:26 +0000326 // Create the block for the RHS expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000327 Succ = ConfluenceBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000328 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000329 FinishBlock(RHSBlock);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000330
Ted Kremenekecc04c92007-11-26 18:20:26 +0000331 // Create the block that will contain the condition.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000332 Block = createBlock(false);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000333
334 if (LHSBlock)
335 Block->addSuccessor(LHSBlock);
336 else {
337 // If we have no LHS expression, add the ConfluenceBlock as a direct
338 // successor for the block containing the condition. Moreover,
339 // we need to reverse the order of the predecessors in the
340 // ConfluenceBlock because the RHSBlock will have been added to
341 // the succcessors already, and we want the first predecessor to the
342 // the block containing the expression for the case when the ternary
343 // expression evaluates to true.
344 Block->addSuccessor(ConfluenceBlock);
345 assert (ConfluenceBlock->pred_size() == 2);
346 std::reverse(ConfluenceBlock->pred_begin(),
347 ConfluenceBlock->pred_end());
348 }
349
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000350 Block->addSuccessor(RHSBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000351
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000352 Block->setTerminator(C);
353 return addStmt(C->getCond());
354 }
Ted Kremenek49a436d2007-08-31 17:03:41 +0000355
356 case Stmt::ChooseExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000357 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000358
359 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
360 ConfluenceBlock->appendStmt(C);
361 FinishBlock(ConfluenceBlock);
362
363 Succ = ConfluenceBlock;
364 Block = NULL;
365 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000366 FinishBlock(LHSBlock);
367
Ted Kremenek49a436d2007-08-31 17:03:41 +0000368 Succ = ConfluenceBlock;
369 Block = NULL;
370 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000371 FinishBlock(RHSBlock);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000372
373 Block = createBlock(false);
374 Block->addSuccessor(LHSBlock);
375 Block->addSuccessor(RHSBlock);
376 Block->setTerminator(C);
377 return addStmt(C->getCond());
378 }
Ted Kremenek7926f7c2007-08-28 16:18:58 +0000379
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000380 case Stmt::DeclStmtClass: {
Ted Kremenek53061c82008-10-06 20:56:19 +0000381 DeclStmt *DS = cast<DeclStmt>(Terminator);
382 if (DS->hasSolitaryDecl()) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000383 Block->appendStmt(Terminator);
Ted Kremenek53061c82008-10-06 20:56:19 +0000384 return WalkAST_VisitDeclSubExpr(DS->getSolitaryDecl());
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000385 }
386 else {
387 typedef llvm::SmallVector<ScopedDecl*,10> BufTy;
388 BufTy Buf;
389 CFGBlock* B = 0;
Ted Kremenek53061c82008-10-06 20:56:19 +0000390
391 // FIXME: Add a reverse iterator for DeclStmt to avoid this
392 // extra copy.
393 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
394 DI != DE; ++DI)
395 Buf.push_back(*DI);
396
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000397 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
398 // Get the alignment of UnaryDeclStmt, padding out to >=8 bytes.
399 unsigned A = llvm::AlignOf<UnaryDeclStmt>::Alignment < 8
400 ? 8 : llvm::AlignOf<UnaryDeclStmt>::Alignment;
401
402 // Allocate the UnaryDeclStmt using the BumpPtrAllocator. It will
403 // get automatically freed with the CFG.
404 void* Mem = cfg->getAllocator().Allocate(sizeof(UnaryDeclStmt), A);
405 // Append the fake DeclStmt to block.
406 Block->appendStmt(new (Mem) UnaryDeclStmt(*I));
407 B = WalkAST_VisitDeclSubExpr(*I);
408 }
409 return B;
410 }
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000411 }
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000412
Ted Kremenek19bb3562007-08-28 19:26:49 +0000413 case Stmt::AddrLabelExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000414 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000415 AddressTakenLabels.insert(A->getLabel());
416
Ted Kremenek411cdee2008-04-16 21:10:48 +0000417 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000418 return Block;
419 }
Ted Kremenekf50ec102007-09-11 21:29:43 +0000420
Ted Kremenek15c27a82007-08-28 18:30:10 +0000421 case Stmt::StmtExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000422 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000423
Ted Kremenek610a09e2008-09-26 22:58:57 +0000424 case Stmt::SizeOfAlignOfTypeExprClass: {
425 SizeOfAlignOfTypeExpr* E = cast<SizeOfAlignOfTypeExpr>(Terminator);
426
427 // VLA types have expressions that must be evaluated.
428 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
429 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
430 addStmt(VA->getSizeExpr());
431
432 return Block;
433 }
434
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000435 case Stmt::UnaryOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000436 UnaryOperator* U = cast<UnaryOperator>(Terminator);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000437
438 // sizeof(expressions). For such expressions,
439 // the subexpression is not really evaluated, so
440 // we don't care about control-flow within the sizeof.
441 if (U->getOpcode() == UnaryOperator::SizeOf) {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000442 Block->appendStmt(Terminator);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000443 return Block;
444 }
445
446 break;
447 }
448
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000449 case Stmt::BinaryOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000450 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000451
452 if (B->isLogicalOp()) { // && or ||
453 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
454 ConfluenceBlock->appendStmt(B);
455 FinishBlock(ConfluenceBlock);
456
457 // create the block evaluating the LHS
458 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekafe54332007-12-21 19:49:00 +0000459 LHSBlock->setTerminator(B);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000460
461 // create the block evaluating the RHS
462 Succ = ConfluenceBlock;
463 Block = NULL;
464 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu924d9a82008-10-04 05:48:38 +0000465 FinishBlock(RHSBlock);
Ted Kremenekafe54332007-12-21 19:49:00 +0000466
467 // Now link the LHSBlock with RHSBlock.
468 if (B->getOpcode() == BinaryOperator::LOr) {
469 LHSBlock->addSuccessor(ConfluenceBlock);
470 LHSBlock->addSuccessor(RHSBlock);
471 }
472 else {
473 assert (B->getOpcode() == BinaryOperator::LAnd);
474 LHSBlock->addSuccessor(RHSBlock);
475 LHSBlock->addSuccessor(ConfluenceBlock);
476 }
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000477
478 // Generate the blocks for evaluating the LHS.
479 Block = LHSBlock;
480 return addStmt(B->getLHS());
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000481 }
482 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
483 Block->appendStmt(B);
484 addStmt(B->getRHS());
485 return addStmt(B->getLHS());
Ted Kremenek63f58872007-10-01 19:33:33 +0000486 }
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000487
488 break;
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000489 }
Ted Kremenek00c0a302008-09-26 18:17:07 +0000490
491 // Blocks: No support for blocks ... yet
492 case Stmt::BlockExprClass:
493 case Stmt::BlockDeclRefExprClass:
494 return NYS();
Ted Kremenekf4e15fc2008-02-26 02:37:08 +0000495
496 case Stmt::ParenExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000497 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000498
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000499 default:
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000500 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000501 };
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000502
Ted Kremenek411cdee2008-04-16 21:10:48 +0000503 if (AlwaysAddStmt) Block->appendStmt(Terminator);
504 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000505}
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000506
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000507/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
508/// for initializers in Decls.
509CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(ScopedDecl* D) {
510 VarDecl* VD = dyn_cast<VarDecl>(D);
511
512 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000513 return Block;
514
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000515 Expr* Init = VD->getInit();
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000516
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000517 if (Init) {
518 // Optimization: Don't create separate block-level statements for literals.
519 switch (Init->getStmtClass()) {
520 case Stmt::IntegerLiteralClass:
521 case Stmt::CharacterLiteralClass:
522 case Stmt::StringLiteralClass:
523 break;
524 default:
525 Block = addStmt(Init);
526 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000527 }
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000528
529 // If the type of VD is a VLA, then we must process its size expressions.
530 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
531 VA = FindVA(VA->getElementType().getTypePtr()))
532 Block = addStmt(VA->getSizeExpr());
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000533
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000534 return Block;
535}
536
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000537/// WalkAST_VisitChildren - Utility method to call WalkAST on the
538/// children of a Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000539CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000540 CFGBlock* B = Block;
Ted Kremenek411cdee2008-04-16 21:10:48 +0000541 for (Stmt::child_iterator I = Terminator->child_begin(), E = Terminator->child_end() ;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000542 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000543 if (*I) B = WalkAST(*I);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000544
545 return B;
546}
547
Ted Kremenek15c27a82007-08-28 18:30:10 +0000548/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
549/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000550CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
551 Block->appendStmt(Terminator);
552 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000553}
554
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000555/// VisitStmt - Handle statements with no branching control flow.
556CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
557 // We cannot assume that we are in the middle of a basic block, since
558 // the CFG might only be constructed for this single statement. If
559 // we have no current basic block, just create one lazily.
560 if (!Block) Block = createBlock();
561
562 // Simply add the statement to the current block. We actually
563 // insert statements in reverse order; this order is reversed later
564 // when processing the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000565 addStmt(Statement);
566
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000567 return Block;
568}
569
570CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
571 return Block;
572}
573
574CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000575
576 CFGBlock* LastBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000577
Ted Kremenekd34066c2008-02-26 00:22:58 +0000578 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
579 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000580 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000581 }
582
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000583 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000584}
585
586CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
587 // We may see an if statement in the middle of a basic block, or
588 // it may be the first statement we are processing. In either case,
589 // we create a new basic block. First, we create the blocks for
590 // the then...else statements, and then we create the block containing
591 // the if statement. If we were in the middle of a block, we
592 // stop processing that block and reverse its statements. That block
593 // is then the implicit successor for the "then" and "else" clauses.
594
595 // The block we were proccessing is now finished. Make it the
596 // successor block.
597 if (Block) {
598 Succ = Block;
599 FinishBlock(Block);
600 }
601
602 // Process the false branch. NULL out Block so that the recursive
603 // call to Visit will create a new basic block.
604 // Null out Block so that all successor
605 CFGBlock* ElseBlock = Succ;
606
607 if (Stmt* Else = I->getElse()) {
608 SaveAndRestore<CFGBlock*> sv(Succ);
609
610 // NULL out Block so that the recursive call to Visit will
611 // create a new basic block.
612 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000613 ElseBlock = Visit(Else);
614
615 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
616 ElseBlock = sv.get();
617 else if (Block)
618 FinishBlock(ElseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000619 }
620
621 // Process the true branch. NULL out Block so that the recursive
622 // call to Visit will create a new basic block.
623 // Null out Block so that all successor
624 CFGBlock* ThenBlock;
625 {
626 Stmt* Then = I->getThen();
627 assert (Then);
628 SaveAndRestore<CFGBlock*> sv(Succ);
629 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000630 ThenBlock = Visit(Then);
631
632 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
633 ThenBlock = sv.get();
634 else if (Block)
635 FinishBlock(ThenBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000636 }
637
638 // Now create a new block containing the if statement.
639 Block = createBlock(false);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000640
641 // Set the terminator of the new block to the If statement.
642 Block->setTerminator(I);
643
644 // Now add the successors.
645 Block->addSuccessor(ThenBlock);
646 Block->addSuccessor(ElseBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000647
648 // Add the condition as the last statement in the new block. This
649 // may create new blocks as the condition may contain control-flow. Any
650 // newly created blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000651 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000652}
Ted Kremenekf50ec102007-09-11 21:29:43 +0000653
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000654
655CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
656 // If we were in the middle of a block we stop processing that block
657 // and reverse its statements.
658 //
659 // NOTE: If a "return" appears in the middle of a block, this means
660 // that the code afterwards is DEAD (unreachable). We still
661 // keep a basic block for that code; a simple "mark-and-sweep"
662 // from the entry block will be able to report such dead
663 // blocks.
664 if (Block) FinishBlock(Block);
665
666 // Create the new block.
667 Block = createBlock(false);
668
669 // The Exit block is the only successor.
670 Block->addSuccessor(&cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000671
672 // Add the return statement to the block. This may create new blocks
673 // if R contains control-flow (short-circuit operations).
674 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000675}
676
677CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
678 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000679 Visit(L->getSubStmt());
680 CFGBlock* LabelBlock = Block;
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000681
682 if (!LabelBlock) // This can happen when the body is empty, i.e.
683 LabelBlock=createBlock(); // scopes that only contains NullStmts.
684
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000685 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
686 LabelMap[ L ] = LabelBlock;
687
688 // Labels partition blocks, so this is the end of the basic block
Ted Kremenek9cffe732007-08-29 23:20:49 +0000689 // we were processing (L is the block's label). Because this is
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000690 // label (and we have already processed the substatement) there is no
691 // extra control-flow to worry about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000692 LabelBlock->setLabel(L);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000693 FinishBlock(LabelBlock);
694
695 // We set Block to NULL to allow lazy creation of a new block
696 // (if necessary);
697 Block = NULL;
698
699 // This block is now the implicit successor of other blocks.
700 Succ = LabelBlock;
701
702 return LabelBlock;
703}
704
705CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
706 // Goto is a control-flow statement. Thus we stop processing the
707 // current block and create a new one.
708 if (Block) FinishBlock(Block);
709 Block = createBlock(false);
710 Block->setTerminator(G);
711
712 // If we already know the mapping to the label block add the
713 // successor now.
714 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
715
716 if (I == LabelMap.end())
717 // We will need to backpatch this block later.
718 BackpatchBlocks.push_back(Block);
719 else
720 Block->addSuccessor(I->second);
721
722 return Block;
723}
724
725CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
726 // "for" is a control-flow statement. Thus we stop processing the
727 // current block.
728
729 CFGBlock* LoopSuccessor = NULL;
730
731 if (Block) {
732 FinishBlock(Block);
733 LoopSuccessor = Block;
734 }
735 else LoopSuccessor = Succ;
736
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000737 // Because of short-circuit evaluation, the condition of the loop
738 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
739 // blocks that evaluate the condition.
740 CFGBlock* ExitConditionBlock = createBlock(false);
741 CFGBlock* EntryConditionBlock = ExitConditionBlock;
742
743 // Set the terminator for the "exit" condition block.
744 ExitConditionBlock->setTerminator(F);
745
746 // Now add the actual condition to the condition block. Because the
747 // condition itself may contain control-flow, new blocks may be created.
748 if (Stmt* C = F->getCond()) {
749 Block = ExitConditionBlock;
750 EntryConditionBlock = addStmt(C);
751 if (Block) FinishBlock(EntryConditionBlock);
752 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000753
754 // The condition block is the implicit successor for the loop body as
755 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000756 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000757
758 // Now create the loop body.
759 {
760 assert (F->getBody());
761
762 // Save the current values for Block, Succ, and continue and break targets
763 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
764 save_continue(ContinueTargetBlock),
765 save_break(BreakTargetBlock);
Ted Kremeneke9334502008-09-04 21:48:47 +0000766
Ted Kremenekaf603f72007-08-30 18:39:40 +0000767 // Create a new block to contain the (bottom) of the loop body.
768 Block = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000769
Ted Kremeneke9334502008-09-04 21:48:47 +0000770 if (Stmt* I = F->getInc()) {
771 // Generate increment code in its own basic block. This is the target
772 // of continue statements.
773 Succ = addStmt(I);
774 Block = 0;
775 ContinueTargetBlock = Succ;
776 }
777 else {
778 // No increment code. Continues should go the the entry condition block.
779 ContinueTargetBlock = EntryConditionBlock;
780 }
781
782 // All breaks should go to the code following the loop.
783 BreakTargetBlock = LoopSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000784
785 // Now populate the body block, and in the process create new blocks
786 // as we walk the body of the loop.
787 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000788
789 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000790 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000791 else if (Block)
792 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000793
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000794 // This new body block is a successor to our "exit" condition block.
795 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000796 }
797
798 // Link up the condition block with the code that follows the loop.
799 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000800 ExitConditionBlock->addSuccessor(LoopSuccessor);
801
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000802 // If the loop contains initialization, create a new block for those
803 // statements. This block can also contain statements that precede
804 // the loop.
805 if (Stmt* I = F->getInit()) {
806 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000807 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000808 }
809 else {
810 // There is no loop initialization. We are thus basically a while
811 // loop. NULL out Block to force lazy block construction.
812 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000813 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000814 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000815 }
816}
817
818CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
819 // "while" is a control-flow statement. Thus we stop processing the
820 // current block.
821
822 CFGBlock* LoopSuccessor = NULL;
823
824 if (Block) {
825 FinishBlock(Block);
826 LoopSuccessor = Block;
827 }
828 else LoopSuccessor = Succ;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000829
830 // Because of short-circuit evaluation, the condition of the loop
831 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
832 // blocks that evaluate the condition.
833 CFGBlock* ExitConditionBlock = createBlock(false);
834 CFGBlock* EntryConditionBlock = ExitConditionBlock;
835
836 // Set the terminator for the "exit" condition block.
837 ExitConditionBlock->setTerminator(W);
838
839 // Now add the actual condition to the condition block. Because the
840 // condition itself may contain control-flow, new blocks may be created.
841 // Thus we update "Succ" after adding the condition.
842 if (Stmt* C = W->getCond()) {
843 Block = ExitConditionBlock;
844 EntryConditionBlock = addStmt(C);
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000845 assert (Block == EntryConditionBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000846 if (Block) FinishBlock(EntryConditionBlock);
847 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000848
849 // The condition block is the implicit successor for the loop body as
850 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000851 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000852
853 // Process the loop body.
854 {
855 assert (W->getBody());
856
857 // Save the current values for Block, Succ, and continue and break targets
858 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
859 save_continue(ContinueTargetBlock),
860 save_break(BreakTargetBlock);
861
862 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000863 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000864
865 // All breaks should go to the code following the loop.
866 BreakTargetBlock = LoopSuccessor;
867
868 // NULL out Block to force lazy instantiation of blocks for the body.
869 Block = NULL;
870
871 // Create the body. The returned block is the entry to the loop body.
872 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000873
874 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000875 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000876 else if (Block)
877 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000878
879 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000880 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000881 }
882
883 // Link up the condition block with the code that follows the loop.
884 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000885 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000886
887 // There can be no more statements in the condition block
888 // since we loop back to this block. NULL out Block to force
889 // lazy creation of another block.
890 Block = NULL;
891
892 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +0000893 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000894 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000895}
896
897CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
898 // "do...while" is a control-flow statement. Thus we stop processing the
899 // current block.
900
901 CFGBlock* LoopSuccessor = NULL;
902
903 if (Block) {
904 FinishBlock(Block);
905 LoopSuccessor = Block;
906 }
907 else LoopSuccessor = Succ;
908
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000909 // Because of short-circuit evaluation, the condition of the loop
910 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
911 // blocks that evaluate the condition.
912 CFGBlock* ExitConditionBlock = createBlock(false);
913 CFGBlock* EntryConditionBlock = ExitConditionBlock;
914
915 // Set the terminator for the "exit" condition block.
916 ExitConditionBlock->setTerminator(D);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000917
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000918 // Now add the actual condition to the condition block. Because the
919 // condition itself may contain control-flow, new blocks may be created.
920 if (Stmt* C = D->getCond()) {
921 Block = ExitConditionBlock;
922 EntryConditionBlock = addStmt(C);
923 if (Block) FinishBlock(EntryConditionBlock);
924 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000925
Ted Kremenek54827132008-02-27 07:20:00 +0000926 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000927 Succ = EntryConditionBlock;
928
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000929 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000930 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000931 {
932 assert (D->getBody());
933
934 // Save the current values for Block, Succ, and continue and break targets
935 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
936 save_continue(ContinueTargetBlock),
937 save_break(BreakTargetBlock);
938
939 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000940 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000941
942 // All breaks should go to the code following the loop.
943 BreakTargetBlock = LoopSuccessor;
944
945 // NULL out Block to force lazy instantiation of blocks for the body.
946 Block = NULL;
947
948 // Create the body. The returned block is the entry to the loop body.
949 BodyBlock = Visit(D->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000950
Ted Kremenekaf603f72007-08-30 18:39:40 +0000951 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000952 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000953 else if (Block)
954 FinishBlock(BodyBlock);
955
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000956 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000957 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000958 }
959
960 // Link up the condition block with the code that follows the loop.
961 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000962 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000963
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000964 // There can be no more statements in the body block(s)
965 // since we loop back to the body. NULL out Block to force
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000966 // lazy creation of another block.
967 Block = NULL;
968
969 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +0000970 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000971 return BodyBlock;
972}
973
974CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
975 // "continue" is a control-flow statement. Thus we stop processing the
976 // current block.
977 if (Block) FinishBlock(Block);
978
979 // Now create a new block that ends with the continue statement.
980 Block = createBlock(false);
981 Block->setTerminator(C);
982
983 // If there is no target for the continue, then we are looking at an
984 // incomplete AST. Handle this by not registering a successor.
985 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
986
987 return Block;
988}
989
990CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
991 // "break" is a control-flow statement. Thus we stop processing the
992 // current block.
993 if (Block) FinishBlock(Block);
994
995 // Now create a new block that ends with the continue statement.
996 Block = createBlock(false);
997 Block->setTerminator(B);
998
999 // If there is no target for the break, then we are looking at an
1000 // incomplete AST. Handle this by not registering a successor.
1001 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1002
1003 return Block;
1004}
1005
Ted Kremenek411cdee2008-04-16 21:10:48 +00001006CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001007 // "switch" is a control-flow statement. Thus we stop processing the
1008 // current block.
1009 CFGBlock* SwitchSuccessor = NULL;
1010
1011 if (Block) {
1012 FinishBlock(Block);
1013 SwitchSuccessor = Block;
1014 }
1015 else SwitchSuccessor = Succ;
1016
1017 // Save the current "switch" context.
1018 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001019 save_break(BreakTargetBlock),
1020 save_default(DefaultCaseBlock);
1021
1022 // Set the "default" case to be the block after the switch statement.
1023 // If the switch statement contains a "default:", this value will
1024 // be overwritten with the block for that code.
1025 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenek295222c2008-02-13 21:46:34 +00001026
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001027 // Create a new block that will contain the switch statement.
1028 SwitchTerminatedBlock = createBlock(false);
1029
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001030 // Now process the switch body. The code after the switch is the implicit
1031 // successor.
1032 Succ = SwitchSuccessor;
1033 BreakTargetBlock = SwitchSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001034
1035 // When visiting the body, the case statements should automatically get
1036 // linked up to the switch. We also don't keep a pointer to the body,
1037 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001038 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001039 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001040 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001041 if (Block) FinishBlock(BodyBlock);
1042
Ted Kremenek295222c2008-02-13 21:46:34 +00001043 // If we have no "default:" case, the default transition is to the
1044 // code following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001045 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenek295222c2008-02-13 21:46:34 +00001046
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001047 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001048 SwitchTerminatedBlock->setTerminator(Terminator);
1049 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001050 Block = SwitchTerminatedBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001051
Ted Kremenek411cdee2008-04-16 21:10:48 +00001052 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001053}
1054
Ted Kremenek411cdee2008-04-16 21:10:48 +00001055CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001056 // CaseStmts are essentially labels, so they are the
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001057 // first statement in a block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001058
Ted Kremenek411cdee2008-04-16 21:10:48 +00001059 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001060 CFGBlock* CaseBlock = Block;
1061 if (!CaseBlock) CaseBlock = createBlock();
1062
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001063 // Cases statements partition blocks, so this is the top of
1064 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001065 CaseBlock->setLabel(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001066 FinishBlock(CaseBlock);
1067
1068 // Add this block to the list of successors for the block with the
1069 // switch statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001070 assert (SwitchTerminatedBlock);
1071 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001072
1073 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1074 Block = NULL;
1075
1076 // This block is now the implicit successor of other blocks.
1077 Succ = CaseBlock;
1078
Ted Kremenek2677ea82008-03-15 07:45:02 +00001079 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001080}
Ted Kremenek295222c2008-02-13 21:46:34 +00001081
Ted Kremenek411cdee2008-04-16 21:10:48 +00001082CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1083 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001084 DefaultCaseBlock = Block;
1085 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1086
1087 // Default statements partition blocks, so this is the top of
1088 // the basic block we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001089 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001090 FinishBlock(DefaultCaseBlock);
1091
1092 // Unlike case statements, we don't add the default block to the
1093 // successors for the switch statement immediately. This is done
1094 // when we finish processing the switch statement. This allows for
1095 // the default case (including a fall-through to the code after the
1096 // switch statement) to always be the last successor of a switch-terminated
1097 // block.
1098
1099 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1100 Block = NULL;
1101
1102 // This block is now the implicit successor of other blocks.
1103 Succ = DefaultCaseBlock;
1104
1105 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001106}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001107
Ted Kremenek19bb3562007-08-28 19:26:49 +00001108CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1109 // Lazily create the indirect-goto dispatch block if there isn't one
1110 // already.
1111 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1112
1113 if (!IBlock) {
1114 IBlock = createBlock(false);
1115 cfg->setIndirectGotoBlock(IBlock);
1116 }
1117
1118 // IndirectGoto is a control-flow statement. Thus we stop processing the
1119 // current block and create a new one.
1120 if (Block) FinishBlock(Block);
1121 Block = createBlock(false);
1122 Block->setTerminator(I);
1123 Block->addSuccessor(IBlock);
1124 return addStmt(I->getTarget());
1125}
1126
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001127
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001128} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001129
1130/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1131/// block has no successors or predecessors. If this is the first block
1132/// created in the CFG, it is automatically set to be the Entry and Exit
1133/// of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001134CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001135 bool first_block = begin() == end();
1136
1137 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001138 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001139
1140 // If this is the first block, set it as the Entry and Exit.
1141 if (first_block) Entry = Exit = &front();
1142
1143 // Return the block.
1144 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001145}
1146
Ted Kremenek026473c2007-08-23 16:51:22 +00001147/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1148/// CFG is returned to the caller.
1149CFG* CFG::buildCFG(Stmt* Statement) {
1150 CFGBuilder Builder;
1151 return Builder.buildCFG(Statement);
1152}
1153
1154/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001155void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1156
Ted Kremenek63f58872007-10-01 19:33:33 +00001157//===----------------------------------------------------------------------===//
1158// CFG: Queries for BlkExprs.
1159//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001160
Ted Kremenek63f58872007-10-01 19:33:33 +00001161namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001162 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001163}
1164
Ted Kremenek411cdee2008-04-16 21:10:48 +00001165static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1166 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001167 return;
1168
Ted Kremenek411cdee2008-04-16 21:10:48 +00001169 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001170 if (!*I) continue;
1171
1172 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1173 if (B->isAssignmentOp()) Set.insert(B);
1174
1175 FindSubExprAssignments(*I, Set);
1176 }
1177}
1178
Ted Kremenek63f58872007-10-01 19:33:33 +00001179static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1180 BlkExprMapTy* M = new BlkExprMapTy();
1181
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001182 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek411cdee2008-04-16 21:10:48 +00001183 // only assignments that we want to *possibly* register as a block-level
1184 // expression. Basically, if an assignment occurs both in a subexpression
1185 // and at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001186 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1187
Ted Kremenek63f58872007-10-01 19:33:33 +00001188 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1189 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001190 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001191
Ted Kremenek411cdee2008-04-16 21:10:48 +00001192 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1193
1194 // Iterate over the statements again on identify the Expr* and Stmt* at
1195 // the block-level that are block-level expressions.
1196
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001197 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001198 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001199
Ted Kremenek411cdee2008-04-16 21:10:48 +00001200 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001201 // Assignment expressions that are not nested within another
1202 // expression are really "statements" whose value is never
1203 // used by another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001204 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001205 continue;
1206 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001207 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001208 // Special handling for statement expressions. The last statement
1209 // in the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001210 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001211 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001212 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001213 (*M)[C->body_back()] = x;
1214 }
1215 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001216
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001217 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001218 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001219 }
1220
Ted Kremenek411cdee2008-04-16 21:10:48 +00001221 // Look at terminators. The condition is a block-level expression.
1222
1223 Expr* Exp = I->getTerminatorCondition();
1224
1225 if (Exp && M->find(Exp) == M->end()) {
1226 unsigned x = M->size();
1227 (*M)[Exp] = x;
1228 }
1229 }
1230
Ted Kremenek63f58872007-10-01 19:33:33 +00001231 return M;
1232}
1233
Ted Kremenek86946742008-01-17 20:48:37 +00001234CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1235 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001236 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1237
1238 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001239 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek63f58872007-10-01 19:33:33 +00001240
1241 if (I == M->end()) return CFG::BlkExprNumTy();
1242 else return CFG::BlkExprNumTy(I->second);
1243}
1244
1245unsigned CFG::getNumBlkExprs() {
1246 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1247 return M->size();
1248 else {
1249 // We assume callers interested in the number of BlkExprs will want
1250 // the map constructed if it doesn't already exist.
1251 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1252 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1253 }
1254}
1255
Ted Kremenek274f4332008-04-28 18:00:46 +00001256//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001257// Cleanup: CFG dstor.
1258//===----------------------------------------------------------------------===//
1259
Ted Kremenek63f58872007-10-01 19:33:33 +00001260CFG::~CFG() {
1261 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1262}
1263
Ted Kremenek7dba8602007-08-29 21:56:09 +00001264//===----------------------------------------------------------------------===//
1265// CFG pretty printing
1266//===----------------------------------------------------------------------===//
1267
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001268namespace {
1269
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001270class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001271
Ted Kremenek42a509f2007-08-31 21:30:12 +00001272 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1273 StmtMapTy StmtMap;
1274 signed CurrentBlock;
1275 unsigned CurrentStmt;
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001276
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001277public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001278
Ted Kremenek42a509f2007-08-31 21:30:12 +00001279 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1280 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1281 unsigned j = 1;
1282 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1283 BI != BEnd; ++BI, ++j )
1284 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1285 }
1286 }
1287
1288 virtual ~StmtPrinterHelper() {}
1289
1290 void setBlockID(signed i) { CurrentBlock = i; }
1291 void setStmtID(unsigned i) { CurrentStmt = i; }
1292
Ted Kremeneka95d3752008-09-13 05:16:45 +00001293 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001294
Ted Kremenek411cdee2008-04-16 21:10:48 +00001295 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001296
1297 if (I == StmtMap.end())
1298 return false;
1299
1300 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1301 && I->second.second == CurrentStmt)
1302 return false;
1303
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001304 OS << "[B" << I->second.first << "." << I->second.second << "]";
1305 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001306 }
1307};
1308
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001309class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1310 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1311
Ted Kremeneka95d3752008-09-13 05:16:45 +00001312 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001313 StmtPrinterHelper* Helper;
1314public:
Ted Kremeneka95d3752008-09-13 05:16:45 +00001315 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek42a509f2007-08-31 21:30:12 +00001316 : OS(os), Helper(helper) {}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001317
1318 void VisitIfStmt(IfStmt* I) {
1319 OS << "if ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001320 I->getCond()->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001321 }
1322
1323 // Default case.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001324 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001325
1326 void VisitForStmt(ForStmt* F) {
1327 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001328 if (F->getInit()) OS << "...";
1329 OS << "; ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001330 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek535bb202007-08-30 21:28:02 +00001331 OS << "; ";
1332 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001333 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001334 }
1335
1336 void VisitWhileStmt(WhileStmt* W) {
1337 OS << "while " ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001338 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001339 }
1340
1341 void VisitDoStmt(DoStmt* D) {
1342 OS << "do ... while ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001343 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001344 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001345
Ted Kremenek411cdee2008-04-16 21:10:48 +00001346 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001347 OS << "switch ";
Ted Kremenek411cdee2008-04-16 21:10:48 +00001348 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001349 }
1350
Ted Kremenek805e9a82007-08-31 21:49:40 +00001351 void VisitConditionalOperator(ConditionalOperator* C) {
1352 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001353 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001354 }
1355
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001356 void VisitChooseExpr(ChooseExpr* C) {
1357 OS << "__builtin_choose_expr( ";
1358 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001359 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001360 }
1361
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001362 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1363 OS << "goto *";
1364 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001365 }
1366
Ted Kremenek805e9a82007-08-31 21:49:40 +00001367 void VisitBinaryOperator(BinaryOperator* B) {
1368 if (!B->isLogicalOp()) {
1369 VisitExpr(B);
1370 return;
1371 }
1372
1373 B->getLHS()->printPretty(OS,Helper);
1374
1375 switch (B->getOpcode()) {
1376 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001377 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001378 return;
1379 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001380 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001381 return;
1382 default:
1383 assert(false && "Invalid logical operator.");
1384 }
1385 }
1386
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001387 void VisitExpr(Expr* E) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001388 E->printPretty(OS,Helper);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001389 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001390};
Ted Kremenek42a509f2007-08-31 21:30:12 +00001391
1392
Ted Kremeneka95d3752008-09-13 05:16:45 +00001393void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001394 if (Helper) {
1395 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001396 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001397 CompoundStmt* Sub = SE->getSubStmt();
1398
1399 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001400 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001401 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001402 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001403 return;
1404 }
1405 }
1406
1407 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001408 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001409 if (B->getOpcode() == BinaryOperator::Comma) {
1410 OS << "... , ";
1411 Helper->handledStmt(B->getRHS(),OS);
1412 OS << '\n';
1413 return;
1414 }
1415 }
1416 }
1417
Ted Kremenek411cdee2008-04-16 21:10:48 +00001418 Terminator->printPretty(OS, Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001419
1420 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001421 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001422}
1423
Ted Kremeneka95d3752008-09-13 05:16:45 +00001424void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek42a509f2007-08-31 21:30:12 +00001425 StmtPrinterHelper* Helper, bool print_edges) {
1426
1427 if (Helper) Helper->setBlockID(B.getBlockID());
1428
Ted Kremenek7dba8602007-08-29 21:56:09 +00001429 // Print the header.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001430 OS << "\n [ B" << B.getBlockID();
1431
1432 if (&B == &cfg->getEntry())
1433 OS << " (ENTRY) ]\n";
1434 else if (&B == &cfg->getExit())
1435 OS << " (EXIT) ]\n";
1436 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001437 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001438 else
1439 OS << " ]\n";
1440
Ted Kremenek9cffe732007-08-29 23:20:49 +00001441 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001442 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001443
1444 if (print_edges)
1445 OS << " ";
1446
Ted Kremenek411cdee2008-04-16 21:10:48 +00001447 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001448 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001449 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001450 OS << "case ";
1451 C->getLHS()->printPretty(OS);
1452 if (C->getRHS()) {
1453 OS << " ... ";
1454 C->getRHS()->printPretty(OS);
1455 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001456 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001457 else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001458 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001459 else
1460 assert(false && "Invalid label statement in CFGBlock.");
1461
Ted Kremenek9cffe732007-08-29 23:20:49 +00001462 OS << ":\n";
1463 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001464
Ted Kremenekfddd5182007-08-21 21:42:03 +00001465 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001466 unsigned j = 1;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001467
1468 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1469 I != E ; ++I, ++j ) {
1470
Ted Kremenek9cffe732007-08-29 23:20:49 +00001471 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001472 if (print_edges)
1473 OS << " ";
1474
Ted Kremeneka95d3752008-09-13 05:16:45 +00001475 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001476
1477 if (Helper)
1478 Helper->setStmtID(j);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001479
1480 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001481 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001482
Ted Kremenek9cffe732007-08-29 23:20:49 +00001483 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001484 if (B.getTerminator()) {
1485 if (print_edges)
1486 OS << " ";
1487
Ted Kremenek9cffe732007-08-29 23:20:49 +00001488 OS << " T: ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001489
1490 if (Helper) Helper->setBlockID(-1);
1491
1492 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1493 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001494 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001495 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001496
Ted Kremenek9cffe732007-08-29 23:20:49 +00001497 if (print_edges) {
1498 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001499 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001500 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001501
Ted Kremenek42a509f2007-08-31 21:30:12 +00001502 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1503 I != E; ++I, ++i) {
1504
1505 if (i == 8 || (i-8) == 0)
1506 OS << "\n ";
1507
Ted Kremenek9cffe732007-08-29 23:20:49 +00001508 OS << " B" << (*I)->getBlockID();
1509 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001510
1511 OS << '\n';
1512
1513 // Print the successors of this block.
1514 OS << " Successors (" << B.succ_size() << "):";
1515 i = 0;
1516
1517 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1518 I != E; ++I, ++i) {
1519
1520 if (i == 8 || (i-8) % 10 == 0)
1521 OS << "\n ";
1522
1523 OS << " B" << (*I)->getBlockID();
1524 }
1525
Ted Kremenek9cffe732007-08-29 23:20:49 +00001526 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001527 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001528}
1529
1530} // end anonymous namespace
1531
1532/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001533void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001534
1535/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001536void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001537
1538 StmtPrinterHelper Helper(this);
1539
1540 // Print the entry block.
1541 print_block(OS, this, getEntry(), &Helper, true);
1542
1543 // Iterate through the CFGBlocks and print them one by one.
1544 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1545 // Skip the entry block, because we already printed it.
1546 if (&(*I) == &getEntry() || &(*I) == &getExit())
1547 continue;
1548
1549 print_block(OS, this, *I, &Helper, true);
1550 }
1551
1552 // Print the exit block.
1553 print_block(OS, this, getExit(), &Helper, true);
1554}
1555
1556/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001557void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001558
1559/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1560/// Generally this will only be called from CFG::print.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001561void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001562 StmtPrinterHelper Helper(cfg);
1563 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001564}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001565
Ted Kremeneka2925852008-01-30 23:02:42 +00001566/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001567void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremeneka2925852008-01-30 23:02:42 +00001568 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1569 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1570}
1571
Ted Kremenek411cdee2008-04-16 21:10:48 +00001572Expr* CFGBlock::getTerminatorCondition() {
1573
1574 if (!Terminator)
1575 return NULL;
1576
1577 Expr* E = NULL;
1578
1579 switch (Terminator->getStmtClass()) {
1580 default:
1581 break;
1582
1583 case Stmt::ForStmtClass:
1584 E = cast<ForStmt>(Terminator)->getCond();
1585 break;
1586
1587 case Stmt::WhileStmtClass:
1588 E = cast<WhileStmt>(Terminator)->getCond();
1589 break;
1590
1591 case Stmt::DoStmtClass:
1592 E = cast<DoStmt>(Terminator)->getCond();
1593 break;
1594
1595 case Stmt::IfStmtClass:
1596 E = cast<IfStmt>(Terminator)->getCond();
1597 break;
1598
1599 case Stmt::ChooseExprClass:
1600 E = cast<ChooseExpr>(Terminator)->getCond();
1601 break;
1602
1603 case Stmt::IndirectGotoStmtClass:
1604 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1605 break;
1606
1607 case Stmt::SwitchStmtClass:
1608 E = cast<SwitchStmt>(Terminator)->getCond();
1609 break;
1610
1611 case Stmt::ConditionalOperatorClass:
1612 E = cast<ConditionalOperator>(Terminator)->getCond();
1613 break;
1614
1615 case Stmt::BinaryOperatorClass: // '&&' and '||'
1616 E = cast<BinaryOperator>(Terminator)->getLHS();
1617 break;
1618 }
1619
1620 return E ? E->IgnoreParens() : NULL;
1621}
1622
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001623bool CFGBlock::hasBinaryBranchTerminator() const {
1624
1625 if (!Terminator)
1626 return false;
1627
1628 Expr* E = NULL;
1629
1630 switch (Terminator->getStmtClass()) {
1631 default:
1632 return false;
1633
1634 case Stmt::ForStmtClass:
1635 case Stmt::WhileStmtClass:
1636 case Stmt::DoStmtClass:
1637 case Stmt::IfStmtClass:
1638 case Stmt::ChooseExprClass:
1639 case Stmt::ConditionalOperatorClass:
1640 case Stmt::BinaryOperatorClass:
1641 return true;
1642 }
1643
1644 return E ? E->IgnoreParens() : NULL;
1645}
1646
Ted Kremeneka2925852008-01-30 23:02:42 +00001647
Ted Kremenek7dba8602007-08-29 21:56:09 +00001648//===----------------------------------------------------------------------===//
1649// CFG Graphviz Visualization
1650//===----------------------------------------------------------------------===//
1651
Ted Kremenek42a509f2007-08-31 21:30:12 +00001652
1653#ifndef NDEBUG
Chris Lattner00123512007-09-17 06:16:32 +00001654static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001655#endif
1656
1657void CFG::viewCFG() const {
1658#ifndef NDEBUG
1659 StmtPrinterHelper H(this);
1660 GraphHelper = &H;
1661 llvm::ViewGraph(this,"CFG");
1662 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001663#endif
1664}
1665
Ted Kremenek7dba8602007-08-29 21:56:09 +00001666namespace llvm {
1667template<>
1668struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1669 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1670
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001671#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001672 std::string OutSStr;
1673 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001674 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001675 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001676
1677 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1678
1679 // Process string output to make it nicer...
1680 for (unsigned i = 0; i != OutStr.length(); ++i)
1681 if (OutStr[i] == '\n') { // Left justify
1682 OutStr[i] = '\\';
1683 OutStr.insert(OutStr.begin()+i+1, 'l');
1684 }
1685
1686 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001687#else
1688 return "";
1689#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001690 }
1691};
1692} // end namespace llvm