blob: 884bc177fdc839a05a48e5de4c8f260faf60465f [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: {
476 bool NoReturn = false;
477 CallExpr *C = cast<CallExpr>(Terminator);
478 Expr *CEE = C->getCallee()->IgnoreParenCasts();
479 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {
480 // FIXME: We can follow objective-c methods and C++ member functions...
481 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DRE->getDecl())) {
482 if (FD->hasAttr<NoReturnAttr>())
483 NoReturn = true;
484 }
485 }
486
487 if (!NoReturn)
488 break;
489
490 if (Block) {
491 if (!FinishBlock(Block))
492 return 0;
493 }
494
495 // Create new block with no successor for the remaining pieces.
496 Block = createBlock(false);
497 Block->appendStmt(Terminator);
498
499 // Wire this to the exit block directly.
500 Block->addSuccessor(&cfg->getExit());
501
502 return WalkAST_VisitChildren(Terminator);
503 }
504
Mike Stump6d9828c2009-07-17 01:31:16 +0000505 default:
506 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000507 };
Mike Stump6d9828c2009-07-17 01:31:16 +0000508
Ted Kremenek411cdee2008-04-16 21:10:48 +0000509 if (AlwaysAddStmt) Block->appendStmt(Terminator);
510 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000511}
Mike Stump6d9828c2009-07-17 01:31:16 +0000512
513/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions for
514/// initializers in Decls.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000515CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000516 VarDecl* VD = dyn_cast<VarDecl>(D);
517
518 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000519 return Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000520
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000521 Expr* Init = VD->getInit();
Mike Stump6d9828c2009-07-17 01:31:16 +0000522
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000523 if (Init) {
Mike Stump54cc43f2009-02-26 08:00:25 +0000524 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000525 switch (Init->getStmtClass()) {
526 case Stmt::IntegerLiteralClass:
527 case Stmt::CharacterLiteralClass:
528 case Stmt::StringLiteralClass:
529 break;
530 default:
531 Block = addStmt(Init);
532 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000533 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000534
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000535 // If the type of VD is a VLA, then we must process its size expressions.
536 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
537 VA = FindVA(VA->getElementType().getTypePtr()))
Mike Stump6d9828c2009-07-17 01:31:16 +0000538 Block = addStmt(VA->getSizeExpr());
539
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000540 return Block;
541}
542
Mike Stump6d9828c2009-07-17 01:31:16 +0000543/// WalkAST_VisitChildren - Utility method to call WalkAST on the children of a
544/// Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000545CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000546 CFGBlock* B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000547 for (Stmt::child_iterator I = Terminator->child_begin(),
548 E = Terminator->child_end();
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000549 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000550 if (*I) B = WalkAST(*I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000551
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000552 return B;
553}
554
Ted Kremenek15c27a82007-08-28 18:30:10 +0000555/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
556/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000557CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
558 Block->appendStmt(Terminator);
Mike Stump6d9828c2009-07-17 01:31:16 +0000559 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000560}
561
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000562/// VisitStmt - Handle statements with no branching control flow.
563CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000564 // We cannot assume that we are in the middle of a basic block, since the CFG
565 // might only be constructed for this single statement. If we have no current
566 // basic block, just create one lazily.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000567 if (!Block) Block = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000568
569 // Simply add the statement to the current block. We actually insert
570 // statements in reverse order; this order is reversed later when processing
571 // the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000572 addStmt(Statement);
573
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000574 return Block;
575}
576
577CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
578 return Block;
579}
580
581CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000582
Ted Kremeneked47fc62009-07-03 00:10:50 +0000583 CFGBlock* LastBlock = Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000584
Ted Kremenekd34066c2008-02-26 00:22:58 +0000585 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
586 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000587 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000588 }
589
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000590 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000591}
592
593CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000594 // We may see an if statement in the middle of a basic block, or it may be the
595 // first statement we are processing. In either case, we create a new basic
596 // block. First, we create the blocks for the then...else statements, and
597 // then we create the block containing the if statement. If we were in the
598 // middle of a block, we stop processing that block and reverse its
599 // statements. That block is then the implicit successor for the "then" and
600 // "else" clauses.
601
602 // The block we were proccessing is now finished. Make it the successor
603 // block.
604 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000605 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000606 if (!FinishBlock(Block))
607 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000608 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000609
610 // Process the false branch. NULL out Block so that the recursive call to
611 // Visit will create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000612 // Null out Block so that all successor
613 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000614
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000615 if (Stmt* Else = I->getElse()) {
616 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000617
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000618 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000619 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000620 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000621 ElseBlock = Visit(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000622
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000623 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
624 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000625 else if (Block) {
626 if (!FinishBlock(ElseBlock))
627 return 0;
628 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000629 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000630
631 // Process the true branch. NULL out Block so that the recursive call to
632 // Visit will create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000633 // Null out Block so that all successor
634 CFGBlock* ThenBlock;
635 {
636 Stmt* Then = I->getThen();
637 assert (Then);
638 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000639 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000640 ThenBlock = Visit(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000641
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000642 if (!ThenBlock) {
643 // We can reach here if the "then" body has all NullStmts.
644 // Create an empty block so we can distinguish between true and false
645 // branches in path-sensitive analyses.
646 ThenBlock = createBlock(false);
647 ThenBlock->addSuccessor(sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000648 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000649 if (!FinishBlock(ThenBlock))
650 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000651 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000652 }
653
Mike Stump6d9828c2009-07-17 01:31:16 +0000654 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000655 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000656
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000657 // Set the terminator of the new block to the If statement.
658 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000659
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000660 // Now add the successors.
661 Block->addSuccessor(ThenBlock);
662 Block->addSuccessor(ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000663
664 // Add the condition as the last statement in the new block. This may create
665 // new blocks as the condition may contain control-flow. Any newly created
666 // blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000667 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000668}
Mike Stump6d9828c2009-07-17 01:31:16 +0000669
670
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000671CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000672 // If we were in the middle of a block we stop processing that block and
673 // reverse its statements.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000674 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000675 // NOTE: If a "return" appears in the middle of a block, this means that the
676 // code afterwards is DEAD (unreachable). We still keep a basic block
677 // for that code; a simple "mark-and-sweep" from the entry block will be
678 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000679 if (Block) FinishBlock(Block);
680
681 // Create the new block.
682 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000683
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000684 // The Exit block is the only successor.
685 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000686
687 // Add the return statement to the block. This may create new blocks if R
688 // contains control-flow (short-circuit operations).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000689 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000690}
691
692CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
693 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000694 Visit(L->getSubStmt());
695 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000696
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000697 if (!LabelBlock) // This can happen when the body is empty, i.e.
698 LabelBlock=createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000699
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000700 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
701 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000702
703 // Labels partition blocks, so this is the end of the basic block we were
704 // processing (L is the block's label). Because this is label (and we have
705 // already processed the substatement) there is no extra control-flow to worry
706 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000707 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000708 if (!FinishBlock(LabelBlock))
709 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000710
711 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000712 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000713
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000714 // This block is now the implicit successor of other blocks.
715 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000716
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000717 return LabelBlock;
718}
719
720CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000721 // Goto is a control-flow statement. Thus we stop processing the current
722 // block and create a new one.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000723 if (Block) FinishBlock(Block);
724 Block = createBlock(false);
725 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000726
727 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000728 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000729
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000730 if (I == LabelMap.end())
731 // We will need to backpatch this block later.
732 BackpatchBlocks.push_back(Block);
733 else
734 Block->addSuccessor(I->second);
735
Mike Stump6d9828c2009-07-17 01:31:16 +0000736 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000737}
738
739CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000740 // "for" is a control-flow statement. Thus we stop processing the current
741 // block.
742
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000743 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000744
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000745 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000746 if (!FinishBlock(Block))
747 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000748 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000749 } else LoopSuccessor = Succ;
750
751 // Because of short-circuit evaluation, the condition of the loop can span
752 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
753 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000754 CFGBlock* ExitConditionBlock = createBlock(false);
755 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000756
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000757 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000758 ExitConditionBlock->setTerminator(F);
759
760 // Now add the actual condition to the condition block. Because the condition
761 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000762 if (Stmt* C = F->getCond()) {
763 Block = ExitConditionBlock;
764 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000765 if (Block) {
766 if (!FinishBlock(EntryConditionBlock))
767 return 0;
768 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000769 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000770
Mike Stump6d9828c2009-07-17 01:31:16 +0000771 // The condition block is the implicit successor for the loop body as well as
772 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000773 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000774
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000775 // Now create the loop body.
776 {
777 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000778
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000779 // Save the current values for Block, Succ, and continue and break targets
780 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
781 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000782 save_break(BreakTargetBlock);
783
Ted Kremenekaf603f72007-08-30 18:39:40 +0000784 // Create a new block to contain the (bottom) of the loop body.
785 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000786
Ted Kremeneke9334502008-09-04 21:48:47 +0000787 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000788 // Generate increment code in its own basic block. This is the target of
789 // continue statements.
Ted Kremenekd0172432008-11-24 20:50:24 +0000790 Succ = Visit(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000791 } else {
792 // No increment code. Create a special, empty, block that is used as the
793 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000794 assert(Succ == EntryConditionBlock);
795 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000796 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000797
Ted Kremenek3575f842009-04-28 00:51:56 +0000798 // Finish up the increment (or empty) block if it hasn't been already.
799 if (Block) {
800 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000801 if (!FinishBlock(Block))
802 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000803 Block = 0;
804 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000805
Ted Kremenek3575f842009-04-28 00:51:56 +0000806 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000807
Ted Kremenek3575f842009-04-28 00:51:56 +0000808 // The starting block for the loop increment is the block that should
809 // represent the 'loop target' for looping back to the start of the loop.
810 ContinueTargetBlock->setLoopTarget(F);
811
Ted Kremeneke9334502008-09-04 21:48:47 +0000812 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000813 BreakTargetBlock = LoopSuccessor;
814
815 // Now populate the body block, and in the process create new blocks as we
816 // walk the body of the loop.
817 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000818
819 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000820 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000821 else if (Block) {
822 if (!FinishBlock(BodyBlock))
823 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000824 }
825
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000826 // This new body block is a successor to our "exit" condition block.
827 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000828 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000829
830 // Link up the condition block with the code that follows the loop. (the
831 // false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000832 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +0000833
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000834 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000835 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000836 if (Stmt* I = F->getInit()) {
837 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000838 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000839 } else {
840 // There is no loop initialization. We are thus basically a while loop.
841 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000843 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000844 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000845 }
846}
847
Ted Kremenek514de5a2008-11-11 17:10:00 +0000848CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
849 // Objective-C fast enumeration 'for' statements:
850 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
851 //
852 // for ( Type newVariable in collection_expression ) { statements }
853 //
854 // becomes:
855 //
856 // prologue:
857 // 1. collection_expression
858 // T. jump to loop_entry
859 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000860 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000861 // 1. ObjCForCollectionStmt [performs binding to newVariable]
862 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
863 // TB:
864 // statements
865 // T. jump to loop_entry
866 // FB:
867 // what comes after
868 //
869 // and
870 //
871 // Type existingItem;
872 // for ( existingItem in expression ) { statements }
873 //
874 // becomes:
875 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000876 // the same with newVariable replaced with existingItem; the binding works
877 // the same except that for one ObjCForCollectionStmt::getElement() returns
878 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +0000879 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000880
Ted Kremenek514de5a2008-11-11 17:10:00 +0000881 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000882
Ted Kremenek514de5a2008-11-11 17:10:00 +0000883 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000884 if (!FinishBlock(Block))
885 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000886 LoopSuccessor = Block;
887 Block = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000888 } else LoopSuccessor = Succ;
889
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000890 // Build the condition blocks.
891 CFGBlock* ExitConditionBlock = createBlock(false);
892 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000893
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000894 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000895 ExitConditionBlock->setTerminator(S);
896
897 // The last statement in the block should be the ObjCForCollectionStmt, which
898 // performs the actual binding to 'element' and determines if there are any
899 // more items in the collection.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000900 ExitConditionBlock->appendStmt(S);
901 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000902
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000903 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +0000904 // generate new blocks as necesary. We DON'T add the statement by default to
905 // the CFG unless it contains control-flow.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000906 EntryConditionBlock = WalkAST(S->getElement(), false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000907 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000908 if (!FinishBlock(EntryConditionBlock))
909 return 0;
910 Block = 0;
911 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000912
913 // The condition block is the implicit successor for the loop body as well as
914 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000915 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000916
Ted Kremenek514de5a2008-11-11 17:10:00 +0000917 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +0000918 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000919 // Save the current values for Succ, continue and break targets.
920 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +0000921 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
922
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000923 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +0000924 ContinueTargetBlock = EntryConditionBlock;
925
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000926 CFGBlock* BodyBlock = Visit(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000927
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000928 if (!BodyBlock)
929 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000930 else if (Block) {
931 if (!FinishBlock(BodyBlock))
932 return 0;
933 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000934
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000935 // This new body block is a successor to our "exit" condition block.
936 ExitConditionBlock->addSuccessor(BodyBlock);
937 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000938
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000939 // Link up the condition block with the code that follows the loop.
940 // (the false branch).
941 ExitConditionBlock->addSuccessor(LoopSuccessor);
942
Ted Kremenek514de5a2008-11-11 17:10:00 +0000943 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000944 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +0000945 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +0000946}
947
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000948CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
949 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +0000950
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000951 // Inline the body.
Ted Kremenekda5348e2009-05-05 23:11:51 +0000952 CFGBlock *SyncBlock = Visit(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000953
Ted Kremenekda5348e2009-05-05 23:11:51 +0000954 // The sync body starts its own basic block. This makes it a little easier
955 // for diagnostic clients.
956 if (SyncBlock) {
957 if (!FinishBlock(SyncBlock))
958 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000959
Ted Kremenekda5348e2009-05-05 23:11:51 +0000960 Block = 0;
961 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000962
Ted Kremenekda5348e2009-05-05 23:11:51 +0000963 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000964
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000965 // Inline the sync expression.
966 return Visit(S->getSynchExpr());
967}
Mike Stump6d9828c2009-07-17 01:31:16 +0000968
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000969CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek90658ec2009-04-07 04:26:02 +0000970 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000971}
Ted Kremenek514de5a2008-11-11 17:10:00 +0000972
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000973CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000974 // "while" is a control-flow statement. Thus we stop processing the current
975 // block.
976
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000977 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000978
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000979 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000980 if (!FinishBlock(Block))
981 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000982 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000983 } else LoopSuccessor = Succ;
984
985 // Because of short-circuit evaluation, the condition of the loop can span
986 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
987 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000988 CFGBlock* ExitConditionBlock = createBlock(false);
989 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000990
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000991 // Set the terminator for the "exit" condition block.
992 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +0000993
994 // Now add the actual condition to the condition block. Because the condition
995 // itself may contain control-flow, new blocks may be created. Thus we update
996 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000997 if (Stmt* C = W->getCond()) {
998 Block = ExitConditionBlock;
999 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001000 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001001 if (Block) {
1002 if (!FinishBlock(EntryConditionBlock))
1003 return 0;
1004 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001005 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001006
1007 // The condition block is the implicit successor for the loop body as well as
1008 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001009 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001010
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001011 // Process the loop body.
1012 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001013 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001014
1015 // Save the current values for Block, Succ, and continue and break targets
1016 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1017 save_continue(ContinueTargetBlock),
1018 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001019
Mike Stump6d9828c2009-07-17 01:31:16 +00001020 // Create an empty block to represent the transition block for looping back
1021 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001022 Block = 0;
1023 assert(Succ == EntryConditionBlock);
1024 Succ = createBlock();
1025 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001026 ContinueTargetBlock = Succ;
1027
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001028 // All breaks should go to the code following the loop.
1029 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001030
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001031 // NULL out Block to force lazy instantiation of blocks for the body.
1032 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001033
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001034 // Create the body. The returned block is the entry to the loop body.
1035 CFGBlock* BodyBlock = Visit(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001036
Ted Kremenekaf603f72007-08-30 18:39:40 +00001037 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001038 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001039 else if (Block) {
1040 if (!FinishBlock(BodyBlock))
1041 return 0;
1042 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001043
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001044 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001045 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001046 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001047
1048 // Link up the condition block with the code that follows the loop. (the
1049 // false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001050 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001051
1052 // There can be no more statements in the condition block since we loop back
1053 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001054 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001055
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001056 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001057 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001058 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001059}
Mike Stump6d9828c2009-07-17 01:31:16 +00001060
Ted Kremenek2fda5042008-12-09 20:20:09 +00001061CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1062 // FIXME: This isn't complete. We basically treat @throw like a return
1063 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001064
1065 // If we were in the middle of a block we stop processing that block and
1066 // reverse its statements.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001067 if (Block) {
1068 if (!FinishBlock(Block))
1069 return 0;
1070 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001071
Ted Kremenek2fda5042008-12-09 20:20:09 +00001072 // Create the new block.
1073 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001074
Ted Kremenek2fda5042008-12-09 20:20:09 +00001075 // The Exit block is the only successor.
1076 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001077
1078 // Add the statement to the block. This may create new blocks if S contains
1079 // control-flow (short-circuit operations).
Ted Kremenek2fda5042008-12-09 20:20:09 +00001080 return addStmt(S);
1081}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001082
1083CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1084 // "do...while" is a control-flow statement. Thus we stop processing the
1085 // current block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001086
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001087 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001088
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001089 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001090 if (!FinishBlock(Block))
1091 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001092 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001093 } else LoopSuccessor = Succ;
1094
1095 // Because of short-circuit evaluation, the condition of the loop can span
1096 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1097 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001098 CFGBlock* ExitConditionBlock = createBlock(false);
1099 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001100
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001101 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001102 ExitConditionBlock->setTerminator(D);
1103
1104 // Now add the actual condition to the condition block. Because the condition
1105 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001106 if (Stmt* C = D->getCond()) {
1107 Block = ExitConditionBlock;
1108 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001109 if (Block) {
1110 if (!FinishBlock(EntryConditionBlock))
1111 return 0;
1112 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001113 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001114
Ted Kremenek54827132008-02-27 07:20:00 +00001115 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001116 Succ = EntryConditionBlock;
1117
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001118 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001119 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001120 {
1121 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001122
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001123 // Save the current values for Block, Succ, and continue and break targets
1124 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1125 save_continue(ContinueTargetBlock),
1126 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001127
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001128 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001129 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001130
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001131 // All breaks should go to the code following the loop.
1132 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001133
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001134 // NULL out Block to force lazy instantiation of blocks for the body.
1135 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001136
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001137 // Create the body. The returned block is the entry to the loop body.
1138 BodyBlock = Visit(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001139
Ted Kremenekaf603f72007-08-30 18:39:40 +00001140 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001141 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001142 else if (Block) {
1143 if (!FinishBlock(BodyBlock))
1144 return 0;
1145 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001146
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001147 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001148 // ExitConditionBlock to represent the "loop back" transition. Create an
1149 // empty block to represent the transition block for looping back to the
1150 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001151 // FIXME: Can we do this more efficiently without adding another block?
1152 Block = NULL;
1153 Succ = BodyBlock;
1154 CFGBlock *LoopBackBlock = createBlock();
1155 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001156
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001157 // Add the loop body entry as a successor to the condition.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001158 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001159 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001160
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001161 // Link up the condition block with the code that follows the loop.
1162 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001163 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001164
1165 // There can be no more statements in the body block(s) since we loop back to
1166 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001167 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001168
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001169 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001170 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001171 return BodyBlock;
1172}
1173
1174CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1175 // "continue" is a control-flow statement. Thus we stop processing the
1176 // current block.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001177 if (Block) {
1178 if (!FinishBlock(Block))
1179 return 0;
1180 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001181
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001182 // Now create a new block that ends with the continue statement.
1183 Block = createBlock(false);
1184 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001185
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001186 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001187 // incomplete AST. This means the CFG cannot be constructed.
1188 if (ContinueTargetBlock)
1189 Block->addSuccessor(ContinueTargetBlock);
1190 else
1191 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001192
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001193 return Block;
1194}
1195
1196CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001197 // "break" is a control-flow statement. Thus we stop processing the current
1198 // block.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001199 if (Block) {
1200 if (!FinishBlock(Block))
1201 return 0;
1202 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001203
Mike Stumpcd7bf232009-07-17 01:04:31 +00001204 // Now create a new block that ends with the break statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001205 Block = createBlock(false);
1206 Block->setTerminator(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001207
1208 // If there is no target for the break, then we are looking at an incomplete
1209 // AST. This means that the CFG cannot be constructed.
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001210 if (BreakTargetBlock)
1211 Block->addSuccessor(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001212 else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001213 badCFG = true;
1214
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001215
Mike Stump6d9828c2009-07-17 01:31:16 +00001216 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001217}
1218
Ted Kremenek411cdee2008-04-16 21:10:48 +00001219CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001220 // "switch" is a control-flow statement. Thus we stop processing the current
1221 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001222 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001223
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001224 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001225 if (!FinishBlock(Block))
1226 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001227 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001228 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001229
1230 // Save the current "switch" context.
1231 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001232 save_break(BreakTargetBlock),
1233 save_default(DefaultCaseBlock);
1234
Mike Stump6d9828c2009-07-17 01:31:16 +00001235 // Set the "default" case to be the block after the switch statement. If the
1236 // switch statement contains a "default:", this value will be overwritten with
1237 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001238 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001239
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001240 // Create a new block that will contain the switch statement.
1241 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001242
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001243 // Now process the switch body. The code after the switch is the implicit
1244 // successor.
1245 Succ = SwitchSuccessor;
1246 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001247
1248 // When visiting the body, the case statements should automatically get linked
1249 // up to the switch. We also don't keep a pointer to the body, since all
1250 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001251 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001252 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001253 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001254 if (Block) {
1255 if (!FinishBlock(BodyBlock))
1256 return 0;
1257 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001258
Mike Stump6d9828c2009-07-17 01:31:16 +00001259 // If we have no "default:" case, the default transition is to the code
1260 // following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001261 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001262
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001263 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001264 SwitchTerminatedBlock->setTerminator(Terminator);
1265 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001266 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001267
Ted Kremenek411cdee2008-04-16 21:10:48 +00001268 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001269}
1270
Ted Kremenek411cdee2008-04-16 21:10:48 +00001271CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001272 // CaseStmts are essentially labels, so they are the first statement in a
1273 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001274
Ted Kremenek411cdee2008-04-16 21:10:48 +00001275 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001276 CFGBlock* CaseBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001277 if (!CaseBlock) CaseBlock = createBlock();
1278
1279 // Cases statements partition blocks, so this is the top of the basic block we
1280 // were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001281 CaseBlock->setLabel(Terminator);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001282 if (!FinishBlock(CaseBlock))
1283 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001284
1285 // Add this block to the list of successors for the block with the switch
1286 // statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001287 assert (SwitchTerminatedBlock);
1288 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001289
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001290 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1291 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001293 // This block is now the implicit successor of other blocks.
1294 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001295
Ted Kremenek2677ea82008-03-15 07:45:02 +00001296 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001297}
Mike Stump6d9828c2009-07-17 01:31:16 +00001298
Ted Kremenek411cdee2008-04-16 21:10:48 +00001299CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1300 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001301 DefaultCaseBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001302 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1303
1304 // Default statements partition blocks, so this is the top of the basic block
1305 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001306 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001307 if (!FinishBlock(DefaultCaseBlock))
1308 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001309
Mike Stump6d9828c2009-07-17 01:31:16 +00001310 // Unlike case statements, we don't add the default block to the successors
1311 // for the switch statement immediately. This is done when we finish
1312 // processing the switch statement. This allows for the default case
1313 // (including a fall-through to the code after the switch statement) to always
1314 // be the last successor of a switch-terminated block.
1315
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001316 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1317 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001318
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001319 // This block is now the implicit successor of other blocks.
1320 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001321
1322 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001323}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001324
Ted Kremenek19bb3562007-08-28 19:26:49 +00001325CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001326 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001327 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001328
Ted Kremenek19bb3562007-08-28 19:26:49 +00001329 if (!IBlock) {
1330 IBlock = createBlock(false);
1331 cfg->setIndirectGotoBlock(IBlock);
1332 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001333
Ted Kremenek19bb3562007-08-28 19:26:49 +00001334 // IndirectGoto is a control-flow statement. Thus we stop processing the
1335 // current block and create a new one.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001336 if (Block) {
1337 if (!FinishBlock(Block))
1338 return 0;
1339 }
Ted Kremenek19bb3562007-08-28 19:26:49 +00001340 Block = createBlock(false);
1341 Block->setTerminator(I);
1342 Block->addSuccessor(IBlock);
1343 return addStmt(I->getTarget());
1344}
1345
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001346
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001347} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001348
Mike Stump6d9828c2009-07-17 01:31:16 +00001349/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1350/// no successors or predecessors. If this is the first block created in the
1351/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001352CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001353 bool first_block = begin() == end();
1354
1355 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001356 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001357
1358 // If this is the first block, set it as the Entry and Exit.
1359 if (first_block) Entry = Exit = &front();
1360
1361 // Return the block.
1362 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001363}
1364
Ted Kremenek026473c2007-08-23 16:51:22 +00001365/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1366/// CFG is returned to the caller.
1367CFG* CFG::buildCFG(Stmt* Statement) {
1368 CFGBuilder Builder;
1369 return Builder.buildCFG(Statement);
1370}
1371
1372/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001373void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1374
Ted Kremenek63f58872007-10-01 19:33:33 +00001375//===----------------------------------------------------------------------===//
1376// CFG: Queries for BlkExprs.
1377//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001378
Ted Kremenek63f58872007-10-01 19:33:33 +00001379namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001380 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001381}
1382
Ted Kremenek411cdee2008-04-16 21:10:48 +00001383static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1384 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001385 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001386
Ted Kremenek411cdee2008-04-16 21:10:48 +00001387 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001388 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001389
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001390 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1391 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001392
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001393 FindSubExprAssignments(*I, Set);
1394 }
1395}
1396
Ted Kremenek63f58872007-10-01 19:33:33 +00001397static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1398 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001399
1400 // Look for assignments that are used as subexpressions. These are the only
1401 // assignments that we want to *possibly* register as a block-level
1402 // expression. Basically, if an assignment occurs both in a subexpression and
1403 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001404 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001405
Ted Kremenek63f58872007-10-01 19:33:33 +00001406 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1407 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001408 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001409
Ted Kremenek411cdee2008-04-16 21:10:48 +00001410 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001411
1412 // Iterate over the statements again on identify the Expr* and Stmt* at the
1413 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001414
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001415 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001416 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001417
Ted Kremenek411cdee2008-04-16 21:10:48 +00001418 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001419 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001420 // expression are really "statements" whose value is never used by
1421 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001422 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001423 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001424 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1425 // Special handling for statement expressions. The last statement in
1426 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001427 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001428 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001429 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001430 (*M)[C->body_back()] = x;
1431 }
1432 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001433
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001434 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001435 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001436 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001437
Ted Kremenek411cdee2008-04-16 21:10:48 +00001438 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001439
Ted Kremenek390e48b2008-11-12 21:11:49 +00001440 Stmt* S = I->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001441
Ted Kremenek390e48b2008-11-12 21:11:49 +00001442 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001443 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001444 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001445 }
1446 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001447
Ted Kremenek63f58872007-10-01 19:33:33 +00001448 return M;
1449}
1450
Ted Kremenek86946742008-01-17 20:48:37 +00001451CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1452 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001453 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001454
Ted Kremenek63f58872007-10-01 19:33:33 +00001455 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001456 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001457
Ted Kremenek63f58872007-10-01 19:33:33 +00001458 if (I == M->end()) return CFG::BlkExprNumTy();
1459 else return CFG::BlkExprNumTy(I->second);
1460}
1461
1462unsigned CFG::getNumBlkExprs() {
1463 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1464 return M->size();
1465 else {
1466 // We assume callers interested in the number of BlkExprs will want
1467 // the map constructed if it doesn't already exist.
1468 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1469 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1470 }
1471}
1472
Ted Kremenek274f4332008-04-28 18:00:46 +00001473//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001474// Cleanup: CFG dstor.
1475//===----------------------------------------------------------------------===//
1476
Ted Kremenek63f58872007-10-01 19:33:33 +00001477CFG::~CFG() {
1478 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1479}
Mike Stump6d9828c2009-07-17 01:31:16 +00001480
Ted Kremenek7dba8602007-08-29 21:56:09 +00001481//===----------------------------------------------------------------------===//
1482// CFG pretty printing
1483//===----------------------------------------------------------------------===//
1484
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001485namespace {
1486
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001487class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001488
Ted Kremenek42a509f2007-08-31 21:30:12 +00001489 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1490 StmtMapTy StmtMap;
1491 signed CurrentBlock;
1492 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001493 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001494public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001495
Chris Lattnere4f21422009-06-30 01:26:17 +00001496 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1497 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001498 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1499 unsigned j = 1;
1500 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1501 BI != BEnd; ++BI, ++j )
1502 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1503 }
1504 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001505
Ted Kremenek42a509f2007-08-31 21:30:12 +00001506 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001507
Chris Lattnere4f21422009-06-30 01:26:17 +00001508 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001509 void setBlockID(signed i) { CurrentBlock = i; }
1510 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001511
Ted Kremeneka95d3752008-09-13 05:16:45 +00001512 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001513
Ted Kremenek411cdee2008-04-16 21:10:48 +00001514 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001515
1516 if (I == StmtMap.end())
1517 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001518
1519 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001520 && I->second.second == CurrentStmt)
1521 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001522
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001523 OS << "[B" << I->second.first << "." << I->second.second << "]";
1524 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001525 }
1526};
Chris Lattnere4f21422009-06-30 01:26:17 +00001527} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001528
Chris Lattnere4f21422009-06-30 01:26:17 +00001529
1530namespace {
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001531class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1532 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001533
Ted Kremeneka95d3752008-09-13 05:16:45 +00001534 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001535 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001536 PrintingPolicy Policy;
1537
Ted Kremenek42a509f2007-08-31 21:30:12 +00001538public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001539 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001540 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001541 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001542
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001543 void VisitIfStmt(IfStmt* I) {
1544 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001545 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001546 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001547
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001548 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001549 void VisitStmt(Stmt* Terminator) {
1550 Terminator->printPretty(OS, Helper, Policy);
1551 }
1552
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001553 void VisitForStmt(ForStmt* F) {
1554 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001555 if (F->getInit()) OS << "...";
1556 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001557 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001558 OS << "; ";
1559 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001560 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001561 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001562
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001563 void VisitWhileStmt(WhileStmt* W) {
1564 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001565 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001566 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001568 void VisitDoStmt(DoStmt* D) {
1569 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001570 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001571 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001572
Ted Kremenek411cdee2008-04-16 21:10:48 +00001573 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001574 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001575 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001576 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001577
Ted Kremenek805e9a82007-08-31 21:49:40 +00001578 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001579 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001580 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001581 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001582
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001583 void VisitChooseExpr(ChooseExpr* C) {
1584 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001585 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001586 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001587 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001588
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001589 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1590 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001591 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001592 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001593
Ted Kremenek805e9a82007-08-31 21:49:40 +00001594 void VisitBinaryOperator(BinaryOperator* B) {
1595 if (!B->isLogicalOp()) {
1596 VisitExpr(B);
1597 return;
1598 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001599
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001600 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001601
Ted Kremenek805e9a82007-08-31 21:49:40 +00001602 switch (B->getOpcode()) {
1603 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001604 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001605 return;
1606 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001607 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001608 return;
1609 default:
1610 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001611 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001612 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001613
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001614 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001615 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001616 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001617};
Chris Lattnere4f21422009-06-30 01:26:17 +00001618} // end anonymous namespace
1619
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
Chris Lattnere4f21422009-06-30 01:26:17 +00001621static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1622 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001623 if (Helper) {
1624 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001625 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001626 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001627
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001628 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001629 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001630 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001631 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001632 return;
1633 }
1634 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001635
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001636 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001637 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001638 if (B->getOpcode() == BinaryOperator::Comma) {
1639 OS << "... , ";
1640 Helper->handledStmt(B->getRHS(),OS);
1641 OS << '\n';
1642 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001643 }
1644 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001645 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001646
Chris Lattnere4f21422009-06-30 01:26:17 +00001647 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001648
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001649 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001650 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001651}
Mike Stump6d9828c2009-07-17 01:31:16 +00001652
Chris Lattnere4f21422009-06-30 01:26:17 +00001653static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1654 const CFGBlock& B,
1655 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001656
Ted Kremenek42a509f2007-08-31 21:30:12 +00001657 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001658
Ted Kremenek7dba8602007-08-29 21:56:09 +00001659 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001660 OS << "\n [ B" << B.getBlockID();
1661
Ted Kremenek42a509f2007-08-31 21:30:12 +00001662 if (&B == &cfg->getEntry())
1663 OS << " (ENTRY) ]\n";
1664 else if (&B == &cfg->getExit())
1665 OS << " (EXIT) ]\n";
1666 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001667 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001668 else
1669 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001670
Ted Kremenek9cffe732007-08-29 23:20:49 +00001671 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001672 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001673
1674 if (print_edges)
1675 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001676
Ted Kremenek411cdee2008-04-16 21:10:48 +00001677 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001678 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001679 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001680 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001681 C->getLHS()->printPretty(OS, Helper,
1682 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001683 if (C->getRHS()) {
1684 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001685 C->getRHS()->printPretty(OS, Helper,
1686 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001687 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001688 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001689 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001690 else
1691 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001692
Ted Kremenek9cffe732007-08-29 23:20:49 +00001693 OS << ":\n";
1694 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001695
Ted Kremenekfddd5182007-08-21 21:42:03 +00001696 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001697 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001698
Ted Kremenek42a509f2007-08-31 21:30:12 +00001699 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1700 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001701
Ted Kremenek9cffe732007-08-29 23:20:49 +00001702 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001703 if (print_edges)
1704 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001705
Ted Kremeneka95d3752008-09-13 05:16:45 +00001706 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001707
Ted Kremenek42a509f2007-08-31 21:30:12 +00001708 if (Helper)
1709 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001710
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001711 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001712 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001713
Ted Kremenek9cffe732007-08-29 23:20:49 +00001714 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001715 if (B.getTerminator()) {
1716 if (print_edges)
1717 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001718
Ted Kremenek9cffe732007-08-29 23:20:49 +00001719 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001720
Ted Kremenek42a509f2007-08-31 21:30:12 +00001721 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001722
Chris Lattnere4f21422009-06-30 01:26:17 +00001723 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1724 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001725 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001726 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001727 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenek9cffe732007-08-29 23:20:49 +00001729 if (print_edges) {
1730 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001731 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001732 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001733
Ted Kremenek42a509f2007-08-31 21:30:12 +00001734 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1735 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001736
Ted Kremenek42a509f2007-08-31 21:30:12 +00001737 if (i == 8 || (i-8) == 0)
1738 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001739
Ted Kremenek9cffe732007-08-29 23:20:49 +00001740 OS << " B" << (*I)->getBlockID();
1741 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001742
Ted Kremenek42a509f2007-08-31 21:30:12 +00001743 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001744
Ted Kremenek42a509f2007-08-31 21:30:12 +00001745 // Print the successors of this block.
1746 OS << " Successors (" << B.succ_size() << "):";
1747 i = 0;
1748
1749 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1750 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001751
Ted Kremenek42a509f2007-08-31 21:30:12 +00001752 if (i == 8 || (i-8) % 10 == 0)
1753 OS << "\n ";
1754
1755 OS << " B" << (*I)->getBlockID();
1756 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001757
Ted Kremenek9cffe732007-08-29 23:20:49 +00001758 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001759 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001760}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001761
Ted Kremenek42a509f2007-08-31 21:30:12 +00001762
1763/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001764void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001765
1766/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001767void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1768 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001769
Ted Kremenek42a509f2007-08-31 21:30:12 +00001770 // Print the entry block.
1771 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001772
Ted Kremenek42a509f2007-08-31 21:30:12 +00001773 // Iterate through the CFGBlocks and print them one by one.
1774 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1775 // Skip the entry block, because we already printed it.
1776 if (&(*I) == &getEntry() || &(*I) == &getExit())
1777 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001778
Ted Kremenek42a509f2007-08-31 21:30:12 +00001779 print_block(OS, this, *I, &Helper, true);
1780 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001781
Ted Kremenek42a509f2007-08-31 21:30:12 +00001782 // Print the exit block.
1783 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001784 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001785}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001786
1787/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001788void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1789 print(llvm::errs(), cfg, LO);
1790}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001791
1792/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1793/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001794void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1795 const LangOptions &LO) const {
1796 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001797 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001798}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001799
Ted Kremeneka2925852008-01-30 23:02:42 +00001800/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001801void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001802 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001803 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001804 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1805}
1806
Ted Kremenek390e48b2008-11-12 21:11:49 +00001807Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001808
Ted Kremenek411cdee2008-04-16 21:10:48 +00001809 if (!Terminator)
1810 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001811
Ted Kremenek411cdee2008-04-16 21:10:48 +00001812 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001813
Ted Kremenek411cdee2008-04-16 21:10:48 +00001814 switch (Terminator->getStmtClass()) {
1815 default:
1816 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001817
Ted Kremenek411cdee2008-04-16 21:10:48 +00001818 case Stmt::ForStmtClass:
1819 E = cast<ForStmt>(Terminator)->getCond();
1820 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001821
Ted Kremenek411cdee2008-04-16 21:10:48 +00001822 case Stmt::WhileStmtClass:
1823 E = cast<WhileStmt>(Terminator)->getCond();
1824 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001825
Ted Kremenek411cdee2008-04-16 21:10:48 +00001826 case Stmt::DoStmtClass:
1827 E = cast<DoStmt>(Terminator)->getCond();
1828 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001829
Ted Kremenek411cdee2008-04-16 21:10:48 +00001830 case Stmt::IfStmtClass:
1831 E = cast<IfStmt>(Terminator)->getCond();
1832 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001833
Ted Kremenek411cdee2008-04-16 21:10:48 +00001834 case Stmt::ChooseExprClass:
1835 E = cast<ChooseExpr>(Terminator)->getCond();
1836 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001837
Ted Kremenek411cdee2008-04-16 21:10:48 +00001838 case Stmt::IndirectGotoStmtClass:
1839 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1840 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001841
Ted Kremenek411cdee2008-04-16 21:10:48 +00001842 case Stmt::SwitchStmtClass:
1843 E = cast<SwitchStmt>(Terminator)->getCond();
1844 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001845
Ted Kremenek411cdee2008-04-16 21:10:48 +00001846 case Stmt::ConditionalOperatorClass:
1847 E = cast<ConditionalOperator>(Terminator)->getCond();
1848 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001849
Ted Kremenek411cdee2008-04-16 21:10:48 +00001850 case Stmt::BinaryOperatorClass: // '&&' and '||'
1851 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001852 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001853
Ted Kremenek390e48b2008-11-12 21:11:49 +00001854 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001855 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001856 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
Ted Kremenek411cdee2008-04-16 21:10:48 +00001858 return E ? E->IgnoreParens() : NULL;
1859}
1860
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001861bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00001862
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001863 if (!Terminator)
1864 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001865
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001866 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001867
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001868 switch (Terminator->getStmtClass()) {
1869 default:
1870 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001871
1872 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001873 case Stmt::WhileStmtClass:
1874 case Stmt::DoStmtClass:
1875 case Stmt::IfStmtClass:
1876 case Stmt::ChooseExprClass:
1877 case Stmt::ConditionalOperatorClass:
1878 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001879 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001880 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001882 return E ? E->IgnoreParens() : NULL;
1883}
1884
Ted Kremeneka2925852008-01-30 23:02:42 +00001885
Ted Kremenek7dba8602007-08-29 21:56:09 +00001886//===----------------------------------------------------------------------===//
1887// CFG Graphviz Visualization
1888//===----------------------------------------------------------------------===//
1889
Ted Kremenek42a509f2007-08-31 21:30:12 +00001890
1891#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00001892static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001893#endif
1894
Chris Lattnere4f21422009-06-30 01:26:17 +00001895void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001896#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00001897 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001898 GraphHelper = &H;
1899 llvm::ViewGraph(this,"CFG");
1900 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001901#endif
1902}
1903
Ted Kremenek7dba8602007-08-29 21:56:09 +00001904namespace llvm {
1905template<>
1906struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson02995ce2009-06-24 17:37:55 +00001907 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
1908 bool ShortNames) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00001909
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001910#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001911 std::string OutSStr;
1912 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001913 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001914 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001915
1916 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1917
1918 // Process string output to make it nicer...
1919 for (unsigned i = 0; i != OutStr.length(); ++i)
1920 if (OutStr[i] == '\n') { // Left justify
1921 OutStr[i] = '\\';
1922 OutStr.insert(OutStr.begin()+i+1, 'l');
1923 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001924
Ted Kremenek7dba8602007-08-29 21:56:09 +00001925 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001926#else
1927 return "";
1928#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001929 }
1930};
1931} // end namespace llvm