blob: b75ec68e1584174c81f268cbe9950dc0cf85e101 [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
Ted Kremeneka34ea072008-08-04 22:51:42 +000053/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000054/// The builder is stateful: an instance of the builder should be used to only
55/// construct a single CFG.
56///
57/// Example usage:
58///
59/// CFGBuilder builder;
60/// CFG* cfg = builder.BuildAST(stmt1);
61///
Ted Kremenekc310e932007-08-21 22:06:14 +000062/// CFG construction is done via a recursive walk of an AST.
63/// We actually parse the AST in reverse order so that the successor
64/// of a basic block is constructed prior to its predecessor. This
65/// allows us to nicely capture implicit fall-throughs without extra
66/// basic blocks.
67///
Ted Kremenek6fa9b882008-01-08 18:15:10 +000068class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenekfddd5182007-08-21 21:42:03 +000069 CFG* cfg;
70 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000071 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000072 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000073 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000074 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000075 CFGBlock* DefaultCaseBlock;
Ted Kremenekfddd5182007-08-21 21:42:03 +000076
Ted Kremenek19bb3562007-08-28 19:26:49 +000077 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000078 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
79 LabelMapTy LabelMap;
80
Ted Kremenek19bb3562007-08-28 19:26:49 +000081 // A list of blocks that end with a "goto" that must be backpatched to
82 // their resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000083 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000084 BackpatchBlocksTy BackpatchBlocks;
85
Ted Kremenek19bb3562007-08-28 19:26:49 +000086 // A list of labels whose address has been taken (for indirect gotos).
87 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
88 LabelSetTy AddressTakenLabels;
89
Ted Kremenekfddd5182007-08-21 21:42:03 +000090public:
Ted Kremenek026473c2007-08-23 16:51:22 +000091 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000092 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000093 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenekfddd5182007-08-21 21:42:03 +000094 // Create an empty CFG.
95 cfg = new CFG();
96 }
97
98 ~CFGBuilder() { delete cfg; }
Ted Kremenekfddd5182007-08-21 21:42:03 +000099
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000100 // buildCFG - Used by external clients to construct the CFG.
101 CFG* buildCFG(Stmt* Statement);
Ted Kremenekc310e932007-08-21 22:06:14 +0000102
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000103 // Visitors to walk an AST and construct the CFG. Called by
104 // buildCFG. Do not call directly!
Ted Kremeneke8ee26b2007-08-22 18:22:34 +0000105
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000106 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000107 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek514de5a2008-11-11 17:10:00 +0000108 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
109 CFGBlock* VisitContinueStmt(ContinueStmt* C);
Ted Kremenek295222c2008-02-13 21:46:34 +0000110 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek514de5a2008-11-11 17:10:00 +0000111 CFGBlock* VisitDoStmt(DoStmt* D);
112 CFGBlock* VisitForStmt(ForStmt* F);
113 CFGBlock* VisitGotoStmt(GotoStmt* G);
114 CFGBlock* VisitIfStmt(IfStmt* I);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000115 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek514de5a2008-11-11 17:10:00 +0000116 CFGBlock* VisitLabelStmt(LabelStmt* L);
117 CFGBlock* VisitNullStmt(NullStmt* Statement);
118 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
119 CFGBlock* VisitReturnStmt(ReturnStmt* R);
120 CFGBlock* VisitStmt(Stmt* Statement);
121 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
122 CFGBlock* VisitWhileStmt(WhileStmt* W);
Ted Kremenekfddd5182007-08-21 21:42:03 +0000123
Ted Kremenek4102af92008-03-13 03:04:22 +0000124 // FIXME: Add support for ObjC-specific control-flow structures.
125
Ted Kremenek274f4332008-04-28 18:00:46 +0000126 // NYS == Not Yet Supported
127 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000128 badCFG = true;
129 return Block;
130 }
131
Ted Kremenek274f4332008-04-28 18:00:46 +0000132 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S) { return NYS(); }
133 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) { return NYS(); }
134 CFGBlock* VisitObjCAtFinallyStmt(ObjCAtFinallyStmt* S) { return NYS(); }
135 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) { return NYS(); }
136
137 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
138 return NYS();
Ted Kremenek4102af92008-03-13 03:04:22 +0000139 }
140
Ted Kremenek00c0a302008-09-26 18:17:07 +0000141 // Blocks.
142 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
143 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
144
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000145private:
146 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000147 CFGBlock* addStmt(Stmt* Terminator);
148 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
149 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000150 CFGBlock* WalkAST_VisitDeclSubExpr(ScopedDecl* D);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000151 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000152 void FinishBlock(CFGBlock* B);
Ted Kremeneke8ee26b2007-08-22 18:22:34 +0000153
Ted Kremenek4102af92008-03-13 03:04:22 +0000154 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000155};
Ted Kremenek610a09e2008-09-26 22:58:57 +0000156
157static VariableArrayType* FindVA(Type* t) {
158 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
159 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
160 if (vat->getSizeExpr())
161 return vat;
162
163 t = vt->getElementType().getTypePtr();
164 }
165
166 return 0;
167}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000168
169/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
170/// represent an arbitrary statement. Examples include a single expression
171/// or a function body (compound statement). The ownership of the returned
172/// CFG is transferred to the caller. If CFG construction fails, this method
173/// returns NULL.
174CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek19bb3562007-08-28 19:26:49 +0000175 assert (cfg);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000176 if (!Statement) return NULL;
177
Ted Kremenek4102af92008-03-13 03:04:22 +0000178 badCFG = false;
179
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000180 // Create an empty block that will serve as the exit block for the CFG.
181 // Since this is the first block added to the CFG, it will be implicitly
182 // registered as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000183 Succ = createBlock();
184 assert (Succ == &cfg->getExit());
185 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000186
187 // Visit the statements and create the CFG.
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000188 CFGBlock* B = Visit(Statement);
189 if (!B) B = Succ;
190
191 if (B) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000192 // Finalize the last constructed block. This usually involves
193 // reversing the order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000194 if (Block) FinishBlock(B);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000195
196 // Backpatch the gotos whose label -> block mappings we didn't know
197 // when we encountered them.
198 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
199 E = BackpatchBlocks.end(); I != E; ++I ) {
200
201 CFGBlock* B = *I;
202 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
203 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
204
205 // If there is no target for the goto, then we are looking at an
206 // incomplete AST. Handle this by not registering a successor.
207 if (LI == LabelMap.end()) continue;
208
209 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000210 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000211
Ted Kremenek19bb3562007-08-28 19:26:49 +0000212 // Add successors to the Indirect Goto Dispatch block (if we have one).
213 if (CFGBlock* B = cfg->getIndirectGotoBlock())
214 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
215 E = AddressTakenLabels.end(); I != E; ++I ) {
216
217 // Lookup the target block.
218 LabelMapTy::iterator LI = LabelMap.find(*I);
219
220 // If there is no target block that contains label, then we are looking
221 // at an incomplete AST. Handle this by not registering a successor.
222 if (LI == LabelMap.end()) continue;
223
224 B->addSuccessor(LI->second);
225 }
Ted Kremenek322f58d2007-09-26 21:23:31 +0000226
Ted Kremenek94b33162007-09-17 16:18:02 +0000227 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000228 }
229
230 // Create an empty entry block that has no predecessors.
231 cfg->setEntry(createBlock());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000232
Ted Kremenek4102af92008-03-13 03:04:22 +0000233 if (badCFG) {
234 delete cfg;
235 cfg = NULL;
236 return NULL;
237 }
238
Ted Kremenek322f58d2007-09-26 21:23:31 +0000239 // NULL out cfg so that repeated calls to the builder will fail and that
240 // the ownership of the constructed CFG is passed to the caller.
241 CFG* t = cfg;
242 cfg = NULL;
243 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000244}
245
246/// createBlock - Used to lazily create blocks that are connected
247/// to the current (global) succcessor.
248CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000249 CFGBlock* B = cfg->createBlock();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000250 if (add_successor && Succ) B->addSuccessor(Succ);
251 return B;
252}
253
254/// FinishBlock - When the last statement has been added to the block,
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000255/// we must reverse the statements because they have been inserted
256/// in reverse order.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000257void CFGBuilder::FinishBlock(CFGBlock* B) {
258 assert (B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000259 B->reverseStmts();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000260}
261
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000262/// addStmt - Used to add statements/expressions to the current CFGBlock
263/// "Block". This method calls WalkAST on the passed statement to see if it
264/// contains any short-circuit expressions. If so, it recursively creates
265/// the necessary blocks for such expressions. It returns the "topmost" block
266/// of the created blocks, or the original value of "Block" when this method
267/// was called if no additional blocks are created.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000268CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenekaf603f72007-08-30 18:39:40 +0000269 if (!Block) Block = createBlock();
Ted Kremenek411cdee2008-04-16 21:10:48 +0000270 return WalkAST(Terminator,true);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000271}
272
273/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000274/// add extra blocks for ternary operators, &&, and ||. We also
275/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000276CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
277 switch (Terminator->getStmtClass()) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000278 case Stmt::ConditionalOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000279 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000280
281 // Create the confluence block that will "merge" the results
282 // of the ternary expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000283 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
284 ConfluenceBlock->appendStmt(C);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000285 FinishBlock(ConfluenceBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000286
287 // Create a block for the LHS expression if there is an LHS expression.
288 // A GCC extension allows LHS to be NULL, causing the condition to
289 // be the value that is returned instead.
290 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000291 Succ = ConfluenceBlock;
292 Block = NULL;
Ted Kremenekecc04c92007-11-26 18:20:26 +0000293 CFGBlock* LHSBlock = NULL;
294 if (C->getLHS()) {
295 LHSBlock = Visit(C->getLHS());
296 FinishBlock(LHSBlock);
297 Block = NULL;
298 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000299
Ted Kremenekecc04c92007-11-26 18:20:26 +0000300 // Create the block for the RHS expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000301 Succ = ConfluenceBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000302 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000303 FinishBlock(RHSBlock);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000304
Ted Kremenekecc04c92007-11-26 18:20:26 +0000305 // Create the block that will contain the condition.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000306 Block = createBlock(false);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000307
308 if (LHSBlock)
309 Block->addSuccessor(LHSBlock);
310 else {
311 // If we have no LHS expression, add the ConfluenceBlock as a direct
312 // successor for the block containing the condition. Moreover,
313 // we need to reverse the order of the predecessors in the
314 // ConfluenceBlock because the RHSBlock will have been added to
315 // the succcessors already, and we want the first predecessor to the
316 // the block containing the expression for the case when the ternary
317 // expression evaluates to true.
318 Block->addSuccessor(ConfluenceBlock);
319 assert (ConfluenceBlock->pred_size() == 2);
320 std::reverse(ConfluenceBlock->pred_begin(),
321 ConfluenceBlock->pred_end());
322 }
323
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000324 Block->addSuccessor(RHSBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000325
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000326 Block->setTerminator(C);
327 return addStmt(C->getCond());
328 }
Ted Kremenek49a436d2007-08-31 17:03:41 +0000329
330 case Stmt::ChooseExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000331 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000332
333 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
334 ConfluenceBlock->appendStmt(C);
335 FinishBlock(ConfluenceBlock);
336
337 Succ = ConfluenceBlock;
338 Block = NULL;
339 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000340 FinishBlock(LHSBlock);
341
Ted Kremenek49a436d2007-08-31 17:03:41 +0000342 Succ = ConfluenceBlock;
343 Block = NULL;
344 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000345 FinishBlock(RHSBlock);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000346
347 Block = createBlock(false);
348 Block->addSuccessor(LHSBlock);
349 Block->addSuccessor(RHSBlock);
350 Block->setTerminator(C);
351 return addStmt(C->getCond());
352 }
Ted Kremenek7926f7c2007-08-28 16:18:58 +0000353
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000354 case Stmt::DeclStmtClass: {
Ted Kremenek53061c82008-10-06 20:56:19 +0000355 DeclStmt *DS = cast<DeclStmt>(Terminator);
356 if (DS->hasSolitaryDecl()) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000357 Block->appendStmt(Terminator);
Ted Kremenek53061c82008-10-06 20:56:19 +0000358 return WalkAST_VisitDeclSubExpr(DS->getSolitaryDecl());
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000359 }
360 else {
361 typedef llvm::SmallVector<ScopedDecl*,10> BufTy;
362 BufTy Buf;
363 CFGBlock* B = 0;
Ted Kremenek53061c82008-10-06 20:56:19 +0000364
365 // FIXME: Add a reverse iterator for DeclStmt to avoid this
366 // extra copy.
367 for (DeclStmt::decl_iterator DI=DS->decl_begin(), DE=DS->decl_end();
368 DI != DE; ++DI)
369 Buf.push_back(*DI);
370
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000371 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000372 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
373 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
374 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000375
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000376 // Allocate the DeclStmt using the BumpPtrAllocator. It will
377 // get automatically freed with the CFG. Note that even though
378 // we are using a DeclGroupOwningRef that wraps a singe Decl*,
379 // that Decl* will not get deallocated because the destroy method
380 // of DG is never called.
381 DeclGroupOwningRef DG(*I);
382 ScopedDecl* D = *I;
383 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
384
385 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(),
386 GetEndLoc(D));
387
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000388 // Append the fake DeclStmt to block.
Ted Kremenek8ffb1592008-10-07 23:09:49 +0000389 Block->appendStmt(DS);
390 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000391 }
392 return B;
393 }
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000394 }
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000395
Ted Kremenek19bb3562007-08-28 19:26:49 +0000396 case Stmt::AddrLabelExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000397 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000398 AddressTakenLabels.insert(A->getLabel());
399
Ted Kremenek411cdee2008-04-16 21:10:48 +0000400 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000401 return Block;
402 }
Ted Kremenekf50ec102007-09-11 21:29:43 +0000403
Ted Kremenek15c27a82007-08-28 18:30:10 +0000404 case Stmt::StmtExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000405 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000406
Ted Kremenek610a09e2008-09-26 22:58:57 +0000407 case Stmt::SizeOfAlignOfTypeExprClass: {
408 SizeOfAlignOfTypeExpr* E = cast<SizeOfAlignOfTypeExpr>(Terminator);
409
410 // VLA types have expressions that must be evaluated.
411 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
412 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
413 addStmt(VA->getSizeExpr());
414
415 return Block;
416 }
417
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000418 case Stmt::UnaryOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000419 UnaryOperator* U = cast<UnaryOperator>(Terminator);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000420
421 // sizeof(expressions). For such expressions,
422 // the subexpression is not really evaluated, so
423 // we don't care about control-flow within the sizeof.
424 if (U->getOpcode() == UnaryOperator::SizeOf) {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000425 Block->appendStmt(Terminator);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000426 return Block;
427 }
428
429 break;
430 }
431
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000432 case Stmt::BinaryOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000433 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000434
435 if (B->isLogicalOp()) { // && or ||
436 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
437 ConfluenceBlock->appendStmt(B);
438 FinishBlock(ConfluenceBlock);
439
440 // create the block evaluating the LHS
441 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekafe54332007-12-21 19:49:00 +0000442 LHSBlock->setTerminator(B);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000443
444 // create the block evaluating the RHS
445 Succ = ConfluenceBlock;
446 Block = NULL;
447 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu924d9a82008-10-04 05:48:38 +0000448 FinishBlock(RHSBlock);
Ted Kremenekafe54332007-12-21 19:49:00 +0000449
450 // Now link the LHSBlock with RHSBlock.
451 if (B->getOpcode() == BinaryOperator::LOr) {
452 LHSBlock->addSuccessor(ConfluenceBlock);
453 LHSBlock->addSuccessor(RHSBlock);
454 }
455 else {
456 assert (B->getOpcode() == BinaryOperator::LAnd);
457 LHSBlock->addSuccessor(RHSBlock);
458 LHSBlock->addSuccessor(ConfluenceBlock);
459 }
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000460
461 // Generate the blocks for evaluating the LHS.
462 Block = LHSBlock;
463 return addStmt(B->getLHS());
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000464 }
465 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
466 Block->appendStmt(B);
467 addStmt(B->getRHS());
468 return addStmt(B->getLHS());
Ted Kremenek63f58872007-10-01 19:33:33 +0000469 }
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000470
471 break;
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000472 }
Ted Kremenek00c0a302008-09-26 18:17:07 +0000473
474 // Blocks: No support for blocks ... yet
475 case Stmt::BlockExprClass:
476 case Stmt::BlockDeclRefExprClass:
477 return NYS();
Ted Kremenekf4e15fc2008-02-26 02:37:08 +0000478
479 case Stmt::ParenExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000480 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000481
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000482 default:
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000483 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000484 };
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000485
Ted Kremenek411cdee2008-04-16 21:10:48 +0000486 if (AlwaysAddStmt) Block->appendStmt(Terminator);
487 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000488}
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000489
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000490/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
491/// for initializers in Decls.
492CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(ScopedDecl* D) {
493 VarDecl* VD = dyn_cast<VarDecl>(D);
494
495 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000496 return Block;
497
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000498 Expr* Init = VD->getInit();
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000499
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000500 if (Init) {
501 // Optimization: Don't create separate block-level statements for literals.
502 switch (Init->getStmtClass()) {
503 case Stmt::IntegerLiteralClass:
504 case Stmt::CharacterLiteralClass:
505 case Stmt::StringLiteralClass:
506 break;
507 default:
508 Block = addStmt(Init);
509 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000510 }
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000511
512 // If the type of VD is a VLA, then we must process its size expressions.
513 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
514 VA = FindVA(VA->getElementType().getTypePtr()))
515 Block = addStmt(VA->getSizeExpr());
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000516
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000517 return Block;
518}
519
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000520/// WalkAST_VisitChildren - Utility method to call WalkAST on the
521/// children of a Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000522CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000523 CFGBlock* B = Block;
Ted Kremenek411cdee2008-04-16 21:10:48 +0000524 for (Stmt::child_iterator I = Terminator->child_begin(), E = Terminator->child_end() ;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000525 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000526 if (*I) B = WalkAST(*I);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000527
528 return B;
529}
530
Ted Kremenek15c27a82007-08-28 18:30:10 +0000531/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
532/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000533CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
534 Block->appendStmt(Terminator);
535 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000536}
537
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000538/// VisitStmt - Handle statements with no branching control flow.
539CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
540 // We cannot assume that we are in the middle of a basic block, since
541 // the CFG might only be constructed for this single statement. If
542 // we have no current basic block, just create one lazily.
543 if (!Block) Block = createBlock();
544
545 // Simply add the statement to the current block. We actually
546 // insert statements in reverse order; this order is reversed later
547 // when processing the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000548 addStmt(Statement);
549
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000550 return Block;
551}
552
553CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
554 return Block;
555}
556
557CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000558
559 CFGBlock* LastBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000560
Ted Kremenekd34066c2008-02-26 00:22:58 +0000561 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
562 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000563 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000564 }
565
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000566 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000567}
568
569CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
570 // We may see an if statement in the middle of a basic block, or
571 // it may be the first statement we are processing. In either case,
572 // we create a new basic block. First, we create the blocks for
573 // the then...else statements, and then we create the block containing
574 // the if statement. If we were in the middle of a block, we
575 // stop processing that block and reverse its statements. That block
576 // is then the implicit successor for the "then" and "else" clauses.
577
578 // The block we were proccessing is now finished. Make it the
579 // successor block.
580 if (Block) {
581 Succ = Block;
582 FinishBlock(Block);
583 }
584
585 // Process the false branch. NULL out Block so that the recursive
586 // call to Visit will create a new basic block.
587 // Null out Block so that all successor
588 CFGBlock* ElseBlock = Succ;
589
590 if (Stmt* Else = I->getElse()) {
591 SaveAndRestore<CFGBlock*> sv(Succ);
592
593 // NULL out Block so that the recursive call to Visit will
594 // create a new basic block.
595 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000596 ElseBlock = Visit(Else);
597
598 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
599 ElseBlock = sv.get();
600 else if (Block)
601 FinishBlock(ElseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000602 }
603
604 // Process the true branch. NULL out Block so that the recursive
605 // call to Visit will create a new basic block.
606 // Null out Block so that all successor
607 CFGBlock* ThenBlock;
608 {
609 Stmt* Then = I->getThen();
610 assert (Then);
611 SaveAndRestore<CFGBlock*> sv(Succ);
612 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000613 ThenBlock = Visit(Then);
614
615 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
616 ThenBlock = sv.get();
617 else if (Block)
618 FinishBlock(ThenBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000619 }
620
621 // Now create a new block containing the if statement.
622 Block = createBlock(false);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000623
624 // Set the terminator of the new block to the If statement.
625 Block->setTerminator(I);
626
627 // Now add the successors.
628 Block->addSuccessor(ThenBlock);
629 Block->addSuccessor(ElseBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000630
631 // Add the condition as the last statement in the new block. This
632 // may create new blocks as the condition may contain control-flow. Any
633 // newly created blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000634 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000635}
Ted Kremenekf50ec102007-09-11 21:29:43 +0000636
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000637
638CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
639 // If we were in the middle of a block we stop processing that block
640 // and reverse its statements.
641 //
642 // NOTE: If a "return" appears in the middle of a block, this means
643 // that the code afterwards is DEAD (unreachable). We still
644 // keep a basic block for that code; a simple "mark-and-sweep"
645 // from the entry block will be able to report such dead
646 // blocks.
647 if (Block) FinishBlock(Block);
648
649 // Create the new block.
650 Block = createBlock(false);
651
652 // The Exit block is the only successor.
653 Block->addSuccessor(&cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000654
655 // Add the return statement to the block. This may create new blocks
656 // if R contains control-flow (short-circuit operations).
657 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000658}
659
660CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
661 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000662 Visit(L->getSubStmt());
663 CFGBlock* LabelBlock = Block;
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000664
665 if (!LabelBlock) // This can happen when the body is empty, i.e.
666 LabelBlock=createBlock(); // scopes that only contains NullStmts.
667
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000668 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
669 LabelMap[ L ] = LabelBlock;
670
671 // Labels partition blocks, so this is the end of the basic block
Ted Kremenek9cffe732007-08-29 23:20:49 +0000672 // we were processing (L is the block's label). Because this is
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000673 // label (and we have already processed the substatement) there is no
674 // extra control-flow to worry about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000675 LabelBlock->setLabel(L);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000676 FinishBlock(LabelBlock);
677
678 // We set Block to NULL to allow lazy creation of a new block
679 // (if necessary);
680 Block = NULL;
681
682 // This block is now the implicit successor of other blocks.
683 Succ = LabelBlock;
684
685 return LabelBlock;
686}
687
688CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
689 // Goto is a control-flow statement. Thus we stop processing the
690 // current block and create a new one.
691 if (Block) FinishBlock(Block);
692 Block = createBlock(false);
693 Block->setTerminator(G);
694
695 // If we already know the mapping to the label block add the
696 // successor now.
697 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
698
699 if (I == LabelMap.end())
700 // We will need to backpatch this block later.
701 BackpatchBlocks.push_back(Block);
702 else
703 Block->addSuccessor(I->second);
704
705 return Block;
706}
707
708CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
709 // "for" is a control-flow statement. Thus we stop processing the
710 // current block.
711
712 CFGBlock* LoopSuccessor = NULL;
713
714 if (Block) {
715 FinishBlock(Block);
716 LoopSuccessor = Block;
717 }
718 else LoopSuccessor = Succ;
719
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000720 // Because of short-circuit evaluation, the condition of the loop
721 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
722 // blocks that evaluate the condition.
723 CFGBlock* ExitConditionBlock = createBlock(false);
724 CFGBlock* EntryConditionBlock = ExitConditionBlock;
725
726 // Set the terminator for the "exit" condition block.
727 ExitConditionBlock->setTerminator(F);
728
729 // Now add the actual condition to the condition block. Because the
730 // condition itself may contain control-flow, new blocks may be created.
731 if (Stmt* C = F->getCond()) {
732 Block = ExitConditionBlock;
733 EntryConditionBlock = addStmt(C);
734 if (Block) FinishBlock(EntryConditionBlock);
735 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000736
737 // The condition block is the implicit successor for the loop body as
738 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000739 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000740
741 // Now create the loop body.
742 {
743 assert (F->getBody());
744
745 // Save the current values for Block, Succ, and continue and break targets
746 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
747 save_continue(ContinueTargetBlock),
748 save_break(BreakTargetBlock);
Ted Kremeneke9334502008-09-04 21:48:47 +0000749
Ted Kremenekaf603f72007-08-30 18:39:40 +0000750 // Create a new block to contain the (bottom) of the loop body.
751 Block = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000752
Ted Kremeneke9334502008-09-04 21:48:47 +0000753 if (Stmt* I = F->getInc()) {
754 // Generate increment code in its own basic block. This is the target
755 // of continue statements.
756 Succ = addStmt(I);
757 Block = 0;
758 ContinueTargetBlock = Succ;
759 }
760 else {
761 // No increment code. Continues should go the the entry condition block.
762 ContinueTargetBlock = EntryConditionBlock;
763 }
764
765 // All breaks should go to the code following the loop.
766 BreakTargetBlock = LoopSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000767
768 // Now populate the body block, and in the process create new blocks
769 // as we walk the body of the loop.
770 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000771
772 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000773 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000774 else if (Block)
775 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000776
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000777 // This new body block is a successor to our "exit" condition block.
778 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000779 }
780
781 // Link up the condition block with the code that follows the loop.
782 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000783 ExitConditionBlock->addSuccessor(LoopSuccessor);
784
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000785 // If the loop contains initialization, create a new block for those
786 // statements. This block can also contain statements that precede
787 // the loop.
788 if (Stmt* I = F->getInit()) {
789 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000790 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000791 }
792 else {
793 // There is no loop initialization. We are thus basically a while
794 // loop. NULL out Block to force lazy block construction.
795 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000796 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000797 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000798 }
799}
800
Ted Kremenek514de5a2008-11-11 17:10:00 +0000801CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
802 // Objective-C fast enumeration 'for' statements:
803 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
804 //
805 // for ( Type newVariable in collection_expression ) { statements }
806 //
807 // becomes:
808 //
809 // prologue:
810 // 1. collection_expression
811 // T. jump to loop_entry
812 // loop_entry:
813 // 1. ObjCForCollectionStmt [performs binding to newVariable]
814 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
815 // TB:
816 // statements
817 // T. jump to loop_entry
818 // FB:
819 // what comes after
820 //
821 // and
822 //
823 // Type existingItem;
824 // for ( existingItem in expression ) { statements }
825 //
826 // becomes:
827 //
828 // the same with newVariable replaced with existingItem; the binding
829 // works the same except that for one ObjCForCollectionStmt::getElement()
830 // returns a DeclStmt and the other returns a DeclRefExpr.
831 //
832
833 CFGBlock* LoopSuccessor = 0;
834
835 if (Block) {
836 FinishBlock(Block);
837 LoopSuccessor = Block;
838 Block = 0;
839 }
840 else LoopSuccessor = Succ;
841
842 // Build the condition block. The condition has no short-circuit evaluation,
843 // so we don't need multiple blocks like other control-flow structures with
844 // conditions.
845 CFGBlock* ConditionBlock = createBlock(false);
846 ConditionBlock->appendStmt(S);
847 ConditionBlock->setTerminator(S); // No need to call FinishBlock; 1 stmt
848
849 // Now create the true branch.
850 Succ = ConditionBlock;
851 CFGBlock* BodyBlock = addStmt(S->getBody());
852 FinishBlock(BodyBlock);
853
854 // Connect up the condition block
855 ConditionBlock->addSuccessor(Block);
856 ConditionBlock->addSuccessor(LoopSuccessor);
857
858 // Now create a prologue block to contain the collection expression.
859 Block = 0;
860 Succ = ConditionBlock;
861 return addStmt(S->getCollection());
862}
863
864
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000865CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
866 // "while" is a control-flow statement. Thus we stop processing the
867 // current block.
868
869 CFGBlock* LoopSuccessor = NULL;
870
871 if (Block) {
872 FinishBlock(Block);
873 LoopSuccessor = Block;
874 }
875 else LoopSuccessor = Succ;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000876
877 // Because of short-circuit evaluation, the condition of the loop
878 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
879 // blocks that evaluate the condition.
880 CFGBlock* ExitConditionBlock = createBlock(false);
881 CFGBlock* EntryConditionBlock = ExitConditionBlock;
882
883 // Set the terminator for the "exit" condition block.
884 ExitConditionBlock->setTerminator(W);
885
886 // Now add the actual condition to the condition block. Because the
887 // condition itself may contain control-flow, new blocks may be created.
888 // Thus we update "Succ" after adding the condition.
889 if (Stmt* C = W->getCond()) {
890 Block = ExitConditionBlock;
891 EntryConditionBlock = addStmt(C);
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000892 assert (Block == EntryConditionBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000893 if (Block) FinishBlock(EntryConditionBlock);
894 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000895
896 // The condition block is the implicit successor for the loop body as
897 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000898 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000899
900 // Process the loop body.
901 {
902 assert (W->getBody());
903
904 // Save the current values for Block, Succ, and continue and break targets
905 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
906 save_continue(ContinueTargetBlock),
907 save_break(BreakTargetBlock);
908
909 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000910 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000911
912 // All breaks should go to the code following the loop.
913 BreakTargetBlock = LoopSuccessor;
914
915 // NULL out Block to force lazy instantiation of blocks for the body.
916 Block = NULL;
917
918 // Create the body. The returned block is the entry to the loop body.
919 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000920
921 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000922 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000923 else if (Block)
924 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000925
926 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000927 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000928 }
929
930 // Link up the condition block with the code that follows the loop.
931 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000932 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000933
934 // There can be no more statements in the condition block
935 // since we loop back to this block. NULL out Block to force
936 // lazy creation of another block.
937 Block = NULL;
938
939 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +0000940 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000941 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000942}
943
944CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
945 // "do...while" is a control-flow statement. Thus we stop processing the
946 // current block.
947
948 CFGBlock* LoopSuccessor = NULL;
949
950 if (Block) {
951 FinishBlock(Block);
952 LoopSuccessor = Block;
953 }
954 else LoopSuccessor = Succ;
955
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000956 // Because of short-circuit evaluation, the condition of the loop
957 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
958 // blocks that evaluate the condition.
959 CFGBlock* ExitConditionBlock = createBlock(false);
960 CFGBlock* EntryConditionBlock = ExitConditionBlock;
961
962 // Set the terminator for the "exit" condition block.
963 ExitConditionBlock->setTerminator(D);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000964
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000965 // Now add the actual condition to the condition block. Because the
966 // condition itself may contain control-flow, new blocks may be created.
967 if (Stmt* C = D->getCond()) {
968 Block = ExitConditionBlock;
969 EntryConditionBlock = addStmt(C);
970 if (Block) FinishBlock(EntryConditionBlock);
971 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000972
Ted Kremenek54827132008-02-27 07:20:00 +0000973 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000974 Succ = EntryConditionBlock;
975
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000976 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000977 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000978 {
979 assert (D->getBody());
980
981 // Save the current values for Block, Succ, and continue and break targets
982 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
983 save_continue(ContinueTargetBlock),
984 save_break(BreakTargetBlock);
985
986 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000987 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000988
989 // All breaks should go to the code following the loop.
990 BreakTargetBlock = LoopSuccessor;
991
992 // NULL out Block to force lazy instantiation of blocks for the body.
993 Block = NULL;
994
995 // Create the body. The returned block is the entry to the loop body.
996 BodyBlock = Visit(D->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000997
Ted Kremenekaf603f72007-08-30 18:39:40 +0000998 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000999 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenekaf603f72007-08-30 18:39:40 +00001000 else if (Block)
1001 FinishBlock(BodyBlock);
1002
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001003 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001004 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001005 }
1006
1007 // Link up the condition block with the code that follows the loop.
1008 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001009 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001010
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001011 // There can be no more statements in the body block(s)
1012 // since we loop back to the body. NULL out Block to force
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001013 // lazy creation of another block.
1014 Block = NULL;
1015
1016 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001017 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001018 return BodyBlock;
1019}
1020
1021CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1022 // "continue" is a control-flow statement. Thus we stop processing the
1023 // current block.
1024 if (Block) FinishBlock(Block);
1025
1026 // Now create a new block that ends with the continue statement.
1027 Block = createBlock(false);
1028 Block->setTerminator(C);
1029
1030 // If there is no target for the continue, then we are looking at an
1031 // incomplete AST. Handle this by not registering a successor.
1032 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
1033
1034 return Block;
1035}
1036
1037CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1038 // "break" is a control-flow statement. Thus we stop processing the
1039 // current block.
1040 if (Block) FinishBlock(Block);
1041
1042 // Now create a new block that ends with the continue statement.
1043 Block = createBlock(false);
1044 Block->setTerminator(B);
1045
1046 // If there is no target for the break, then we are looking at an
1047 // incomplete AST. Handle this by not registering a successor.
1048 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1049
1050 return Block;
1051}
1052
Ted Kremenek411cdee2008-04-16 21:10:48 +00001053CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001054 // "switch" is a control-flow statement. Thus we stop processing the
1055 // current block.
1056 CFGBlock* SwitchSuccessor = NULL;
1057
1058 if (Block) {
1059 FinishBlock(Block);
1060 SwitchSuccessor = Block;
1061 }
1062 else SwitchSuccessor = Succ;
1063
1064 // Save the current "switch" context.
1065 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001066 save_break(BreakTargetBlock),
1067 save_default(DefaultCaseBlock);
1068
1069 // Set the "default" case to be the block after the switch statement.
1070 // If the switch statement contains a "default:", this value will
1071 // be overwritten with the block for that code.
1072 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenek295222c2008-02-13 21:46:34 +00001073
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001074 // Create a new block that will contain the switch statement.
1075 SwitchTerminatedBlock = createBlock(false);
1076
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001077 // Now process the switch body. The code after the switch is the implicit
1078 // successor.
1079 Succ = SwitchSuccessor;
1080 BreakTargetBlock = SwitchSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001081
1082 // When visiting the body, the case statements should automatically get
1083 // linked up to the switch. We also don't keep a pointer to the body,
1084 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001085 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001086 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001087 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001088 if (Block) FinishBlock(BodyBlock);
1089
Ted Kremenek295222c2008-02-13 21:46:34 +00001090 // If we have no "default:" case, the default transition is to the
1091 // code following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001092 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenek295222c2008-02-13 21:46:34 +00001093
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001094 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001095 SwitchTerminatedBlock->setTerminator(Terminator);
1096 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001097 Block = SwitchTerminatedBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001098
Ted Kremenek411cdee2008-04-16 21:10:48 +00001099 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001100}
1101
Ted Kremenek411cdee2008-04-16 21:10:48 +00001102CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001103 // CaseStmts are essentially labels, so they are the
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001104 // first statement in a block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001105
Ted Kremenek411cdee2008-04-16 21:10:48 +00001106 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001107 CFGBlock* CaseBlock = Block;
1108 if (!CaseBlock) CaseBlock = createBlock();
1109
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001110 // Cases statements partition blocks, so this is the top of
1111 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001112 CaseBlock->setLabel(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001113 FinishBlock(CaseBlock);
1114
1115 // Add this block to the list of successors for the block with the
1116 // switch statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001117 assert (SwitchTerminatedBlock);
1118 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001119
1120 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1121 Block = NULL;
1122
1123 // This block is now the implicit successor of other blocks.
1124 Succ = CaseBlock;
1125
Ted Kremenek2677ea82008-03-15 07:45:02 +00001126 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001127}
Ted Kremenek295222c2008-02-13 21:46:34 +00001128
Ted Kremenek411cdee2008-04-16 21:10:48 +00001129CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1130 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001131 DefaultCaseBlock = Block;
1132 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1133
1134 // Default statements partition blocks, so this is the top of
1135 // the basic block we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001136 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001137 FinishBlock(DefaultCaseBlock);
1138
1139 // Unlike case statements, we don't add the default block to the
1140 // successors for the switch statement immediately. This is done
1141 // when we finish processing the switch statement. This allows for
1142 // the default case (including a fall-through to the code after the
1143 // switch statement) to always be the last successor of a switch-terminated
1144 // block.
1145
1146 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1147 Block = NULL;
1148
1149 // This block is now the implicit successor of other blocks.
1150 Succ = DefaultCaseBlock;
1151
1152 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001153}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001154
Ted Kremenek19bb3562007-08-28 19:26:49 +00001155CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1156 // Lazily create the indirect-goto dispatch block if there isn't one
1157 // already.
1158 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1159
1160 if (!IBlock) {
1161 IBlock = createBlock(false);
1162 cfg->setIndirectGotoBlock(IBlock);
1163 }
1164
1165 // IndirectGoto is a control-flow statement. Thus we stop processing the
1166 // current block and create a new one.
1167 if (Block) FinishBlock(Block);
1168 Block = createBlock(false);
1169 Block->setTerminator(I);
1170 Block->addSuccessor(IBlock);
1171 return addStmt(I->getTarget());
1172}
1173
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001174
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001175} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001176
1177/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1178/// block has no successors or predecessors. If this is the first block
1179/// created in the CFG, it is automatically set to be the Entry and Exit
1180/// of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001181CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001182 bool first_block = begin() == end();
1183
1184 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001185 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001186
1187 // If this is the first block, set it as the Entry and Exit.
1188 if (first_block) Entry = Exit = &front();
1189
1190 // Return the block.
1191 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001192}
1193
Ted Kremenek026473c2007-08-23 16:51:22 +00001194/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1195/// CFG is returned to the caller.
1196CFG* CFG::buildCFG(Stmt* Statement) {
1197 CFGBuilder Builder;
1198 return Builder.buildCFG(Statement);
1199}
1200
1201/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001202void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1203
Ted Kremenek63f58872007-10-01 19:33:33 +00001204//===----------------------------------------------------------------------===//
1205// CFG: Queries for BlkExprs.
1206//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001207
Ted Kremenek63f58872007-10-01 19:33:33 +00001208namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001209 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001210}
1211
Ted Kremenek411cdee2008-04-16 21:10:48 +00001212static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1213 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001214 return;
1215
Ted Kremenek411cdee2008-04-16 21:10:48 +00001216 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001217 if (!*I) continue;
1218
1219 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1220 if (B->isAssignmentOp()) Set.insert(B);
1221
1222 FindSubExprAssignments(*I, Set);
1223 }
1224}
1225
Ted Kremenek63f58872007-10-01 19:33:33 +00001226static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1227 BlkExprMapTy* M = new BlkExprMapTy();
1228
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001229 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek411cdee2008-04-16 21:10:48 +00001230 // only assignments that we want to *possibly* register as a block-level
1231 // expression. Basically, if an assignment occurs both in a subexpression
1232 // and at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001233 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1234
Ted Kremenek63f58872007-10-01 19:33:33 +00001235 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1236 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001237 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001238
Ted Kremenek411cdee2008-04-16 21:10:48 +00001239 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1240
1241 // Iterate over the statements again on identify the Expr* and Stmt* at
1242 // the block-level that are block-level expressions.
1243
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001244 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001245 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001246
Ted Kremenek411cdee2008-04-16 21:10:48 +00001247 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001248 // Assignment expressions that are not nested within another
1249 // expression are really "statements" whose value is never
1250 // used by another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001251 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001252 continue;
1253 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001254 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001255 // Special handling for statement expressions. The last statement
1256 // in the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001257 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001258 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001259 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001260 (*M)[C->body_back()] = x;
1261 }
1262 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001263
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001264 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001265 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001266 }
1267
Ted Kremenek411cdee2008-04-16 21:10:48 +00001268 // Look at terminators. The condition is a block-level expression.
1269
1270 Expr* Exp = I->getTerminatorCondition();
1271
1272 if (Exp && M->find(Exp) == M->end()) {
1273 unsigned x = M->size();
1274 (*M)[Exp] = x;
1275 }
1276 }
1277
Ted Kremenek63f58872007-10-01 19:33:33 +00001278 return M;
1279}
1280
Ted Kremenek86946742008-01-17 20:48:37 +00001281CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1282 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001283 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1284
1285 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001286 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek63f58872007-10-01 19:33:33 +00001287
1288 if (I == M->end()) return CFG::BlkExprNumTy();
1289 else return CFG::BlkExprNumTy(I->second);
1290}
1291
1292unsigned CFG::getNumBlkExprs() {
1293 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1294 return M->size();
1295 else {
1296 // We assume callers interested in the number of BlkExprs will want
1297 // the map constructed if it doesn't already exist.
1298 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1299 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1300 }
1301}
1302
Ted Kremenek274f4332008-04-28 18:00:46 +00001303//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001304// Cleanup: CFG dstor.
1305//===----------------------------------------------------------------------===//
1306
Ted Kremenek63f58872007-10-01 19:33:33 +00001307CFG::~CFG() {
1308 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1309}
1310
Ted Kremenek7dba8602007-08-29 21:56:09 +00001311//===----------------------------------------------------------------------===//
1312// CFG pretty printing
1313//===----------------------------------------------------------------------===//
1314
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001315namespace {
1316
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001317class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001318
Ted Kremenek42a509f2007-08-31 21:30:12 +00001319 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1320 StmtMapTy StmtMap;
1321 signed CurrentBlock;
1322 unsigned CurrentStmt;
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001323
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001324public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001325
Ted Kremenek42a509f2007-08-31 21:30:12 +00001326 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1327 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1328 unsigned j = 1;
1329 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1330 BI != BEnd; ++BI, ++j )
1331 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1332 }
1333 }
1334
1335 virtual ~StmtPrinterHelper() {}
1336
1337 void setBlockID(signed i) { CurrentBlock = i; }
1338 void setStmtID(unsigned i) { CurrentStmt = i; }
1339
Ted Kremeneka95d3752008-09-13 05:16:45 +00001340 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001341
Ted Kremenek411cdee2008-04-16 21:10:48 +00001342 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001343
1344 if (I == StmtMap.end())
1345 return false;
1346
1347 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1348 && I->second.second == CurrentStmt)
1349 return false;
1350
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001351 OS << "[B" << I->second.first << "." << I->second.second << "]";
1352 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001353 }
1354};
1355
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001356class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1357 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1358
Ted Kremeneka95d3752008-09-13 05:16:45 +00001359 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001360 StmtPrinterHelper* Helper;
1361public:
Ted Kremeneka95d3752008-09-13 05:16:45 +00001362 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek42a509f2007-08-31 21:30:12 +00001363 : OS(os), Helper(helper) {}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001364
1365 void VisitIfStmt(IfStmt* I) {
1366 OS << "if ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001367 I->getCond()->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001368 }
1369
1370 // Default case.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001371 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001372
1373 void VisitForStmt(ForStmt* F) {
1374 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001375 if (F->getInit()) OS << "...";
1376 OS << "; ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001377 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek535bb202007-08-30 21:28:02 +00001378 OS << "; ";
1379 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001380 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001381 }
1382
1383 void VisitWhileStmt(WhileStmt* W) {
1384 OS << "while " ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001385 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001386 }
1387
1388 void VisitDoStmt(DoStmt* D) {
1389 OS << "do ... while ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001390 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001391 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001392
Ted Kremenek411cdee2008-04-16 21:10:48 +00001393 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001394 OS << "switch ";
Ted Kremenek411cdee2008-04-16 21:10:48 +00001395 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001396 }
1397
Ted Kremenek805e9a82007-08-31 21:49:40 +00001398 void VisitConditionalOperator(ConditionalOperator* C) {
1399 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001400 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001401 }
1402
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001403 void VisitChooseExpr(ChooseExpr* C) {
1404 OS << "__builtin_choose_expr( ";
1405 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001406 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001407 }
1408
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001409 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1410 OS << "goto *";
1411 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001412 }
1413
Ted Kremenek805e9a82007-08-31 21:49:40 +00001414 void VisitBinaryOperator(BinaryOperator* B) {
1415 if (!B->isLogicalOp()) {
1416 VisitExpr(B);
1417 return;
1418 }
1419
1420 B->getLHS()->printPretty(OS,Helper);
1421
1422 switch (B->getOpcode()) {
1423 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001424 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001425 return;
1426 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001427 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001428 return;
1429 default:
1430 assert(false && "Invalid logical operator.");
1431 }
1432 }
1433
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001434 void VisitExpr(Expr* E) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001435 E->printPretty(OS,Helper);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001436 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001437};
Ted Kremenek42a509f2007-08-31 21:30:12 +00001438
1439
Ted Kremeneka95d3752008-09-13 05:16:45 +00001440void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001441 if (Helper) {
1442 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001443 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001444 CompoundStmt* Sub = SE->getSubStmt();
1445
1446 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001447 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001448 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001449 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001450 return;
1451 }
1452 }
1453
1454 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001455 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001456 if (B->getOpcode() == BinaryOperator::Comma) {
1457 OS << "... , ";
1458 Helper->handledStmt(B->getRHS(),OS);
1459 OS << '\n';
1460 return;
1461 }
1462 }
1463 }
1464
Ted Kremenek411cdee2008-04-16 21:10:48 +00001465 Terminator->printPretty(OS, Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001466
1467 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001468 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001469}
1470
Ted Kremeneka95d3752008-09-13 05:16:45 +00001471void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek42a509f2007-08-31 21:30:12 +00001472 StmtPrinterHelper* Helper, bool print_edges) {
1473
1474 if (Helper) Helper->setBlockID(B.getBlockID());
1475
Ted Kremenek7dba8602007-08-29 21:56:09 +00001476 // Print the header.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001477 OS << "\n [ B" << B.getBlockID();
1478
1479 if (&B == &cfg->getEntry())
1480 OS << " (ENTRY) ]\n";
1481 else if (&B == &cfg->getExit())
1482 OS << " (EXIT) ]\n";
1483 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001484 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001485 else
1486 OS << " ]\n";
1487
Ted Kremenek9cffe732007-08-29 23:20:49 +00001488 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001489 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001490
1491 if (print_edges)
1492 OS << " ";
1493
Ted Kremenek411cdee2008-04-16 21:10:48 +00001494 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001495 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001496 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001497 OS << "case ";
1498 C->getLHS()->printPretty(OS);
1499 if (C->getRHS()) {
1500 OS << " ... ";
1501 C->getRHS()->printPretty(OS);
1502 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001503 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001504 else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001505 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001506 else
1507 assert(false && "Invalid label statement in CFGBlock.");
1508
Ted Kremenek9cffe732007-08-29 23:20:49 +00001509 OS << ":\n";
1510 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001511
Ted Kremenekfddd5182007-08-21 21:42:03 +00001512 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001513 unsigned j = 1;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001514
1515 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1516 I != E ; ++I, ++j ) {
1517
Ted Kremenek9cffe732007-08-29 23:20:49 +00001518 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001519 if (print_edges)
1520 OS << " ";
1521
Ted Kremeneka95d3752008-09-13 05:16:45 +00001522 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001523
1524 if (Helper)
1525 Helper->setStmtID(j);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001526
1527 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001528 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001529
Ted Kremenek9cffe732007-08-29 23:20:49 +00001530 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001531 if (B.getTerminator()) {
1532 if (print_edges)
1533 OS << " ";
1534
Ted Kremenek9cffe732007-08-29 23:20:49 +00001535 OS << " T: ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001536
1537 if (Helper) Helper->setBlockID(-1);
1538
1539 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1540 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001541 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001542 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001543
Ted Kremenek9cffe732007-08-29 23:20:49 +00001544 if (print_edges) {
1545 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001546 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001547 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001548
Ted Kremenek42a509f2007-08-31 21:30:12 +00001549 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1550 I != E; ++I, ++i) {
1551
1552 if (i == 8 || (i-8) == 0)
1553 OS << "\n ";
1554
Ted Kremenek9cffe732007-08-29 23:20:49 +00001555 OS << " B" << (*I)->getBlockID();
1556 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001557
1558 OS << '\n';
1559
1560 // Print the successors of this block.
1561 OS << " Successors (" << B.succ_size() << "):";
1562 i = 0;
1563
1564 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1565 I != E; ++I, ++i) {
1566
1567 if (i == 8 || (i-8) % 10 == 0)
1568 OS << "\n ";
1569
1570 OS << " B" << (*I)->getBlockID();
1571 }
1572
Ted Kremenek9cffe732007-08-29 23:20:49 +00001573 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001574 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001575}
1576
1577} // end anonymous namespace
1578
1579/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001580void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001581
1582/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001583void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001584
1585 StmtPrinterHelper Helper(this);
1586
1587 // Print the entry block.
1588 print_block(OS, this, getEntry(), &Helper, true);
1589
1590 // Iterate through the CFGBlocks and print them one by one.
1591 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1592 // Skip the entry block, because we already printed it.
1593 if (&(*I) == &getEntry() || &(*I) == &getExit())
1594 continue;
1595
1596 print_block(OS, this, *I, &Helper, true);
1597 }
1598
1599 // Print the exit block.
1600 print_block(OS, this, getExit(), &Helper, true);
1601}
1602
1603/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001604void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001605
1606/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1607/// Generally this will only be called from CFG::print.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001608void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001609 StmtPrinterHelper Helper(cfg);
1610 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001611}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001612
Ted Kremeneka2925852008-01-30 23:02:42 +00001613/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001614void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremeneka2925852008-01-30 23:02:42 +00001615 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1616 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1617}
1618
Ted Kremenek411cdee2008-04-16 21:10:48 +00001619Expr* CFGBlock::getTerminatorCondition() {
1620
1621 if (!Terminator)
1622 return NULL;
1623
1624 Expr* E = NULL;
1625
1626 switch (Terminator->getStmtClass()) {
1627 default:
1628 break;
1629
1630 case Stmt::ForStmtClass:
1631 E = cast<ForStmt>(Terminator)->getCond();
1632 break;
1633
1634 case Stmt::WhileStmtClass:
1635 E = cast<WhileStmt>(Terminator)->getCond();
1636 break;
1637
1638 case Stmt::DoStmtClass:
1639 E = cast<DoStmt>(Terminator)->getCond();
1640 break;
1641
1642 case Stmt::IfStmtClass:
1643 E = cast<IfStmt>(Terminator)->getCond();
1644 break;
1645
1646 case Stmt::ChooseExprClass:
1647 E = cast<ChooseExpr>(Terminator)->getCond();
1648 break;
1649
1650 case Stmt::IndirectGotoStmtClass:
1651 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1652 break;
1653
1654 case Stmt::SwitchStmtClass:
1655 E = cast<SwitchStmt>(Terminator)->getCond();
1656 break;
1657
1658 case Stmt::ConditionalOperatorClass:
1659 E = cast<ConditionalOperator>(Terminator)->getCond();
1660 break;
1661
1662 case Stmt::BinaryOperatorClass: // '&&' and '||'
1663 E = cast<BinaryOperator>(Terminator)->getLHS();
1664 break;
1665 }
1666
1667 return E ? E->IgnoreParens() : NULL;
1668}
1669
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001670bool CFGBlock::hasBinaryBranchTerminator() const {
1671
1672 if (!Terminator)
1673 return false;
1674
1675 Expr* E = NULL;
1676
1677 switch (Terminator->getStmtClass()) {
1678 default:
1679 return false;
1680
1681 case Stmt::ForStmtClass:
1682 case Stmt::WhileStmtClass:
1683 case Stmt::DoStmtClass:
1684 case Stmt::IfStmtClass:
1685 case Stmt::ChooseExprClass:
1686 case Stmt::ConditionalOperatorClass:
1687 case Stmt::BinaryOperatorClass:
1688 return true;
1689 }
1690
1691 return E ? E->IgnoreParens() : NULL;
1692}
1693
Ted Kremeneka2925852008-01-30 23:02:42 +00001694
Ted Kremenek7dba8602007-08-29 21:56:09 +00001695//===----------------------------------------------------------------------===//
1696// CFG Graphviz Visualization
1697//===----------------------------------------------------------------------===//
1698
Ted Kremenek42a509f2007-08-31 21:30:12 +00001699
1700#ifndef NDEBUG
Chris Lattner00123512007-09-17 06:16:32 +00001701static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001702#endif
1703
1704void CFG::viewCFG() const {
1705#ifndef NDEBUG
1706 StmtPrinterHelper H(this);
1707 GraphHelper = &H;
1708 llvm::ViewGraph(this,"CFG");
1709 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001710#endif
1711}
1712
Ted Kremenek7dba8602007-08-29 21:56:09 +00001713namespace llvm {
1714template<>
1715struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1716 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1717
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001718#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001719 std::string OutSStr;
1720 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001721 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001722 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001723
1724 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1725
1726 // Process string output to make it nicer...
1727 for (unsigned i = 0; i != OutStr.length(); ++i)
1728 if (OutStr[i] == '\n') { // Left justify
1729 OutStr[i] = '\\';
1730 OutStr.insert(OutStr.begin()+i+1, 'l');
1731 }
1732
1733 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001734#else
1735 return "";
1736#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001737 }
1738};
1739} // end namespace llvm