blob: 821a578dbcfc0dabf317d09a6bbf0191c1c12dfb [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremenek4aa1e8b2007-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 Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramera3b13412010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000047
Benjamin Kramera3b13412010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek66de6032010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000050
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000051private:
Benjamin Kramera3b13412010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000053};
Mike Stump31feda52009-07-17 01:31:16 +000054
Ted Kremenekbe9b33b2008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000056/// The builder is stateful: an instance of the builder should be used to only
57/// construct a single CFG.
58///
59/// Example usage:
60///
61/// CFGBuilder builder;
62/// CFG* cfg = builder.BuildAST(stmt1);
63///
Mike Stump31feda52009-07-17 01:31:16 +000064/// CFG construction is done via a recursive walk of an AST. We actually parse
65/// the AST in reverse order so that the successor of a basic block is
66/// constructed prior to its predecessor. This allows us to nicely capture
67/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stump0d76d072009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +000072
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenek1aebee02007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremeneke1810de2007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenek879d8e12007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +000080
Ted Kremenekeda180e22007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek8a632182007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +000084
85 // A list of blocks that end with a "goto" that must be backpatched to their
86 // resolved targets upon completion of CFG construction.
Ted Kremenekde979ae2007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +000089
Ted Kremenekeda180e22007-08-28 19:26:49 +000090 // A list of labels whose address has been taken (for indirect gotos).
91 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
92 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +000093
94public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +000095 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
96 Block(NULL), Succ(NULL),
Ted Kremeneke1810de2007-08-22 21:51:58 +000097 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +000098 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
99 TryTerminatedBlock(NULL) {}
Mike Stump31feda52009-07-17 01:31:16 +0000100
Ted Kremenek9aae5132007-08-23 21:42:29 +0000101 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
103 bool pruneTriviallyFalseEdges, bool AddEHEdges,
Mike Stump04c68512010-01-21 15:20:48 +0000104 bool AddScopes);
Mike Stump31feda52009-07-17 01:31:16 +0000105
Ted Kremenek93668002009-07-17 22:18:43 +0000106private:
107 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000108 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
109 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
110 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000111 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000112 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
113 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
114 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000115 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000116 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000117 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000118 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000119 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000120 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000121 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000122 CFGBlock *VisitDeclStmt(DeclStmt *DS);
123 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000124 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
125 CFGBlock *VisitDoStmt(DoStmt *D);
126 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000127 CFGBlock *VisitGotoStmt(GotoStmt* G);
128 CFGBlock *VisitIfStmt(IfStmt *I);
129 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
130 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000131 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000132 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
133 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
134 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
135 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
136 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
137 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000138 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
139 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000140 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
141 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000142
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000143 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
144 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000145 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000146
Ted Kremenek6065ef62008-04-28 18:00:46 +0000147 // NYS == Not Yet Supported
148 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000149 badCFG = true;
150 return Block;
151 }
Mike Stump31feda52009-07-17 01:31:16 +0000152
Mike Stump92244b02010-01-19 22:00:14 +0000153 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
154 if (!AddScopes)
155 return B;
156
157 if (B == 0)
158 B = createBlock();
159 B->StartScope(S, cfg->getBumpVectorContext());
160 return B;
161 }
162
163 void EndScope(Stmt *S) {
164 if (!AddScopes)
165 return;
166
167 if (Block == 0)
168 Block = createBlock();
169 Block->EndScope(S, cfg->getBumpVectorContext());
170 }
171
Ted Kremenek93668002009-07-17 22:18:43 +0000172 void autoCreateBlock() { if (!Block) Block = createBlock(); }
173 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek55957a82009-05-02 00:13:27 +0000174 bool FinishBlock(CFGBlock* B);
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000175 CFGBlock *addStmt(Stmt *S) {
176 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000177 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000178
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000179 void AppendStmt(CFGBlock *B, Stmt *S,
180 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
181 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000182 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000183
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000184 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
185 B->addSuccessor(S, cfg->getBumpVectorContext());
186 }
Mike Stump11289f42009-09-09 15:08:12 +0000187
Ted Kremenek963cc312009-07-24 06:55:42 +0000188 /// TryResult - a class representing a variant over the values
189 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
190 /// and is used by the CFGBuilder to decide if a branch condition
191 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000192 class TryResult {
193 int X;
194 public:
195 TryResult(bool b) : X(b ? 1 : 0) {}
196 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000197
Ted Kremenek30754282009-07-24 04:47:11 +0000198 bool isTrue() const { return X == 1; }
199 bool isFalse() const { return X == 0; }
200 bool isKnown() const { return X >= 0; }
201 void negate() {
202 assert(isKnown());
203 X ^= 0x1;
204 }
205 };
Mike Stump11289f42009-09-09 15:08:12 +0000206
Mike Stump773582d2009-07-23 23:25:26 +0000207 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
208 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000209 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000210 if (!PruneTriviallyFalseEdges)
211 return TryResult();
212
Mike Stump773582d2009-07-23 23:25:26 +0000213 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000214 if (!S->isTypeDependent() && !S->isValueDependent() &&
215 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000216 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000217
218 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000219 }
220
Ted Kremenekb64d1832008-03-13 03:04:22 +0000221 bool badCFG;
Mike Stump04c68512010-01-21 15:20:48 +0000222
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000223 // True iff trivially false edges should be pruned from the CFG.
224 bool PruneTriviallyFalseEdges;
225
Mike Stump04c68512010-01-21 15:20:48 +0000226 // True iff EH edges on CallExprs should be added to the CFG.
227 bool AddEHEdges;
228
229 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump92244b02010-01-19 22:00:14 +0000230 bool AddScopes;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000231};
Mike Stump31feda52009-07-17 01:31:16 +0000232
Douglas Gregor4619e432008-12-05 23:32:09 +0000233// FIXME: Add support for dependent-sized array types in C++?
234// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000235static VariableArrayType* FindVA(Type* t) {
236 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
237 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
238 if (vat->getSizeExpr())
239 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000240
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000241 t = vt->getElementType().getTypePtr();
242 }
Mike Stump31feda52009-07-17 01:31:16 +0000243
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000244 return 0;
245}
Mike Stump31feda52009-07-17 01:31:16 +0000246
247/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
248/// arbitrary statement. Examples include a single expression or a function
249/// body (compound statement). The ownership of the returned CFG is
250/// transferred to the caller. If CFG construction fails, this method returns
251/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000252CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000253 bool pruneTriviallyFalseEdges,
Mike Stumpcc3a8532010-01-21 17:21:23 +0000254 bool addehedges, bool AddScopes) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000255
Mike Stumpcc3a8532010-01-21 17:21:23 +0000256 AddEHEdges = addehedges;
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000257 PruneTriviallyFalseEdges = pruneTriviallyFalseEdges;
258
Mike Stump0d76d072009-07-20 23:24:15 +0000259 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000260 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000261 if (!Statement)
262 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000263
Mike Stump92244b02010-01-19 22:00:14 +0000264 this->AddScopes = AddScopes;
Ted Kremenekb64d1832008-03-13 03:04:22 +0000265 badCFG = false;
Mike Stump31feda52009-07-17 01:31:16 +0000266
267 // Create an empty block that will serve as the exit block for the CFG. Since
268 // this is the first block added to the CFG, it will be implicitly registered
269 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000270 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000271 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000272 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000273
Ted Kremenek9aae5132007-08-23 21:42:29 +0000274 // Visit the statements and create the CFG.
Ted Kremenek93668002009-07-17 22:18:43 +0000275 CFGBlock* B = addStmt(Statement);
Mike Stump6bf1c082010-01-21 02:21:40 +0000276
277 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
278 // FIXME: Add code for base initializers and member initializers.
279 (void)CD;
280 }
Ted Kremenek8aed4902009-10-20 23:46:25 +0000281 if (!B)
282 B = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000283
Daniel Dunbar260918c2010-02-22 05:58:59 +0000284 if (B) {
285 // Finalize the last constructed block. This usually involves reversing the
286 // order of the statements in the block.
287 if (Block) FinishBlock(B);
Mike Stump31feda52009-07-17 01:31:16 +0000288
Daniel Dunbar260918c2010-02-22 05:58:59 +0000289 // Backpatch the gotos whose label -> block mappings we didn't know when we
290 // encountered them.
291 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenek9aae5132007-08-23 21:42:29 +0000292 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000293
Daniel Dunbar260918c2010-02-22 05:58:59 +0000294 CFGBlock* B = *I;
295 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
296 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenek9aae5132007-08-23 21:42:29 +0000297
Daniel Dunbar260918c2010-02-22 05:58:59 +0000298 // If there is no target for the goto, then we are looking at an
299 // incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000300 if (LI == LabelMap.end()) continue;
Mike Stump31feda52009-07-17 01:31:16 +0000301
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000302 AddSuccessor(B, LI->second);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000303 }
Mike Stump31feda52009-07-17 01:31:16 +0000304
Daniel Dunbar260918c2010-02-22 05:58:59 +0000305 // Add successors to the Indirect Goto Dispatch block (if we have one).
306 if (CFGBlock* B = cfg->getIndirectGotoBlock())
307 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
308 E = AddressTakenLabels.end(); I != E; ++I ) {
309
310 // Lookup the target block.
311 LabelMapTy::iterator LI = LabelMap.find(*I);
312
313 // If there is no target block that contains label, then we are looking
314 // at an incomplete AST. Handle this by not registering a successor.
315 if (LI == LabelMap.end()) continue;
316
317 AddSuccessor(B, LI->second);
318 }
319
320 Succ = B;
321 }
Mike Stump31feda52009-07-17 01:31:16 +0000322
323 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000324 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000325
Ted Kremenekab929bb2009-10-20 23:59:28 +0000326 return badCFG ? NULL : cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000327}
Mike Stump31feda52009-07-17 01:31:16 +0000328
Ted Kremenek9aae5132007-08-23 21:42:29 +0000329/// createBlock - Used to lazily create blocks that are connected
330/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000331CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000332 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000333 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000334 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000335 return B;
336}
Mike Stump31feda52009-07-17 01:31:16 +0000337
Ted Kremenek0868eea2009-09-24 18:45:41 +0000338/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek55957a82009-05-02 00:13:27 +0000339bool CFGBuilder::FinishBlock(CFGBlock* B) {
340 if (badCFG)
341 return false;
342
Ted Kremenek93668002009-07-17 22:18:43 +0000343 assert(B);
Ted Kremenek55957a82009-05-02 00:13:27 +0000344 return true;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000345}
346
Ted Kremenek93668002009-07-17 22:18:43 +0000347/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000348/// blocks for ternary operators, &&, and ||. We also process "," and
349/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000350CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000351tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000352 if (!S) {
353 badCFG = true;
354 return 0;
355 }
Ted Kremenek93668002009-07-17 22:18:43 +0000356 switch (S->getStmtClass()) {
357 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000358 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000359
360 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000361 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000362
Ted Kremenek93668002009-07-17 22:18:43 +0000363 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000364 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremenek93668002009-07-17 22:18:43 +0000366 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000367 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000368
Ted Kremenek93668002009-07-17 22:18:43 +0000369 case Stmt::BreakStmtClass:
370 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek93668002009-07-17 22:18:43 +0000372 case Stmt::CallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000373 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000374
Ted Kremenek93668002009-07-17 22:18:43 +0000375 case Stmt::CaseStmtClass:
376 return VisitCaseStmt(cast<CaseStmt>(S));
377
378 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000379 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000380
Ted Kremenek93668002009-07-17 22:18:43 +0000381 case Stmt::CompoundStmtClass:
382 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek93668002009-07-17 22:18:43 +0000384 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000385 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenek93668002009-07-17 22:18:43 +0000387 case Stmt::ContinueStmtClass:
388 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenekb27378c2010-01-19 20:40:33 +0000390 case Stmt::CXXCatchStmtClass:
391 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
392
Zhongxing Xu7e612172010-04-13 09:38:01 +0000393 case Stmt::CXXMemberCallExprClass:
394 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
395
Ted Kremenekb27378c2010-01-19 20:40:33 +0000396 case Stmt::CXXThrowExprClass:
397 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000398
Ted Kremenekb27378c2010-01-19 20:40:33 +0000399 case Stmt::CXXTryStmtClass:
400 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000401
Ted Kremenek93668002009-07-17 22:18:43 +0000402 case Stmt::DeclStmtClass:
403 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000404
Ted Kremenek93668002009-07-17 22:18:43 +0000405 case Stmt::DefaultStmtClass:
406 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000407
Ted Kremenek93668002009-07-17 22:18:43 +0000408 case Stmt::DoStmtClass:
409 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ted Kremenek93668002009-07-17 22:18:43 +0000411 case Stmt::ForStmtClass:
412 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenek93668002009-07-17 22:18:43 +0000414 case Stmt::GotoStmtClass:
415 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000416
Ted Kremenek93668002009-07-17 22:18:43 +0000417 case Stmt::IfStmtClass:
418 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000419
Ted Kremenek93668002009-07-17 22:18:43 +0000420 case Stmt::IndirectGotoStmtClass:
421 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000422
Ted Kremenek93668002009-07-17 22:18:43 +0000423 case Stmt::LabelStmtClass:
424 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000425
Ted Kremenek5868ec62010-04-11 17:02:10 +0000426 case Stmt::MemberExprClass:
427 return VisitMemberExpr(cast<MemberExpr>(S), asc);
428
Ted Kremenek93668002009-07-17 22:18:43 +0000429 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000430 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
431
Ted Kremenek93668002009-07-17 22:18:43 +0000432 case Stmt::ObjCAtSynchronizedStmtClass:
433 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek93668002009-07-17 22:18:43 +0000435 case Stmt::ObjCAtThrowStmtClass:
436 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000437
Ted Kremenek93668002009-07-17 22:18:43 +0000438 case Stmt::ObjCAtTryStmtClass:
439 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000440
Ted Kremenek93668002009-07-17 22:18:43 +0000441 case Stmt::ObjCForCollectionStmtClass:
442 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000443
Ted Kremenek93668002009-07-17 22:18:43 +0000444 case Stmt::ParenExprClass:
445 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000446 goto tryAgain;
447
Ted Kremenek93668002009-07-17 22:18:43 +0000448 case Stmt::NullStmtClass:
449 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenek93668002009-07-17 22:18:43 +0000451 case Stmt::ReturnStmtClass:
452 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000453
Ted Kremenek93668002009-07-17 22:18:43 +0000454 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000455 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000456
Ted Kremenek93668002009-07-17 22:18:43 +0000457 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000458 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000459
Ted Kremenek93668002009-07-17 22:18:43 +0000460 case Stmt::SwitchStmtClass:
461 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenek93668002009-07-17 22:18:43 +0000463 case Stmt::WhileStmtClass:
464 return VisitWhileStmt(cast<WhileStmt>(S));
465 }
466}
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000468CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
469 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000470 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000471 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Ted Kremenek93668002009-07-17 22:18:43 +0000474 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000475}
Mike Stump31feda52009-07-17 01:31:16 +0000476
Ted Kremenek93668002009-07-17 22:18:43 +0000477/// VisitChildren - Visit the children of a Stmt.
478CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
479 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000480 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000481 E = Terminator->child_end(); I != E; ++I) {
482 if (*I) B = Visit(*I);
483 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000484 return B;
485}
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000487CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
488 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000489 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000490
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000491 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000492 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000493 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000494 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000495
Ted Kremenek9aae5132007-08-23 21:42:29 +0000496 return Block;
497}
Mike Stump11289f42009-09-09 15:08:12 +0000498
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000499CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
500 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000501 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000502 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000503 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremenek93668002009-07-17 22:18:43 +0000505 if (!FinishBlock(ConfluenceBlock))
506 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000507
Ted Kremenek93668002009-07-17 22:18:43 +0000508 // create the block evaluating the LHS
509 CFGBlock* LHSBlock = createBlock(false);
510 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Ted Kremenek93668002009-07-17 22:18:43 +0000512 // create the block evaluating the RHS
513 Succ = ConfluenceBlock;
514 Block = NULL;
515 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000516
517 if (RHSBlock) {
518 if (!FinishBlock(RHSBlock))
519 return 0;
520 }
521 else {
522 // Create an empty block for cases where the RHS doesn't require
523 // any explicit statements in the CFG.
524 RHSBlock = createBlock();
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Mike Stump773582d2009-07-23 23:25:26 +0000527 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000528 TryResult KnownVal = TryEvaluateBool(B->getLHS());
529 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
530 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000531
Ted Kremenek93668002009-07-17 22:18:43 +0000532 // Now link the LHSBlock with RHSBlock.
533 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000534 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
535 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000536 } else {
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000537 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000538 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
539 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000540 }
Mike Stump11289f42009-09-09 15:08:12 +0000541
Ted Kremenek93668002009-07-17 22:18:43 +0000542 // Generate the blocks for evaluating the LHS.
543 Block = LHSBlock;
544 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000545 }
Ted Kremenek93668002009-07-17 22:18:43 +0000546 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000547 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000548 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000549 addStmt(B->getRHS());
550 return addStmt(B->getLHS());
551 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000552 else if (B->isAssignmentOp()) {
553 if (asc.alwaysAdd()) {
554 autoCreateBlock();
555 AppendStmt(Block, B, asc);
556 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000557
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000558 Visit(B->getRHS());
559 return Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
560 }
Mike Stump11289f42009-09-09 15:08:12 +0000561
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000562 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000563}
564
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000565CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
566 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000567 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000568 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000569 }
570 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000571}
572
Ted Kremenek93668002009-07-17 22:18:43 +0000573CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
574 // "break" is a control-flow statement. Thus we stop processing the current
575 // block.
576 if (Block && !FinishBlock(Block))
577 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000578
Ted Kremenek93668002009-07-17 22:18:43 +0000579 // Now create a new block that ends with the break statement.
580 Block = createBlock(false);
581 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000582
Ted Kremenek93668002009-07-17 22:18:43 +0000583 // If there is no target for the break, then we are looking at an incomplete
584 // AST. This means that the CFG cannot be constructed.
585 if (BreakTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000586 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000587 else
588 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000589
590
Ted Kremenek9aae5132007-08-23 21:42:29 +0000591 return Block;
592}
Mike Stump11289f42009-09-09 15:08:12 +0000593
Mike Stump04c68512010-01-21 15:20:48 +0000594static bool CanThrow(Expr *E) {
595 QualType Ty = E->getType();
596 if (Ty->isFunctionPointerType())
597 Ty = Ty->getAs<PointerType>()->getPointeeType();
598 else if (Ty->isBlockPointerType())
599 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000600
Mike Stump04c68512010-01-21 15:20:48 +0000601 const FunctionType *FT = Ty->getAs<FunctionType>();
602 if (FT) {
603 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
604 if (Proto->hasEmptyExceptionSpec())
605 return false;
606 }
607 return true;
608}
609
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000610CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000611 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000612 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000613 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000614 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000615 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000616
Mike Stump04c68512010-01-21 15:20:48 +0000617 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000618
619 // Languages without exceptions are assumed to not throw.
620 if (Context->getLangOptions().Exceptions) {
Mike Stump04c68512010-01-21 15:20:48 +0000621 if (AddEHEdges)
622 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000623 }
624
625 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000626 if (FD->hasAttr<NoReturnAttr>())
627 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000628 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000629 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000630 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000631
Mike Stump04c68512010-01-21 15:20:48 +0000632 if (!CanThrow(C->getCallee()))
633 AddEHEdge = false;
634
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000635 if (!NoReturn && !AddEHEdge) {
636 if (asc.asLValue())
637 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
638 else
639 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Mike Stump92244b02010-01-19 22:00:14 +0000642 if (Block) {
643 Succ = Block;
644 if (!FinishBlock(Block))
645 return 0;
646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Mike Stump92244b02010-01-19 22:00:14 +0000648 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000649 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000650
Mike Stump92244b02010-01-19 22:00:14 +0000651 if (NoReturn) {
652 // Wire this to the exit block directly.
653 AddSuccessor(Block, &cfg->getExit());
654 }
Mike Stump04c68512010-01-21 15:20:48 +0000655 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000656 // Add exceptional edges.
657 if (TryTerminatedBlock)
658 AddSuccessor(Block, TryTerminatedBlock);
659 else
660 AddSuccessor(Block, &cfg->getExit());
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Mike Stump8c5d7992009-07-25 21:26:53 +0000663 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000664}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000665
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000666CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
667 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000668 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000669 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000670 if (!FinishBlock(ConfluenceBlock))
671 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Ted Kremenek5868ec62010-04-11 17:02:10 +0000673 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
674 : AddStmtChoice::AlwaysAdd;
675
Ted Kremenek21822592009-07-17 18:20:32 +0000676 Succ = ConfluenceBlock;
677 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000678 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000679 if (!FinishBlock(LHSBlock))
680 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Ted Kremenek21822592009-07-17 18:20:32 +0000682 Succ = ConfluenceBlock;
683 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000684 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000685 if (!FinishBlock(RHSBlock))
686 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Ted Kremenek21822592009-07-17 18:20:32 +0000688 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000689 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000690 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000691 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
692 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000693 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000694 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000695}
Mike Stump11289f42009-09-09 15:08:12 +0000696
697
698CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump92244b02010-01-19 22:00:14 +0000699 EndScope(C);
700
Mike Stump11289f42009-09-09 15:08:12 +0000701 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000702
703 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
704 I != E; ++I ) {
705 LastBlock = addStmt(*I);
Mike Stump11289f42009-09-09 15:08:12 +0000706
Ted Kremenekce499c22009-08-27 23:16:26 +0000707 if (badCFG)
708 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000709 }
Mike Stump92244b02010-01-19 22:00:14 +0000710
711 LastBlock = StartScope(C, LastBlock);
712
Ted Kremenek93668002009-07-17 22:18:43 +0000713 return LastBlock;
714}
Mike Stump11289f42009-09-09 15:08:12 +0000715
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000716CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
717 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000718 // Create the confluence block that will "merge" the results of the ternary
719 // expression.
720 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000721 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000722 if (!FinishBlock(ConfluenceBlock))
723 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000724
Ted Kremenek5868ec62010-04-11 17:02:10 +0000725 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
726 : AddStmtChoice::AlwaysAdd;
727
Ted Kremenek51d40b02009-07-17 18:15:54 +0000728 // Create a block for the LHS expression if there is an LHS expression. A
729 // GCC extension allows LHS to be NULL, causing the condition to be the
730 // value that is returned instead.
731 // e.g: x ?: y is shorthand for: x ? x : y;
732 Succ = ConfluenceBlock;
733 Block = NULL;
734 CFGBlock* LHSBlock = NULL;
735 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000736 LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000737 if (!FinishBlock(LHSBlock))
738 return 0;
739 Block = NULL;
740 }
Mike Stump11289f42009-09-09 15:08:12 +0000741
Ted Kremenek51d40b02009-07-17 18:15:54 +0000742 // Create the block for the RHS expression.
743 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000744 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000745 if (!FinishBlock(RHSBlock))
746 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000747
Ted Kremenek51d40b02009-07-17 18:15:54 +0000748 // Create the block that will contain the condition.
749 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000750
Mike Stump773582d2009-07-23 23:25:26 +0000751 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000752 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000753 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000754 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000755 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000756 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000757 // If we know the condition is false, add NULL as the successor for
758 // the block containing the condition. In this case, the confluence
759 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000760 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000761 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000762 } else {
763 // If we have no LHS expression, add the ConfluenceBlock as a direct
764 // successor for the block containing the condition. Moreover, we need to
765 // reverse the order of the predecessors in the ConfluenceBlock because
766 // the RHSBlock will have been added to the succcessors already, and we
767 // want the first predecessor to the the block containing the expression
768 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000769 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000770 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000771 std::reverse(ConfluenceBlock->pred_begin(),
772 ConfluenceBlock->pred_end());
773 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000776 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000777 Block->setTerminator(C);
778 return addStmt(C->getCond());
779}
780
Ted Kremenek93668002009-07-17 22:18:43 +0000781CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
782 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000783
Ted Kremenek93668002009-07-17 22:18:43 +0000784 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000785 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000786 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000787 }
Mike Stump11289f42009-09-09 15:08:12 +0000788
Ted Kremenek93668002009-07-17 22:18:43 +0000789 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
792 typedef llvm::SmallVector<Decl*,10> BufTy;
793 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000794
Ted Kremenek93668002009-07-17 22:18:43 +0000795 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
796 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
797 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
798 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000799
Ted Kremenek93668002009-07-17 22:18:43 +0000800 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
801 // automatically freed with the CFG.
802 DeclGroupRef DG(*I);
803 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000804 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000805 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000806
Ted Kremenek93668002009-07-17 22:18:43 +0000807 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000808 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000809 B = VisitDeclSubExpr(D);
810 }
Mike Stump11289f42009-09-09 15:08:12 +0000811
812 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000813}
Mike Stump11289f42009-09-09 15:08:12 +0000814
Ted Kremenek93668002009-07-17 22:18:43 +0000815/// VisitDeclSubExpr - Utility method to add block-level expressions for
816/// initializers in Decls.
817CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
818 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000819
Ted Kremenek93668002009-07-17 22:18:43 +0000820 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000821
Ted Kremenek93668002009-07-17 22:18:43 +0000822 if (!VD)
823 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000824
Ted Kremenek93668002009-07-17 22:18:43 +0000825 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000826
Ted Kremenek93668002009-07-17 22:18:43 +0000827 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000828 AddStmtChoice::Kind k =
829 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
830 : AddStmtChoice::NotAlwaysAdd;
831 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000832 }
Mike Stump11289f42009-09-09 15:08:12 +0000833
Ted Kremenek93668002009-07-17 22:18:43 +0000834 // If the type of VD is a VLA, then we must process its size expressions.
835 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
836 VA = FindVA(VA->getElementType().getTypePtr()))
837 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000838
Ted Kremenek93668002009-07-17 22:18:43 +0000839 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000840}
841
842CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000843 // We may see an if statement in the middle of a basic block, or it may be the
844 // first statement we are processing. In either case, we create a new basic
845 // block. First, we create the blocks for the then...else statements, and
846 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000847 // middle of a block, we stop processing that block. That block is then the
848 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000849
850 // The block we were proccessing is now finished. Make it the successor
851 // block.
852 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000853 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000854 if (!FinishBlock(Block))
855 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000856 }
Mike Stump31feda52009-07-17 01:31:16 +0000857
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000858 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000859 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000860
Ted Kremenek9aae5132007-08-23 21:42:29 +0000861 if (Stmt* Else = I->getElse()) {
862 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000863
Ted Kremenek9aae5132007-08-23 21:42:29 +0000864 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000865 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000866 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000867 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000868
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000869 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
870 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000871 else if (Block) {
872 if (!FinishBlock(ElseBlock))
873 return 0;
874 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000875 }
Mike Stump31feda52009-07-17 01:31:16 +0000876
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000877 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000878 CFGBlock* ThenBlock;
879 {
880 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000881 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000882 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000883 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000884 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000885
Ted Kremenek1b379512009-04-01 03:52:47 +0000886 if (!ThenBlock) {
887 // We can reach here if the "then" body has all NullStmts.
888 // Create an empty block so we can distinguish between true and false
889 // branches in path-sensitive analyses.
890 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000891 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000892 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000893 if (!FinishBlock(ThenBlock))
894 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000895 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000896 }
897
Mike Stump31feda52009-07-17 01:31:16 +0000898 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000899 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000900
Ted Kremenek9aae5132007-08-23 21:42:29 +0000901 // Set the terminator of the new block to the If statement.
902 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000903
Mike Stump773582d2009-07-23 23:25:26 +0000904 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000905 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000906
Ted Kremenek9aae5132007-08-23 21:42:29 +0000907 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000908 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
909 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000910
911 // Add the condition as the last statement in the new block. This may create
912 // new blocks as the condition may contain control-flow. Any newly created
913 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000914 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000915
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000916 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
917 // and the condition variable initialization to the CFG.
918 if (VarDecl *VD = I->getConditionVariable()) {
919 if (Expr *Init = VD->getInit()) {
920 autoCreateBlock();
921 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
922 addStmt(Init);
923 }
924 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000925
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000926 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000927}
Mike Stump31feda52009-07-17 01:31:16 +0000928
929
Ted Kremenek9aae5132007-08-23 21:42:29 +0000930CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000931 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000932 //
Mike Stump31feda52009-07-17 01:31:16 +0000933 // NOTE: If a "return" appears in the middle of a block, this means that the
934 // code afterwards is DEAD (unreachable). We still keep a basic block
935 // for that code; a simple "mark-and-sweep" from the entry block will be
936 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000937 if (Block)
938 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000939
940 // Create the new block.
941 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000942
Ted Kremenek9aae5132007-08-23 21:42:29 +0000943 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000944 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000945
946 // Add the return statement to the block. This may create new blocks if R
947 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000948 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000949}
950
951CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
952 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000953 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000954 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000955
Ted Kremenek93668002009-07-17 22:18:43 +0000956 if (!LabelBlock) // This can happen when the body is empty, i.e.
957 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000958
Ted Kremenek93668002009-07-17 22:18:43 +0000959 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000960 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000961
962 // Labels partition blocks, so this is the end of the basic block we were
963 // processing (L is the block's label). Because this is label (and we have
964 // already processed the substatement) there is no extra control-flow to worry
965 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000966 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000967 if (!FinishBlock(LabelBlock))
968 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000969
970 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000971 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000972
Ted Kremenek9aae5132007-08-23 21:42:29 +0000973 // This block is now the implicit successor of other blocks.
974 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000975
Ted Kremenek9aae5132007-08-23 21:42:29 +0000976 return LabelBlock;
977}
978
979CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000980 // Goto is a control-flow statement. Thus we stop processing the current
981 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000982 if (Block)
983 FinishBlock(Block);
984
Ted Kremenek9aae5132007-08-23 21:42:29 +0000985 Block = createBlock(false);
986 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000987
988 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000989 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000990
Ted Kremenek9aae5132007-08-23 21:42:29 +0000991 if (I == LabelMap.end())
992 // We will need to backpatch this block later.
993 BackpatchBlocks.push_back(Block);
994 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000995 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000996
Mike Stump31feda52009-07-17 01:31:16 +0000997 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000998}
999
1000CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001001 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001002
Mike Stump014b3ea2009-07-21 01:12:51 +00001003 // "for" is a control-flow statement. Thus we stop processing the current
1004 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001005 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001006 if (!FinishBlock(Block))
1007 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001008 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001009 } else
1010 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001011
Ted Kremenek304a9532010-05-21 20:30:15 +00001012 // Save the current value for the break targets.
1013 // All breaks should go to the code following the loop.
1014 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
1015 BreakTargetBlock = LoopSuccessor;
1016
Mike Stump31feda52009-07-17 01:31:16 +00001017 // Because of short-circuit evaluation, the condition of the loop can span
1018 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1019 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001020 CFGBlock* ExitConditionBlock = createBlock(false);
1021 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001022
Ted Kremenek81e14852007-08-27 19:46:09 +00001023 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001024 ExitConditionBlock->setTerminator(F);
1025
1026 // Now add the actual condition to the condition block. Because the condition
1027 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001028 if (Stmt* C = F->getCond()) {
1029 Block = ExitConditionBlock;
1030 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +00001031 assert(Block == EntryConditionBlock);
1032
1033 // If this block contains a condition variable, add both the condition
1034 // variable and initializer to the CFG.
1035 if (VarDecl *VD = F->getConditionVariable()) {
1036 if (Expr *Init = VD->getInit()) {
1037 autoCreateBlock();
1038 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1039 EntryConditionBlock = addStmt(Init);
1040 assert(Block == EntryConditionBlock);
1041 }
1042 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001043
Ted Kremenek55957a82009-05-02 00:13:27 +00001044 if (Block) {
1045 if (!FinishBlock(EntryConditionBlock))
1046 return 0;
1047 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001048 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001049
Mike Stump31feda52009-07-17 01:31:16 +00001050 // The condition block is the implicit successor for the loop body as well as
1051 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001052 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001053
Mike Stump773582d2009-07-23 23:25:26 +00001054 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001055 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001056
Mike Stump773582d2009-07-23 23:25:26 +00001057 if (F->getCond())
1058 KnownVal = TryEvaluateBool(F->getCond());
1059
Ted Kremenek9aae5132007-08-23 21:42:29 +00001060 // Now create the loop body.
1061 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001062 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001063
Ted Kremenek304a9532010-05-21 20:30:15 +00001064 // Save the current values for Block, Succ, and continue targets.
1065 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1066 save_continue(ContinueTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001067
Ted Kremeneke9610502007-08-30 18:39:40 +00001068 // Create a new block to contain the (bottom) of the loop body.
1069 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001070
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001071 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001072 // Generate increment code in its own basic block. This is the target of
1073 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001074 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001075 } else {
1076 // No increment code. Create a special, empty, block that is used as the
1077 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001078 assert(Succ == EntryConditionBlock);
1079 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001080 }
Mike Stump31feda52009-07-17 01:31:16 +00001081
Ted Kremenek902393b2009-04-28 00:51:56 +00001082 // Finish up the increment (or empty) block if it hasn't been already.
1083 if (Block) {
1084 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +00001085 if (!FinishBlock(Block))
1086 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001087 Block = 0;
1088 }
Mike Stump31feda52009-07-17 01:31:16 +00001089
Ted Kremenek902393b2009-04-28 00:51:56 +00001090 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001091
Ted Kremenek902393b2009-04-28 00:51:56 +00001092 // The starting block for the loop increment is the block that should
1093 // represent the 'loop target' for looping back to the start of the loop.
1094 ContinueTargetBlock->setLoopTarget(F);
1095
Mike Stump31feda52009-07-17 01:31:16 +00001096 // Now populate the body block, and in the process create new blocks as we
1097 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001098 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001099
1100 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +00001101 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +00001102 else if (Block && !FinishBlock(BodyBlock))
1103 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001104
Ted Kremenek30754282009-07-24 04:47:11 +00001105 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001106 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001107 }
Mike Stump31feda52009-07-17 01:31:16 +00001108
Ted Kremenek30754282009-07-24 04:47:11 +00001109 // Link up the condition block with the code that follows the loop. (the
1110 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001111 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001112
Ted Kremenek9aae5132007-08-23 21:42:29 +00001113 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001114 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001115 if (Stmt* I = F->getInit()) {
1116 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001117 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001118 } else {
1119 // There is no loop initialization. We are thus basically a while loop.
1120 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001121 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001122 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001123 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001124 }
1125}
1126
Ted Kremenek5868ec62010-04-11 17:02:10 +00001127CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1128 if (asc.alwaysAdd()) {
1129 autoCreateBlock();
1130 AppendStmt(Block, M, asc);
1131 }
1132 return Visit(M->getBase(),
1133 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1134 : AddStmtChoice::AsLValueNotAlwaysAdd);
1135}
1136
Ted Kremenek9d56e642008-11-11 17:10:00 +00001137CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1138 // Objective-C fast enumeration 'for' statements:
1139 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1140 //
1141 // for ( Type newVariable in collection_expression ) { statements }
1142 //
1143 // becomes:
1144 //
1145 // prologue:
1146 // 1. collection_expression
1147 // T. jump to loop_entry
1148 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001149 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001150 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1151 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1152 // TB:
1153 // statements
1154 // T. jump to loop_entry
1155 // FB:
1156 // what comes after
1157 //
1158 // and
1159 //
1160 // Type existingItem;
1161 // for ( existingItem in expression ) { statements }
1162 //
1163 // becomes:
1164 //
Mike Stump31feda52009-07-17 01:31:16 +00001165 // the same with newVariable replaced with existingItem; the binding works
1166 // the same except that for one ObjCForCollectionStmt::getElement() returns
1167 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001168 //
Mike Stump31feda52009-07-17 01:31:16 +00001169
Ted Kremenek9d56e642008-11-11 17:10:00 +00001170 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001171
Ted Kremenek9d56e642008-11-11 17:10:00 +00001172 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001173 if (!FinishBlock(Block))
1174 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001175 LoopSuccessor = Block;
1176 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001177 } else
1178 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001179
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001180 // Build the condition blocks.
1181 CFGBlock* ExitConditionBlock = createBlock(false);
1182 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001183
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001184 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001185 ExitConditionBlock->setTerminator(S);
1186
1187 // The last statement in the block should be the ObjCForCollectionStmt, which
1188 // performs the actual binding to 'element' and determines if there are any
1189 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001190 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001191 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001192
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001193 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001194 // generate new blocks as necesary. We DON'T add the statement by default to
1195 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001196 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001197 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001198 if (!FinishBlock(EntryConditionBlock))
1199 return 0;
1200 Block = 0;
1201 }
Mike Stump31feda52009-07-17 01:31:16 +00001202
1203 // The condition block is the implicit successor for the loop body as well as
1204 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001205 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001206
Ted Kremenek9d56e642008-11-11 17:10:00 +00001207 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001208 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001209 // Save the current values for Succ, continue and break targets.
1210 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001211 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1212
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001213 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001214 ContinueTargetBlock = EntryConditionBlock;
1215
Ted Kremenek93668002009-07-17 22:18:43 +00001216 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001217
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001218 if (!BodyBlock)
1219 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001220 else if (Block) {
1221 if (!FinishBlock(BodyBlock))
1222 return 0;
1223 }
Mike Stump31feda52009-07-17 01:31:16 +00001224
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001225 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001226 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001227 }
Mike Stump31feda52009-07-17 01:31:16 +00001228
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001229 // Link up the condition block with the code that follows the loop.
1230 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001231 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001232
Ted Kremenek9d56e642008-11-11 17:10:00 +00001233 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001234 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001235 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001236}
1237
Ted Kremenek49805452009-05-02 01:49:13 +00001238CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1239 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001240
Ted Kremenek49805452009-05-02 01:49:13 +00001241 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001242 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001243
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001244 // The sync body starts its own basic block. This makes it a little easier
1245 // for diagnostic clients.
1246 if (SyncBlock) {
1247 if (!FinishBlock(SyncBlock))
1248 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001249
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001250 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001251 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001252 }
Mike Stump31feda52009-07-17 01:31:16 +00001253
Ted Kremenek49805452009-05-02 01:49:13 +00001254 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001255 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001256}
Mike Stump31feda52009-07-17 01:31:16 +00001257
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001258CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001259 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001260 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001261}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001262
Ted Kremenek9aae5132007-08-23 21:42:29 +00001263CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001264 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001265
Mike Stump014b3ea2009-07-21 01:12:51 +00001266 // "while" is a control-flow statement. Thus we stop processing the current
1267 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001268 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001269 if (!FinishBlock(Block))
1270 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001271 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001272 } else
1273 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001274
1275 // Because of short-circuit evaluation, the condition of the loop can span
1276 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1277 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001278 CFGBlock* ExitConditionBlock = createBlock(false);
1279 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001280
Ted Kremenek81e14852007-08-27 19:46:09 +00001281 // Set the terminator for the "exit" condition block.
1282 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001283
1284 // Now add the actual condition to the condition block. Because the condition
1285 // itself may contain control-flow, new blocks may be created. Thus we update
1286 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001287 if (Stmt* C = W->getCond()) {
1288 Block = ExitConditionBlock;
1289 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001290 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001291
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001292 // If this block contains a condition variable, add both the condition
1293 // variable and initializer to the CFG.
1294 if (VarDecl *VD = W->getConditionVariable()) {
1295 if (Expr *Init = VD->getInit()) {
1296 autoCreateBlock();
1297 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1298 EntryConditionBlock = addStmt(Init);
1299 assert(Block == EntryConditionBlock);
1300 }
1301 }
1302
Ted Kremenek55957a82009-05-02 00:13:27 +00001303 if (Block) {
1304 if (!FinishBlock(EntryConditionBlock))
1305 return 0;
1306 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001307 }
Mike Stump31feda52009-07-17 01:31:16 +00001308
1309 // The condition block is the implicit successor for the loop body as well as
1310 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001311 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001312
Mike Stump773582d2009-07-23 23:25:26 +00001313 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001314 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001315
Ted Kremenek9aae5132007-08-23 21:42:29 +00001316 // Process the loop body.
1317 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001318 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001319
1320 // Save the current values for Block, Succ, and continue and break targets
1321 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1322 save_continue(ContinueTargetBlock),
1323 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001324
Mike Stump31feda52009-07-17 01:31:16 +00001325 // Create an empty block to represent the transition block for looping back
1326 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001327 Block = 0;
1328 assert(Succ == EntryConditionBlock);
1329 Succ = createBlock();
1330 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001331 ContinueTargetBlock = Succ;
1332
Ted Kremenek9aae5132007-08-23 21:42:29 +00001333 // All breaks should go to the code following the loop.
1334 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001335
Ted Kremenek9aae5132007-08-23 21:42:29 +00001336 // NULL out Block to force lazy instantiation of blocks for the body.
1337 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001338
Ted Kremenek9aae5132007-08-23 21:42:29 +00001339 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001340 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001341
Ted Kremeneke9610502007-08-30 18:39:40 +00001342 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001343 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001344 else if (Block) {
1345 if (!FinishBlock(BodyBlock))
1346 return 0;
1347 }
Mike Stump31feda52009-07-17 01:31:16 +00001348
Ted Kremenek30754282009-07-24 04:47:11 +00001349 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001350 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001351 }
Mike Stump31feda52009-07-17 01:31:16 +00001352
Ted Kremenek30754282009-07-24 04:47:11 +00001353 // Link up the condition block with the code that follows the loop. (the
1354 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001355 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001356
1357 // There can be no more statements in the condition block since we loop back
1358 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001359 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001360
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001361 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001362 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001363 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001364}
Mike Stump11289f42009-09-09 15:08:12 +00001365
1366
Ted Kremenek93668002009-07-17 22:18:43 +00001367CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1368 // FIXME: For now we pretend that @catch and the code it contains does not
1369 // exit.
1370 return Block;
1371}
Mike Stump31feda52009-07-17 01:31:16 +00001372
Ted Kremenek93041ba2008-12-09 20:20:09 +00001373CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1374 // FIXME: This isn't complete. We basically treat @throw like a return
1375 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001376
Ted Kremenek0868eea2009-09-24 18:45:41 +00001377 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001378 if (Block && !FinishBlock(Block))
1379 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001380
Ted Kremenek93041ba2008-12-09 20:20:09 +00001381 // Create the new block.
1382 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001383
Ted Kremenek93041ba2008-12-09 20:20:09 +00001384 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001385 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001386
1387 // Add the statement to the block. This may create new blocks if S contains
1388 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001389 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001390}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001391
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001392CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001393 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001394 if (Block && !FinishBlock(Block))
1395 return 0;
1396
1397 // Create the new block.
1398 Block = createBlock(false);
1399
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001400 if (TryTerminatedBlock)
1401 // The current try statement is the only successor.
1402 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001403 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001404 // otherwise the Exit block is the only successor.
1405 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001406
1407 // Add the statement to the block. This may create new blocks if S contains
1408 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001409 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001410}
1411
Ted Kremenek93668002009-07-17 22:18:43 +00001412CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001413 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001414
Mike Stump8d50b6a2009-07-21 01:27:50 +00001415 // "do...while" is a control-flow statement. Thus we stop processing the
1416 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001417 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001418 if (!FinishBlock(Block))
1419 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001420 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001421 } else
1422 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001423
1424 // Because of short-circuit evaluation, the condition of the loop can span
1425 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1426 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001427 CFGBlock* ExitConditionBlock = createBlock(false);
1428 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001429
Ted Kremenek81e14852007-08-27 19:46:09 +00001430 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001431 ExitConditionBlock->setTerminator(D);
1432
1433 // Now add the actual condition to the condition block. Because the condition
1434 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001435 if (Stmt* C = D->getCond()) {
1436 Block = ExitConditionBlock;
1437 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001438 if (Block) {
1439 if (!FinishBlock(EntryConditionBlock))
1440 return 0;
1441 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001442 }
Mike Stump31feda52009-07-17 01:31:16 +00001443
Ted Kremeneka1523a32008-02-27 07:20:00 +00001444 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001445 Succ = EntryConditionBlock;
1446
Mike Stump773582d2009-07-23 23:25:26 +00001447 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001448 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001449
Ted Kremenek9aae5132007-08-23 21:42:29 +00001450 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001451 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001452 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001453 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001454
Ted Kremenek9aae5132007-08-23 21:42:29 +00001455 // Save the current values for Block, Succ, and continue and break targets
1456 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001457 save_continue(ContinueTargetBlock),
1458 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001459
Ted Kremenek9aae5132007-08-23 21:42:29 +00001460 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001461 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremenek9aae5132007-08-23 21:42:29 +00001463 // All breaks should go to the code following the loop.
1464 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001465
Ted Kremenek9aae5132007-08-23 21:42:29 +00001466 // NULL out Block to force lazy instantiation of blocks for the body.
1467 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001468
Ted Kremenek9aae5132007-08-23 21:42:29 +00001469 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001470 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001471
Ted Kremeneke9610502007-08-30 18:39:40 +00001472 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001473 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001474 else if (Block) {
1475 if (!FinishBlock(BodyBlock))
1476 return 0;
1477 }
Mike Stump31feda52009-07-17 01:31:16 +00001478
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001479 // Add an intermediate block between the BodyBlock and the
Mike Stump31feda52009-07-17 01:31:16 +00001480 // ExitConditionBlock to represent the "loop back" transition. Create an
1481 // empty block to represent the transition block for looping back to the
1482 // head of the loop.
Ted Kremenekcb37bb02009-04-28 04:22:00 +00001483 // FIXME: Can we do this more efficiently without adding another block?
1484 Block = NULL;
1485 Succ = BodyBlock;
1486 CFGBlock *LoopBackBlock = createBlock();
1487 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001488
Ted Kremenek30754282009-07-24 04:47:11 +00001489 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001490 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001491 }
Mike Stump31feda52009-07-17 01:31:16 +00001492
Ted Kremenek30754282009-07-24 04:47:11 +00001493 // Link up the condition block with the code that follows the loop.
1494 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001495 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001496
1497 // There can be no more statements in the body block(s) since we loop back to
1498 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001499 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001500
Ted Kremenek9aae5132007-08-23 21:42:29 +00001501 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001502 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001503 return BodyBlock;
1504}
1505
1506CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1507 // "continue" is a control-flow statement. Thus we stop processing the
1508 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001509 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001510 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001511
Ted Kremenek9aae5132007-08-23 21:42:29 +00001512 // Now create a new block that ends with the continue statement.
1513 Block = createBlock(false);
1514 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001515
Ted Kremenek9aae5132007-08-23 21:42:29 +00001516 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001517 // incomplete AST. This means the CFG cannot be constructed.
1518 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001519 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001520 else
1521 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001522
Ted Kremenek9aae5132007-08-23 21:42:29 +00001523 return Block;
1524}
Mike Stump11289f42009-09-09 15:08:12 +00001525
Ted Kremenek0747de62009-07-18 00:47:21 +00001526CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001527 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001528
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001529 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001530 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001531 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001532 }
Mike Stump11289f42009-09-09 15:08:12 +00001533
Ted Kremenek93668002009-07-17 22:18:43 +00001534 // VLA types have expressions that must be evaluated.
1535 if (E->isArgumentType()) {
1536 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1537 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1538 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001539 }
Mike Stump11289f42009-09-09 15:08:12 +00001540
Mike Stump31feda52009-07-17 01:31:16 +00001541 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001542}
Mike Stump11289f42009-09-09 15:08:12 +00001543
Ted Kremenek93668002009-07-17 22:18:43 +00001544/// VisitStmtExpr - Utility method to handle (nested) statement
1545/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001546CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1547 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001548 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001549 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001550 }
Ted Kremenek93668002009-07-17 22:18:43 +00001551 return VisitCompoundStmt(SE->getSubStmt());
1552}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001553
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001554CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001555 // "switch" is a control-flow statement. Thus we stop processing the current
1556 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001557 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001558
Ted Kremenek9aae5132007-08-23 21:42:29 +00001559 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001560 if (!FinishBlock(Block))
1561 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001562 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001563 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001564
1565 // Save the current "switch" context.
1566 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001567 save_break(BreakTargetBlock),
1568 save_default(DefaultCaseBlock);
1569
Mike Stump31feda52009-07-17 01:31:16 +00001570 // Set the "default" case to be the block after the switch statement. If the
1571 // switch statement contains a "default:", this value will be overwritten with
1572 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001573 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001574
Ted Kremenek9aae5132007-08-23 21:42:29 +00001575 // Create a new block that will contain the switch statement.
1576 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001577
Ted Kremenek9aae5132007-08-23 21:42:29 +00001578 // Now process the switch body. The code after the switch is the implicit
1579 // successor.
1580 Succ = SwitchSuccessor;
1581 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001582
1583 // When visiting the body, the case statements should automatically get linked
1584 // up to the switch. We also don't keep a pointer to the body, since all
1585 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001586 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001587 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001588 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001589 if (Block) {
1590 if (!FinishBlock(BodyBlock))
1591 return 0;
1592 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001593
Mike Stump31feda52009-07-17 01:31:16 +00001594 // If we have no "default:" case, the default transition is to the code
1595 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001596 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001597
Ted Kremenek81e14852007-08-27 19:46:09 +00001598 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001599 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001600 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001601 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001602 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001603
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001604 // Finally, if the SwitchStmt contains a condition variable, add both the
1605 // SwitchStmt and the condition variable initialization to the CFG.
1606 if (VarDecl *VD = Terminator->getConditionVariable()) {
1607 if (Expr *Init = VD->getInit()) {
1608 autoCreateBlock();
1609 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1610 addStmt(Init);
1611 }
1612 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001613
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001614 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001615}
1616
Ted Kremenek93668002009-07-17 22:18:43 +00001617CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001618 // CaseStmts are essentially labels, so they are the first statement in a
1619 // block.
Ted Kremenek55e91e82007-08-30 18:48:11 +00001620
Ted Kremenek93668002009-07-17 22:18:43 +00001621 if (CS->getSubStmt())
1622 addStmt(CS->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001623
Ted Kremenek55e91e82007-08-30 18:48:11 +00001624 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001625 if (!CaseBlock)
1626 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001627
1628 // Cases statements partition blocks, so this is the top of the basic block we
1629 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001630 CaseBlock->setLabel(CS);
1631
Ted Kremenek55957a82009-05-02 00:13:27 +00001632 if (!FinishBlock(CaseBlock))
1633 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001634
1635 // Add this block to the list of successors for the block with the switch
1636 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001637 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001638 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001639
Ted Kremenek9aae5132007-08-23 21:42:29 +00001640 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1641 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001642
Ted Kremenek9aae5132007-08-23 21:42:29 +00001643 // This block is now the implicit successor of other blocks.
1644 Succ = CaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001645
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001646 return CaseBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001647}
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001649CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001650 if (Terminator->getSubStmt())
1651 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001652
Ted Kremenek654c78f2008-02-13 22:05:39 +00001653 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001654
1655 if (!DefaultCaseBlock)
1656 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001657
1658 // Default statements partition blocks, so this is the top of the basic block
1659 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001660 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001661
Ted Kremenek55957a82009-05-02 00:13:27 +00001662 if (!FinishBlock(DefaultCaseBlock))
1663 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001664
Mike Stump31feda52009-07-17 01:31:16 +00001665 // Unlike case statements, we don't add the default block to the successors
1666 // for the switch statement immediately. This is done when we finish
1667 // processing the switch statement. This allows for the default case
1668 // (including a fall-through to the code after the switch statement) to always
1669 // be the last successor of a switch-terminated block.
1670
Ted Kremenek654c78f2008-02-13 22:05:39 +00001671 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1672 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001673
Ted Kremenek654c78f2008-02-13 22:05:39 +00001674 // This block is now the implicit successor of other blocks.
1675 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001676
1677 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001678}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001679
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001680CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1681 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1682 // current block.
1683 CFGBlock* TrySuccessor = NULL;
1684
1685 if (Block) {
1686 if (!FinishBlock(Block))
1687 return 0;
1688 TrySuccessor = Block;
1689 } else TrySuccessor = Succ;
1690
Mike Stump0bdba6c2010-01-20 01:15:34 +00001691 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001692
1693 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001694 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001695 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001696 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001697
Mike Stump0bdba6c2010-01-20 01:15:34 +00001698 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001699 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1700 // The code after the try is the implicit successor.
1701 Succ = TrySuccessor;
1702 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001703 if (CS->getExceptionDecl() == 0) {
1704 HasCatchAll = true;
1705 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001706 Block = NULL;
1707 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1708 if (CatchBlock == 0)
1709 return 0;
1710 // Add this block to the list of successors for the block with the try
1711 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001712 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001713 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001714 if (!HasCatchAll) {
1715 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001716 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001717 else
Mike Stump845384a2010-01-20 01:30:58 +00001718 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001719 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001720
1721 // The code after the try is the implicit successor.
1722 Succ = TrySuccessor;
1723
Mike Stump845384a2010-01-20 01:30:58 +00001724 // Save the current "try" context.
1725 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1726 TryTerminatedBlock = NewTryTerminatedBlock;
1727
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001728 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001729 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001730 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001731 return Block;
1732}
1733
1734CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1735 // CXXCatchStmt are treated like labels, so they are the first statement in a
1736 // block.
1737
1738 if (CS->getHandlerBlock())
1739 addStmt(CS->getHandlerBlock());
1740
1741 CFGBlock* CatchBlock = Block;
1742 if (!CatchBlock)
1743 CatchBlock = createBlock();
1744
1745 CatchBlock->setLabel(CS);
1746
1747 if (!FinishBlock(CatchBlock))
1748 return 0;
1749
1750 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1751 Block = NULL;
1752
1753 return CatchBlock;
1754}
1755
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001756CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00001757 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001758 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001759 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001760 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001761 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001762 return VisitChildren(C);
1763}
1764
Ted Kremenekeda180e22007-08-28 19:26:49 +00001765CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001766 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001767 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001768
Ted Kremenekeda180e22007-08-28 19:26:49 +00001769 if (!IBlock) {
1770 IBlock = createBlock(false);
1771 cfg->setIndirectGotoBlock(IBlock);
1772 }
Mike Stump31feda52009-07-17 01:31:16 +00001773
Ted Kremenekeda180e22007-08-28 19:26:49 +00001774 // IndirectGoto is a control-flow statement. Thus we stop processing the
1775 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001776 if (Block && !FinishBlock(Block))
1777 return 0;
1778
Ted Kremenekeda180e22007-08-28 19:26:49 +00001779 Block = createBlock(false);
1780 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001781 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001782 return addStmt(I->getTarget());
1783}
1784
Ted Kremenek04cca642007-08-23 21:26:19 +00001785} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001786
Mike Stump31feda52009-07-17 01:31:16 +00001787/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1788/// no successors or predecessors. If this is the first block created in the
1789/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001790CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001791 bool first_block = begin() == end();
1792
1793 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001794 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1795 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1796 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001797
1798 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001799 if (first_block)
1800 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001801
1802 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001803 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001804}
1805
Ted Kremenek889073f2007-08-23 16:51:22 +00001806/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1807/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001808CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001809 bool PruneTriviallyFalse,
Mike Stump04c68512010-01-21 15:20:48 +00001810 bool AddEHEdges, bool AddScopes) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001811 CFGBuilder Builder;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001812 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
1813 AddEHEdges, AddScopes);
Ted Kremenek889073f2007-08-23 16:51:22 +00001814}
1815
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001816//===----------------------------------------------------------------------===//
1817// CFG: Queries for BlkExprs.
1818//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001819
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001820namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001821 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001822}
1823
Ted Kremenekbff98442009-12-23 23:37:10 +00001824static void FindSubExprAssignments(Stmt *S,
1825 llvm::SmallPtrSet<Expr*,50>& Set) {
1826 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001827 return;
Mike Stump31feda52009-07-17 01:31:16 +00001828
Ted Kremenekbff98442009-12-23 23:37:10 +00001829 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001830 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00001831 if (!child)
1832 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001833
Ted Kremenekbff98442009-12-23 23:37:10 +00001834 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001835 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001836
Ted Kremenekbff98442009-12-23 23:37:10 +00001837 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001838 }
1839}
1840
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001841static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1842 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001843
1844 // Look for assignments that are used as subexpressions. These are the only
1845 // assignments that we want to *possibly* register as a block-level
1846 // expression. Basically, if an assignment occurs both in a subexpression and
1847 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001848 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001849
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001850 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001851 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001852 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001853
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001854 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001855
1856 // Iterate over the statements again on identify the Expr* and Stmt* at the
1857 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001858
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001859 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001860 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001861
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001862 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001863 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001864 // expression are really "statements" whose value is never used by
1865 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001866 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001867 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001868 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1869 // Special handling for statement expressions. The last statement in
1870 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001871 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001872 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001873 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001874 (*M)[C->body_back()] = x;
1875 }
1876 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001877
Ted Kremenek95a123c2008-01-26 00:03:27 +00001878 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001879 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001880 }
Mike Stump31feda52009-07-17 01:31:16 +00001881
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001882 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001883
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001884 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001885
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001886 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001887 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001888 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001889 }
1890 }
Mike Stump31feda52009-07-17 01:31:16 +00001891
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001892 return M;
1893}
1894
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001895CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1896 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001897 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001898
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001899 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001900 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00001901 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001902}
1903
1904unsigned CFG::getNumBlkExprs() {
1905 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1906 return M->size();
1907 else {
1908 // We assume callers interested in the number of BlkExprs will want
1909 // the map constructed if it doesn't already exist.
1910 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1911 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1912 }
1913}
1914
Ted Kremenek6065ef62008-04-28 18:00:46 +00001915//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001916// Cleanup: CFG dstor.
1917//===----------------------------------------------------------------------===//
1918
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001919CFG::~CFG() {
1920 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1921}
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001923//===----------------------------------------------------------------------===//
1924// CFG pretty printing
1925//===----------------------------------------------------------------------===//
1926
Ted Kremenek7e776b12007-08-22 18:22:34 +00001927namespace {
1928
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001929class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001930 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1931 StmtMapTy StmtMap;
1932 signed CurrentBlock;
1933 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001934 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001935public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001936
Chris Lattnerc61089a2009-06-30 01:26:17 +00001937 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1938 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001939 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1940 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001941 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001942 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001943 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001944 }
1945 }
Mike Stump31feda52009-07-17 01:31:16 +00001946
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001947 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001948
Chris Lattnerc61089a2009-06-30 01:26:17 +00001949 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001950 void setBlockID(signed i) { CurrentBlock = i; }
1951 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001952
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001953 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001954
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001955 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001956
1957 if (I == StmtMap.end())
1958 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001959
1960 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00001961 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001962 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001963 }
Mike Stump31feda52009-07-17 01:31:16 +00001964
Ted Kremenek60983dc2010-01-19 20:52:05 +00001965 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001966 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001967 }
1968};
Chris Lattnerc61089a2009-06-30 01:26:17 +00001969} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001970
Chris Lattnerc61089a2009-06-30 01:26:17 +00001971
1972namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001973class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00001974 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00001975
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001976 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001977 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00001978 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001979public:
Douglas Gregor7de59662009-05-29 20:38:28 +00001980 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00001981 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00001982 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00001983
Ted Kremenek9aae5132007-08-23 21:42:29 +00001984 void VisitIfStmt(IfStmt* I) {
1985 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00001986 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001987 }
Mike Stump31feda52009-07-17 01:31:16 +00001988
Ted Kremenek9aae5132007-08-23 21:42:29 +00001989 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00001990 void VisitStmt(Stmt* Terminator) {
1991 Terminator->printPretty(OS, Helper, Policy);
1992 }
1993
Ted Kremenek9aae5132007-08-23 21:42:29 +00001994 void VisitForStmt(ForStmt* F) {
1995 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001996 if (F->getInit())
1997 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00001998 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00001999 if (Stmt* C = F->getCond())
2000 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002001 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002002 if (F->getInc())
2003 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002004 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002005 }
Mike Stump31feda52009-07-17 01:31:16 +00002006
Ted Kremenek9aae5132007-08-23 21:42:29 +00002007 void VisitWhileStmt(WhileStmt* W) {
2008 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002009 if (Stmt* C = W->getCond())
2010 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002011 }
Mike Stump31feda52009-07-17 01:31:16 +00002012
Ted Kremenek9aae5132007-08-23 21:42:29 +00002013 void VisitDoStmt(DoStmt* D) {
2014 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002015 if (Stmt* C = D->getCond())
2016 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002017 }
Mike Stump31feda52009-07-17 01:31:16 +00002018
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002019 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002020 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002021 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002022 }
Mike Stump31feda52009-07-17 01:31:16 +00002023
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002024 void VisitCXXTryStmt(CXXTryStmt* CS) {
2025 OS << "try ...";
2026 }
2027
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002028 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002029 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002030 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002031 }
Mike Stump31feda52009-07-17 01:31:16 +00002032
Ted Kremenek391f94a2007-08-31 22:29:13 +00002033 void VisitChooseExpr(ChooseExpr* C) {
2034 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002035 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002036 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002037 }
Mike Stump31feda52009-07-17 01:31:16 +00002038
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002039 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2040 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002041 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002042 }
Mike Stump31feda52009-07-17 01:31:16 +00002043
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002044 void VisitBinaryOperator(BinaryOperator* B) {
2045 if (!B->isLogicalOp()) {
2046 VisitExpr(B);
2047 return;
2048 }
Mike Stump31feda52009-07-17 01:31:16 +00002049
Douglas Gregor7de59662009-05-29 20:38:28 +00002050 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002051
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002052 switch (B->getOpcode()) {
2053 case BinaryOperator::LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002054 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002055 return;
2056 case BinaryOperator::LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002057 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002058 return;
2059 default:
2060 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002061 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002062 }
Mike Stump31feda52009-07-17 01:31:16 +00002063
Ted Kremenek12687ff2007-08-27 21:54:41 +00002064 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002065 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002066 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002067};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002068} // end anonymous namespace
2069
Mike Stump31feda52009-07-17 01:31:16 +00002070
Chris Lattnerc61089a2009-06-30 01:26:17 +00002071static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002072 const CFGElement &E) {
2073 Stmt *Terminator = E;
2074
2075 if (E.asStartScope()) {
2076 OS << "start scope\n";
2077 return;
2078 }
2079 if (E.asEndScope()) {
2080 OS << "end scope\n";
2081 return;
2082 }
2083
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002084 if (Helper) {
2085 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002086 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002087 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00002088
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002089 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00002090 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00002091 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00002092 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002093 return;
2094 }
2095 }
Mike Stump31feda52009-07-17 01:31:16 +00002096
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002097 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002098 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002099 if (B->getOpcode() == BinaryOperator::Comma) {
2100 OS << "... , ";
2101 Helper->handledStmt(B->getRHS(),OS);
2102 OS << '\n';
2103 return;
Mike Stump31feda52009-07-17 01:31:16 +00002104 }
2105 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002106 }
Mike Stump31feda52009-07-17 01:31:16 +00002107
Chris Lattnerc61089a2009-06-30 01:26:17 +00002108 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002109
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002110 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002111 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002112}
Mike Stump31feda52009-07-17 01:31:16 +00002113
Chris Lattnerc61089a2009-06-30 01:26:17 +00002114static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2115 const CFGBlock& B,
2116 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002117
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002118 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002119
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002120 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002121 OS << "\n [ B" << B.getBlockID();
2122
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002123 if (&B == &cfg->getEntry())
2124 OS << " (ENTRY) ]\n";
2125 else if (&B == &cfg->getExit())
2126 OS << " (EXIT) ]\n";
2127 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002128 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002129 else
2130 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002131
Ted Kremenek71eca012007-08-29 23:20:49 +00002132 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002133 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002134
2135 if (print_edges)
2136 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002137
Mike Stump92244b02010-01-19 22:00:14 +00002138 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002139 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002140 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002141 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002142 C->getLHS()->printPretty(OS, Helper,
2143 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002144 if (C->getRHS()) {
2145 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002146 C->getRHS()->printPretty(OS, Helper,
2147 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002148 }
Mike Stump92244b02010-01-19 22:00:14 +00002149 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002150 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002151 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002152 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002153 if (CS->getExceptionDecl())
2154 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2155 0);
2156 else
2157 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002158 OS << ")";
2159
2160 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002161 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002162
Ted Kremenek71eca012007-08-29 23:20:49 +00002163 OS << ":\n";
2164 }
Mike Stump31feda52009-07-17 01:31:16 +00002165
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002166 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002167 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002168
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002169 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2170 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002171
Ted Kremenek71eca012007-08-29 23:20:49 +00002172 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002173 if (print_edges)
2174 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002175
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002176 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002177
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002178 if (Helper)
2179 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002180
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002181 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002182 }
Mike Stump31feda52009-07-17 01:31:16 +00002183
Ted Kremenek71eca012007-08-29 23:20:49 +00002184 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002185 if (B.getTerminator()) {
2186 if (print_edges)
2187 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002188
Ted Kremenek71eca012007-08-29 23:20:49 +00002189 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002190
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002191 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002192
Chris Lattnerc61089a2009-06-30 01:26:17 +00002193 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2194 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002195 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002196 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002197 }
Mike Stump31feda52009-07-17 01:31:16 +00002198
Ted Kremenek71eca012007-08-29 23:20:49 +00002199 if (print_edges) {
2200 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002201 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002202 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002203
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002204 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2205 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002206
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002207 if (i == 8 || (i-8) == 0)
2208 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002209
Ted Kremenek71eca012007-08-29 23:20:49 +00002210 OS << " B" << (*I)->getBlockID();
2211 }
Mike Stump31feda52009-07-17 01:31:16 +00002212
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002213 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002214
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002215 // Print the successors of this block.
2216 OS << " Successors (" << B.succ_size() << "):";
2217 i = 0;
2218
2219 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2220 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002221
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002222 if (i == 8 || (i-8) % 10 == 0)
2223 OS << "\n ";
2224
Mike Stump0d76d072009-07-20 23:24:15 +00002225 if (*I)
2226 OS << " B" << (*I)->getBlockID();
2227 else
2228 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002229 }
Mike Stump31feda52009-07-17 01:31:16 +00002230
Ted Kremenek71eca012007-08-29 23:20:49 +00002231 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002232 }
Mike Stump31feda52009-07-17 01:31:16 +00002233}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002234
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002235
2236/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002237void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002238
2239/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002240void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2241 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002242
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002243 // Print the entry block.
2244 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002245
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002246 // Iterate through the CFGBlocks and print them one by one.
2247 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2248 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002249 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002250 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002251
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002252 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002253 }
Mike Stump31feda52009-07-17 01:31:16 +00002254
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002255 // Print the exit block.
2256 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002257 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002258}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002259
2260/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002261void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2262 print(llvm::errs(), cfg, LO);
2263}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002264
2265/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2266/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002267void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2268 const LangOptions &LO) const {
2269 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002270 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002271}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002272
Ted Kremenek15647632008-01-30 23:02:42 +00002273/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002274void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002275 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002276 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002277 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2278}
2279
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002280Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002281
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002282 if (!Terminator)
2283 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002284
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002285 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002286
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002287 switch (Terminator->getStmtClass()) {
2288 default:
2289 break;
Mike Stump31feda52009-07-17 01:31:16 +00002290
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002291 case Stmt::ForStmtClass:
2292 E = cast<ForStmt>(Terminator)->getCond();
2293 break;
Mike Stump31feda52009-07-17 01:31:16 +00002294
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002295 case Stmt::WhileStmtClass:
2296 E = cast<WhileStmt>(Terminator)->getCond();
2297 break;
Mike Stump31feda52009-07-17 01:31:16 +00002298
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002299 case Stmt::DoStmtClass:
2300 E = cast<DoStmt>(Terminator)->getCond();
2301 break;
Mike Stump31feda52009-07-17 01:31:16 +00002302
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002303 case Stmt::IfStmtClass:
2304 E = cast<IfStmt>(Terminator)->getCond();
2305 break;
Mike Stump31feda52009-07-17 01:31:16 +00002306
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002307 case Stmt::ChooseExprClass:
2308 E = cast<ChooseExpr>(Terminator)->getCond();
2309 break;
Mike Stump31feda52009-07-17 01:31:16 +00002310
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002311 case Stmt::IndirectGotoStmtClass:
2312 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2313 break;
Mike Stump31feda52009-07-17 01:31:16 +00002314
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002315 case Stmt::SwitchStmtClass:
2316 E = cast<SwitchStmt>(Terminator)->getCond();
2317 break;
Mike Stump31feda52009-07-17 01:31:16 +00002318
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002319 case Stmt::ConditionalOperatorClass:
2320 E = cast<ConditionalOperator>(Terminator)->getCond();
2321 break;
Mike Stump31feda52009-07-17 01:31:16 +00002322
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002323 case Stmt::BinaryOperatorClass: // '&&' and '||'
2324 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002325 break;
Mike Stump31feda52009-07-17 01:31:16 +00002326
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002327 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002328 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002329 }
Mike Stump31feda52009-07-17 01:31:16 +00002330
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002331 return E ? E->IgnoreParens() : NULL;
2332}
2333
Ted Kremenek92137a32008-05-16 16:06:00 +00002334bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002335
Ted Kremenek92137a32008-05-16 16:06:00 +00002336 if (!Terminator)
2337 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002338
Ted Kremenek92137a32008-05-16 16:06:00 +00002339 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002340
Ted Kremenek92137a32008-05-16 16:06:00 +00002341 switch (Terminator->getStmtClass()) {
2342 default:
2343 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002344
2345 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002346 case Stmt::WhileStmtClass:
2347 case Stmt::DoStmtClass:
2348 case Stmt::IfStmtClass:
2349 case Stmt::ChooseExprClass:
2350 case Stmt::ConditionalOperatorClass:
2351 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002352 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002353 }
Mike Stump31feda52009-07-17 01:31:16 +00002354
Ted Kremenek92137a32008-05-16 16:06:00 +00002355 return E ? E->IgnoreParens() : NULL;
2356}
2357
Ted Kremenek15647632008-01-30 23:02:42 +00002358
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002359//===----------------------------------------------------------------------===//
2360// CFG Graphviz Visualization
2361//===----------------------------------------------------------------------===//
2362
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002363
2364#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002365static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002366#endif
2367
Chris Lattnerc61089a2009-06-30 01:26:17 +00002368void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002369#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002370 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002371 GraphHelper = &H;
2372 llvm::ViewGraph(this,"CFG");
2373 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002374#endif
2375}
2376
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002377namespace llvm {
2378template<>
2379struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002380
2381 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2382
2383 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002384
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002385#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002386 std::string OutSStr;
2387 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002388 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002389 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002390
2391 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2392
2393 // Process string output to make it nicer...
2394 for (unsigned i = 0; i != OutStr.length(); ++i)
2395 if (OutStr[i] == '\n') { // Left justify
2396 OutStr[i] = '\\';
2397 OutStr.insert(OutStr.begin()+i+1, 'l');
2398 }
Mike Stump31feda52009-07-17 01:31:16 +00002399
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002400 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002401#else
2402 return "";
2403#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002404 }
2405};
2406} // end namespace llvm