blob: 7ad688fd68d98220e19bc54231bb62316e0e374c [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
Ted Kremeneke41611a2009-07-16 18:13:04 +000015#include "clang/Analysis/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 Kremenek83c01da2008-01-11 00:40:29 +000025
Ted Kremenekfddd5182007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Ted Kremenekbefef2f2007-08-23 21:26:19 +000030// SaveAndRestore - A utility class that uses RIIA to save and restore
31// the value of a variable.
32template<typename T>
Ted Kremenek6fa9b882008-01-08 18:15:10 +000033struct VISIBILITY_HIDDEN SaveAndRestore {
Ted Kremenekbefef2f2007-08-23 21:26:19 +000034 SaveAndRestore(T& x) : X(x), old_value(x) {}
35 ~SaveAndRestore() { X = old_value; }
Ted Kremenekb6f7b722007-08-30 18:13:31 +000036 T get() { return old_value; }
37
Ted Kremenekbefef2f2007-08-23 21:26:19 +000038 T& X;
39 T old_value;
40};
Mike Stump6d9828c2009-07-17 01:31:16 +000041
Douglas Gregor4afa39d2009-01-20 01:17:11 +000042static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000043 if (VarDecl* VD = dyn_cast<VarDecl>(D))
44 if (Expr* Ex = VD->getInit())
45 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000046
47 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000048}
Mike Stump6d9828c2009-07-17 01:31:16 +000049
Ted Kremeneka34ea072008-08-04 22:51:42 +000050/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000051/// The builder is stateful: an instance of the builder should be used to only
52/// construct a single CFG.
53///
54/// Example usage:
55///
56/// CFGBuilder builder;
57/// CFG* cfg = builder.BuildAST(stmt1);
58///
Mike Stump6d9828c2009-07-17 01:31:16 +000059/// CFG construction is done via a recursive walk of an AST. We actually parse
60/// the AST in reverse order so that the successor of a basic block is
61/// constructed prior to its predecessor. This allows us to nicely capture
62/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000063///
Mike Stump6d9828c2009-07-17 01:31:16 +000064class VISIBILITY_HIDDEN CFGBuilder : public StmtVisitor<CFGBuilder,CFGBlock*> {
Ted Kremenekfddd5182007-08-21 21:42:03 +000065 CFG* cfg;
66 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000067 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000068 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000069 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000070 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000071 CFGBlock* DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000072
Ted Kremenek19bb3562007-08-28 19:26:49 +000073 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000074 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
75 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000076
77 // A list of blocks that end with a "goto" that must be backpatched to their
78 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000079 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000080 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000081
Ted Kremenek19bb3562007-08-28 19:26:49 +000082 // A list of labels whose address has been taken (for indirect gotos).
83 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
84 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000085
86public:
Ted Kremenek026473c2007-08-23 16:51:22 +000087 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000088 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000089 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenekfddd5182007-08-21 21:42:03 +000090 // Create an empty CFG.
Mike Stump6d9828c2009-07-17 01:31:16 +000091 cfg = new CFG();
Ted Kremenekfddd5182007-08-21 21:42:03 +000092 }
Mike Stump6d9828c2009-07-17 01:31:16 +000093
Ted Kremenekfddd5182007-08-21 21:42:03 +000094 ~CFGBuilder() { delete cfg; }
Mike Stump6d9828c2009-07-17 01:31:16 +000095
Ted Kremenekd4fdee32007-08-23 21:42:29 +000096 // buildCFG - Used by external clients to construct the CFG.
97 CFG* buildCFG(Stmt* Statement);
Mike Stump6d9828c2009-07-17 01:31:16 +000098
99 // Visitors to walk an AST and construct the CFG. Called by buildCFG. Do not
100 // call directly!
101
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000102 CFGBlock* VisitBreakStmt(BreakStmt* B);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000103 CFGBlock* VisitCaseStmt(CaseStmt* Terminator);
Ted Kremenek514de5a2008-11-11 17:10:00 +0000104 CFGBlock* VisitCompoundStmt(CompoundStmt* C);
105 CFGBlock* VisitContinueStmt(ContinueStmt* C);
Ted Kremenek295222c2008-02-13 21:46:34 +0000106 CFGBlock* VisitDefaultStmt(DefaultStmt* D);
Ted Kremenek514de5a2008-11-11 17:10:00 +0000107 CFGBlock* VisitDoStmt(DoStmt* D);
108 CFGBlock* VisitForStmt(ForStmt* F);
109 CFGBlock* VisitGotoStmt(GotoStmt* G);
110 CFGBlock* VisitIfStmt(IfStmt* I);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000111 CFGBlock* VisitIndirectGotoStmt(IndirectGotoStmt* I);
Ted Kremenek514de5a2008-11-11 17:10:00 +0000112 CFGBlock* VisitLabelStmt(LabelStmt* L);
113 CFGBlock* VisitNullStmt(NullStmt* Statement);
114 CFGBlock* VisitObjCForCollectionStmt(ObjCForCollectionStmt* S);
115 CFGBlock* VisitReturnStmt(ReturnStmt* R);
116 CFGBlock* VisitStmt(Stmt* Statement);
117 CFGBlock* VisitSwitchStmt(SwitchStmt* Terminator);
118 CFGBlock* VisitWhileStmt(WhileStmt* W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000119
Ted Kremenek4102af92008-03-13 03:04:22 +0000120 // FIXME: Add support for ObjC-specific control-flow structures.
Mike Stumpcd7bf232009-07-17 01:04:31 +0000121
Ted Kremenek274f4332008-04-28 18:00:46 +0000122 // NYS == Not Yet Supported
123 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000124 badCFG = true;
125 return Block;
126 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000127
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000128 CFGBlock* VisitObjCAtTryStmt(ObjCAtTryStmt* S);
Mike Stump6d9828c2009-07-17 01:31:16 +0000129 CFGBlock* VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
130 // FIXME: For now we pretend that @catch and the code it contains does not
131 // exit.
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000132 return Block;
133 }
134
Mike Stump6d9828c2009-07-17 01:31:16 +0000135 // FIXME: This is not completely supported. We basically @throw like a
136 // 'return'.
Ted Kremenek2fda5042008-12-09 20:20:09 +0000137 CFGBlock* VisitObjCAtThrowStmt(ObjCAtThrowStmt* S);
Ted Kremenek274f4332008-04-28 18:00:46 +0000138
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000139 CFGBlock* VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S);
Mike Stump6d9828c2009-07-17 01:31:16 +0000140
Ted Kremenek00c0a302008-09-26 18:17:07 +0000141 // Blocks.
142 CFGBlock* VisitBlockExpr(BlockExpr* E) { return NYS(); }
Mike Stump6d9828c2009-07-17 01:31:16 +0000143 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);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000150 CFGBlock* WalkAST_VisitDeclSubExpr(Decl* D);
Ted Kremenek411cdee2008-04-16 21:10:48 +0000151 CFGBlock* WalkAST_VisitStmtExpr(StmtExpr* Terminator);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000152 bool FinishBlock(CFGBlock* B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000153
Ted Kremenek4102af92008-03-13 03:04:22 +0000154 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000155};
Mike Stump6d9828c2009-07-17 01:31:16 +0000156
Douglas Gregor898574e2008-12-05 23:32:09 +0000157// FIXME: Add support for dependent-sized array types in C++?
158// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000159static VariableArrayType* FindVA(Type* t) {
160 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
161 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
162 if (vat->getSizeExpr())
163 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000164
Ted Kremenek610a09e2008-09-26 22:58:57 +0000165 t = vt->getElementType().getTypePtr();
166 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000167
Ted Kremenek610a09e2008-09-26 22:58:57 +0000168 return 0;
169}
Mike Stump6d9828c2009-07-17 01:31:16 +0000170
171/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
172/// arbitrary statement. Examples include a single expression or a function
173/// body (compound statement). The ownership of the returned CFG is
174/// transferred to the caller. If CFG construction fails, this method returns
175/// NULL.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000176CFG* CFGBuilder::buildCFG(Stmt* Statement) {
Ted Kremenek19bb3562007-08-28 19:26:49 +0000177 assert (cfg);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000178 if (!Statement) return NULL;
179
Ted Kremenek4102af92008-03-13 03:04:22 +0000180 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000181
182 // Create an empty block that will serve as the exit block for the CFG. Since
183 // this is the first block added to the CFG, it will be implicitly registered
184 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000185 Succ = createBlock();
186 assert (Succ == &cfg->getExit());
187 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000188
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000189 // Visit the statements and create the CFG.
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000190 CFGBlock* B = Visit(Statement);
191 if (!B) B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000192
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000193 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000194 // Finalize the last constructed block. This usually involves reversing the
195 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000196 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000197
198 // Backpatch the gotos whose label -> block mappings we didn't know when we
199 // encountered them.
200 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000201 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000202
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000203 CFGBlock* B = *I;
204 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
205 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
206
207 // If there is no target for the goto, then we are looking at an
208 // incomplete AST. Handle this by not registering a successor.
209 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000210
211 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000212 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000213
Ted Kremenek19bb3562007-08-28 19:26:49 +0000214 // Add successors to the Indirect Goto Dispatch block (if we have one).
215 if (CFGBlock* B = cfg->getIndirectGotoBlock())
216 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
217 E = AddressTakenLabels.end(); I != E; ++I ) {
218
219 // Lookup the target block.
220 LabelMapTy::iterator LI = LabelMap.find(*I);
221
222 // If there is no target block that contains label, then we are looking
223 // at an incomplete AST. Handle this by not registering a successor.
224 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000225
226 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000227 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000228
Ted Kremenek94b33162007-09-17 16:18:02 +0000229 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000230 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000231
232 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000233 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000234
Ted Kremenek4102af92008-03-13 03:04:22 +0000235 if (badCFG) {
236 delete cfg;
237 cfg = NULL;
238 return NULL;
239 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000240
241 // NULL out cfg so that repeated calls to the builder will fail and that the
242 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000243 CFG* t = cfg;
244 cfg = NULL;
245 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000246}
Mike Stump6d9828c2009-07-17 01:31:16 +0000247
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000248/// createBlock - Used to lazily create blocks that are connected
249/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000250CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000251 CFGBlock* B = cfg->createBlock();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000252 if (add_successor && Succ) B->addSuccessor(Succ);
253 return B;
254}
Mike Stump6d9828c2009-07-17 01:31:16 +0000255
256/// FinishBlock - When the last statement has been added to the block, we must
257/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000258bool CFGBuilder::FinishBlock(CFGBlock* B) {
259 if (badCFG)
260 return false;
261
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000262 assert (B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000263 B->reverseStmts();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000264 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000265}
266
Mike Stump6d9828c2009-07-17 01:31:16 +0000267/// addStmt - Used to add statements/expressions to the current CFGBlock
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000268/// "Block". This method calls WalkAST on the passed statement to see if it
Mike Stump6d9828c2009-07-17 01:31:16 +0000269/// contains any short-circuit expressions. If so, it recursively creates the
270/// necessary blocks for such expressions. It returns the "topmost" block of
271/// the created blocks, or the original value of "Block" when this method was
272/// 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
Mike Stump6d9828c2009-07-17 01:31:16 +0000278/// WalkAST - Used by addStmt to walk the subtree of a statement and add extra
279/// blocks for ternary operators, &&, and ||. We also process "," and
280/// DeclStmts (which may contain nested control-flow).
Mike Stumpcd7bf232009-07-17 01:04:31 +0000281CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) {
Ted Kremenek411cdee2008-04-16 21:10:48 +0000282 switch (Terminator->getStmtClass()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000283 case Stmt::ConditionalOperatorClass: {
284 ConditionalOperator* C = cast<ConditionalOperator>(Terminator);
Ted Kremenekecc04c92007-11-26 18:20:26 +0000285
Mike Stump6d9828c2009-07-17 01:31:16 +0000286 // Create the confluence block that will "merge" the results of the ternary
287 // expression.
288 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
289 ConfluenceBlock->appendStmt(C);
290 if (!FinishBlock(ConfluenceBlock))
291 return 0;
Ted Kremenekecc04c92007-11-26 18:20:26 +0000292
Mike Stump6d9828c2009-07-17 01:31:16 +0000293 // Create a block for the LHS expression if there is an LHS expression. A
294 // GCC extension allows LHS to be NULL, causing the condition to be the
295 // value that is returned instead.
296 // e.g: x ?: y is shorthand for: x ? x : y;
297 Succ = ConfluenceBlock;
298 Block = NULL;
299 CFGBlock* LHSBlock = NULL;
300 if (C->getLHS()) {
301 LHSBlock = Visit(C->getLHS());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000302 if (!FinishBlock(LHSBlock))
303 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000304 Block = NULL;
305 }
Ted Kremenekf50ec102007-09-11 21:29:43 +0000306
Mike Stump6d9828c2009-07-17 01:31:16 +0000307 // Create the block for the RHS expression.
308 Succ = ConfluenceBlock;
309 CFGBlock* RHSBlock = Visit(C->getRHS());
310 if (!FinishBlock(RHSBlock))
311 return 0;
312
313 // Create the block that will contain the condition.
314 Block = createBlock(false);
315
316 if (LHSBlock)
317 Block->addSuccessor(LHSBlock);
318 else {
319 // If we have no LHS expression, add the ConfluenceBlock as a direct
320 // successor for the block containing the condition. Moreover, we need to
321 // reverse the order of the predecessors in the ConfluenceBlock because
322 // the RHSBlock will have been added to the succcessors already, and we
323 // want the first predecessor to the the block containing the expression
324 // for the case when the ternary expression evaluates to true.
325 Block->addSuccessor(ConfluenceBlock);
326 assert (ConfluenceBlock->pred_size() == 2);
327 std::reverse(ConfluenceBlock->pred_begin(),
328 ConfluenceBlock->pred_end());
329 }
330
331 Block->addSuccessor(RHSBlock);
332
333 Block->setTerminator(C);
334 return addStmt(C->getCond());
335 }
336
337 case Stmt::ChooseExprClass: {
338 ChooseExpr* C = cast<ChooseExpr>(Terminator);
339
340 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
341 ConfluenceBlock->appendStmt(C);
342 if (!FinishBlock(ConfluenceBlock))
343 return 0;
344
345 Succ = ConfluenceBlock;
346 Block = NULL;
347 CFGBlock* LHSBlock = Visit(C->getLHS());
348 if (!FinishBlock(LHSBlock))
349 return 0;
350
351 Succ = ConfluenceBlock;
352 Block = NULL;
353 CFGBlock* RHSBlock = Visit(C->getRHS());
354 if (!FinishBlock(RHSBlock))
355 return 0;
356
357 Block = createBlock(false);
358 Block->addSuccessor(LHSBlock);
359 Block->addSuccessor(RHSBlock);
360 Block->setTerminator(C);
361 return addStmt(C->getCond());
362 }
363
364 case Stmt::DeclStmtClass: {
365 DeclStmt *DS = cast<DeclStmt>(Terminator);
366 if (DS->isSingleDecl()) {
367 Block->appendStmt(Terminator);
368 return WalkAST_VisitDeclSubExpr(DS->getSingleDecl());
369 }
370
371 CFGBlock* B = 0;
372
373 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
374 typedef llvm::SmallVector<Decl*,10> BufTy;
375 BufTy Buf(DS->decl_begin(), DS->decl_end());
376
377 for (BufTy::reverse_iterator I=Buf.rbegin(), E=Buf.rend(); I!=E; ++I) {
378 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
379 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
380 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
381
382 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
383 // automatically freed with the CFG.
384 DeclGroupRef DG(*I);
385 Decl* D = *I;
386 void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
387
388 DeclStmt* DS = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
389
390 // Append the fake DeclStmt to block.
391 Block->appendStmt(DS);
392 B = WalkAST_VisitDeclSubExpr(D);
393 }
394 return B;
395 }
396
397 case Stmt::AddrLabelExprClass: {
398 AddrLabelExpr* A = cast<AddrLabelExpr>(Terminator);
399 AddressTakenLabels.insert(A->getLabel());
400
401 if (AlwaysAddStmt) Block->appendStmt(Terminator);
402 return Block;
403 }
404
405 case Stmt::StmtExprClass:
406 return WalkAST_VisitStmtExpr(cast<StmtExpr>(Terminator));
407
408 case Stmt::SizeOfAlignOfExprClass: {
409 SizeOfAlignOfExpr* E = cast<SizeOfAlignOfExpr>(Terminator);
410
411 // VLA types have expressions that must be evaluated.
412 if (E->isArgumentType()) {
413 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
414 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
415 addStmt(VA->getSizeExpr());
416 }
417 // Expressions in sizeof/alignof are not evaluated and thus have no
418 // control flow.
419 else
420 Block->appendStmt(Terminator);
421
422 return Block;
423 }
424
425 case Stmt::BinaryOperatorClass: {
426 BinaryOperator* B = cast<BinaryOperator>(Terminator);
427
428 if (B->isLogicalOp()) { // && or ||
429 CFGBlock* ConfluenceBlock = (Block) ? Block : createBlock();
430 ConfluenceBlock->appendStmt(B);
431 if (!FinishBlock(ConfluenceBlock))
432 return 0;
433
434 // create the block evaluating the LHS
435 CFGBlock* LHSBlock = createBlock(false);
436 LHSBlock->setTerminator(B);
437
438 // create the block evaluating the RHS
Ted Kremenek49a436d2007-08-31 17:03:41 +0000439 Succ = ConfluenceBlock;
440 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000441 CFGBlock* RHSBlock = Visit(B->getRHS());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000442 if (!FinishBlock(RHSBlock))
443 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000444
445 // Now link the LHSBlock with RHSBlock.
446 if (B->getOpcode() == BinaryOperator::LOr) {
447 LHSBlock->addSuccessor(ConfluenceBlock);
448 LHSBlock->addSuccessor(RHSBlock);
449 } else {
450 assert (B->getOpcode() == BinaryOperator::LAnd);
451 LHSBlock->addSuccessor(RHSBlock);
452 LHSBlock->addSuccessor(ConfluenceBlock);
453 }
454
455 // Generate the blocks for evaluating the LHS.
456 Block = LHSBlock;
457 return addStmt(B->getLHS());
458 } else if (B->getOpcode() == BinaryOperator::Comma) { // ,
459 Block->appendStmt(B);
460 addStmt(B->getRHS());
461 return addStmt(B->getLHS());
Ted Kremenek49a436d2007-08-31 17:03:41 +0000462 }
Ted Kremenek7926f7c2007-08-28 16:18:58 +0000463
Mike Stump6d9828c2009-07-17 01:31:16 +0000464 break;
465 }
Ted Kremenek53061c82008-10-06 20:56:19 +0000466
Ted Kremenek00c0a302008-09-26 18:17:07 +0000467 // Blocks: No support for blocks ... yet
Mike Stump6d9828c2009-07-17 01:31:16 +0000468 case Stmt::BlockExprClass:
469 case Stmt::BlockDeclRefExprClass:
470 return NYS();
471
472 case Stmt::ParenExprClass:
473 return WalkAST(cast<ParenExpr>(Terminator)->getSubExpr(), AlwaysAddStmt);
474
Mike Stumpcd7bf232009-07-17 01:04:31 +0000475 case Stmt::CallExprClass: {
Chris Lattner01bc1602009-07-17 15:50:19 +0000476 // If this is a call to a no-return function, this stops the block here.
477 FunctionDecl *FD = cast<CallExpr>(Terminator)->getDirectCallee();
478 if (FD == 0 || !FD->hasAttr<NoReturnAttr>())
Mike Stumpcd7bf232009-07-17 01:04:31 +0000479 break;
480
Chris Lattner01bc1602009-07-17 15:50:19 +0000481 if (Block && !FinishBlock(Block))
482 return 0;
Mike Stumpcd7bf232009-07-17 01:04:31 +0000483
484 // Create new block with no successor for the remaining pieces.
485 Block = createBlock(false);
486 Block->appendStmt(Terminator);
487
488 // Wire this to the exit block directly.
489 Block->addSuccessor(&cfg->getExit());
490
491 return WalkAST_VisitChildren(Terminator);
492 }
493
Mike Stump6d9828c2009-07-17 01:31:16 +0000494 default:
Chris Lattner01bc1602009-07-17 15:50:19 +0000495 // TODO: We can follow objective-c methods (message sends).
Mike Stump6d9828c2009-07-17 01:31:16 +0000496 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000497 };
Mike Stump6d9828c2009-07-17 01:31:16 +0000498
Ted Kremenek411cdee2008-04-16 21:10:48 +0000499 if (AlwaysAddStmt) Block->appendStmt(Terminator);
500 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000501}
Mike Stump6d9828c2009-07-17 01:31:16 +0000502
503/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions for
504/// initializers in Decls.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000505CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000506 VarDecl* VD = dyn_cast<VarDecl>(D);
507
508 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000509 return Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000510
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000511 Expr* Init = VD->getInit();
Mike Stump6d9828c2009-07-17 01:31:16 +0000512
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000513 if (Init) {
Mike Stump54cc43f2009-02-26 08:00:25 +0000514 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000515 switch (Init->getStmtClass()) {
516 case Stmt::IntegerLiteralClass:
517 case Stmt::CharacterLiteralClass:
518 case Stmt::StringLiteralClass:
519 break;
520 default:
521 Block = addStmt(Init);
522 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000523 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000524
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000525 // If the type of VD is a VLA, then we must process its size expressions.
526 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
527 VA = FindVA(VA->getElementType().getTypePtr()))
Mike Stump6d9828c2009-07-17 01:31:16 +0000528 Block = addStmt(VA->getSizeExpr());
529
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000530 return Block;
531}
532
Mike Stump6d9828c2009-07-17 01:31:16 +0000533/// WalkAST_VisitChildren - Utility method to call WalkAST on the children of a
534/// Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000535CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000536 CFGBlock* B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000537 for (Stmt::child_iterator I = Terminator->child_begin(),
538 E = Terminator->child_end();
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000539 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000540 if (*I) B = WalkAST(*I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000541
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000542 return B;
543}
544
Ted Kremenek15c27a82007-08-28 18:30:10 +0000545/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
546/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000547CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
548 Block->appendStmt(Terminator);
Mike Stump6d9828c2009-07-17 01:31:16 +0000549 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000550}
551
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000552/// VisitStmt - Handle statements with no branching control flow.
553CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000554 // We cannot assume that we are in the middle of a basic block, since the CFG
555 // might only be constructed for this single statement. If we have no current
556 // basic block, just create one lazily.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000557 if (!Block) Block = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000558
559 // Simply add the statement to the current block. We actually insert
560 // statements in reverse order; this order is reversed later when processing
561 // the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000562 addStmt(Statement);
563
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000564 return Block;
565}
566
567CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
568 return Block;
569}
570
571CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000572
Ted Kremeneked47fc62009-07-03 00:10:50 +0000573 CFGBlock* LastBlock = Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000574
Ted Kremenekd34066c2008-02-26 00:22:58 +0000575 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
576 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000577 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000578 }
579
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000580 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000581}
582
583CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000584 // We may see an if statement in the middle of a basic block, or it may be the
585 // first statement we are processing. In either case, we create a new basic
586 // block. First, we create the blocks for the then...else statements, and
587 // then we create the block containing the if statement. If we were in the
588 // middle of a block, we stop processing that block and reverse its
589 // statements. That block is then the implicit successor for the "then" and
590 // "else" clauses.
591
592 // The block we were proccessing is now finished. Make it the successor
593 // block.
594 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000595 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000596 if (!FinishBlock(Block))
597 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000598 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000599
600 // Process the false branch. NULL out Block so that the recursive call to
601 // Visit will create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000602 // Null out Block so that all successor
603 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000604
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000605 if (Stmt* Else = I->getElse()) {
606 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000607
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000608 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000609 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000610 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000611 ElseBlock = Visit(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000612
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000613 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
614 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000615 else if (Block) {
616 if (!FinishBlock(ElseBlock))
617 return 0;
618 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000619 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000620
621 // Process the true branch. NULL out Block so that the recursive call to
622 // Visit will create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000623 // Null out Block so that all successor
624 CFGBlock* ThenBlock;
625 {
626 Stmt* Then = I->getThen();
627 assert (Then);
628 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000629 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000630 ThenBlock = Visit(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000631
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000632 if (!ThenBlock) {
633 // We can reach here if the "then" body has all NullStmts.
634 // Create an empty block so we can distinguish between true and false
635 // branches in path-sensitive analyses.
636 ThenBlock = createBlock(false);
637 ThenBlock->addSuccessor(sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000638 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000639 if (!FinishBlock(ThenBlock))
640 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000641 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000642 }
643
Mike Stump6d9828c2009-07-17 01:31:16 +0000644 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000645 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000646
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000647 // Set the terminator of the new block to the If statement.
648 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000649
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000650 // Now add the successors.
651 Block->addSuccessor(ThenBlock);
652 Block->addSuccessor(ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000653
654 // Add the condition as the last statement in the new block. This may create
655 // new blocks as the condition may contain control-flow. Any newly created
656 // blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000657 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000658}
Mike Stump6d9828c2009-07-17 01:31:16 +0000659
660
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000661CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000662 // If we were in the middle of a block we stop processing that block and
663 // reverse its statements.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000664 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000665 // NOTE: If a "return" appears in the middle of a block, this means that the
666 // code afterwards is DEAD (unreachable). We still keep a basic block
667 // for that code; a simple "mark-and-sweep" from the entry block will be
668 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000669 if (Block) FinishBlock(Block);
670
671 // Create the new block.
672 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000673
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000674 // The Exit block is the only successor.
675 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000676
677 // Add the return statement to the block. This may create new blocks if R
678 // contains control-flow (short-circuit operations).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000679 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000680}
681
682CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
683 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000684 Visit(L->getSubStmt());
685 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000686
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000687 if (!LabelBlock) // This can happen when the body is empty, i.e.
688 LabelBlock=createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000689
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000690 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
691 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000692
693 // Labels partition blocks, so this is the end of the basic block we were
694 // processing (L is the block's label). Because this is label (and we have
695 // already processed the substatement) there is no extra control-flow to worry
696 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000697 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000698 if (!FinishBlock(LabelBlock))
699 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000700
701 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000702 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000703
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000704 // This block is now the implicit successor of other blocks.
705 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000706
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000707 return LabelBlock;
708}
709
710CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000711 // Goto is a control-flow statement. Thus we stop processing the current
712 // block and create a new one.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000713 if (Block) FinishBlock(Block);
714 Block = createBlock(false);
715 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000716
717 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000718 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000719
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000720 if (I == LabelMap.end())
721 // We will need to backpatch this block later.
722 BackpatchBlocks.push_back(Block);
723 else
724 Block->addSuccessor(I->second);
725
Mike Stump6d9828c2009-07-17 01:31:16 +0000726 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000727}
728
729CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000730 // "for" is a control-flow statement. Thus we stop processing the current
731 // block.
732
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000733 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000734
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000735 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000736 if (!FinishBlock(Block))
737 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000738 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000739 } else LoopSuccessor = Succ;
740
741 // Because of short-circuit evaluation, the condition of the loop can span
742 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
743 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000744 CFGBlock* ExitConditionBlock = createBlock(false);
745 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000746
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000747 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000748 ExitConditionBlock->setTerminator(F);
749
750 // Now add the actual condition to the condition block. Because the condition
751 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000752 if (Stmt* C = F->getCond()) {
753 Block = ExitConditionBlock;
754 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000755 if (Block) {
756 if (!FinishBlock(EntryConditionBlock))
757 return 0;
758 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000759 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000760
Mike Stump6d9828c2009-07-17 01:31:16 +0000761 // The condition block is the implicit successor for the loop body as well as
762 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000763 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000764
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000765 // Now create the loop body.
766 {
767 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000768
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000769 // Save the current values for Block, Succ, and continue and break targets
770 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
771 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000772 save_break(BreakTargetBlock);
773
Ted Kremenekaf603f72007-08-30 18:39:40 +0000774 // Create a new block to contain the (bottom) of the loop body.
775 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000776
Ted Kremeneke9334502008-09-04 21:48:47 +0000777 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000778 // Generate increment code in its own basic block. This is the target of
779 // continue statements.
Ted Kremenekd0172432008-11-24 20:50:24 +0000780 Succ = Visit(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000781 } else {
782 // No increment code. Create a special, empty, block that is used as the
783 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000784 assert(Succ == EntryConditionBlock);
785 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000786 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000787
Ted Kremenek3575f842009-04-28 00:51:56 +0000788 // Finish up the increment (or empty) block if it hasn't been already.
789 if (Block) {
790 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000791 if (!FinishBlock(Block))
792 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000793 Block = 0;
794 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000795
Ted Kremenek3575f842009-04-28 00:51:56 +0000796 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000797
Ted Kremenek3575f842009-04-28 00:51:56 +0000798 // The starting block for the loop increment is the block that should
799 // represent the 'loop target' for looping back to the start of the loop.
800 ContinueTargetBlock->setLoopTarget(F);
801
Ted Kremeneke9334502008-09-04 21:48:47 +0000802 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000803 BreakTargetBlock = LoopSuccessor;
804
805 // Now populate the body block, and in the process create new blocks as we
806 // walk the body of the loop.
807 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000808
809 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000810 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000811 else if (Block) {
812 if (!FinishBlock(BodyBlock))
813 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000814 }
815
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000816 // This new body block is a successor to our "exit" condition block.
817 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000818 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000819
820 // Link up the condition block with the code that follows the loop. (the
821 // false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000822 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +0000823
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000824 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000825 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 if (Stmt* I = F->getInit()) {
827 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000828 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000829 } else {
830 // There is no loop initialization. We are thus basically a while loop.
831 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000832 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000833 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000834 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000835 }
836}
837
Ted Kremenek514de5a2008-11-11 17:10:00 +0000838CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
839 // Objective-C fast enumeration 'for' statements:
840 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
841 //
842 // for ( Type newVariable in collection_expression ) { statements }
843 //
844 // becomes:
845 //
846 // prologue:
847 // 1. collection_expression
848 // T. jump to loop_entry
849 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000850 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000851 // 1. ObjCForCollectionStmt [performs binding to newVariable]
852 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
853 // TB:
854 // statements
855 // T. jump to loop_entry
856 // FB:
857 // what comes after
858 //
859 // and
860 //
861 // Type existingItem;
862 // for ( existingItem in expression ) { statements }
863 //
864 // becomes:
865 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000866 // the same with newVariable replaced with existingItem; the binding works
867 // the same except that for one ObjCForCollectionStmt::getElement() returns
868 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +0000869 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000870
Ted Kremenek514de5a2008-11-11 17:10:00 +0000871 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000872
Ted Kremenek514de5a2008-11-11 17:10:00 +0000873 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000874 if (!FinishBlock(Block))
875 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000876 LoopSuccessor = Block;
877 Block = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000878 } else LoopSuccessor = Succ;
879
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000880 // Build the condition blocks.
881 CFGBlock* ExitConditionBlock = createBlock(false);
882 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000883
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000884 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000885 ExitConditionBlock->setTerminator(S);
886
887 // The last statement in the block should be the ObjCForCollectionStmt, which
888 // performs the actual binding to 'element' and determines if there are any
889 // more items in the collection.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000890 ExitConditionBlock->appendStmt(S);
891 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000892
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000893 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +0000894 // generate new blocks as necesary. We DON'T add the statement by default to
895 // the CFG unless it contains control-flow.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000896 EntryConditionBlock = WalkAST(S->getElement(), false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000897 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000898 if (!FinishBlock(EntryConditionBlock))
899 return 0;
900 Block = 0;
901 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000902
903 // The condition block is the implicit successor for the loop body as well as
904 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000905 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000906
Ted Kremenek514de5a2008-11-11 17:10:00 +0000907 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +0000908 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000909 // Save the current values for Succ, continue and break targets.
910 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +0000911 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
912
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000913 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +0000914 ContinueTargetBlock = EntryConditionBlock;
915
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000916 CFGBlock* BodyBlock = Visit(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000917
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000918 if (!BodyBlock)
919 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000920 else if (Block) {
921 if (!FinishBlock(BodyBlock))
922 return 0;
923 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000924
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000925 // This new body block is a successor to our "exit" condition block.
926 ExitConditionBlock->addSuccessor(BodyBlock);
927 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000928
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000929 // Link up the condition block with the code that follows the loop.
930 // (the false branch).
931 ExitConditionBlock->addSuccessor(LoopSuccessor);
932
Ted Kremenek514de5a2008-11-11 17:10:00 +0000933 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000934 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +0000935 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +0000936}
937
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000938CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
939 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +0000940
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000941 // Inline the body.
Ted Kremenekda5348e2009-05-05 23:11:51 +0000942 CFGBlock *SyncBlock = Visit(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000943
Ted Kremenekda5348e2009-05-05 23:11:51 +0000944 // The sync body starts its own basic block. This makes it a little easier
945 // for diagnostic clients.
946 if (SyncBlock) {
947 if (!FinishBlock(SyncBlock))
948 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000949
Ted Kremenekda5348e2009-05-05 23:11:51 +0000950 Block = 0;
951 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000952
Ted Kremenekda5348e2009-05-05 23:11:51 +0000953 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000954
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000955 // Inline the sync expression.
956 return Visit(S->getSynchExpr());
957}
Mike Stump6d9828c2009-07-17 01:31:16 +0000958
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000959CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek90658ec2009-04-07 04:26:02 +0000960 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000961}
Ted Kremenek514de5a2008-11-11 17:10:00 +0000962
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000963CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000964 // "while" is a control-flow statement. Thus we stop processing the current
965 // block.
966
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000967 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000968
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000969 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000970 if (!FinishBlock(Block))
971 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000972 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000973 } else LoopSuccessor = Succ;
974
975 // Because of short-circuit evaluation, the condition of the loop can span
976 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
977 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000978 CFGBlock* ExitConditionBlock = createBlock(false);
979 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000980
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000981 // Set the terminator for the "exit" condition block.
982 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +0000983
984 // Now add the actual condition to the condition block. Because the condition
985 // itself may contain control-flow, new blocks may be created. Thus we update
986 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000987 if (Stmt* C = W->getCond()) {
988 Block = ExitConditionBlock;
989 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +0000990 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000991 if (Block) {
992 if (!FinishBlock(EntryConditionBlock))
993 return 0;
994 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000995 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000996
997 // The condition block is the implicit successor for the loop body as well as
998 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000999 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001000
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001001 // Process the loop body.
1002 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001003 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001004
1005 // Save the current values for Block, Succ, and continue and break targets
1006 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1007 save_continue(ContinueTargetBlock),
1008 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001009
Mike Stump6d9828c2009-07-17 01:31:16 +00001010 // Create an empty block to represent the transition block for looping back
1011 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001012 Block = 0;
1013 assert(Succ == EntryConditionBlock);
1014 Succ = createBlock();
1015 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001016 ContinueTargetBlock = Succ;
1017
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001018 // All breaks should go to the code following the loop.
1019 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001020
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001021 // NULL out Block to force lazy instantiation of blocks for the body.
1022 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001023
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001024 // Create the body. The returned block is the entry to the loop body.
1025 CFGBlock* BodyBlock = Visit(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001026
Ted Kremenekaf603f72007-08-30 18:39:40 +00001027 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001028 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001029 else if (Block) {
1030 if (!FinishBlock(BodyBlock))
1031 return 0;
1032 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001033
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001034 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001035 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001036 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001037
1038 // Link up the condition block with the code that follows the loop. (the
1039 // false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001040 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001041
1042 // There can be no more statements in the condition block since we loop back
1043 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001044 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001045
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001046 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001047 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001048 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001049}
Mike Stump6d9828c2009-07-17 01:31:16 +00001050
Ted Kremenek2fda5042008-12-09 20:20:09 +00001051CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1052 // FIXME: This isn't complete. We basically treat @throw like a return
1053 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001054
1055 // If we were in the middle of a block we stop processing that block and
1056 // reverse its statements.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001057 if (Block) {
1058 if (!FinishBlock(Block))
1059 return 0;
1060 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001061
Ted Kremenek2fda5042008-12-09 20:20:09 +00001062 // Create the new block.
1063 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001064
Ted Kremenek2fda5042008-12-09 20:20:09 +00001065 // The Exit block is the only successor.
1066 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001067
1068 // Add the statement to the block. This may create new blocks if S contains
1069 // control-flow (short-circuit operations).
Ted Kremenek2fda5042008-12-09 20:20:09 +00001070 return addStmt(S);
1071}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001072
1073CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1074 // "do...while" is a control-flow statement. Thus we stop processing the
1075 // current block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001076
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001077 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001078
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001079 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001080 if (!FinishBlock(Block))
1081 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001082 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001083 } else LoopSuccessor = Succ;
1084
1085 // Because of short-circuit evaluation, the condition of the loop can span
1086 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1087 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001088 CFGBlock* ExitConditionBlock = createBlock(false);
1089 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001090
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001091 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001092 ExitConditionBlock->setTerminator(D);
1093
1094 // Now add the actual condition to the condition block. Because the condition
1095 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001096 if (Stmt* C = D->getCond()) {
1097 Block = ExitConditionBlock;
1098 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001099 if (Block) {
1100 if (!FinishBlock(EntryConditionBlock))
1101 return 0;
1102 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001103 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001104
Ted Kremenek54827132008-02-27 07:20:00 +00001105 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001106 Succ = EntryConditionBlock;
1107
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001108 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001109 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001110 {
1111 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001112
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001113 // Save the current values for Block, Succ, and continue and break targets
1114 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1115 save_continue(ContinueTargetBlock),
1116 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001117
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001118 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001119 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001120
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001121 // All breaks should go to the code following the loop.
1122 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001123
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001124 // NULL out Block to force lazy instantiation of blocks for the body.
1125 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001126
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001127 // Create the body. The returned block is the entry to the loop body.
1128 BodyBlock = Visit(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001129
Ted Kremenekaf603f72007-08-30 18:39:40 +00001130 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001131 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001132 else if (Block) {
1133 if (!FinishBlock(BodyBlock))
1134 return 0;
1135 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001136
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001137 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001138 // ExitConditionBlock to represent the "loop back" transition. Create an
1139 // empty block to represent the transition block for looping back to the
1140 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001141 // FIXME: Can we do this more efficiently without adding another block?
1142 Block = NULL;
1143 Succ = BodyBlock;
1144 CFGBlock *LoopBackBlock = createBlock();
1145 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001146
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001147 // Add the loop body entry as a successor to the condition.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001148 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001149 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001150
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001151 // Link up the condition block with the code that follows the loop.
1152 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001153 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001154
1155 // There can be no more statements in the body block(s) since we loop back to
1156 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001157 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001158
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001159 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001160 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001161 return BodyBlock;
1162}
1163
1164CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1165 // "continue" is a control-flow statement. Thus we stop processing the
1166 // current block.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001167 if (Block) {
1168 if (!FinishBlock(Block))
1169 return 0;
1170 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001171
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001172 // Now create a new block that ends with the continue statement.
1173 Block = createBlock(false);
1174 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001175
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001176 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001177 // incomplete AST. This means the CFG cannot be constructed.
1178 if (ContinueTargetBlock)
1179 Block->addSuccessor(ContinueTargetBlock);
1180 else
1181 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001182
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001183 return Block;
1184}
1185
1186CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001187 // "break" is a control-flow statement. Thus we stop processing the current
1188 // block.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001189 if (Block) {
1190 if (!FinishBlock(Block))
1191 return 0;
1192 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001193
Mike Stumpcd7bf232009-07-17 01:04:31 +00001194 // Now create a new block that ends with the break statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001195 Block = createBlock(false);
1196 Block->setTerminator(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001197
1198 // If there is no target for the break, then we are looking at an incomplete
1199 // AST. This means that the CFG cannot be constructed.
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001200 if (BreakTargetBlock)
1201 Block->addSuccessor(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001202 else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001203 badCFG = true;
1204
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001205
Mike Stump6d9828c2009-07-17 01:31:16 +00001206 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001207}
1208
Ted Kremenek411cdee2008-04-16 21:10:48 +00001209CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001210 // "switch" is a control-flow statement. Thus we stop processing the current
1211 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001212 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001213
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001214 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001215 if (!FinishBlock(Block))
1216 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001217 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001218 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001219
1220 // Save the current "switch" context.
1221 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001222 save_break(BreakTargetBlock),
1223 save_default(DefaultCaseBlock);
1224
Mike Stump6d9828c2009-07-17 01:31:16 +00001225 // Set the "default" case to be the block after the switch statement. If the
1226 // switch statement contains a "default:", this value will be overwritten with
1227 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001228 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001229
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001230 // Create a new block that will contain the switch statement.
1231 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001232
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001233 // Now process the switch body. The code after the switch is the implicit
1234 // successor.
1235 Succ = SwitchSuccessor;
1236 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001237
1238 // When visiting the body, the case statements should automatically get linked
1239 // up to the switch. We also don't keep a pointer to the body, since all
1240 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001241 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001242 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001243 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001244 if (Block) {
1245 if (!FinishBlock(BodyBlock))
1246 return 0;
1247 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001248
Mike Stump6d9828c2009-07-17 01:31:16 +00001249 // If we have no "default:" case, the default transition is to the code
1250 // following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001251 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001253 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001254 SwitchTerminatedBlock->setTerminator(Terminator);
1255 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001256 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001257
Ted Kremenek411cdee2008-04-16 21:10:48 +00001258 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001259}
1260
Ted Kremenek411cdee2008-04-16 21:10:48 +00001261CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001262 // CaseStmts are essentially labels, so they are the first statement in a
1263 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001264
Ted Kremenek411cdee2008-04-16 21:10:48 +00001265 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001266 CFGBlock* CaseBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001267 if (!CaseBlock) CaseBlock = createBlock();
1268
1269 // Cases statements partition blocks, so this is the top of the basic block we
1270 // were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001271 CaseBlock->setLabel(Terminator);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001272 if (!FinishBlock(CaseBlock))
1273 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001274
1275 // Add this block to the list of successors for the block with the switch
1276 // statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001277 assert (SwitchTerminatedBlock);
1278 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001279
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1281 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001282
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001283 // This block is now the implicit successor of other blocks.
1284 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001285
Ted Kremenek2677ea82008-03-15 07:45:02 +00001286 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001287}
Mike Stump6d9828c2009-07-17 01:31:16 +00001288
Ted Kremenek411cdee2008-04-16 21:10:48 +00001289CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1290 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001291 DefaultCaseBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001292 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1293
1294 // Default statements partition blocks, so this is the top of the basic block
1295 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001296 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001297 if (!FinishBlock(DefaultCaseBlock))
1298 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001299
Mike Stump6d9828c2009-07-17 01:31:16 +00001300 // Unlike case statements, we don't add the default block to the successors
1301 // for the switch statement immediately. This is done when we finish
1302 // processing the switch statement. This allows for the default case
1303 // (including a fall-through to the code after the switch statement) to always
1304 // be the last successor of a switch-terminated block.
1305
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001306 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1307 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001308
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001309 // This block is now the implicit successor of other blocks.
1310 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001311
1312 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001313}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001314
Ted Kremenek19bb3562007-08-28 19:26:49 +00001315CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001316 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001317 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001318
Ted Kremenek19bb3562007-08-28 19:26:49 +00001319 if (!IBlock) {
1320 IBlock = createBlock(false);
1321 cfg->setIndirectGotoBlock(IBlock);
1322 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001323
Ted Kremenek19bb3562007-08-28 19:26:49 +00001324 // IndirectGoto is a control-flow statement. Thus we stop processing the
1325 // current block and create a new one.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001326 if (Block) {
1327 if (!FinishBlock(Block))
1328 return 0;
1329 }
Ted Kremenek19bb3562007-08-28 19:26:49 +00001330 Block = createBlock(false);
1331 Block->setTerminator(I);
1332 Block->addSuccessor(IBlock);
1333 return addStmt(I->getTarget());
1334}
1335
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001336
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001337} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001338
Mike Stump6d9828c2009-07-17 01:31:16 +00001339/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1340/// no successors or predecessors. If this is the first block created in the
1341/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001342CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001343 bool first_block = begin() == end();
1344
1345 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001346 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001347
1348 // If this is the first block, set it as the Entry and Exit.
1349 if (first_block) Entry = Exit = &front();
1350
1351 // Return the block.
1352 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001353}
1354
Ted Kremenek026473c2007-08-23 16:51:22 +00001355/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1356/// CFG is returned to the caller.
1357CFG* CFG::buildCFG(Stmt* Statement) {
1358 CFGBuilder Builder;
1359 return Builder.buildCFG(Statement);
1360}
1361
1362/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001363void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1364
Ted Kremenek63f58872007-10-01 19:33:33 +00001365//===----------------------------------------------------------------------===//
1366// CFG: Queries for BlkExprs.
1367//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001368
Ted Kremenek63f58872007-10-01 19:33:33 +00001369namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001370 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001371}
1372
Ted Kremenek411cdee2008-04-16 21:10:48 +00001373static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1374 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001375 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001376
Ted Kremenek411cdee2008-04-16 21:10:48 +00001377 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001378 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001379
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001380 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1381 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001382
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001383 FindSubExprAssignments(*I, Set);
1384 }
1385}
1386
Ted Kremenek63f58872007-10-01 19:33:33 +00001387static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1388 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001389
1390 // Look for assignments that are used as subexpressions. These are the only
1391 // assignments that we want to *possibly* register as a block-level
1392 // expression. Basically, if an assignment occurs both in a subexpression and
1393 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001394 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001395
Ted Kremenek63f58872007-10-01 19:33:33 +00001396 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1397 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001398 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001399
Ted Kremenek411cdee2008-04-16 21:10:48 +00001400 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001401
1402 // Iterate over the statements again on identify the Expr* and Stmt* at the
1403 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001404
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001405 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001406 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001407
Ted Kremenek411cdee2008-04-16 21:10:48 +00001408 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001409 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001410 // expression are really "statements" whose value is never used by
1411 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001412 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001413 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001414 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1415 // Special handling for statement expressions. The last statement in
1416 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001417 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001418 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001419 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001420 (*M)[C->body_back()] = x;
1421 }
1422 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001423
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001424 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001425 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001426 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001427
Ted Kremenek411cdee2008-04-16 21:10:48 +00001428 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001429
Ted Kremenek390e48b2008-11-12 21:11:49 +00001430 Stmt* S = I->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
Ted Kremenek390e48b2008-11-12 21:11:49 +00001432 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001433 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001434 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001435 }
1436 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001437
Ted Kremenek63f58872007-10-01 19:33:33 +00001438 return M;
1439}
1440
Ted Kremenek86946742008-01-17 20:48:37 +00001441CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1442 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001443 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
Ted Kremenek63f58872007-10-01 19:33:33 +00001445 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001446 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001447
Ted Kremenek63f58872007-10-01 19:33:33 +00001448 if (I == M->end()) return CFG::BlkExprNumTy();
1449 else return CFG::BlkExprNumTy(I->second);
1450}
1451
1452unsigned CFG::getNumBlkExprs() {
1453 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1454 return M->size();
1455 else {
1456 // We assume callers interested in the number of BlkExprs will want
1457 // the map constructed if it doesn't already exist.
1458 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1459 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1460 }
1461}
1462
Ted Kremenek274f4332008-04-28 18:00:46 +00001463//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001464// Cleanup: CFG dstor.
1465//===----------------------------------------------------------------------===//
1466
Ted Kremenek63f58872007-10-01 19:33:33 +00001467CFG::~CFG() {
1468 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1469}
Mike Stump6d9828c2009-07-17 01:31:16 +00001470
Ted Kremenek7dba8602007-08-29 21:56:09 +00001471//===----------------------------------------------------------------------===//
1472// CFG pretty printing
1473//===----------------------------------------------------------------------===//
1474
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001475namespace {
1476
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001477class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001478
Ted Kremenek42a509f2007-08-31 21:30:12 +00001479 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1480 StmtMapTy StmtMap;
1481 signed CurrentBlock;
1482 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001483 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001484public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001485
Chris Lattnere4f21422009-06-30 01:26:17 +00001486 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1487 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001488 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1489 unsigned j = 1;
1490 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1491 BI != BEnd; ++BI, ++j )
1492 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1493 }
1494 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001495
Ted Kremenek42a509f2007-08-31 21:30:12 +00001496 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001497
Chris Lattnere4f21422009-06-30 01:26:17 +00001498 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001499 void setBlockID(signed i) { CurrentBlock = i; }
1500 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001501
Ted Kremeneka95d3752008-09-13 05:16:45 +00001502 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001503
Ted Kremenek411cdee2008-04-16 21:10:48 +00001504 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001505
1506 if (I == StmtMap.end())
1507 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001508
1509 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001510 && I->second.second == CurrentStmt)
1511 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001512
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001513 OS << "[B" << I->second.first << "." << I->second.second << "]";
1514 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001515 }
1516};
Chris Lattnere4f21422009-06-30 01:26:17 +00001517} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001518
Chris Lattnere4f21422009-06-30 01:26:17 +00001519
1520namespace {
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001521class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1522 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001523
Ted Kremeneka95d3752008-09-13 05:16:45 +00001524 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001525 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001526 PrintingPolicy Policy;
1527
Ted Kremenek42a509f2007-08-31 21:30:12 +00001528public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001529 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001530 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001531 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001532
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001533 void VisitIfStmt(IfStmt* I) {
1534 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001535 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001536 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001537
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001538 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001539 void VisitStmt(Stmt* Terminator) {
1540 Terminator->printPretty(OS, Helper, Policy);
1541 }
1542
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001543 void VisitForStmt(ForStmt* F) {
1544 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001545 if (F->getInit()) OS << "...";
1546 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001547 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001548 OS << "; ";
1549 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001550 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001551 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001552
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001553 void VisitWhileStmt(WhileStmt* W) {
1554 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001555 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001556 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001557
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001558 void VisitDoStmt(DoStmt* D) {
1559 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001560 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001561 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001562
Ted Kremenek411cdee2008-04-16 21:10:48 +00001563 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001564 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001565 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001566 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
Ted Kremenek805e9a82007-08-31 21:49:40 +00001568 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001569 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001570 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001571 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001572
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001573 void VisitChooseExpr(ChooseExpr* C) {
1574 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001575 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001576 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001577 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001578
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001579 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1580 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001581 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001582 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001583
Ted Kremenek805e9a82007-08-31 21:49:40 +00001584 void VisitBinaryOperator(BinaryOperator* B) {
1585 if (!B->isLogicalOp()) {
1586 VisitExpr(B);
1587 return;
1588 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001590 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001591
Ted Kremenek805e9a82007-08-31 21:49:40 +00001592 switch (B->getOpcode()) {
1593 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001594 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001595 return;
1596 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001597 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001598 return;
1599 default:
1600 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001601 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001602 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001603
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001604 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001605 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001606 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001607};
Chris Lattnere4f21422009-06-30 01:26:17 +00001608} // end anonymous namespace
1609
Mike Stump6d9828c2009-07-17 01:31:16 +00001610
Chris Lattnere4f21422009-06-30 01:26:17 +00001611static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1612 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001613 if (Helper) {
1614 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001615 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001616 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001618 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001619 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001620 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001621 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001622 return;
1623 }
1624 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001625
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001626 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001627 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001628 if (B->getOpcode() == BinaryOperator::Comma) {
1629 OS << "... , ";
1630 Helper->handledStmt(B->getRHS(),OS);
1631 OS << '\n';
1632 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001633 }
1634 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001635 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001636
Chris Lattnere4f21422009-06-30 01:26:17 +00001637 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001638
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001639 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001640 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001641}
Mike Stump6d9828c2009-07-17 01:31:16 +00001642
Chris Lattnere4f21422009-06-30 01:26:17 +00001643static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1644 const CFGBlock& B,
1645 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001646
Ted Kremenek42a509f2007-08-31 21:30:12 +00001647 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001648
Ted Kremenek7dba8602007-08-29 21:56:09 +00001649 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001650 OS << "\n [ B" << B.getBlockID();
1651
Ted Kremenek42a509f2007-08-31 21:30:12 +00001652 if (&B == &cfg->getEntry())
1653 OS << " (ENTRY) ]\n";
1654 else if (&B == &cfg->getExit())
1655 OS << " (EXIT) ]\n";
1656 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001657 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001658 else
1659 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001660
Ted Kremenek9cffe732007-08-29 23:20:49 +00001661 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001662 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001663
1664 if (print_edges)
1665 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001666
Ted Kremenek411cdee2008-04-16 21:10:48 +00001667 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001668 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001669 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001670 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001671 C->getLHS()->printPretty(OS, Helper,
1672 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001673 if (C->getRHS()) {
1674 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001675 C->getRHS()->printPretty(OS, Helper,
1676 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001677 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001678 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001679 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001680 else
1681 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001682
Ted Kremenek9cffe732007-08-29 23:20:49 +00001683 OS << ":\n";
1684 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001685
Ted Kremenekfddd5182007-08-21 21:42:03 +00001686 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001687 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001688
Ted Kremenek42a509f2007-08-31 21:30:12 +00001689 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1690 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001691
Ted Kremenek9cffe732007-08-29 23:20:49 +00001692 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001693 if (print_edges)
1694 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001695
Ted Kremeneka95d3752008-09-13 05:16:45 +00001696 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001697
Ted Kremenek42a509f2007-08-31 21:30:12 +00001698 if (Helper)
1699 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001700
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001701 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001702 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001703
Ted Kremenek9cffe732007-08-29 23:20:49 +00001704 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001705 if (B.getTerminator()) {
1706 if (print_edges)
1707 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001708
Ted Kremenek9cffe732007-08-29 23:20:49 +00001709 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001710
Ted Kremenek42a509f2007-08-31 21:30:12 +00001711 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001712
Chris Lattnere4f21422009-06-30 01:26:17 +00001713 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1714 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001715 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001716 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001717 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001718
Ted Kremenek9cffe732007-08-29 23:20:49 +00001719 if (print_edges) {
1720 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001721 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001722 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001723
Ted Kremenek42a509f2007-08-31 21:30:12 +00001724 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1725 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001726
Ted Kremenek42a509f2007-08-31 21:30:12 +00001727 if (i == 8 || (i-8) == 0)
1728 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001729
Ted Kremenek9cffe732007-08-29 23:20:49 +00001730 OS << " B" << (*I)->getBlockID();
1731 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001732
Ted Kremenek42a509f2007-08-31 21:30:12 +00001733 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001734
Ted Kremenek42a509f2007-08-31 21:30:12 +00001735 // Print the successors of this block.
1736 OS << " Successors (" << B.succ_size() << "):";
1737 i = 0;
1738
1739 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1740 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
Ted Kremenek42a509f2007-08-31 21:30:12 +00001742 if (i == 8 || (i-8) % 10 == 0)
1743 OS << "\n ";
1744
1745 OS << " B" << (*I)->getBlockID();
1746 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001747
Ted Kremenek9cffe732007-08-29 23:20:49 +00001748 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001749 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001750}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001751
Ted Kremenek42a509f2007-08-31 21:30:12 +00001752
1753/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001754void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001755
1756/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001757void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1758 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001759
Ted Kremenek42a509f2007-08-31 21:30:12 +00001760 // Print the entry block.
1761 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001762
Ted Kremenek42a509f2007-08-31 21:30:12 +00001763 // Iterate through the CFGBlocks and print them one by one.
1764 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1765 // Skip the entry block, because we already printed it.
1766 if (&(*I) == &getEntry() || &(*I) == &getExit())
1767 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001768
Ted Kremenek42a509f2007-08-31 21:30:12 +00001769 print_block(OS, this, *I, &Helper, true);
1770 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001771
Ted Kremenek42a509f2007-08-31 21:30:12 +00001772 // Print the exit block.
1773 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001774 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001775}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001776
1777/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001778void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1779 print(llvm::errs(), cfg, LO);
1780}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001781
1782/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1783/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001784void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1785 const LangOptions &LO) const {
1786 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001787 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001788}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001789
Ted Kremeneka2925852008-01-30 23:02:42 +00001790/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001791void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001792 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001793 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001794 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1795}
1796
Ted Kremenek390e48b2008-11-12 21:11:49 +00001797Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Ted Kremenek411cdee2008-04-16 21:10:48 +00001799 if (!Terminator)
1800 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001801
Ted Kremenek411cdee2008-04-16 21:10:48 +00001802 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001803
Ted Kremenek411cdee2008-04-16 21:10:48 +00001804 switch (Terminator->getStmtClass()) {
1805 default:
1806 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001807
Ted Kremenek411cdee2008-04-16 21:10:48 +00001808 case Stmt::ForStmtClass:
1809 E = cast<ForStmt>(Terminator)->getCond();
1810 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001811
Ted Kremenek411cdee2008-04-16 21:10:48 +00001812 case Stmt::WhileStmtClass:
1813 E = cast<WhileStmt>(Terminator)->getCond();
1814 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001815
Ted Kremenek411cdee2008-04-16 21:10:48 +00001816 case Stmt::DoStmtClass:
1817 E = cast<DoStmt>(Terminator)->getCond();
1818 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001819
Ted Kremenek411cdee2008-04-16 21:10:48 +00001820 case Stmt::IfStmtClass:
1821 E = cast<IfStmt>(Terminator)->getCond();
1822 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001823
Ted Kremenek411cdee2008-04-16 21:10:48 +00001824 case Stmt::ChooseExprClass:
1825 E = cast<ChooseExpr>(Terminator)->getCond();
1826 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001827
Ted Kremenek411cdee2008-04-16 21:10:48 +00001828 case Stmt::IndirectGotoStmtClass:
1829 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1830 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001831
Ted Kremenek411cdee2008-04-16 21:10:48 +00001832 case Stmt::SwitchStmtClass:
1833 E = cast<SwitchStmt>(Terminator)->getCond();
1834 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001835
Ted Kremenek411cdee2008-04-16 21:10:48 +00001836 case Stmt::ConditionalOperatorClass:
1837 E = cast<ConditionalOperator>(Terminator)->getCond();
1838 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001839
Ted Kremenek411cdee2008-04-16 21:10:48 +00001840 case Stmt::BinaryOperatorClass: // '&&' and '||'
1841 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001842 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001843
Ted Kremenek390e48b2008-11-12 21:11:49 +00001844 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001845 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001846 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremenek411cdee2008-04-16 21:10:48 +00001848 return E ? E->IgnoreParens() : NULL;
1849}
1850
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001851bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00001852
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001853 if (!Terminator)
1854 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001856 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001858 switch (Terminator->getStmtClass()) {
1859 default:
1860 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001861
1862 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001863 case Stmt::WhileStmtClass:
1864 case Stmt::DoStmtClass:
1865 case Stmt::IfStmtClass:
1866 case Stmt::ChooseExprClass:
1867 case Stmt::ConditionalOperatorClass:
1868 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001869 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001870 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001871
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001872 return E ? E->IgnoreParens() : NULL;
1873}
1874
Ted Kremeneka2925852008-01-30 23:02:42 +00001875
Ted Kremenek7dba8602007-08-29 21:56:09 +00001876//===----------------------------------------------------------------------===//
1877// CFG Graphviz Visualization
1878//===----------------------------------------------------------------------===//
1879
Ted Kremenek42a509f2007-08-31 21:30:12 +00001880
1881#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00001882static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001883#endif
1884
Chris Lattnere4f21422009-06-30 01:26:17 +00001885void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001886#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00001887 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001888 GraphHelper = &H;
1889 llvm::ViewGraph(this,"CFG");
1890 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001891#endif
1892}
1893
Ted Kremenek7dba8602007-08-29 21:56:09 +00001894namespace llvm {
1895template<>
1896struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson02995ce2009-06-24 17:37:55 +00001897 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
1898 bool ShortNames) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00001899
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001900#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001901 std::string OutSStr;
1902 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001903 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001904 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001905
1906 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1907
1908 // Process string output to make it nicer...
1909 for (unsigned i = 0; i != OutStr.length(); ++i)
1910 if (OutStr[i] == '\n') { // Left justify
1911 OutStr[i] = '\\';
1912 OutStr.insert(OutStr.begin()+i+1, 'l');
1913 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001914
Ted Kremenek7dba8602007-08-29 21:56:09 +00001915 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001916#else
1917 return "";
1918#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001919 }
1920};
1921} // end namespace llvm