blob: 804c2d2bd5eaf0d1a9dded119a684bd80af9ffe5 [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
Douglas Gregor4afa39d2009-01-20 01:17:11 +000045static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000046 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(); }
Ted Kremenek2fda5042008-12-09 20:20:09 +0000135
136 // FIXME: This is not completely supported. We basically @throw like
137 // a 'return'.
138 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenek274f4332008-04-28 18:00:46 +0000139
140 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S){
141 return NYS();
Ted Kremenek4102af92008-03-13 03:04:22 +0000142 }
143
Ted Kremenek00c0a302008-09-26 18:17:07 +0000144 // Blocks.
145 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
146 CFGBlock* VisitBlockDeclRefExpr(BlockDeclRefExpr* E) { return NYS(); }
147
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000148private:
149 CFGBlock* createBlock(bool add_successor = true);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000150 CFGBlock* addStmt(Stmt* Terminator);
151 CFGBlock* WalkAST(Stmt* Terminator, bool AlwaysAddStmt);
152 CFGBlock* WalkAST_VisitChildren(Stmt* Terminator);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000153 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000154 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000155 void FinishBlock(CFGBlock* B);
Ted Kremeneke8ee26b2007-08-22 18:22:34 +0000156
Ted Kremenek4102af92008-03-13 03:04:22 +0000157 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000158};
Ted Kremenek610a09e2008-09-26 22:58:57 +0000159
Douglas Gregor898574e2008-12-05 23:32:09 +0000160// FIXME: Add support for dependent-sized array types in C++?
161// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000162static VariableArrayType* FindVA(Type* t) {
163 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
164 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
165 if (vat->getSizeExpr())
166 return vat;
167
168 t = vt->getElementType().getTypePtr();
169 }
170
171 return 0;
172}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000173
174/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can
175/// represent an arbitrary statement. Examples include a single expression
176/// or a function body (compound statement). The ownership of the returned
177/// CFG is transferred to the caller. If CFG construction fails, this method
178/// returns NULL.
179CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek19bb3562007-08-28 19:26:49 +0000180 assert (cfg);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000181 if (!Statement) return NULL;
182
Ted Kremenek4102af92008-03-13 03:04:22 +0000183 badCFG = false;
184
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000185 // Create an empty block that will serve as the exit block for the CFG.
186 // Since this is the first block added to the CFG, it will be implicitly
187 // registered as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000188 Succ = createBlock();
189 assert (Succ == &cfg->getExit());
190 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000191
192 // Visit the statements and create the CFG.
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000193 CFGBlock* B = Visit(Statement);
194 if (!B) B = Succ;
195
196 if (B) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000197 // Finalize the last constructed block. This usually involves
198 // reversing the order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000199 if (Block) FinishBlock(B);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000200
201 // Backpatch the gotos whose label -> block mappings we didn't know
202 // when we encountered them.
203 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
204 E = BackpatchBlocks.end(); I != E; ++I ) {
205
206 CFGBlock* B = *I;
207 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
208 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
209
210 // If there is no target for the goto, then we are looking at an
211 // incomplete AST. Handle this by not registering a successor.
212 if (LI == LabelMap.end()) continue;
213
214 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000215 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000216
Ted Kremenek19bb3562007-08-28 19:26:49 +0000217 // Add successors to the Indirect Goto Dispatch block (if we have one).
218 if (CFGBlock* B = cfg->getIndirectGotoBlock())
219 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
220 E = AddressTakenLabels.end(); I != E; ++I ) {
221
222 // Lookup the target block.
223 LabelMapTy::iterator LI = LabelMap.find(*I);
224
225 // If there is no target block that contains label, then we are looking
226 // at an incomplete AST. Handle this by not registering a successor.
227 if (LI == LabelMap.end()) continue;
228
229 B->addSuccessor(LI->second);
230 }
Ted Kremenek322f58d2007-09-26 21:23:31 +0000231
Ted Kremenek94b33162007-09-17 16:18:02 +0000232 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000233 }
234
235 // Create an empty entry block that has no predecessors.
236 cfg->setEntry(createBlock());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000237
Ted Kremenek4102af92008-03-13 03:04:22 +0000238 if (badCFG) {
239 delete cfg;
240 cfg = NULL;
241 return NULL;
242 }
243
Ted Kremenek322f58d2007-09-26 21:23:31 +0000244 // NULL out cfg so that repeated calls to the builder will fail and that
245 // the ownership of the constructed CFG is passed to the caller.
246 CFG* t = cfg;
247 cfg = NULL;
248 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000249}
250
251/// createBlock - Used to lazily create blocks that are connected
252/// to the current (global) succcessor.
253CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000254 CFGBlock* B = cfg->createBlock();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000255 if (add_successor && Succ) B->addSuccessor(Succ);
256 return B;
257}
258
259/// FinishBlock - When the last statement has been added to the block,
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000260/// we must reverse the statements because they have been inserted
261/// in reverse order.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000262void CFGBuilder::FinishBlock(CFGBlock* B) {
263 assert (B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000264 B->reverseStmts();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000265}
266
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000267/// addStmt - Used to add statements/expressions to the current CFGBlock
268/// "Block". This method calls WalkAST on the passed statement to see if it
269/// contains any short-circuit expressions. If so, it recursively creates
270/// the necessary blocks for such expressions. It returns the "topmost" block
271/// of the created blocks, or the original value of "Block" when this method
272/// was called if no additional blocks are created.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000273CFGBlock* CFGBuilder::addStmt(Stmt* Terminator) {
Ted Kremenekaf603f72007-08-30 18:39:40 +0000274 if (!Block) Block = createBlock();
Ted Kremenek411cdee2008-04-16 21:10:48 +0000275 return WalkAST(Terminator,true);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000276}
277
278/// WalkAST - Used by addStmt to walk the subtree of a statement and
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000279/// add extra blocks for ternary operators, &&, and ||. We also
280/// process "," and DeclStmts (which may contain nested control-flow).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000281CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
282 switch (Terminator->getStmtClass()) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000283 case Stmt::ConditionalOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000284 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000285
286 // Create the confluence block that will "merge" the results
287 // of the ternary expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000288 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
289 ConfluenceBlock->appendStmt(C);
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000290 FinishBlock(ConfluenceBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000291
292 // Create a block for the LHS expression if there is an LHS expression.
293 // A GCC extension allows LHS to be NULL, causing the condition to
294 // be the value that is returned instead.
295 // e.g: x ?: y is shorthand for: x ? x : y;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000296 Succ = ConfluenceBlock;
297 Block = NULL;
Ted Kremenekecc04c92007-11-26 18:20:26 +0000298 CFGBlock* LHSBlock = NULL;
299 if (C->getLHS()) {
300 LHSBlock = Visit(C->getLHS());
301 FinishBlock(LHSBlock);
302 Block = NULL;
303 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000304
Ted Kremenekecc04c92007-11-26 18:20:26 +0000305 // Create the block for the RHS expression.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000306 Succ = ConfluenceBlock;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000307 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000308 FinishBlock(RHSBlock);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000309
Ted Kremenekecc04c92007-11-26 18:20:26 +0000310 // Create the block that will contain the condition.
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000311 Block = createBlock(false);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000312
313 if (LHSBlock)
314 Block->addSuccessor(LHSBlock);
315 else {
316 // If we have no LHS expression, add the ConfluenceBlock as a direct
317 // successor for the block containing the condition. Moreover,
318 // we need to reverse the order of the predecessors in the
319 // ConfluenceBlock because the RHSBlock will have been added to
320 // the succcessors already, and we want the first predecessor to the
321 // the block containing the expression for the case when the ternary
322 // expression evaluates to true.
323 Block->addSuccessor(ConfluenceBlock);
324 assert (ConfluenceBlock->pred_size() == 2);
325 std::reverse(ConfluenceBlock->pred_begin(),
326 ConfluenceBlock->pred_end());
327 }
328
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000329 Block->addSuccessor(RHSBlock);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000330
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000331 Block->setTerminator(C);
332 return addStmt(C->getCond());
333 }
Ted Kremenek49a436d2007-08-31 17:03:41 +0000334
335 case Stmt::ChooseExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000336 ChooseExpr* C = cast<ChooseExpr>(Terminator);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000337
338 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
339 ConfluenceBlock->appendStmt(C);
340 FinishBlock(ConfluenceBlock);
341
342 Succ = ConfluenceBlock;
343 Block = NULL;
344 CFGBlock* LHSBlock = Visit(C->getLHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000345 FinishBlock(LHSBlock);
346
Ted Kremenek49a436d2007-08-31 17:03:41 +0000347 Succ = ConfluenceBlock;
348 Block = NULL;
349 CFGBlock* RHSBlock = Visit(C->getRHS());
Ted Kremenekf50ec102007-09-11 21:29:43 +0000350 FinishBlock(RHSBlock);
Ted Kremenek49a436d2007-08-31 17:03:41 +0000351
352 Block = createBlock(false);
353 Block->addSuccessor(LHSBlock);
354 Block->addSuccessor(RHSBlock);
355 Block->setTerminator(C);
356 return addStmt(C->getCond());
357 }
Ted Kremenek7926f7c2007-08-28 16:18:58 +0000358
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000359 case Stmt::DeclStmtClass: {
Ted Kremenek53061c82008-10-06 20:56:19 +0000360 DeclStmt *DS = cast<DeclStmt>(Terminator);
Chris Lattner7e24e822009-03-28 06:33:19 +0000361 if (DS->isSingleDecl()) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000362 Block->appendStmt(Terminator);
Chris Lattner7e24e822009-03-28 06:33:19 +0000363 return WalkAST_VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000364 }
Chris Lattner7e24e822009-03-28 06:33:19 +0000365
Chris Lattner7e24e822009-03-28 06:33:19 +0000366 CFGBlock* B = 0;
Ted Kremenek53061c82008-10-06 20:56:19 +0000367
Chris Lattner7e24e822009-03-28 06:33:19 +0000368 // FIXME: Add a reverse iterator for DeclStmt to avoid this
369 // extra copy.
Chris Lattnere66a8cf2009-03-28 06:53:40 +0000370 typedef llvm::SmallVector<Decl*,10> BufTy;
371 BufTy Buf(DS->decl_begin(), DS->decl_end());
Chris Lattner7e24e822009-03-28 06:33:19 +0000372
373 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
374 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
375 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
376 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Ted Kremenek53061c82008-10-06 20:56:19 +0000377
Chris Lattner7e24e822009-03-28 06:33:19 +0000378 // Allocate the DeclStmt using the BumpPtrAllocator. It will
379 // get automatically freed with the CFG.
380 DeclGroupRef DG(*I);
381 Decl* D = *I;
382 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
383
Chris Lattnere66a8cf2009-03-28 06:53:40 +0000384 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Chris Lattner7e24e822009-03-28 06:33:19 +0000385
386 // Append the fake DeclStmt to block.
387 Block->appendStmt(DS);
388 B = WalkAST_VisitDeclSubExpr(D);
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000389 }
Chris Lattner7e24e822009-03-28 06:33:19 +0000390 return B;
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000391 }
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000392
Ted Kremenek19bb3562007-08-28 19:26:49 +0000393 case Stmt::AddrLabelExprClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000394 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000395 AddressTakenLabels.insert(A->getLabel());
396
Ted Kremenek411cdee2008-04-16 21:10:48 +0000397 if (AlwaysAddStmt) Block->appendStmt(Terminator);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000398 return Block;
399 }
Ted Kremenekf50ec102007-09-11 21:29:43 +0000400
Ted Kremenek15c27a82007-08-28 18:30:10 +0000401 case Stmt::StmtExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000402 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000403
Sebastian Redl05189992008-11-11 17:56:53 +0000404 case Stmt::SizeOfAlignOfExprClass: {
405 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
Ted Kremenek610a09e2008-09-26 22:58:57 +0000406
407 // VLA types have expressions that must be evaluated.
Sebastian Redl05189992008-11-11 17:56:53 +0000408 if (E->isArgumentType()) {
409 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
410 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
411 addStmt(VA->getSizeExpr());
412 }
413 // Expressions in sizeof/alignof are not evaluated and thus have no
414 // control flow.
415 else
416 Block->appendStmt(Terminator);
Ted Kremenek610a09e2008-09-26 22:58:57 +0000417
418 return Block;
419 }
420
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000421 case Stmt::BinaryOperatorClass: {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000422 BinaryOperator* B = cast<BinaryOperator>(Terminator);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000423
424 if (B->isLogicalOp()) { // && or ||
425 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
426 ConfluenceBlock->appendStmt(B);
427 FinishBlock(ConfluenceBlock);
428
429 // create the block evaluating the LHS
430 CFGBlock* LHSBlock = createBlock(false);
Ted Kremenekafe54332007-12-21 19:49:00 +0000431 LHSBlock->setTerminator(B);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000432
433 // create the block evaluating the RHS
434 Succ = ConfluenceBlock;
435 Block = NULL;
436 CFGBlock* RHSBlock = Visit(B->getRHS());
Zhongxing Xu924d9a82008-10-04 05:48:38 +0000437 FinishBlock(RHSBlock);
Ted Kremenekafe54332007-12-21 19:49:00 +0000438
439 // Now link the LHSBlock with RHSBlock.
440 if (B->getOpcode() == BinaryOperator::LOr) {
441 LHSBlock->addSuccessor(ConfluenceBlock);
442 LHSBlock->addSuccessor(RHSBlock);
443 }
444 else {
445 assert (B->getOpcode() == BinaryOperator::LAnd);
446 LHSBlock->addSuccessor(RHSBlock);
447 LHSBlock->addSuccessor(ConfluenceBlock);
448 }
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000449
450 // Generate the blocks for evaluating the LHS.
451 Block = LHSBlock;
452 return addStmt(B->getLHS());
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000453 }
454 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
455 Block->appendStmt(B);
456 addStmt(B->getRHS());
457 return addStmt(B->getLHS());
Ted Kremenek63f58872007-10-01 19:33:33 +0000458 }
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000459
460 break;
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000461 }
Ted Kremenek00c0a302008-09-26 18:17:07 +0000462
463 // Blocks: No support for blocks ... yet
464 case Stmt::BlockExprClass:
465 case Stmt::BlockDeclRefExprClass:
466 return NYS();
Ted Kremenekf4e15fc2008-02-26 02:37:08 +0000467
468 case Stmt::ParenExprClass:
Ted Kremenek411cdee2008-04-16 21:10:48 +0000469 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +0000470
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000471 default:
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000472 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000473 };
Ted Kremeneka651e0e2007-12-13 22:44:18 +0000474
Ted Kremenek411cdee2008-04-16 21:10:48 +0000475 if (AlwaysAddStmt) Block->appendStmt(Terminator);
476 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000477}
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000478
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000479/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions
480/// for initializers in Decls.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000481CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000482 VarDecl* VD = dyn_cast<VarDecl>(D);
483
484 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000485 return Block;
486
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000487 Expr* Init = VD->getInit();
Ted Kremenek8f54c1f2007-10-30 21:48:34 +0000488
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000489 if (Init) {
Mike Stump54cc43f2009-02-26 08:00:25 +0000490 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000491 switch (Init->getStmtClass()) {
492 case Stmt::IntegerLiteralClass:
493 case Stmt::CharacterLiteralClass:
494 case Stmt::StringLiteralClass:
495 break;
496 default:
497 Block = addStmt(Init);
498 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000499 }
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000500
501 // If the type of VD is a VLA, then we must process its size expressions.
502 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
503 VA = FindVA(VA->getElementType().getTypePtr()))
504 Block = addStmt(VA->getSizeExpr());
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000505
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000506 return Block;
507}
508
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000509/// WalkAST_VisitChildren - Utility method to call WalkAST on the
510/// children of a Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000511CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000512 CFGBlock* B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000513 for (Stmt::child_iterator I = Terminator->child_begin(),
514 E = Terminator->child_end();
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000515 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000516 if (*I) B = WalkAST(*I);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000517
518 return B;
519}
520
Ted Kremenek15c27a82007-08-28 18:30:10 +0000521/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
522/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000523CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
524 Block->appendStmt(Terminator);
525 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000526}
527
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000528/// VisitStmt - Handle statements with no branching control flow.
529CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
530 // We cannot assume that we are in the middle of a basic block, since
531 // the CFG might only be constructed for this single statement. If
532 // we have no current basic block, just create one lazily.
533 if (!Block) Block = createBlock();
534
535 // Simply add the statement to the current block. We actually
536 // insert statements in reverse order; this order is reversed later
537 // when processing the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000538 addStmt(Statement);
539
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000540 return Block;
541}
542
543CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
544 return Block;
545}
546
547CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000548
549 CFGBlock* LastBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000550
Ted Kremenekd34066c2008-02-26 00:22:58 +0000551 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
552 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000553 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000554 }
555
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000556 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000557}
558
559CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
560 // We may see an if statement in the middle of a basic block, or
561 // it may be the first statement we are processing. In either case,
562 // we create a new basic block. First, we create the blocks for
563 // the then...else statements, and then we create the block containing
564 // the if statement. If we were in the middle of a block, we
565 // stop processing that block and reverse its statements. That block
566 // is then the implicit successor for the "then" and "else" clauses.
567
568 // The block we were proccessing is now finished. Make it the
569 // successor block.
570 if (Block) {
571 Succ = Block;
572 FinishBlock(Block);
573 }
574
575 // Process the false branch. NULL out Block so that the recursive
576 // call to Visit will create a new basic block.
577 // Null out Block so that all successor
578 CFGBlock* ElseBlock = Succ;
579
580 if (Stmt* Else = I->getElse()) {
581 SaveAndRestore<CFGBlock*> sv(Succ);
582
583 // NULL out Block so that the recursive call to Visit will
584 // create a new basic block.
585 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000586 ElseBlock = Visit(Else);
587
588 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
589 ElseBlock = sv.get();
590 else if (Block)
591 FinishBlock(ElseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000592 }
593
594 // Process the true branch. NULL out Block so that the recursive
595 // call to Visit will create a new basic block.
596 // Null out Block so that all successor
597 CFGBlock* ThenBlock;
598 {
599 Stmt* Then = I->getThen();
600 assert (Then);
601 SaveAndRestore<CFGBlock*> sv(Succ);
602 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000603 ThenBlock = Visit(Then);
604
605 if (!ThenBlock) // Can occur when the Then body has all NullStmts.
606 ThenBlock = sv.get();
607 else if (Block)
608 FinishBlock(ThenBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000609 }
610
611 // Now create a new block containing the if statement.
612 Block = createBlock(false);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000613
614 // Set the terminator of the new block to the If statement.
615 Block->setTerminator(I);
616
617 // Now add the successors.
618 Block->addSuccessor(ThenBlock);
619 Block->addSuccessor(ElseBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000620
621 // Add the condition as the last statement in the new block. This
622 // may create new blocks as the condition may contain control-flow. Any
623 // newly created blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000624 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000625}
Ted Kremenekf50ec102007-09-11 21:29:43 +0000626
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000627
628CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
629 // If we were in the middle of a block we stop processing that block
630 // and reverse its statements.
631 //
632 // NOTE: If a "return" appears in the middle of a block, this means
633 // that the code afterwards is DEAD (unreachable). We still
634 // keep a basic block for that code; a simple "mark-and-sweep"
635 // from the entry block will be able to report such dead
636 // blocks.
637 if (Block) FinishBlock(Block);
638
639 // Create the new block.
640 Block = createBlock(false);
641
642 // The Exit block is the only successor.
643 Block->addSuccessor(&cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000644
645 // Add the return statement to the block. This may create new blocks
646 // if R contains control-flow (short-circuit operations).
647 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000648}
649
650CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
651 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000652 Visit(L->getSubStmt());
653 CFGBlock* LabelBlock = Block;
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000654
655 if (!LabelBlock) // This can happen when the body is empty, i.e.
656 LabelBlock=createBlock(); // scopes that only contains NullStmts.
657
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000658 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
659 LabelMap[ L ] = LabelBlock;
660
661 // Labels partition blocks, so this is the end of the basic block
Ted Kremenek9cffe732007-08-29 23:20:49 +0000662 // we were processing (L is the block's label). Because this is
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000663 // label (and we have already processed the substatement) there is no
664 // extra control-flow to worry about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000665 LabelBlock->setLabel(L);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000666 FinishBlock(LabelBlock);
667
668 // We set Block to NULL to allow lazy creation of a new block
669 // (if necessary);
670 Block = NULL;
671
672 // This block is now the implicit successor of other blocks.
673 Succ = LabelBlock;
674
675 return LabelBlock;
676}
677
678CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
679 // Goto is a control-flow statement. Thus we stop processing the
680 // current block and create a new one.
681 if (Block) FinishBlock(Block);
682 Block = createBlock(false);
683 Block->setTerminator(G);
684
685 // If we already know the mapping to the label block add the
686 // successor now.
687 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
688
689 if (I == LabelMap.end())
690 // We will need to backpatch this block later.
691 BackpatchBlocks.push_back(Block);
692 else
693 Block->addSuccessor(I->second);
694
695 return Block;
696}
697
698CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
699 // "for" is a control-flow statement. Thus we stop processing the
700 // current block.
701
702 CFGBlock* LoopSuccessor = NULL;
703
704 if (Block) {
705 FinishBlock(Block);
706 LoopSuccessor = Block;
707 }
708 else LoopSuccessor = Succ;
709
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000710 // Because of short-circuit evaluation, the condition of the loop
711 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
712 // blocks that evaluate the condition.
713 CFGBlock* ExitConditionBlock = createBlock(false);
714 CFGBlock* EntryConditionBlock = ExitConditionBlock;
715
716 // Set the terminator for the "exit" condition block.
717 ExitConditionBlock->setTerminator(F);
718
719 // Now add the actual condition to the condition block. Because the
720 // condition itself may contain control-flow, new blocks may be created.
721 if (Stmt* C = F->getCond()) {
722 Block = ExitConditionBlock;
723 EntryConditionBlock = addStmt(C);
724 if (Block) FinishBlock(EntryConditionBlock);
725 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000726
727 // The condition block is the implicit successor for the loop body as
728 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000729 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000730
731 // Now create the loop body.
732 {
733 assert (F->getBody());
734
735 // Save the current values for Block, Succ, and continue and break targets
736 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
737 save_continue(ContinueTargetBlock),
738 save_break(BreakTargetBlock);
Ted Kremeneke9334502008-09-04 21:48:47 +0000739
Ted Kremenekaf603f72007-08-30 18:39:40 +0000740 // Create a new block to contain the (bottom) of the loop body.
741 Block = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000742
Ted Kremeneke9334502008-09-04 21:48:47 +0000743 if (Stmt* I = F->getInc()) {
744 // Generate increment code in its own basic block. This is the target
745 // of continue statements.
Ted Kremenekd0172432008-11-24 20:50:24 +0000746 Succ = Visit(I);
747
748 // Finish up the increment block if it hasn't been already.
749 if (Block) {
750 assert (Block == Succ);
751 FinishBlock(Block);
752 Block = 0;
753 }
754
Ted Kremeneke9334502008-09-04 21:48:47 +0000755 ContinueTargetBlock = Succ;
756 }
757 else {
758 // No increment code. Continues should go the the entry condition block.
759 ContinueTargetBlock = EntryConditionBlock;
760 }
761
762 // All breaks should go to the code following the loop.
763 BreakTargetBlock = LoopSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000764
765 // Now populate the body block, and in the process create new blocks
766 // as we walk the body of the loop.
767 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000768
769 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000770 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000771 else if (Block)
772 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000773
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000774 // This new body block is a successor to our "exit" condition block.
775 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000776 }
777
778 // Link up the condition block with the code that follows the loop.
779 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000780 ExitConditionBlock->addSuccessor(LoopSuccessor);
781
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000782 // If the loop contains initialization, create a new block for those
783 // statements. This block can also contain statements that precede
784 // the loop.
785 if (Stmt* I = F->getInit()) {
786 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000787 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000788 }
789 else {
790 // There is no loop initialization. We are thus basically a while
791 // loop. NULL out Block to force lazy block construction.
792 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000793 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000794 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000795 }
796}
797
Ted Kremenek514de5a2008-11-11 17:10:00 +0000798CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
799 // Objective-C fast enumeration 'for' statements:
800 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
801 //
802 // for ( Type newVariable in collection_expression ) { statements }
803 //
804 // becomes:
805 //
806 // prologue:
807 // 1. collection_expression
808 // T. jump to loop_entry
809 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000810 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000811 // 1. ObjCForCollectionStmt [performs binding to newVariable]
812 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
813 // TB:
814 // statements
815 // T. jump to loop_entry
816 // FB:
817 // what comes after
818 //
819 // and
820 //
821 // Type existingItem;
822 // for ( existingItem in expression ) { statements }
823 //
824 // becomes:
825 //
826 // the same with newVariable replaced with existingItem; the binding
827 // works the same except that for one ObjCForCollectionStmt::getElement()
828 // returns a DeclStmt and the other returns a DeclRefExpr.
829 //
830
831 CFGBlock* LoopSuccessor = 0;
832
833 if (Block) {
834 FinishBlock(Block);
835 LoopSuccessor = Block;
836 Block = 0;
837 }
838 else LoopSuccessor = Succ;
839
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000840 // Build the condition blocks.
841 CFGBlock* ExitConditionBlock = createBlock(false);
842 CFGBlock* EntryConditionBlock = ExitConditionBlock;
843
844 // Set the terminator for the "exit" condition block.
845 ExitConditionBlock->setTerminator(S);
846
847 // The last statement in the block should be the ObjCForCollectionStmt,
848 // which performs the actual binding to 'element' and determines if there
849 // are any more items in the collection.
850 ExitConditionBlock->appendStmt(S);
851 Block = ExitConditionBlock;
852
853 // Walk the 'element' expression to see if there are any side-effects. We
854 // generate new blocks as necesary. We DON'T add the statement by default
855 // to the CFG unless it contains control-flow.
856 EntryConditionBlock = WalkAST(S->getElement(), false);
857 if (Block) { FinishBlock(EntryConditionBlock); Block = 0; }
858
859 // The condition block is the implicit successor for the loop body as
860 // well as any code above the loop.
861 Succ = EntryConditionBlock;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000862
863 // Now create the true branch.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000864 {
865 // Save the current values for Succ, continue and break targets.
866 SaveAndRestore<CFGBlock*> save_Succ(Succ),
867 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
868
869 BreakTargetBlock = LoopSuccessor;
870 ContinueTargetBlock = EntryConditionBlock;
871
872 CFGBlock* BodyBlock = Visit(S->getBody());
873
874 if (!BodyBlock)
875 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
876 else if (Block)
877 FinishBlock(BodyBlock);
878
879 // This new body block is a successor to our "exit" condition block.
880 ExitConditionBlock->addSuccessor(BodyBlock);
881 }
Ted Kremenekfc335522008-11-13 06:36:45 +0000882
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000883 // Link up the condition block with the code that follows the loop.
884 // (the false branch).
885 ExitConditionBlock->addSuccessor(LoopSuccessor);
886
Ted Kremenek514de5a2008-11-11 17:10:00 +0000887 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000888 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +0000889 return addStmt(S->getCollection());
890}
891
892
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000893CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
894 // "while" is a control-flow statement. Thus we stop processing the
895 // current block.
896
897 CFGBlock* LoopSuccessor = NULL;
898
899 if (Block) {
900 FinishBlock(Block);
901 LoopSuccessor = Block;
902 }
903 else LoopSuccessor = Succ;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000904
905 // Because of short-circuit evaluation, the condition of the loop
906 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
907 // blocks that evaluate the condition.
908 CFGBlock* ExitConditionBlock = createBlock(false);
909 CFGBlock* EntryConditionBlock = ExitConditionBlock;
910
911 // Set the terminator for the "exit" condition block.
912 ExitConditionBlock->setTerminator(W);
913
914 // Now add the actual condition to the condition block. Because the
915 // condition itself may contain control-flow, new blocks may be created.
916 // Thus we update "Succ" after adding the condition.
917 if (Stmt* C = W->getCond()) {
918 Block = ExitConditionBlock;
919 EntryConditionBlock = addStmt(C);
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000920 assert (Block == EntryConditionBlock);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000921 if (Block) FinishBlock(EntryConditionBlock);
922 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000923
924 // The condition block is the implicit successor for the loop body as
925 // well as any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000926 Succ = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000927
928 // Process the loop body.
929 {
930 assert (W->getBody());
931
932 // Save the current values for Block, Succ, and continue and break targets
933 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
934 save_continue(ContinueTargetBlock),
935 save_break(BreakTargetBlock);
936
937 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000938 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000939
940 // All breaks should go to the code following the loop.
941 BreakTargetBlock = LoopSuccessor;
942
943 // NULL out Block to force lazy instantiation of blocks for the body.
944 Block = NULL;
945
946 // Create the body. The returned block is the entry to the loop body.
947 CFGBlock* BodyBlock = Visit(W->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000948
949 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000950 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenekaf603f72007-08-30 18:39:40 +0000951 else if (Block)
952 FinishBlock(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000953
954 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000955 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000956 }
957
958 // Link up the condition block with the code that follows the loop.
959 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000960 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000961
962 // There can be no more statements in the condition block
963 // since we loop back to this block. NULL out Block to force
964 // lazy creation of another block.
965 Block = NULL;
966
967 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +0000968 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000969 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000970}
Ted Kremenek2fda5042008-12-09 20:20:09 +0000971
972CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
973 // FIXME: This isn't complete. We basically treat @throw like a return
974 // statement.
975
976 // If we were in the middle of a block we stop processing that block
977 // and reverse its statements.
978 if (Block) FinishBlock(Block);
979
980 // Create the new block.
981 Block = createBlock(false);
982
983 // The Exit block is the only successor.
984 Block->addSuccessor(&cfg->getExit());
985
986 // Add the statement to the block. This may create new blocks
987 // if S contains control-flow (short-circuit operations).
988 return addStmt(S);
989}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000990
991CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
992 // "do...while" is a control-flow statement. Thus we stop processing the
993 // current block.
994
995 CFGBlock* LoopSuccessor = NULL;
996
997 if (Block) {
998 FinishBlock(Block);
999 LoopSuccessor = Block;
1000 }
1001 else LoopSuccessor = Succ;
1002
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001003 // Because of short-circuit evaluation, the condition of the loop
1004 // can span multiple basic blocks. Thus we need the "Entry" and "Exit"
1005 // blocks that evaluate the condition.
1006 CFGBlock* ExitConditionBlock = createBlock(false);
1007 CFGBlock* EntryConditionBlock = ExitConditionBlock;
1008
1009 // Set the terminator for the "exit" condition block.
1010 ExitConditionBlock->setTerminator(D);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001011
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001012 // Now add the actual condition to the condition block. Because the
1013 // condition itself may contain control-flow, new blocks may be created.
1014 if (Stmt* C = D->getCond()) {
1015 Block = ExitConditionBlock;
1016 EntryConditionBlock = addStmt(C);
1017 if (Block) FinishBlock(EntryConditionBlock);
1018 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001019
Ted Kremenek54827132008-02-27 07:20:00 +00001020 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001021 Succ = EntryConditionBlock;
1022
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001023 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001024 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001025 {
1026 assert (D->getBody());
1027
1028 // Save the current values for Block, Succ, and continue and break targets
1029 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1030 save_continue(ContinueTargetBlock),
1031 save_break(BreakTargetBlock);
1032
1033 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001034 ContinueTargetBlock = EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001035
1036 // All breaks should go to the code following the loop.
1037 BreakTargetBlock = LoopSuccessor;
1038
1039 // NULL out Block to force lazy instantiation of blocks for the body.
1040 Block = NULL;
1041
1042 // Create the body. The returned block is the entry to the loop body.
1043 BodyBlock = Visit(D->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001044
Ted Kremenekaf603f72007-08-30 18:39:40 +00001045 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001046 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenekaf603f72007-08-30 18:39:40 +00001047 else if (Block)
1048 FinishBlock(BodyBlock);
1049
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001050 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001051 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001052 }
1053
1054 // Link up the condition block with the code that follows the loop.
1055 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001056 ExitConditionBlock->addSuccessor(LoopSuccessor);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001057
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001058 // There can be no more statements in the body block(s)
1059 // since we loop back to the body. NULL out Block to force
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001060 // lazy creation of another block.
1061 Block = NULL;
1062
1063 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001064 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001065 return BodyBlock;
1066}
1067
1068CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1069 // "continue" is a control-flow statement. Thus we stop processing the
1070 // current block.
1071 if (Block) FinishBlock(Block);
1072
1073 // Now create a new block that ends with the continue statement.
1074 Block = createBlock(false);
1075 Block->setTerminator(C);
1076
1077 // If there is no target for the continue, then we are looking at an
1078 // incomplete AST. Handle this by not registering a successor.
1079 if (ContinueTargetBlock) Block->addSuccessor(ContinueTargetBlock);
1080
1081 return Block;
1082}
1083
1084CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
1085 // "break" is a control-flow statement. Thus we stop processing the
1086 // current block.
1087 if (Block) FinishBlock(Block);
1088
1089 // Now create a new block that ends with the continue statement.
1090 Block = createBlock(false);
1091 Block->setTerminator(B);
1092
1093 // If there is no target for the break, then we are looking at an
1094 // incomplete AST. Handle this by not registering a successor.
1095 if (BreakTargetBlock) Block->addSuccessor(BreakTargetBlock);
1096
1097 return Block;
1098}
1099
Ted Kremenek411cdee2008-04-16 21:10:48 +00001100CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001101 // "switch" is a control-flow statement. Thus we stop processing the
1102 // current block.
1103 CFGBlock* SwitchSuccessor = NULL;
1104
1105 if (Block) {
1106 FinishBlock(Block);
1107 SwitchSuccessor = Block;
1108 }
1109 else SwitchSuccessor = Succ;
1110
1111 // Save the current "switch" context.
1112 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001113 save_break(BreakTargetBlock),
1114 save_default(DefaultCaseBlock);
1115
1116 // Set the "default" case to be the block after the switch statement.
1117 // If the switch statement contains a "default:", this value will
1118 // be overwritten with the block for that code.
1119 DefaultCaseBlock = SwitchSuccessor;
Ted Kremenek295222c2008-02-13 21:46:34 +00001120
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001121 // Create a new block that will contain the switch statement.
1122 SwitchTerminatedBlock = createBlock(false);
1123
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001124 // Now process the switch body. The code after the switch is the implicit
1125 // successor.
1126 Succ = SwitchSuccessor;
1127 BreakTargetBlock = SwitchSuccessor;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001128
1129 // When visiting the body, the case statements should automatically get
1130 // linked up to the switch. We also don't keep a pointer to the body,
1131 // since all control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001132 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001133 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001134 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001135 if (Block) FinishBlock(BodyBlock);
1136
Ted Kremenek295222c2008-02-13 21:46:34 +00001137 // If we have no "default:" case, the default transition is to the
1138 // code following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001139 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Ted Kremenek295222c2008-02-13 21:46:34 +00001140
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001141 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001142 SwitchTerminatedBlock->setTerminator(Terminator);
1143 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001144 Block = SwitchTerminatedBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001145
Ted Kremenek411cdee2008-04-16 21:10:48 +00001146 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001147}
1148
Ted Kremenek411cdee2008-04-16 21:10:48 +00001149CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001150 // CaseStmts are essentially labels, so they are the
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001151 // first statement in a block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001152
Ted Kremenek411cdee2008-04-16 21:10:48 +00001153 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001154 CFGBlock* CaseBlock = Block;
1155 if (!CaseBlock) CaseBlock = createBlock();
1156
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001157 // Cases statements partition blocks, so this is the top of
1158 // the basic block we were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001159 CaseBlock->setLabel(Terminator);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001160 FinishBlock(CaseBlock);
1161
1162 // Add this block to the list of successors for the block with the
1163 // switch statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001164 assert (SwitchTerminatedBlock);
1165 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001166
1167 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1168 Block = NULL;
1169
1170 // This block is now the implicit successor of other blocks.
1171 Succ = CaseBlock;
1172
Ted Kremenek2677ea82008-03-15 07:45:02 +00001173 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001174}
Ted Kremenek295222c2008-02-13 21:46:34 +00001175
Ted Kremenek411cdee2008-04-16 21:10:48 +00001176CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1177 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001178 DefaultCaseBlock = Block;
1179 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1180
1181 // Default statements partition blocks, so this is the top of
1182 // the basic block we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001183 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001184 FinishBlock(DefaultCaseBlock);
1185
1186 // Unlike case statements, we don't add the default block to the
1187 // successors for the switch statement immediately. This is done
1188 // when we finish processing the switch statement. This allows for
1189 // the default case (including a fall-through to the code after the
1190 // switch statement) to always be the last successor of a switch-terminated
1191 // block.
1192
1193 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1194 Block = NULL;
1195
1196 // This block is now the implicit successor of other blocks.
1197 Succ = DefaultCaseBlock;
1198
1199 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001200}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001201
Ted Kremenek19bb3562007-08-28 19:26:49 +00001202CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1203 // Lazily create the indirect-goto dispatch block if there isn't one
1204 // already.
1205 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
1206
1207 if (!IBlock) {
1208 IBlock = createBlock(false);
1209 cfg->setIndirectGotoBlock(IBlock);
1210 }
1211
1212 // IndirectGoto is a control-flow statement. Thus we stop processing the
1213 // current block and create a new one.
1214 if (Block) FinishBlock(Block);
1215 Block = createBlock(false);
1216 Block->setTerminator(I);
1217 Block->addSuccessor(IBlock);
1218 return addStmt(I->getTarget());
1219}
1220
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001221
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001222} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001223
1224/// createBlock - Constructs and adds a new CFGBlock to the CFG. The
1225/// block has no successors or predecessors. If this is the first block
1226/// created in the CFG, it is automatically set to be the Entry and Exit
1227/// of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001228CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001229 bool first_block = begin() == end();
1230
1231 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001232 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001233
1234 // If this is the first block, set it as the Entry and Exit.
1235 if (first_block) Entry = Exit = &front();
1236
1237 // Return the block.
1238 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001239}
1240
Ted Kremenek026473c2007-08-23 16:51:22 +00001241/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1242/// CFG is returned to the caller.
1243CFG* CFG::buildCFG(Stmt* Statement) {
1244 CFGBuilder Builder;
1245 return Builder.buildCFG(Statement);
1246}
1247
1248/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001249void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1250
Ted Kremenek63f58872007-10-01 19:33:33 +00001251//===----------------------------------------------------------------------===//
1252// CFG: Queries for BlkExprs.
1253//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001254
Ted Kremenek63f58872007-10-01 19:33:33 +00001255namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001256 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001257}
1258
Ted Kremenek411cdee2008-04-16 21:10:48 +00001259static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1260 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001261 return;
1262
Ted Kremenek411cdee2008-04-16 21:10:48 +00001263 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001264 if (!*I) continue;
1265
1266 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1267 if (B->isAssignmentOp()) Set.insert(B);
1268
1269 FindSubExprAssignments(*I, Set);
1270 }
1271}
1272
Ted Kremenek63f58872007-10-01 19:33:33 +00001273static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1274 BlkExprMapTy* M = new BlkExprMapTy();
1275
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001276 // Look for assignments that are used as subexpressions. These are the
Ted Kremenek411cdee2008-04-16 21:10:48 +00001277 // only assignments that we want to *possibly* register as a block-level
1278 // expression. Basically, if an assignment occurs both in a subexpression
1279 // and at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001280 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
1281
Ted Kremenek63f58872007-10-01 19:33:33 +00001282 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1283 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001284 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001285
Ted Kremenek411cdee2008-04-16 21:10:48 +00001286 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
1287
1288 // Iterate over the statements again on identify the Expr* and Stmt* at
1289 // the block-level that are block-level expressions.
1290
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001291 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001292 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001293
Ted Kremenek411cdee2008-04-16 21:10:48 +00001294 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001295 // Assignment expressions that are not nested within another
1296 // expression are really "statements" whose value is never
1297 // used by another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001298 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001299 continue;
1300 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001301 else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001302 // Special handling for statement expressions. The last statement
1303 // in the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001304 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001305 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001306 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001307 (*M)[C->body_back()] = x;
1308 }
1309 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001310
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001311 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001312 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001313 }
1314
Ted Kremenek411cdee2008-04-16 21:10:48 +00001315 // Look at terminators. The condition is a block-level expression.
1316
Ted Kremenek390e48b2008-11-12 21:11:49 +00001317 Stmt* S = I->getTerminatorCondition();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001318
Ted Kremenek390e48b2008-11-12 21:11:49 +00001319 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001320 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001321 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001322 }
1323 }
1324
Ted Kremenek63f58872007-10-01 19:33:33 +00001325 return M;
1326}
1327
Ted Kremenek86946742008-01-17 20:48:37 +00001328CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1329 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001330 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
1331
1332 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001333 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek63f58872007-10-01 19:33:33 +00001334
1335 if (I == M->end()) return CFG::BlkExprNumTy();
1336 else return CFG::BlkExprNumTy(I->second);
1337}
1338
1339unsigned CFG::getNumBlkExprs() {
1340 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1341 return M->size();
1342 else {
1343 // We assume callers interested in the number of BlkExprs will want
1344 // the map constructed if it doesn't already exist.
1345 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1346 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1347 }
1348}
1349
Ted Kremenek274f4332008-04-28 18:00:46 +00001350//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001351// Cleanup: CFG dstor.
1352//===----------------------------------------------------------------------===//
1353
Ted Kremenek63f58872007-10-01 19:33:33 +00001354CFG::~CFG() {
1355 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1356}
1357
Ted Kremenek7dba8602007-08-29 21:56:09 +00001358//===----------------------------------------------------------------------===//
1359// CFG pretty printing
1360//===----------------------------------------------------------------------===//
1361
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001362namespace {
1363
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001364class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001365
Ted Kremenek42a509f2007-08-31 21:30:12 +00001366 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1367 StmtMapTy StmtMap;
1368 signed CurrentBlock;
1369 unsigned CurrentStmt;
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001370
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001371public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001372
Ted Kremenek42a509f2007-08-31 21:30:12 +00001373 StmtPrinterHelper(const CFG* cfg) : CurrentBlock(0), CurrentStmt(0) {
1374 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1375 unsigned j = 1;
1376 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1377 BI != BEnd; ++BI, ++j )
1378 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1379 }
1380 }
1381
1382 virtual ~StmtPrinterHelper() {}
1383
1384 void setBlockID(signed i) { CurrentBlock = i; }
1385 void setStmtID(unsigned i) { CurrentStmt = i; }
1386
Ted Kremeneka95d3752008-09-13 05:16:45 +00001387 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001388
Ted Kremenek411cdee2008-04-16 21:10:48 +00001389 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001390
1391 if (I == StmtMap.end())
1392 return false;
1393
1394 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
1395 && I->second.second == CurrentStmt)
1396 return false;
1397
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001398 OS << "[B" << I->second.first << "." << I->second.second << "]";
1399 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001400 }
1401};
1402
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001403class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1404 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
1405
Ted Kremeneka95d3752008-09-13 05:16:45 +00001406 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001407 StmtPrinterHelper* Helper;
1408public:
Ted Kremeneka95d3752008-09-13 05:16:45 +00001409 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper)
Ted Kremenek42a509f2007-08-31 21:30:12 +00001410 : OS(os), Helper(helper) {}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001411
1412 void VisitIfStmt(IfStmt* I) {
1413 OS << "if ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001414 I->getCond()->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001415 }
1416
1417 // Default case.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001418 void VisitStmt(Stmt* Terminator) { Terminator->printPretty(OS); }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001419
1420 void VisitForStmt(ForStmt* F) {
1421 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001422 if (F->getInit()) OS << "...";
1423 OS << "; ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001424 if (Stmt* C = F->getCond()) C->printPretty(OS,Helper);
Ted Kremenek535bb202007-08-30 21:28:02 +00001425 OS << "; ";
1426 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001427 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001428 }
1429
1430 void VisitWhileStmt(WhileStmt* W) {
1431 OS << "while " ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001432 if (Stmt* C = W->getCond()) C->printPretty(OS,Helper);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433 }
1434
1435 void VisitDoStmt(DoStmt* D) {
1436 OS << "do ... while ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001437 if (Stmt* C = D->getCond()) C->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001438 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001439
Ted Kremenek411cdee2008-04-16 21:10:48 +00001440 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001441 OS << "switch ";
Ted Kremenek411cdee2008-04-16 21:10:48 +00001442 Terminator->getCond()->printPretty(OS,Helper);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001443 }
1444
Ted Kremenek805e9a82007-08-31 21:49:40 +00001445 void VisitConditionalOperator(ConditionalOperator* C) {
1446 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001447 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001448 }
1449
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001450 void VisitChooseExpr(ChooseExpr* C) {
1451 OS << "__builtin_choose_expr( ";
1452 C->getCond()->printPretty(OS,Helper);
Ted Kremeneka2925852008-01-30 23:02:42 +00001453 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001454 }
1455
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001456 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1457 OS << "goto *";
1458 I->getTarget()->printPretty(OS,Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001459 }
1460
Ted Kremenek805e9a82007-08-31 21:49:40 +00001461 void VisitBinaryOperator(BinaryOperator* B) {
1462 if (!B->isLogicalOp()) {
1463 VisitExpr(B);
1464 return;
1465 }
1466
1467 B->getLHS()->printPretty(OS,Helper);
1468
1469 switch (B->getOpcode()) {
1470 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001471 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001472 return;
1473 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001474 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001475 return;
1476 default:
1477 assert(false && "Invalid logical operator.");
1478 }
1479 }
1480
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001481 void VisitExpr(Expr* E) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001482 E->printPretty(OS,Helper);
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001483 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001484};
Ted Kremenek42a509f2007-08-31 21:30:12 +00001485
1486
Ted Kremeneka95d3752008-09-13 05:16:45 +00001487void print_stmt(llvm::raw_ostream&OS, StmtPrinterHelper* Helper, Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001488 if (Helper) {
1489 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001490 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001491 CompoundStmt* Sub = SE->getSubStmt();
1492
1493 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001494 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001495 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001496 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001497 return;
1498 }
1499 }
1500
1501 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001502 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001503 if (B->getOpcode() == BinaryOperator::Comma) {
1504 OS << "... , ";
1505 Helper->handledStmt(B->getRHS(),OS);
1506 OS << '\n';
1507 return;
1508 }
1509 }
1510 }
1511
Ted Kremenek411cdee2008-04-16 21:10:48 +00001512 Terminator->printPretty(OS, Helper);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001513
1514 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001515 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001516}
1517
Ted Kremeneka95d3752008-09-13 05:16:45 +00001518void print_block(llvm::raw_ostream& OS, const CFG* cfg, const CFGBlock& B,
Ted Kremenek42a509f2007-08-31 21:30:12 +00001519 StmtPrinterHelper* Helper, bool print_edges) {
1520
1521 if (Helper) Helper->setBlockID(B.getBlockID());
1522
Ted Kremenek7dba8602007-08-29 21:56:09 +00001523 // Print the header.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001524 OS << "\n [ B" << B.getBlockID();
1525
1526 if (&B == &cfg->getEntry())
1527 OS << " (ENTRY) ]\n";
1528 else if (&B == &cfg->getExit())
1529 OS << " (EXIT) ]\n";
1530 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001531 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001532 else
1533 OS << " ]\n";
1534
Ted Kremenek9cffe732007-08-29 23:20:49 +00001535 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001536 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001537
1538 if (print_edges)
1539 OS << " ";
1540
Ted Kremenek411cdee2008-04-16 21:10:48 +00001541 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001542 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001543 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001544 OS << "case ";
1545 C->getLHS()->printPretty(OS);
1546 if (C->getRHS()) {
1547 OS << " ... ";
1548 C->getRHS()->printPretty(OS);
1549 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001550 }
Ted Kremenek411cdee2008-04-16 21:10:48 +00001551 else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001552 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001553 else
1554 assert(false && "Invalid label statement in CFGBlock.");
1555
Ted Kremenek9cffe732007-08-29 23:20:49 +00001556 OS << ":\n";
1557 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001558
Ted Kremenekfddd5182007-08-21 21:42:03 +00001559 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001560 unsigned j = 1;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001561
1562 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1563 I != E ; ++I, ++j ) {
1564
Ted Kremenek9cffe732007-08-29 23:20:49 +00001565 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001566 if (print_edges)
1567 OS << " ";
1568
Ted Kremeneka95d3752008-09-13 05:16:45 +00001569 OS << llvm::format("%3d", j) << ": ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001570
1571 if (Helper)
1572 Helper->setStmtID(j);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001573
1574 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001575 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001576
Ted Kremenek9cffe732007-08-29 23:20:49 +00001577 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001578 if (B.getTerminator()) {
1579 if (print_edges)
1580 OS << " ";
1581
Ted Kremenek9cffe732007-08-29 23:20:49 +00001582 OS << " T: ";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001583
1584 if (Helper) Helper->setBlockID(-1);
1585
1586 CFGBlockTerminatorPrint TPrinter(OS,Helper);
1587 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001588 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001589 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001590
Ted Kremenek9cffe732007-08-29 23:20:49 +00001591 if (print_edges) {
1592 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001593 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001594 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001595
Ted Kremenek42a509f2007-08-31 21:30:12 +00001596 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1597 I != E; ++I, ++i) {
1598
1599 if (i == 8 || (i-8) == 0)
1600 OS << "\n ";
1601
Ted Kremenek9cffe732007-08-29 23:20:49 +00001602 OS << " B" << (*I)->getBlockID();
1603 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001604
1605 OS << '\n';
1606
1607 // Print the successors of this block.
1608 OS << " Successors (" << B.succ_size() << "):";
1609 i = 0;
1610
1611 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1612 I != E; ++I, ++i) {
1613
1614 if (i == 8 || (i-8) % 10 == 0)
1615 OS << "\n ";
1616
1617 OS << " B" << (*I)->getBlockID();
1618 }
1619
Ted Kremenek9cffe732007-08-29 23:20:49 +00001620 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001621 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001622}
1623
1624} // end anonymous namespace
1625
1626/// dump - A simple pretty printer of a CFG that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001627void CFG::dump() const { print(llvm::errs()); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001628
1629/// print - A simple pretty printer of a CFG that outputs to an ostream.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001630void CFG::print(llvm::raw_ostream& OS) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001631
1632 StmtPrinterHelper Helper(this);
1633
1634 // Print the entry block.
1635 print_block(OS, this, getEntry(), &Helper, true);
1636
1637 // Iterate through the CFGBlocks and print them one by one.
1638 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1639 // Skip the entry block, because we already printed it.
1640 if (&(*I) == &getEntry() || &(*I) == &getExit())
1641 continue;
1642
1643 print_block(OS, this, *I, &Helper, true);
1644 }
1645
1646 // Print the exit block.
1647 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001648 OS.flush();
Ted Kremenek42a509f2007-08-31 21:30:12 +00001649}
1650
1651/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001652void CFGBlock::dump(const CFG* cfg) const { print(llvm::errs(), cfg); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001653
1654/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1655/// Generally this will only be called from CFG::print.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001656void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001657 StmtPrinterHelper Helper(cfg);
1658 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001659}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001660
Ted Kremeneka2925852008-01-30 23:02:42 +00001661/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Ted Kremeneka95d3752008-09-13 05:16:45 +00001662void CFGBlock::printTerminator(llvm::raw_ostream& OS) const {
Ted Kremeneka2925852008-01-30 23:02:42 +00001663 CFGBlockTerminatorPrint TPrinter(OS,NULL);
1664 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1665}
1666
Ted Kremenek390e48b2008-11-12 21:11:49 +00001667Stmt* CFGBlock::getTerminatorCondition() {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001668
1669 if (!Terminator)
1670 return NULL;
1671
1672 Expr* E = NULL;
1673
1674 switch (Terminator->getStmtClass()) {
1675 default:
1676 break;
1677
1678 case Stmt::ForStmtClass:
1679 E = cast<ForStmt>(Terminator)->getCond();
1680 break;
1681
1682 case Stmt::WhileStmtClass:
1683 E = cast<WhileStmt>(Terminator)->getCond();
1684 break;
1685
1686 case Stmt::DoStmtClass:
1687 E = cast<DoStmt>(Terminator)->getCond();
1688 break;
1689
1690 case Stmt::IfStmtClass:
1691 E = cast<IfStmt>(Terminator)->getCond();
1692 break;
1693
1694 case Stmt::ChooseExprClass:
1695 E = cast<ChooseExpr>(Terminator)->getCond();
1696 break;
1697
1698 case Stmt::IndirectGotoStmtClass:
1699 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1700 break;
1701
1702 case Stmt::SwitchStmtClass:
1703 E = cast<SwitchStmt>(Terminator)->getCond();
1704 break;
1705
1706 case Stmt::ConditionalOperatorClass:
1707 E = cast<ConditionalOperator>(Terminator)->getCond();
1708 break;
1709
1710 case Stmt::BinaryOperatorClass: // '&&' and '||'
1711 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001712 break;
1713
1714 case Stmt::ObjCForCollectionStmtClass:
1715 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001716 }
1717
1718 return E ? E->IgnoreParens() : NULL;
1719}
1720
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001721bool CFGBlock::hasBinaryBranchTerminator() const {
1722
1723 if (!Terminator)
1724 return false;
1725
1726 Expr* E = NULL;
1727
1728 switch (Terminator->getStmtClass()) {
1729 default:
1730 return false;
1731
1732 case Stmt::ForStmtClass:
1733 case Stmt::WhileStmtClass:
1734 case Stmt::DoStmtClass:
1735 case Stmt::IfStmtClass:
1736 case Stmt::ChooseExprClass:
1737 case Stmt::ConditionalOperatorClass:
1738 case Stmt::BinaryOperatorClass:
1739 return true;
1740 }
1741
1742 return E ? E->IgnoreParens() : NULL;
1743}
1744
Ted Kremeneka2925852008-01-30 23:02:42 +00001745
Ted Kremenek7dba8602007-08-29 21:56:09 +00001746//===----------------------------------------------------------------------===//
1747// CFG Graphviz Visualization
1748//===----------------------------------------------------------------------===//
1749
Ted Kremenek42a509f2007-08-31 21:30:12 +00001750
1751#ifndef NDEBUG
Chris Lattner00123512007-09-17 06:16:32 +00001752static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001753#endif
1754
1755void CFG::viewCFG() const {
1756#ifndef NDEBUG
1757 StmtPrinterHelper H(this);
1758 GraphHelper = &H;
1759 llvm::ViewGraph(this,"CFG");
1760 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001761#endif
1762}
1763
Ted Kremenek7dba8602007-08-29 21:56:09 +00001764namespace llvm {
1765template<>
1766struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
1767 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
1768
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001769#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001770 std::string OutSStr;
1771 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001772 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001773 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001774
1775 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1776
1777 // Process string output to make it nicer...
1778 for (unsigned i = 0; i != OutStr.length(); ++i)
1779 if (OutStr[i] == '\n') { // Left justify
1780 OutStr[i] = '\\';
1781 OutStr.insert(OutStr.begin()+i+1, 'l');
1782 }
1783
1784 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001785#else
1786 return "";
1787#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001788 }
1789};
1790} // end namespace llvm