blob: 02fe22c00fea84925f22afd577e9c5fc7e2b5ef0 [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);
Zhongxing Xua0042542009-07-17 07:29:51 +0000478 if (FunctionDecl *FD = C->getDirectCallee())
479 if (FD->hasAttr<NoReturnAttr>())
480 NoReturn = true;
Mike Stumpcd7bf232009-07-17 01:04:31 +0000481
482 if (!NoReturn)
483 break;
484
485 if (Block) {
486 if (!FinishBlock(Block))
487 return 0;
488 }
489
490 // Create new block with no successor for the remaining pieces.
491 Block = createBlock(false);
492 Block->appendStmt(Terminator);
493
494 // Wire this to the exit block directly.
495 Block->addSuccessor(&cfg->getExit());
496
497 return WalkAST_VisitChildren(Terminator);
498 }
499
Mike Stump6d9828c2009-07-17 01:31:16 +0000500 default:
501 break;
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000502 };
Mike Stump6d9828c2009-07-17 01:31:16 +0000503
Ted Kremenek411cdee2008-04-16 21:10:48 +0000504 if (AlwaysAddStmt) Block->appendStmt(Terminator);
505 return WalkAST_VisitChildren(Terminator);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000506}
Mike Stump6d9828c2009-07-17 01:31:16 +0000507
508/// WalkAST_VisitDeclSubExpr - Utility method to add block-level expressions for
509/// initializers in Decls.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000510CFGBlock* CFGBuilder::WalkAST_VisitDeclSubExpr(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000511 VarDecl* VD = dyn_cast<VarDecl>(D);
512
513 if (!VD)
Ted Kremenekd6603222007-11-18 20:06:01 +0000514 return Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000515
Ted Kremenekc7eb9032008-08-06 23:20:50 +0000516 Expr* Init = VD->getInit();
Mike Stump6d9828c2009-07-17 01:31:16 +0000517
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000518 if (Init) {
Mike Stump54cc43f2009-02-26 08:00:25 +0000519 // Optimization: Don't create separate block-level statements for literals.
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000520 switch (Init->getStmtClass()) {
521 case Stmt::IntegerLiteralClass:
522 case Stmt::CharacterLiteralClass:
523 case Stmt::StringLiteralClass:
524 break;
525 default:
526 Block = addStmt(Init);
527 }
Ted Kremenekae2a98c2008-02-29 22:32:24 +0000528 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000529
Ted Kremenekfcd06f72008-09-26 16:26:36 +0000530 // If the type of VD is a VLA, then we must process its size expressions.
531 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
532 VA = FindVA(VA->getElementType().getTypePtr()))
Mike Stump6d9828c2009-07-17 01:31:16 +0000533 Block = addStmt(VA->getSizeExpr());
534
Ted Kremenekb49e1aa2007-08-28 18:14:37 +0000535 return Block;
536}
537
Mike Stump6d9828c2009-07-17 01:31:16 +0000538/// WalkAST_VisitChildren - Utility method to call WalkAST on the children of a
539/// Stmt.
Ted Kremenek411cdee2008-04-16 21:10:48 +0000540CFGBlock* CFGBuilder::WalkAST_VisitChildren(Stmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000541 CFGBlock* B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000542 for (Stmt::child_iterator I = Terminator->child_begin(),
543 E = Terminator->child_end();
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000544 I != E; ++I)
Ted Kremenek322f58d2007-09-26 21:23:31 +0000545 if (*I) B = WalkAST(*I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000546
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000547 return B;
548}
549
Ted Kremenek15c27a82007-08-28 18:30:10 +0000550/// WalkAST_VisitStmtExpr - Utility method to handle (nested) statement
551/// expressions (a GCC extension).
Ted Kremenek411cdee2008-04-16 21:10:48 +0000552CFGBlock* CFGBuilder::WalkAST_VisitStmtExpr(StmtExpr* Terminator) {
553 Block->appendStmt(Terminator);
Mike Stump6d9828c2009-07-17 01:31:16 +0000554 return VisitCompoundStmt(Terminator->getSubStmt());
Ted Kremenek15c27a82007-08-28 18:30:10 +0000555}
556
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000557/// VisitStmt - Handle statements with no branching control flow.
558CFGBlock* CFGBuilder::VisitStmt(Stmt* Statement) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000559 // We cannot assume that we are in the middle of a basic block, since the CFG
560 // might only be constructed for this single statement. If we have no current
561 // basic block, just create one lazily.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000562 if (!Block) Block = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000563
564 // Simply add the statement to the current block. We actually insert
565 // statements in reverse order; this order is reversed later when processing
566 // the containing element in the AST.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000567 addStmt(Statement);
568
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000569 return Block;
570}
571
572CFGBlock* CFGBuilder::VisitNullStmt(NullStmt* Statement) {
573 return Block;
574}
575
576CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000577
Ted Kremeneked47fc62009-07-03 00:10:50 +0000578 CFGBlock* LastBlock = Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000579
Ted Kremenekd34066c2008-02-26 00:22:58 +0000580 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
581 I != E; ++I ) {
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000582 LastBlock = Visit(*I);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000583 }
584
Ted Kremeneka716d7a2008-03-17 17:19:44 +0000585 return LastBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000586}
587
588CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000589 // We may see an if statement in the middle of a basic block, or it may be the
590 // first statement we are processing. In either case, we create a new basic
591 // block. First, we create the blocks for the then...else statements, and
592 // then we create the block containing the if statement. If we were in the
593 // middle of a block, we stop processing that block and reverse its
594 // statements. That block is then the implicit successor for the "then" and
595 // "else" clauses.
596
597 // The block we were proccessing is now finished. Make it the successor
598 // block.
599 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000600 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000601 if (!FinishBlock(Block))
602 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000603 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000604
605 // Process the false branch. NULL out Block so that the recursive call to
606 // Visit will create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000607 // Null out Block so that all successor
608 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000609
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000610 if (Stmt* Else = I->getElse()) {
611 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000612
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000613 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000614 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000615 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000616 ElseBlock = Visit(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000617
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000618 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
619 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000620 else if (Block) {
621 if (!FinishBlock(ElseBlock))
622 return 0;
623 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000624 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000625
626 // Process the true branch. NULL out Block so that the recursive call to
627 // Visit will create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000628 // Null out Block so that all successor
629 CFGBlock* ThenBlock;
630 {
631 Stmt* Then = I->getThen();
632 assert (Then);
633 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000634 Block = NULL;
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000635 ThenBlock = Visit(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000636
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000637 if (!ThenBlock) {
638 // We can reach here if the "then" body has all NullStmts.
639 // Create an empty block so we can distinguish between true and false
640 // branches in path-sensitive analyses.
641 ThenBlock = createBlock(false);
642 ThenBlock->addSuccessor(sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000643 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000644 if (!FinishBlock(ThenBlock))
645 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000646 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000647 }
648
Mike Stump6d9828c2009-07-17 01:31:16 +0000649 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000650 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000651
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000652 // Set the terminator of the new block to the If statement.
653 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000654
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000655 // Now add the successors.
656 Block->addSuccessor(ThenBlock);
657 Block->addSuccessor(ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000658
659 // Add the condition as the last statement in the new block. This may create
660 // new blocks as the condition may contain control-flow. Any newly created
661 // blocks will be pointed to be "Block".
Ted Kremeneka2925852008-01-30 23:02:42 +0000662 return addStmt(I->getCond()->IgnoreParens());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000663}
Mike Stump6d9828c2009-07-17 01:31:16 +0000664
665
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000666CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000667 // If we were in the middle of a block we stop processing that block and
668 // reverse its statements.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000669 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000670 // NOTE: If a "return" appears in the middle of a block, this means that the
671 // code afterwards is DEAD (unreachable). We still keep a basic block
672 // for that code; a simple "mark-and-sweep" from the entry block will be
673 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000674 if (Block) FinishBlock(Block);
675
676 // Create the new block.
677 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000678
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000679 // The Exit block is the only successor.
680 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000681
682 // Add the return statement to the block. This may create new blocks if R
683 // contains control-flow (short-circuit operations).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000684 return addStmt(R);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000685}
686
687CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
688 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek2677ea82008-03-15 07:45:02 +0000689 Visit(L->getSubStmt());
690 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000691
Ted Kremenek16e4dc82007-08-30 18:20:57 +0000692 if (!LabelBlock) // This can happen when the body is empty, i.e.
693 LabelBlock=createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000694
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000695 assert (LabelMap.find(L) == LabelMap.end() && "label already in map");
696 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000697
698 // Labels partition blocks, so this is the end of the basic block we were
699 // processing (L is the block's label). Because this is label (and we have
700 // already processed the substatement) there is no extra control-flow to worry
701 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000702 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000703 if (!FinishBlock(LabelBlock))
704 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000705
706 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000707 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000708
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000709 // This block is now the implicit successor of other blocks.
710 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000711
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000712 return LabelBlock;
713}
714
715CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000716 // Goto is a control-flow statement. Thus we stop processing the current
717 // block and create a new one.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000718 if (Block) FinishBlock(Block);
719 Block = createBlock(false);
720 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000721
722 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000723 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000724
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000725 if (I == LabelMap.end())
726 // We will need to backpatch this block later.
727 BackpatchBlocks.push_back(Block);
728 else
729 Block->addSuccessor(I->second);
730
Mike Stump6d9828c2009-07-17 01:31:16 +0000731 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000732}
733
734CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000735 // "for" is a control-flow statement. Thus we stop processing the current
736 // block.
737
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000738 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000739
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000740 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000741 if (!FinishBlock(Block))
742 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000743 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000744 } else LoopSuccessor = Succ;
745
746 // Because of short-circuit evaluation, the condition of the loop can span
747 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
748 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000749 CFGBlock* ExitConditionBlock = createBlock(false);
750 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000751
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000752 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000753 ExitConditionBlock->setTerminator(F);
754
755 // Now add the actual condition to the condition block. Because the condition
756 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000757 if (Stmt* C = F->getCond()) {
758 Block = ExitConditionBlock;
759 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000760 if (Block) {
761 if (!FinishBlock(EntryConditionBlock))
762 return 0;
763 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000764 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000765
Mike Stump6d9828c2009-07-17 01:31:16 +0000766 // The condition block is the implicit successor for the loop body as well as
767 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000768 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000769
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000770 // Now create the loop body.
771 {
772 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000773
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000774 // Save the current values for Block, Succ, and continue and break targets
775 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
776 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000777 save_break(BreakTargetBlock);
778
Ted Kremenekaf603f72007-08-30 18:39:40 +0000779 // Create a new block to contain the (bottom) of the loop body.
780 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000781
Ted Kremeneke9334502008-09-04 21:48:47 +0000782 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000783 // Generate increment code in its own basic block. This is the target of
784 // continue statements.
Ted Kremenekd0172432008-11-24 20:50:24 +0000785 Succ = Visit(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000786 } else {
787 // No increment code. Create a special, empty, block that is used as the
788 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000789 assert(Succ == EntryConditionBlock);
790 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000791 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000792
Ted Kremenek3575f842009-04-28 00:51:56 +0000793 // Finish up the increment (or empty) block if it hasn't been already.
794 if (Block) {
795 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000796 if (!FinishBlock(Block))
797 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000798 Block = 0;
799 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000800
Ted Kremenek3575f842009-04-28 00:51:56 +0000801 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000802
Ted Kremenek3575f842009-04-28 00:51:56 +0000803 // The starting block for the loop increment is the block that should
804 // represent the 'loop target' for looping back to the start of the loop.
805 ContinueTargetBlock->setLoopTarget(F);
806
Ted Kremeneke9334502008-09-04 21:48:47 +0000807 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000808 BreakTargetBlock = LoopSuccessor;
809
810 // Now populate the body block, and in the process create new blocks as we
811 // walk the body of the loop.
812 CFGBlock* BodyBlock = Visit(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000813
814 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000815 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000816 else if (Block) {
817 if (!FinishBlock(BodyBlock))
818 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000819 }
820
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000821 // This new body block is a successor to our "exit" condition block.
822 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000823 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000824
825 // Link up the condition block with the code that follows the loop. (the
826 // false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000827 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +0000828
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000829 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000830 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000831 if (Stmt* I = F->getInit()) {
832 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000833 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000834 } else {
835 // There is no loop initialization. We are thus basically a while loop.
836 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000837 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000838 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000839 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000840 }
841}
842
Ted Kremenek514de5a2008-11-11 17:10:00 +0000843CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
844 // Objective-C fast enumeration 'for' statements:
845 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
846 //
847 // for ( Type newVariable in collection_expression ) { statements }
848 //
849 // becomes:
850 //
851 // prologue:
852 // 1. collection_expression
853 // T. jump to loop_entry
854 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000855 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000856 // 1. ObjCForCollectionStmt [performs binding to newVariable]
857 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
858 // TB:
859 // statements
860 // T. jump to loop_entry
861 // FB:
862 // what comes after
863 //
864 // and
865 //
866 // Type existingItem;
867 // for ( existingItem in expression ) { statements }
868 //
869 // becomes:
870 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000871 // the same with newVariable replaced with existingItem; the binding works
872 // the same except that for one ObjCForCollectionStmt::getElement() returns
873 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +0000874 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000875
Ted Kremenek514de5a2008-11-11 17:10:00 +0000876 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000877
Ted Kremenek514de5a2008-11-11 17:10:00 +0000878 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000879 if (!FinishBlock(Block))
880 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000881 LoopSuccessor = Block;
882 Block = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000883 } else LoopSuccessor = Succ;
884
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000885 // Build the condition blocks.
886 CFGBlock* ExitConditionBlock = createBlock(false);
887 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000888
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000889 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000890 ExitConditionBlock->setTerminator(S);
891
892 // The last statement in the block should be the ObjCForCollectionStmt, which
893 // performs the actual binding to 'element' and determines if there are any
894 // more items in the collection.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000895 ExitConditionBlock->appendStmt(S);
896 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000897
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000898 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +0000899 // generate new blocks as necesary. We DON'T add the statement by default to
900 // the CFG unless it contains control-flow.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000901 EntryConditionBlock = WalkAST(S->getElement(), false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000902 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000903 if (!FinishBlock(EntryConditionBlock))
904 return 0;
905 Block = 0;
906 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000907
908 // The condition block is the implicit successor for the loop body as well as
909 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000910 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000911
Ted Kremenek514de5a2008-11-11 17:10:00 +0000912 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +0000913 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000914 // Save the current values for Succ, continue and break targets.
915 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +0000916 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
917
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000918 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +0000919 ContinueTargetBlock = EntryConditionBlock;
920
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000921 CFGBlock* BodyBlock = Visit(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000922
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000923 if (!BodyBlock)
924 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000925 else if (Block) {
926 if (!FinishBlock(BodyBlock))
927 return 0;
928 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000929
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000930 // This new body block is a successor to our "exit" condition block.
931 ExitConditionBlock->addSuccessor(BodyBlock);
932 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000933
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000934 // Link up the condition block with the code that follows the loop.
935 // (the false branch).
936 ExitConditionBlock->addSuccessor(LoopSuccessor);
937
Ted Kremenek514de5a2008-11-11 17:10:00 +0000938 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000939 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +0000940 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +0000941}
942
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000943CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
944 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +0000945
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000946 // Inline the body.
Ted Kremenekda5348e2009-05-05 23:11:51 +0000947 CFGBlock *SyncBlock = Visit(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000948
Ted Kremenekda5348e2009-05-05 23:11:51 +0000949 // The sync body starts its own basic block. This makes it a little easier
950 // for diagnostic clients.
951 if (SyncBlock) {
952 if (!FinishBlock(SyncBlock))
953 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000954
Ted Kremenekda5348e2009-05-05 23:11:51 +0000955 Block = 0;
956 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000957
Ted Kremenekda5348e2009-05-05 23:11:51 +0000958 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000959
Ted Kremenekb3b0b362009-05-02 01:49:13 +0000960 // Inline the sync expression.
961 return Visit(S->getSynchExpr());
962}
Mike Stump6d9828c2009-07-17 01:31:16 +0000963
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000964CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek90658ec2009-04-07 04:26:02 +0000965 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +0000966}
Ted Kremenek514de5a2008-11-11 17:10:00 +0000967
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000968CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000969 // "while" is a control-flow statement. Thus we stop processing the current
970 // block.
971
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000972 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000973
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000974 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000975 if (!FinishBlock(Block))
976 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000977 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000978 } else LoopSuccessor = Succ;
979
980 // Because of short-circuit evaluation, the condition of the loop can span
981 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
982 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000983 CFGBlock* ExitConditionBlock = createBlock(false);
984 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000985
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000986 // Set the terminator for the "exit" condition block.
987 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +0000988
989 // Now add the actual condition to the condition block. Because the condition
990 // itself may contain control-flow, new blocks may be created. Thus we update
991 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000992 if (Stmt* C = W->getCond()) {
993 Block = ExitConditionBlock;
994 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +0000995 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000996 if (Block) {
997 if (!FinishBlock(EntryConditionBlock))
998 return 0;
999 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001000 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001001
1002 // The condition block is the implicit successor for the loop body as well as
1003 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001004 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001005
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001006 // Process the loop body.
1007 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001008 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001009
1010 // Save the current values for Block, Succ, and continue and break targets
1011 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1012 save_continue(ContinueTargetBlock),
1013 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001014
Mike Stump6d9828c2009-07-17 01:31:16 +00001015 // Create an empty block to represent the transition block for looping back
1016 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001017 Block = 0;
1018 assert(Succ == EntryConditionBlock);
1019 Succ = createBlock();
1020 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001021 ContinueTargetBlock = Succ;
1022
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001023 // All breaks should go to the code following the loop.
1024 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001025
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001026 // NULL out Block to force lazy instantiation of blocks for the body.
1027 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001028
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001029 // Create the body. The returned block is the entry to the loop body.
1030 CFGBlock* BodyBlock = Visit(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001031
Ted Kremenekaf603f72007-08-30 18:39:40 +00001032 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001033 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001034 else if (Block) {
1035 if (!FinishBlock(BodyBlock))
1036 return 0;
1037 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001038
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001039 // Add the loop body entry as a successor to the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001040 ExitConditionBlock->addSuccessor(BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001041 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001042
1043 // Link up the condition block with the code that follows the loop. (the
1044 // false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001045 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001046
1047 // There can be no more statements in the condition block since we loop back
1048 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001049 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001050
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001051 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001052 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001053 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001054}
Mike Stump6d9828c2009-07-17 01:31:16 +00001055
Ted Kremenek2fda5042008-12-09 20:20:09 +00001056CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1057 // FIXME: This isn't complete. We basically treat @throw like a return
1058 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001059
1060 // If we were in the middle of a block we stop processing that block and
1061 // reverse its statements.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001062 if (Block) {
1063 if (!FinishBlock(Block))
1064 return 0;
1065 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001066
Ted Kremenek2fda5042008-12-09 20:20:09 +00001067 // Create the new block.
1068 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001069
Ted Kremenek2fda5042008-12-09 20:20:09 +00001070 // The Exit block is the only successor.
1071 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001072
1073 // Add the statement to the block. This may create new blocks if S contains
1074 // control-flow (short-circuit operations).
Ted Kremenek2fda5042008-12-09 20:20:09 +00001075 return addStmt(S);
1076}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001077
1078CFGBlock* CFGBuilder::VisitDoStmt(DoStmt* D) {
1079 // "do...while" is a control-flow statement. Thus we stop processing the
1080 // current block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001081
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001082 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001083
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001084 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001085 if (!FinishBlock(Block))
1086 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001087 LoopSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001088 } else LoopSuccessor = Succ;
1089
1090 // Because of short-circuit evaluation, the condition of the loop can span
1091 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1092 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001093 CFGBlock* ExitConditionBlock = createBlock(false);
1094 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001095
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001096 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001097 ExitConditionBlock->setTerminator(D);
1098
1099 // Now add the actual condition to the condition block. Because the condition
1100 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001101 if (Stmt* C = D->getCond()) {
1102 Block = ExitConditionBlock;
1103 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001104 if (Block) {
1105 if (!FinishBlock(EntryConditionBlock))
1106 return 0;
1107 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001108 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001109
Ted Kremenek54827132008-02-27 07:20:00 +00001110 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001111 Succ = EntryConditionBlock;
1112
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001113 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001114 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001115 {
1116 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001117
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001118 // Save the current values for Block, Succ, and continue and break targets
1119 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1120 save_continue(ContinueTargetBlock),
1121 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001122
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001123 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001124 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001125
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001126 // All breaks should go to the code following the loop.
1127 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001128
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001129 // NULL out Block to force lazy instantiation of blocks for the body.
1130 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001131
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001132 // Create the body. The returned block is the entry to the loop body.
1133 BodyBlock = Visit(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001134
Ted Kremenekaf603f72007-08-30 18:39:40 +00001135 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001136 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001137 else if (Block) {
1138 if (!FinishBlock(BodyBlock))
1139 return 0;
1140 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001141
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001142 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001143 // ExitConditionBlock to represent the "loop back" transition. Create an
1144 // empty block to represent the transition block for looping back to the
1145 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001146 // FIXME: Can we do this more efficiently without adding another block?
1147 Block = NULL;
1148 Succ = BodyBlock;
1149 CFGBlock *LoopBackBlock = createBlock();
1150 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001151
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001152 // Add the loop body entry as a successor to the condition.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001153 ExitConditionBlock->addSuccessor(LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001154 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001155
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001156 // Link up the condition block with the code that follows the loop.
1157 // (the false branch).
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001158 ExitConditionBlock->addSuccessor(LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001159
1160 // There can be no more statements in the body block(s) since we loop back to
1161 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001162 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001163
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001164 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001165 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001166 return BodyBlock;
1167}
1168
1169CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1170 // "continue" is a control-flow statement. Thus we stop processing the
1171 // current block.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001172 if (Block) {
1173 if (!FinishBlock(Block))
1174 return 0;
1175 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001176
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001177 // Now create a new block that ends with the continue statement.
1178 Block = createBlock(false);
1179 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001180
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001181 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001182 // incomplete AST. This means the CFG cannot be constructed.
1183 if (ContinueTargetBlock)
1184 Block->addSuccessor(ContinueTargetBlock);
1185 else
1186 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001187
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001188 return Block;
1189}
1190
1191CFGBlock* CFGBuilder::VisitBreakStmt(BreakStmt* B) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001192 // "break" is a control-flow statement. Thus we stop processing the current
1193 // block.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001194 if (Block) {
1195 if (!FinishBlock(Block))
1196 return 0;
1197 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001198
Mike Stumpcd7bf232009-07-17 01:04:31 +00001199 // Now create a new block that ends with the break statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001200 Block = createBlock(false);
1201 Block->setTerminator(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001202
1203 // If there is no target for the break, then we are looking at an incomplete
1204 // AST. This means that the CFG cannot be constructed.
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001205 if (BreakTargetBlock)
1206 Block->addSuccessor(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001207 else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001208 badCFG = true;
1209
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001210
Mike Stump6d9828c2009-07-17 01:31:16 +00001211 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001212}
1213
Ted Kremenek411cdee2008-04-16 21:10:48 +00001214CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001215 // "switch" is a control-flow statement. Thus we stop processing the current
1216 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001217 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001218
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001219 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001220 if (!FinishBlock(Block))
1221 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001222 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001223 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001224
1225 // Save the current "switch" context.
1226 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001227 save_break(BreakTargetBlock),
1228 save_default(DefaultCaseBlock);
1229
Mike Stump6d9828c2009-07-17 01:31:16 +00001230 // Set the "default" case to be the block after the switch statement. If the
1231 // switch statement contains a "default:", this value will be overwritten with
1232 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001233 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001234
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001235 // Create a new block that will contain the switch statement.
1236 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001237
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001238 // Now process the switch body. The code after the switch is the implicit
1239 // successor.
1240 Succ = SwitchSuccessor;
1241 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001242
1243 // When visiting the body, the case statements should automatically get linked
1244 // up to the switch. We also don't keep a pointer to the body, since all
1245 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001246 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001247 Block = NULL;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001248 CFGBlock *BodyBlock = Visit(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001249 if (Block) {
1250 if (!FinishBlock(BodyBlock))
1251 return 0;
1252 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001253
Mike Stump6d9828c2009-07-17 01:31:16 +00001254 // If we have no "default:" case, the default transition is to the code
1255 // following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001256 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001257
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001258 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001259 SwitchTerminatedBlock->setTerminator(Terminator);
1260 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001261 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001262
Ted Kremenek411cdee2008-04-16 21:10:48 +00001263 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001264}
1265
Ted Kremenek411cdee2008-04-16 21:10:48 +00001266CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001267 // CaseStmts are essentially labels, so they are the first statement in a
1268 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001269
Ted Kremenek411cdee2008-04-16 21:10:48 +00001270 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001271 CFGBlock* CaseBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001272 if (!CaseBlock) CaseBlock = createBlock();
1273
1274 // Cases statements partition blocks, so this is the top of the basic block we
1275 // were processing (the "case XXX:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001276 CaseBlock->setLabel(Terminator);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001277 if (!FinishBlock(CaseBlock))
1278 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001279
1280 // Add this block to the list of successors for the block with the switch
1281 // statement.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001282 assert (SwitchTerminatedBlock);
1283 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001284
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001285 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1286 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001287
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001288 // This block is now the implicit successor of other blocks.
1289 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001290
Ted Kremenek2677ea82008-03-15 07:45:02 +00001291 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001292}
Mike Stump6d9828c2009-07-17 01:31:16 +00001293
Ted Kremenek411cdee2008-04-16 21:10:48 +00001294CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
1295 if (Terminator->getSubStmt()) Visit(Terminator->getSubStmt());
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001296 DefaultCaseBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001297 if (!DefaultCaseBlock) DefaultCaseBlock = createBlock();
1298
1299 // Default statements partition blocks, so this is the top of the basic block
1300 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001301 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001302 if (!FinishBlock(DefaultCaseBlock))
1303 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001304
Mike Stump6d9828c2009-07-17 01:31:16 +00001305 // Unlike case statements, we don't add the default block to the successors
1306 // for the switch statement immediately. This is done when we finish
1307 // processing the switch statement. This allows for the default case
1308 // (including a fall-through to the code after the switch statement) to always
1309 // be the last successor of a switch-terminated block.
1310
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001311 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1312 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001313
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001314 // This block is now the implicit successor of other blocks.
1315 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001316
1317 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001318}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001319
Ted Kremenek19bb3562007-08-28 19:26:49 +00001320CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001321 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001322 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001323
Ted Kremenek19bb3562007-08-28 19:26:49 +00001324 if (!IBlock) {
1325 IBlock = createBlock(false);
1326 cfg->setIndirectGotoBlock(IBlock);
1327 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001328
Ted Kremenek19bb3562007-08-28 19:26:49 +00001329 // IndirectGoto is a control-flow statement. Thus we stop processing the
1330 // current block and create a new one.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001331 if (Block) {
1332 if (!FinishBlock(Block))
1333 return 0;
1334 }
Ted Kremenek19bb3562007-08-28 19:26:49 +00001335 Block = createBlock(false);
1336 Block->setTerminator(I);
1337 Block->addSuccessor(IBlock);
1338 return addStmt(I->getTarget());
1339}
1340
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001341
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001342} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001343
Mike Stump6d9828c2009-07-17 01:31:16 +00001344/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1345/// no successors or predecessors. If this is the first block created in the
1346/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001347CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001348 bool first_block = begin() == end();
1349
1350 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001351 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001352
1353 // If this is the first block, set it as the Entry and Exit.
1354 if (first_block) Entry = Exit = &front();
1355
1356 // Return the block.
1357 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001358}
1359
Ted Kremenek026473c2007-08-23 16:51:22 +00001360/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1361/// CFG is returned to the caller.
1362CFG* CFG::buildCFG(Stmt* Statement) {
1363 CFGBuilder Builder;
1364 return Builder.buildCFG(Statement);
1365}
1366
1367/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001368void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1369
Ted Kremenek63f58872007-10-01 19:33:33 +00001370//===----------------------------------------------------------------------===//
1371// CFG: Queries for BlkExprs.
1372//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001373
Ted Kremenek63f58872007-10-01 19:33:33 +00001374namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001375 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001376}
1377
Ted Kremenek411cdee2008-04-16 21:10:48 +00001378static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1379 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001380 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001381
Ted Kremenek411cdee2008-04-16 21:10:48 +00001382 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001383 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001384
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001385 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1386 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001387
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001388 FindSubExprAssignments(*I, Set);
1389 }
1390}
1391
Ted Kremenek63f58872007-10-01 19:33:33 +00001392static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1393 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001394
1395 // Look for assignments that are used as subexpressions. These are the only
1396 // assignments that we want to *possibly* register as a block-level
1397 // expression. Basically, if an assignment occurs both in a subexpression and
1398 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001399 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001400
Ted Kremenek63f58872007-10-01 19:33:33 +00001401 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1402 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001403 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001404
Ted Kremenek411cdee2008-04-16 21:10:48 +00001405 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001406
1407 // Iterate over the statements again on identify the Expr* and Stmt* at the
1408 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001409
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001410 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001411 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001412
Ted Kremenek411cdee2008-04-16 21:10:48 +00001413 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001414 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001415 // expression are really "statements" whose value is never used by
1416 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001417 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001418 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001419 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1420 // Special handling for statement expressions. The last statement in
1421 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001422 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001423 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001424 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001425 (*M)[C->body_back()] = x;
1426 }
1427 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001428
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001429 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001430 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001431 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenek411cdee2008-04-16 21:10:48 +00001433 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001434
Ted Kremenek390e48b2008-11-12 21:11:49 +00001435 Stmt* S = I->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001436
Ted Kremenek390e48b2008-11-12 21:11:49 +00001437 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001438 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001439 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001440 }
1441 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001442
Ted Kremenek63f58872007-10-01 19:33:33 +00001443 return M;
1444}
1445
Ted Kremenek86946742008-01-17 20:48:37 +00001446CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1447 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001448 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001449
Ted Kremenek63f58872007-10-01 19:33:33 +00001450 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001451 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
Ted Kremenek63f58872007-10-01 19:33:33 +00001453 if (I == M->end()) return CFG::BlkExprNumTy();
1454 else return CFG::BlkExprNumTy(I->second);
1455}
1456
1457unsigned CFG::getNumBlkExprs() {
1458 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1459 return M->size();
1460 else {
1461 // We assume callers interested in the number of BlkExprs will want
1462 // the map constructed if it doesn't already exist.
1463 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1464 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1465 }
1466}
1467
Ted Kremenek274f4332008-04-28 18:00:46 +00001468//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001469// Cleanup: CFG dstor.
1470//===----------------------------------------------------------------------===//
1471
Ted Kremenek63f58872007-10-01 19:33:33 +00001472CFG::~CFG() {
1473 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1474}
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
Ted Kremenek7dba8602007-08-29 21:56:09 +00001476//===----------------------------------------------------------------------===//
1477// CFG pretty printing
1478//===----------------------------------------------------------------------===//
1479
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001480namespace {
1481
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001482class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001483
Ted Kremenek42a509f2007-08-31 21:30:12 +00001484 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1485 StmtMapTy StmtMap;
1486 signed CurrentBlock;
1487 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001488 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001489public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001490
Chris Lattnere4f21422009-06-30 01:26:17 +00001491 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1492 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001493 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1494 unsigned j = 1;
1495 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1496 BI != BEnd; ++BI, ++j )
1497 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1498 }
1499 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001500
Ted Kremenek42a509f2007-08-31 21:30:12 +00001501 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001502
Chris Lattnere4f21422009-06-30 01:26:17 +00001503 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001504 void setBlockID(signed i) { CurrentBlock = i; }
1505 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001506
Ted Kremeneka95d3752008-09-13 05:16:45 +00001507 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001508
Ted Kremenek411cdee2008-04-16 21:10:48 +00001509 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001510
1511 if (I == StmtMap.end())
1512 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001513
1514 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001515 && I->second.second == CurrentStmt)
1516 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001517
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001518 OS << "[B" << I->second.first << "." << I->second.second << "]";
1519 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001520 }
1521};
Chris Lattnere4f21422009-06-30 01:26:17 +00001522} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001523
Chris Lattnere4f21422009-06-30 01:26:17 +00001524
1525namespace {
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001526class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1527 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001528
Ted Kremeneka95d3752008-09-13 05:16:45 +00001529 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001530 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001531 PrintingPolicy Policy;
1532
Ted Kremenek42a509f2007-08-31 21:30:12 +00001533public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001534 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001535 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001536 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001537
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001538 void VisitIfStmt(IfStmt* I) {
1539 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001540 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001541 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001542
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001543 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001544 void VisitStmt(Stmt* Terminator) {
1545 Terminator->printPretty(OS, Helper, Policy);
1546 }
1547
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001548 void VisitForStmt(ForStmt* F) {
1549 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001550 if (F->getInit()) OS << "...";
1551 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001552 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001553 OS << "; ";
1554 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001555 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001556 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001557
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001558 void VisitWhileStmt(WhileStmt* W) {
1559 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001560 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
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 VisitDoStmt(DoStmt* D) {
1564 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001565 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001566 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
Ted Kremenek411cdee2008-04-16 21:10:48 +00001568 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001569 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001570 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001571 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001572
Ted Kremenek805e9a82007-08-31 21:49:40 +00001573 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001574 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001575 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001576 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001577
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001578 void VisitChooseExpr(ChooseExpr* C) {
1579 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001580 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001581 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001582 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001583
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001584 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1585 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001586 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001587 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001588
Ted Kremenek805e9a82007-08-31 21:49:40 +00001589 void VisitBinaryOperator(BinaryOperator* B) {
1590 if (!B->isLogicalOp()) {
1591 VisitExpr(B);
1592 return;
1593 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001594
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001595 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001596
Ted Kremenek805e9a82007-08-31 21:49:40 +00001597 switch (B->getOpcode()) {
1598 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001599 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001600 return;
1601 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001602 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001603 return;
1604 default:
1605 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001606 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001607 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001608
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001609 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001610 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001611 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001612};
Chris Lattnere4f21422009-06-30 01:26:17 +00001613} // end anonymous namespace
1614
Mike Stump6d9828c2009-07-17 01:31:16 +00001615
Chris Lattnere4f21422009-06-30 01:26:17 +00001616static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1617 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001618 if (Helper) {
1619 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001620 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001621 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001622
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001623 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001624 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001625 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001626 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001627 return;
1628 }
1629 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001630
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001631 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001632 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001633 if (B->getOpcode() == BinaryOperator::Comma) {
1634 OS << "... , ";
1635 Helper->handledStmt(B->getRHS(),OS);
1636 OS << '\n';
1637 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001638 }
1639 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001640 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001641
Chris Lattnere4f21422009-06-30 01:26:17 +00001642 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001643
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001644 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001645 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001646}
Mike Stump6d9828c2009-07-17 01:31:16 +00001647
Chris Lattnere4f21422009-06-30 01:26:17 +00001648static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1649 const CFGBlock& B,
1650 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Ted Kremenek42a509f2007-08-31 21:30:12 +00001652 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001653
Ted Kremenek7dba8602007-08-29 21:56:09 +00001654 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001655 OS << "\n [ B" << B.getBlockID();
1656
Ted Kremenek42a509f2007-08-31 21:30:12 +00001657 if (&B == &cfg->getEntry())
1658 OS << " (ENTRY) ]\n";
1659 else if (&B == &cfg->getExit())
1660 OS << " (EXIT) ]\n";
1661 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001662 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001663 else
1664 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001665
Ted Kremenek9cffe732007-08-29 23:20:49 +00001666 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001667 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001668
1669 if (print_edges)
1670 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001671
Ted Kremenek411cdee2008-04-16 21:10:48 +00001672 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001673 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001674 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001675 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001676 C->getLHS()->printPretty(OS, Helper,
1677 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001678 if (C->getRHS()) {
1679 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001680 C->getRHS()->printPretty(OS, Helper,
1681 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001682 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001683 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001684 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001685 else
1686 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001687
Ted Kremenek9cffe732007-08-29 23:20:49 +00001688 OS << ":\n";
1689 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001690
Ted Kremenekfddd5182007-08-21 21:42:03 +00001691 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001692 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001693
Ted Kremenek42a509f2007-08-31 21:30:12 +00001694 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1695 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001696
Ted Kremenek9cffe732007-08-29 23:20:49 +00001697 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001698 if (print_edges)
1699 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001700
Ted Kremeneka95d3752008-09-13 05:16:45 +00001701 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001702
Ted Kremenek42a509f2007-08-31 21:30:12 +00001703 if (Helper)
1704 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001705
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001706 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001707 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001708
Ted Kremenek9cffe732007-08-29 23:20:49 +00001709 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001710 if (B.getTerminator()) {
1711 if (print_edges)
1712 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001713
Ted Kremenek9cffe732007-08-29 23:20:49 +00001714 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001715
Ted Kremenek42a509f2007-08-31 21:30:12 +00001716 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001717
Chris Lattnere4f21422009-06-30 01:26:17 +00001718 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1719 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001720 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001721 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001722 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001723
Ted Kremenek9cffe732007-08-29 23:20:49 +00001724 if (print_edges) {
1725 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001726 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001727 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001728
Ted Kremenek42a509f2007-08-31 21:30:12 +00001729 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1730 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001731
Ted Kremenek42a509f2007-08-31 21:30:12 +00001732 if (i == 8 || (i-8) == 0)
1733 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001734
Ted Kremenek9cffe732007-08-29 23:20:49 +00001735 OS << " B" << (*I)->getBlockID();
1736 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001737
Ted Kremenek42a509f2007-08-31 21:30:12 +00001738 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001739
Ted Kremenek42a509f2007-08-31 21:30:12 +00001740 // Print the successors of this block.
1741 OS << " Successors (" << B.succ_size() << "):";
1742 i = 0;
1743
1744 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1745 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001746
Ted Kremenek42a509f2007-08-31 21:30:12 +00001747 if (i == 8 || (i-8) % 10 == 0)
1748 OS << "\n ";
1749
1750 OS << " B" << (*I)->getBlockID();
1751 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001752
Ted Kremenek9cffe732007-08-29 23:20:49 +00001753 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001754 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001755}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001756
Ted Kremenek42a509f2007-08-31 21:30:12 +00001757
1758/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001759void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001760
1761/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001762void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1763 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001764
Ted Kremenek42a509f2007-08-31 21:30:12 +00001765 // Print the entry block.
1766 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001767
Ted Kremenek42a509f2007-08-31 21:30:12 +00001768 // Iterate through the CFGBlocks and print them one by one.
1769 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1770 // Skip the entry block, because we already printed it.
1771 if (&(*I) == &getEntry() || &(*I) == &getExit())
1772 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001773
Ted Kremenek42a509f2007-08-31 21:30:12 +00001774 print_block(OS, this, *I, &Helper, true);
1775 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001776
Ted Kremenek42a509f2007-08-31 21:30:12 +00001777 // Print the exit block.
1778 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001779 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001780}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001781
1782/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001783void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1784 print(llvm::errs(), cfg, LO);
1785}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001786
1787/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1788/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001789void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1790 const LangOptions &LO) const {
1791 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001792 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001793}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001794
Ted Kremeneka2925852008-01-30 23:02:42 +00001795/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001796void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001797 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001798 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001799 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1800}
1801
Ted Kremenek390e48b2008-11-12 21:11:49 +00001802Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001803
Ted Kremenek411cdee2008-04-16 21:10:48 +00001804 if (!Terminator)
1805 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001806
Ted Kremenek411cdee2008-04-16 21:10:48 +00001807 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001808
Ted Kremenek411cdee2008-04-16 21:10:48 +00001809 switch (Terminator->getStmtClass()) {
1810 default:
1811 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001812
Ted Kremenek411cdee2008-04-16 21:10:48 +00001813 case Stmt::ForStmtClass:
1814 E = cast<ForStmt>(Terminator)->getCond();
1815 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001816
Ted Kremenek411cdee2008-04-16 21:10:48 +00001817 case Stmt::WhileStmtClass:
1818 E = cast<WhileStmt>(Terminator)->getCond();
1819 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001820
Ted Kremenek411cdee2008-04-16 21:10:48 +00001821 case Stmt::DoStmtClass:
1822 E = cast<DoStmt>(Terminator)->getCond();
1823 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001824
Ted Kremenek411cdee2008-04-16 21:10:48 +00001825 case Stmt::IfStmtClass:
1826 E = cast<IfStmt>(Terminator)->getCond();
1827 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001828
Ted Kremenek411cdee2008-04-16 21:10:48 +00001829 case Stmt::ChooseExprClass:
1830 E = cast<ChooseExpr>(Terminator)->getCond();
1831 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001832
Ted Kremenek411cdee2008-04-16 21:10:48 +00001833 case Stmt::IndirectGotoStmtClass:
1834 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1835 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001836
Ted Kremenek411cdee2008-04-16 21:10:48 +00001837 case Stmt::SwitchStmtClass:
1838 E = cast<SwitchStmt>(Terminator)->getCond();
1839 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001840
Ted Kremenek411cdee2008-04-16 21:10:48 +00001841 case Stmt::ConditionalOperatorClass:
1842 E = cast<ConditionalOperator>(Terminator)->getCond();
1843 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001844
Ted Kremenek411cdee2008-04-16 21:10:48 +00001845 case Stmt::BinaryOperatorClass: // '&&' and '||'
1846 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001847 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001848
Ted Kremenek390e48b2008-11-12 21:11:49 +00001849 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001850 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001851 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001852
Ted Kremenek411cdee2008-04-16 21:10:48 +00001853 return E ? E->IgnoreParens() : NULL;
1854}
1855
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001856bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001858 if (!Terminator)
1859 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001860
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001861 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001862
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001863 switch (Terminator->getStmtClass()) {
1864 default:
1865 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001866
1867 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001868 case Stmt::WhileStmtClass:
1869 case Stmt::DoStmtClass:
1870 case Stmt::IfStmtClass:
1871 case Stmt::ChooseExprClass:
1872 case Stmt::ConditionalOperatorClass:
1873 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00001874 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001875 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Ted Kremenek9c2535a2008-05-16 16:06:00 +00001877 return E ? E->IgnoreParens() : NULL;
1878}
1879
Ted Kremeneka2925852008-01-30 23:02:42 +00001880
Ted Kremenek7dba8602007-08-29 21:56:09 +00001881//===----------------------------------------------------------------------===//
1882// CFG Graphviz Visualization
1883//===----------------------------------------------------------------------===//
1884
Ted Kremenek42a509f2007-08-31 21:30:12 +00001885
1886#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00001887static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001888#endif
1889
Chris Lattnere4f21422009-06-30 01:26:17 +00001890void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001891#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00001892 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001893 GraphHelper = &H;
1894 llvm::ViewGraph(this,"CFG");
1895 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001896#endif
1897}
1898
Ted Kremenek7dba8602007-08-29 21:56:09 +00001899namespace llvm {
1900template<>
1901struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson02995ce2009-06-24 17:37:55 +00001902 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
1903 bool ShortNames) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00001904
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001905#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00001906 std::string OutSStr;
1907 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001908 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00001909 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00001910
1911 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
1912
1913 // Process string output to make it nicer...
1914 for (unsigned i = 0; i != OutStr.length(); ++i)
1915 if (OutStr[i] == '\n') { // Left justify
1916 OutStr[i] = '\\';
1917 OutStr.insert(OutStr.begin()+i+1, 'l');
1918 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001919
Ted Kremenek7dba8602007-08-29 21:56:09 +00001920 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00001921#else
1922 return "";
1923#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00001924 }
1925};
1926} // end namespace llvm