blob: 3720d20c288bee4395192794ed35dfd12efdf9ca [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());
John McCalle3027922010-08-25 11:45:40 +0000529 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000530 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000531
Ted Kremenek93668002009-07-17 22:18:43 +0000532 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000533 if (B->getOpcode() == BO_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 {
John McCalle3027922010-08-25 11:45:40 +0000537 assert(B->getOpcode() == BO_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 }
John McCalle3027922010-08-25 11:45:40 +0000546 else if (B->getOpcode() == BO_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 ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +0000705 // If we hit a segment of code just containing ';' (NullStmts), we can
706 // get a null block back. In such cases, just use the LastBlock
707 if (CFGBlock *newBlock = addStmt(*I))
708 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +0000709
Ted Kremenekce499c22009-08-27 23:16:26 +0000710 if (badCFG)
711 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000712 }
Mike Stump92244b02010-01-19 22:00:14 +0000713
714 LastBlock = StartScope(C, LastBlock);
715
Ted Kremenek93668002009-07-17 22:18:43 +0000716 return LastBlock;
717}
Mike Stump11289f42009-09-09 15:08:12 +0000718
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000719CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
720 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000721 // Create the confluence block that will "merge" the results of the ternary
722 // expression.
723 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000724 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000725 if (!FinishBlock(ConfluenceBlock))
726 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000727
Ted Kremenek5868ec62010-04-11 17:02:10 +0000728 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
729 : AddStmtChoice::AlwaysAdd;
730
Ted Kremenek51d40b02009-07-17 18:15:54 +0000731 // Create a block for the LHS expression if there is an LHS expression. A
732 // GCC extension allows LHS to be NULL, causing the condition to be the
733 // value that is returned instead.
734 // e.g: x ?: y is shorthand for: x ? x : y;
735 Succ = ConfluenceBlock;
736 Block = NULL;
737 CFGBlock* LHSBlock = NULL;
738 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000739 LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000740 if (!FinishBlock(LHSBlock))
741 return 0;
742 Block = NULL;
743 }
Mike Stump11289f42009-09-09 15:08:12 +0000744
Ted Kremenek51d40b02009-07-17 18:15:54 +0000745 // Create the block for the RHS expression.
746 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000747 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000748 if (!FinishBlock(RHSBlock))
749 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000750
Ted Kremenek51d40b02009-07-17 18:15:54 +0000751 // Create the block that will contain the condition.
752 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000753
Mike Stump773582d2009-07-23 23:25:26 +0000754 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000755 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000756 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000757 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000758 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000759 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000760 // If we know the condition is false, add NULL as the successor for
761 // the block containing the condition. In this case, the confluence
762 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000763 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000764 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000765 } else {
766 // If we have no LHS expression, add the ConfluenceBlock as a direct
767 // successor for the block containing the condition. Moreover, we need to
768 // reverse the order of the predecessors in the ConfluenceBlock because
769 // the RHSBlock will have been added to the succcessors already, and we
770 // want the first predecessor to the the block containing the expression
771 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000772 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000773 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000774 std::reverse(ConfluenceBlock->pred_begin(),
775 ConfluenceBlock->pred_end());
776 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000779 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000780 Block->setTerminator(C);
781 return addStmt(C->getCond());
782}
783
Ted Kremenek93668002009-07-17 22:18:43 +0000784CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
785 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000786
Ted Kremenek93668002009-07-17 22:18:43 +0000787 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000788 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000789 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
Ted Kremenek93668002009-07-17 22:18:43 +0000792 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000793
Ted Kremenek93668002009-07-17 22:18:43 +0000794 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
795 typedef llvm::SmallVector<Decl*,10> BufTy;
796 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenek93668002009-07-17 22:18:43 +0000798 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
799 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
800 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
801 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000802
Ted Kremenek93668002009-07-17 22:18:43 +0000803 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
804 // automatically freed with the CFG.
805 DeclGroupRef DG(*I);
806 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000807 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000808 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000811 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000812 B = VisitDeclSubExpr(D);
813 }
Mike Stump11289f42009-09-09 15:08:12 +0000814
815 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000816}
Mike Stump11289f42009-09-09 15:08:12 +0000817
Ted Kremenek93668002009-07-17 22:18:43 +0000818/// VisitDeclSubExpr - Utility method to add block-level expressions for
819/// initializers in Decls.
820CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
821 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000822
Ted Kremenek93668002009-07-17 22:18:43 +0000823 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000824
Ted Kremenek93668002009-07-17 22:18:43 +0000825 if (!VD)
826 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000827
Ted Kremenek93668002009-07-17 22:18:43 +0000828 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000829
Ted Kremenek93668002009-07-17 22:18:43 +0000830 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000831 AddStmtChoice::Kind k =
832 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
833 : AddStmtChoice::NotAlwaysAdd;
834 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000835 }
Mike Stump11289f42009-09-09 15:08:12 +0000836
Ted Kremenek93668002009-07-17 22:18:43 +0000837 // If the type of VD is a VLA, then we must process its size expressions.
838 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
839 VA = FindVA(VA->getElementType().getTypePtr()))
840 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000841
Ted Kremenek93668002009-07-17 22:18:43 +0000842 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000843}
844
845CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000846 // We may see an if statement in the middle of a basic block, or it may be the
847 // first statement we are processing. In either case, we create a new basic
848 // block. First, we create the blocks for the then...else statements, and
849 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000850 // middle of a block, we stop processing that block. That block is then the
851 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000852
853 // The block we were proccessing is now finished. Make it the successor
854 // block.
855 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000856 Succ = Block;
Ted Kremenek55957a82009-05-02 00:13:27 +0000857 if (!FinishBlock(Block))
858 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000859 }
Mike Stump31feda52009-07-17 01:31:16 +0000860
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000861 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000862 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000863
Ted Kremenek9aae5132007-08-23 21:42:29 +0000864 if (Stmt* Else = I->getElse()) {
865 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000866
Ted Kremenek9aae5132007-08-23 21:42:29 +0000867 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000868 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000869 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000870 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000871
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000872 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
873 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000874 else if (Block) {
875 if (!FinishBlock(ElseBlock))
876 return 0;
877 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000878 }
Mike Stump31feda52009-07-17 01:31:16 +0000879
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000880 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000881 CFGBlock* ThenBlock;
882 {
883 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000884 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000885 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000886 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000887 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000888
Ted Kremenek1b379512009-04-01 03:52:47 +0000889 if (!ThenBlock) {
890 // We can reach here if the "then" body has all NullStmts.
891 // Create an empty block so we can distinguish between true and false
892 // branches in path-sensitive analyses.
893 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000894 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +0000895 } else if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +0000896 if (!FinishBlock(ThenBlock))
897 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000898 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000899 }
900
Mike Stump31feda52009-07-17 01:31:16 +0000901 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000902 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000903
Ted Kremenek9aae5132007-08-23 21:42:29 +0000904 // Set the terminator of the new block to the If statement.
905 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +0000906
Mike Stump773582d2009-07-23 23:25:26 +0000907 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000908 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +0000909
Ted Kremenek9aae5132007-08-23 21:42:29 +0000910 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000911 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
912 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +0000913
914 // Add the condition as the last statement in the new block. This may create
915 // new blocks as the condition may contain control-flow. Any newly created
916 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000917 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000918
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000919 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
920 // and the condition variable initialization to the CFG.
921 if (VarDecl *VD = I->getConditionVariable()) {
922 if (Expr *Init = VD->getInit()) {
923 autoCreateBlock();
924 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
925 addStmt(Init);
926 }
927 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000928
Ted Kremeneka7bcbde2009-12-23 04:49:01 +0000929 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000930}
Mike Stump31feda52009-07-17 01:31:16 +0000931
932
Ted Kremenek9aae5132007-08-23 21:42:29 +0000933CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +0000934 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000935 //
Mike Stump31feda52009-07-17 01:31:16 +0000936 // NOTE: If a "return" appears in the middle of a block, this means that the
937 // code afterwards is DEAD (unreachable). We still keep a basic block
938 // for that code; a simple "mark-and-sweep" from the entry block will be
939 // able to report such dead blocks.
Ted Kremenek0868eea2009-09-24 18:45:41 +0000940 if (Block)
941 FinishBlock(Block);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000942
943 // Create the new block.
944 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +0000945
Ted Kremenek9aae5132007-08-23 21:42:29 +0000946 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000947 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +0000948
949 // Add the return statement to the block. This may create new blocks if R
950 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000951 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000952}
953
954CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
955 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +0000956 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +0000957 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +0000958
Ted Kremenek93668002009-07-17 22:18:43 +0000959 if (!LabelBlock) // This can happen when the body is empty, i.e.
960 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +0000961
Ted Kremenek93668002009-07-17 22:18:43 +0000962 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenek9aae5132007-08-23 21:42:29 +0000963 LabelMap[ L ] = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000964
965 // Labels partition blocks, so this is the end of the basic block we were
966 // processing (L is the block's label). Because this is label (and we have
967 // already processed the substatement) there is no extra control-flow to worry
968 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +0000969 LabelBlock->setLabel(L);
Ted Kremenek55957a82009-05-02 00:13:27 +0000970 if (!FinishBlock(LabelBlock))
971 return 0;
Mike Stump31feda52009-07-17 01:31:16 +0000972
973 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000974 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +0000975
Ted Kremenek9aae5132007-08-23 21:42:29 +0000976 // This block is now the implicit successor of other blocks.
977 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000978
Ted Kremenek9aae5132007-08-23 21:42:29 +0000979 return LabelBlock;
980}
981
982CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +0000983 // Goto is a control-flow statement. Thus we stop processing the current
984 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +0000985 if (Block)
986 FinishBlock(Block);
987
Ted Kremenek9aae5132007-08-23 21:42:29 +0000988 Block = createBlock(false);
989 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +0000990
991 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000992 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000993
Ted Kremenek9aae5132007-08-23 21:42:29 +0000994 if (I == LabelMap.end())
995 // We will need to backpatch this block later.
996 BackpatchBlocks.push_back(Block);
997 else
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000998 AddSuccessor(Block, I->second);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000999
Mike Stump31feda52009-07-17 01:31:16 +00001000 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001001}
1002
1003CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001004 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001005
Mike Stump014b3ea2009-07-21 01:12:51 +00001006 // "for" is a control-flow statement. Thus we stop processing the current
1007 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001008 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001009 if (!FinishBlock(Block))
1010 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001011 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001012 } else
1013 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001014
Ted Kremenek304a9532010-05-21 20:30:15 +00001015 // Save the current value for the break targets.
1016 // All breaks should go to the code following the loop.
1017 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
1018 BreakTargetBlock = LoopSuccessor;
1019
Mike Stump31feda52009-07-17 01:31:16 +00001020 // Because of short-circuit evaluation, the condition of the loop can span
1021 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1022 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001023 CFGBlock* ExitConditionBlock = createBlock(false);
1024 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001025
Ted Kremenek81e14852007-08-27 19:46:09 +00001026 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001027 ExitConditionBlock->setTerminator(F);
1028
1029 // Now add the actual condition to the condition block. Because the condition
1030 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001031 if (Stmt* C = F->getCond()) {
1032 Block = ExitConditionBlock;
1033 EntryConditionBlock = addStmt(C);
Ted Kremenekec92f942009-12-24 01:49:06 +00001034 assert(Block == EntryConditionBlock);
1035
1036 // If this block contains a condition variable, add both the condition
1037 // variable and initializer to the CFG.
1038 if (VarDecl *VD = F->getConditionVariable()) {
1039 if (Expr *Init = VD->getInit()) {
1040 autoCreateBlock();
1041 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1042 EntryConditionBlock = addStmt(Init);
1043 assert(Block == EntryConditionBlock);
1044 }
1045 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001046
Ted Kremenek55957a82009-05-02 00:13:27 +00001047 if (Block) {
1048 if (!FinishBlock(EntryConditionBlock))
1049 return 0;
1050 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001051 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001052
Mike Stump31feda52009-07-17 01:31:16 +00001053 // The condition block is the implicit successor for the loop body as well as
1054 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001055 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001056
Mike Stump773582d2009-07-23 23:25:26 +00001057 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001058 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001059
Mike Stump773582d2009-07-23 23:25:26 +00001060 if (F->getCond())
1061 KnownVal = TryEvaluateBool(F->getCond());
1062
Ted Kremenek9aae5132007-08-23 21:42:29 +00001063 // Now create the loop body.
1064 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001065 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001066
Ted Kremenek304a9532010-05-21 20:30:15 +00001067 // Save the current values for Block, Succ, and continue targets.
1068 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1069 save_continue(ContinueTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001070
Ted Kremeneke9610502007-08-30 18:39:40 +00001071 // Create a new block to contain the (bottom) of the loop body.
1072 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001073
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001074 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001075 // Generate increment code in its own basic block. This is the target of
1076 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001077 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001078 } else {
1079 // No increment code. Create a special, empty, block that is used as the
1080 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001081 assert(Succ == EntryConditionBlock);
1082 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001083 }
Mike Stump31feda52009-07-17 01:31:16 +00001084
Ted Kremenek902393b2009-04-28 00:51:56 +00001085 // Finish up the increment (or empty) block if it hasn't been already.
1086 if (Block) {
1087 assert(Block == Succ);
Ted Kremenek55957a82009-05-02 00:13:27 +00001088 if (!FinishBlock(Block))
1089 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001090 Block = 0;
1091 }
Mike Stump31feda52009-07-17 01:31:16 +00001092
Ted Kremenek902393b2009-04-28 00:51:56 +00001093 ContinueTargetBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001094
Ted Kremenek902393b2009-04-28 00:51:56 +00001095 // The starting block for the loop increment is the block that should
1096 // represent the 'loop target' for looping back to the start of the loop.
1097 ContinueTargetBlock->setLoopTarget(F);
1098
Mike Stump31feda52009-07-17 01:31:16 +00001099 // Now populate the body block, and in the process create new blocks as we
1100 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001101 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001102
1103 if (!BodyBlock)
Zhongxing Xua778b022009-08-20 02:56:48 +00001104 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek30754282009-07-24 04:47:11 +00001105 else if (Block && !FinishBlock(BodyBlock))
1106 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001107
Ted Kremenek30754282009-07-24 04:47:11 +00001108 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001109 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001110 }
Mike Stump31feda52009-07-17 01:31:16 +00001111
Ted Kremenek30754282009-07-24 04:47:11 +00001112 // Link up the condition block with the code that follows the loop. (the
1113 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001114 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001115
Ted Kremenek9aae5132007-08-23 21:42:29 +00001116 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001117 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001118 if (Stmt* I = F->getInit()) {
1119 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001120 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001121 } else {
1122 // There is no loop initialization. We are thus basically a while loop.
1123 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001124 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001125 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001126 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001127 }
1128}
1129
Ted Kremenek5868ec62010-04-11 17:02:10 +00001130CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1131 if (asc.alwaysAdd()) {
1132 autoCreateBlock();
1133 AppendStmt(Block, M, asc);
1134 }
1135 return Visit(M->getBase(),
1136 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1137 : AddStmtChoice::AsLValueNotAlwaysAdd);
1138}
1139
Ted Kremenek9d56e642008-11-11 17:10:00 +00001140CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1141 // Objective-C fast enumeration 'for' statements:
1142 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1143 //
1144 // for ( Type newVariable in collection_expression ) { statements }
1145 //
1146 // becomes:
1147 //
1148 // prologue:
1149 // 1. collection_expression
1150 // T. jump to loop_entry
1151 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001152 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001153 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1154 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1155 // TB:
1156 // statements
1157 // T. jump to loop_entry
1158 // FB:
1159 // what comes after
1160 //
1161 // and
1162 //
1163 // Type existingItem;
1164 // for ( existingItem in expression ) { statements }
1165 //
1166 // becomes:
1167 //
Mike Stump31feda52009-07-17 01:31:16 +00001168 // the same with newVariable replaced with existingItem; the binding works
1169 // the same except that for one ObjCForCollectionStmt::getElement() returns
1170 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001171 //
Mike Stump31feda52009-07-17 01:31:16 +00001172
Ted Kremenek9d56e642008-11-11 17:10:00 +00001173 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001174
Ted Kremenek9d56e642008-11-11 17:10:00 +00001175 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001176 if (!FinishBlock(Block))
1177 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001178 LoopSuccessor = Block;
1179 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001180 } else
1181 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001182
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001183 // Build the condition blocks.
1184 CFGBlock* ExitConditionBlock = createBlock(false);
1185 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001186
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001187 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001188 ExitConditionBlock->setTerminator(S);
1189
1190 // The last statement in the block should be the ObjCForCollectionStmt, which
1191 // performs the actual binding to 'element' and determines if there are any
1192 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001193 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001194 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001195
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001196 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001197 // generate new blocks as necesary. We DON'T add the statement by default to
1198 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001199 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001200 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001201 if (!FinishBlock(EntryConditionBlock))
1202 return 0;
1203 Block = 0;
1204 }
Mike Stump31feda52009-07-17 01:31:16 +00001205
1206 // The condition block is the implicit successor for the loop body as well as
1207 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001208 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001209
Ted Kremenek9d56e642008-11-11 17:10:00 +00001210 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001211 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001212 // Save the current values for Succ, continue and break targets.
1213 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump31feda52009-07-17 01:31:16 +00001214 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1215
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001216 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001217 ContinueTargetBlock = EntryConditionBlock;
1218
Ted Kremenek93668002009-07-17 22:18:43 +00001219 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001220
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001221 if (!BodyBlock)
1222 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001223 else if (Block) {
1224 if (!FinishBlock(BodyBlock))
1225 return 0;
1226 }
Mike Stump31feda52009-07-17 01:31:16 +00001227
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001228 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001229 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001230 }
Mike Stump31feda52009-07-17 01:31:16 +00001231
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001232 // Link up the condition block with the code that follows the loop.
1233 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001234 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001235
Ted Kremenek9d56e642008-11-11 17:10:00 +00001236 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001237 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001238 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001239}
1240
Ted Kremenek49805452009-05-02 01:49:13 +00001241CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1242 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001243
Ted Kremenek49805452009-05-02 01:49:13 +00001244 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001245 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001246
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001247 // The sync body starts its own basic block. This makes it a little easier
1248 // for diagnostic clients.
1249 if (SyncBlock) {
1250 if (!FinishBlock(SyncBlock))
1251 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001252
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001253 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001254 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001255 }
Mike Stump31feda52009-07-17 01:31:16 +00001256
Ted Kremenek49805452009-05-02 01:49:13 +00001257 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001258 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001259}
Mike Stump31feda52009-07-17 01:31:16 +00001260
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001261CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001262 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001263 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001264}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001265
Ted Kremenek9aae5132007-08-23 21:42:29 +00001266CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001267 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001268
Mike Stump014b3ea2009-07-21 01:12:51 +00001269 // "while" is a control-flow statement. Thus we stop processing the current
1270 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001271 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001272 if (!FinishBlock(Block))
1273 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001274 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001275 } else
1276 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001277
1278 // Because of short-circuit evaluation, the condition of the loop can span
1279 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1280 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001281 CFGBlock* ExitConditionBlock = createBlock(false);
1282 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001283
Ted Kremenek81e14852007-08-27 19:46:09 +00001284 // Set the terminator for the "exit" condition block.
1285 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001286
1287 // Now add the actual condition to the condition block. Because the condition
1288 // itself may contain control-flow, new blocks may be created. Thus we update
1289 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001290 if (Stmt* C = W->getCond()) {
1291 Block = ExitConditionBlock;
1292 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001293 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001294
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001295 // If this block contains a condition variable, add both the condition
1296 // variable and initializer to the CFG.
1297 if (VarDecl *VD = W->getConditionVariable()) {
1298 if (Expr *Init = VD->getInit()) {
1299 autoCreateBlock();
1300 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1301 EntryConditionBlock = addStmt(Init);
1302 assert(Block == EntryConditionBlock);
1303 }
1304 }
1305
Ted Kremenek55957a82009-05-02 00:13:27 +00001306 if (Block) {
1307 if (!FinishBlock(EntryConditionBlock))
1308 return 0;
1309 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001310 }
Mike Stump31feda52009-07-17 01:31:16 +00001311
1312 // The condition block is the implicit successor for the loop body as well as
1313 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001314 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001315
Mike Stump773582d2009-07-23 23:25:26 +00001316 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001317 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001318
Ted Kremenek9aae5132007-08-23 21:42:29 +00001319 // Process the loop body.
1320 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001321 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001322
1323 // Save the current values for Block, Succ, and continue and break targets
1324 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1325 save_continue(ContinueTargetBlock),
1326 save_break(BreakTargetBlock);
Ted Kremenek49936f72009-04-28 03:09:44 +00001327
Mike Stump31feda52009-07-17 01:31:16 +00001328 // Create an empty block to represent the transition block for looping back
1329 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001330 Block = 0;
1331 assert(Succ == EntryConditionBlock);
1332 Succ = createBlock();
1333 Succ->setLoopTarget(W);
Mike Stump31feda52009-07-17 01:31:16 +00001334 ContinueTargetBlock = Succ;
1335
Ted Kremenek9aae5132007-08-23 21:42:29 +00001336 // All breaks should go to the code following the loop.
1337 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001338
Ted Kremenek9aae5132007-08-23 21:42:29 +00001339 // NULL out Block to force lazy instantiation of blocks for the body.
1340 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001341
Ted Kremenek9aae5132007-08-23 21:42:29 +00001342 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001343 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001344
Ted Kremeneke9610502007-08-30 18:39:40 +00001345 if (!BodyBlock)
Zhongxing Xu1a3ec572009-08-20 03:21:49 +00001346 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001347 else if (Block) {
1348 if (!FinishBlock(BodyBlock))
1349 return 0;
1350 }
Mike Stump31feda52009-07-17 01:31:16 +00001351
Ted Kremenek30754282009-07-24 04:47:11 +00001352 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001353 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354 }
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek30754282009-07-24 04:47:11 +00001356 // Link up the condition block with the code that follows the loop. (the
1357 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001358 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001359
1360 // There can be no more statements in the condition block since we loop back
1361 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001362 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001363
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001364 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001365 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001366 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001367}
Mike Stump11289f42009-09-09 15:08:12 +00001368
1369
Ted Kremenek93668002009-07-17 22:18:43 +00001370CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1371 // FIXME: For now we pretend that @catch and the code it contains does not
1372 // exit.
1373 return Block;
1374}
Mike Stump31feda52009-07-17 01:31:16 +00001375
Ted Kremenek93041ba2008-12-09 20:20:09 +00001376CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1377 // FIXME: This isn't complete. We basically treat @throw like a return
1378 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001379
Ted Kremenek0868eea2009-09-24 18:45:41 +00001380 // If we were in the middle of a block we stop processing that block.
Ted Kremenek93668002009-07-17 22:18:43 +00001381 if (Block && !FinishBlock(Block))
1382 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001383
Ted Kremenek93041ba2008-12-09 20:20:09 +00001384 // Create the new block.
1385 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001386
Ted Kremenek93041ba2008-12-09 20:20:09 +00001387 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001388 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001389
1390 // Add the statement to the block. This may create new blocks if S contains
1391 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001392 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001393}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001394
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001395CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001396 // If we were in the middle of a block we stop processing that block.
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001397 if (Block && !FinishBlock(Block))
1398 return 0;
1399
1400 // Create the new block.
1401 Block = createBlock(false);
1402
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001403 if (TryTerminatedBlock)
1404 // The current try statement is the only successor.
1405 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001406 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001407 // otherwise the Exit block is the only successor.
1408 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001409
1410 // Add the statement to the block. This may create new blocks if S contains
1411 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001412 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001413}
1414
Ted Kremenek93668002009-07-17 22:18:43 +00001415CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001416 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001417
Mike Stump8d50b6a2009-07-21 01:27:50 +00001418 // "do...while" is a control-flow statement. Thus we stop processing the
1419 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001420 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001421 if (!FinishBlock(Block))
1422 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001423 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001424 } else
1425 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001426
1427 // Because of short-circuit evaluation, the condition of the loop can span
1428 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1429 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001430 CFGBlock* ExitConditionBlock = createBlock(false);
1431 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001432
Ted Kremenek81e14852007-08-27 19:46:09 +00001433 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001434 ExitConditionBlock->setTerminator(D);
1435
1436 // Now add the actual condition to the condition block. Because the condition
1437 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001438 if (Stmt* C = D->getCond()) {
1439 Block = ExitConditionBlock;
1440 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001441 if (Block) {
1442 if (!FinishBlock(EntryConditionBlock))
1443 return 0;
1444 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001445 }
Mike Stump31feda52009-07-17 01:31:16 +00001446
Ted Kremeneka1523a32008-02-27 07:20:00 +00001447 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001448 Succ = EntryConditionBlock;
1449
Mike Stump773582d2009-07-23 23:25:26 +00001450 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001451 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001452
Ted Kremenek9aae5132007-08-23 21:42:29 +00001453 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001454 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001455 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001456 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001457
Ted Kremenek9aae5132007-08-23 21:42:29 +00001458 // Save the current values for Block, Succ, and continue and break targets
1459 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001460 save_continue(ContinueTargetBlock),
1461 save_break(BreakTargetBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremenek9aae5132007-08-23 21:42:29 +00001463 // All continues within this loop should go to the condition block
Ted Kremenek81e14852007-08-27 19:46:09 +00001464 ContinueTargetBlock = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001465
Ted Kremenek9aae5132007-08-23 21:42:29 +00001466 // All breaks should go to the code following the loop.
1467 BreakTargetBlock = LoopSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001468
Ted Kremenek9aae5132007-08-23 21:42:29 +00001469 // NULL out Block to force lazy instantiation of blocks for the body.
1470 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001471
Ted Kremenek9aae5132007-08-23 21:42:29 +00001472 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001473 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001474
Ted Kremeneke9610502007-08-30 18:39:40 +00001475 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001476 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001477 else if (Block) {
1478 if (!FinishBlock(BodyBlock))
1479 return 0;
1480 }
Mike Stump31feda52009-07-17 01:31:16 +00001481
Ted Kremenek110974d2010-08-17 20:59:56 +00001482 if (!KnownVal.isFalse()) {
1483 // Add an intermediate block between the BodyBlock and the
1484 // ExitConditionBlock to represent the "loop back" transition. Create an
1485 // empty block to represent the transition block for looping back to the
1486 // head of the loop.
1487 // FIXME: Can we do this more efficiently without adding another block?
1488 Block = NULL;
1489 Succ = BodyBlock;
1490 CFGBlock *LoopBackBlock = createBlock();
1491 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001492
Ted Kremenek110974d2010-08-17 20:59:56 +00001493 // Add the loop body entry as a successor to the condition.
1494 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1495 }
1496 else
1497 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001498 }
Mike Stump31feda52009-07-17 01:31:16 +00001499
Ted Kremenek30754282009-07-24 04:47:11 +00001500 // Link up the condition block with the code that follows the loop.
1501 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001502 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001503
1504 // There can be no more statements in the body block(s) since we loop back to
1505 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001506 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001507
Ted Kremenek9aae5132007-08-23 21:42:29 +00001508 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001509 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001510 return BodyBlock;
1511}
1512
1513CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1514 // "continue" is a control-flow statement. Thus we stop processing the
1515 // current block.
Ted Kremenek93668002009-07-17 22:18:43 +00001516 if (Block && !FinishBlock(Block))
Ted Kremenek55957a82009-05-02 00:13:27 +00001517 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001518
Ted Kremenek9aae5132007-08-23 21:42:29 +00001519 // Now create a new block that ends with the continue statement.
1520 Block = createBlock(false);
1521 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001522
Ted Kremenek9aae5132007-08-23 21:42:29 +00001523 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001524 // incomplete AST. This means the CFG cannot be constructed.
1525 if (ContinueTargetBlock)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001526 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek882cf062009-04-07 18:53:24 +00001527 else
1528 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001529
Ted Kremenek9aae5132007-08-23 21:42:29 +00001530 return Block;
1531}
Mike Stump11289f42009-09-09 15:08:12 +00001532
Ted Kremenek0747de62009-07-18 00:47:21 +00001533CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001534 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001535
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001536 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001537 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001538 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001539 }
Mike Stump11289f42009-09-09 15:08:12 +00001540
Ted Kremenek93668002009-07-17 22:18:43 +00001541 // VLA types have expressions that must be evaluated.
1542 if (E->isArgumentType()) {
1543 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1544 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1545 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001546 }
Mike Stump11289f42009-09-09 15:08:12 +00001547
Mike Stump31feda52009-07-17 01:31:16 +00001548 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001549}
Mike Stump11289f42009-09-09 15:08:12 +00001550
Ted Kremenek93668002009-07-17 22:18:43 +00001551/// VisitStmtExpr - Utility method to handle (nested) statement
1552/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001553CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1554 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001555 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001556 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001557 }
Ted Kremenek93668002009-07-17 22:18:43 +00001558 return VisitCompoundStmt(SE->getSubStmt());
1559}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001560
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001561CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001562 // "switch" is a control-flow statement. Thus we stop processing the current
1563 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001564 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001565
Ted Kremenek9aae5132007-08-23 21:42:29 +00001566 if (Block) {
Ted Kremenek55957a82009-05-02 00:13:27 +00001567 if (!FinishBlock(Block))
1568 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001569 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001570 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001571
1572 // Save the current "switch" context.
1573 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001574 save_break(BreakTargetBlock),
1575 save_default(DefaultCaseBlock);
1576
Mike Stump31feda52009-07-17 01:31:16 +00001577 // Set the "default" case to be the block after the switch statement. If the
1578 // switch statement contains a "default:", this value will be overwritten with
1579 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001580 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001581
Ted Kremenek9aae5132007-08-23 21:42:29 +00001582 // Create a new block that will contain the switch statement.
1583 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001584
Ted Kremenek9aae5132007-08-23 21:42:29 +00001585 // Now process the switch body. The code after the switch is the implicit
1586 // successor.
1587 Succ = SwitchSuccessor;
1588 BreakTargetBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001589
1590 // When visiting the body, the case statements should automatically get linked
1591 // up to the switch. We also don't keep a pointer to the body, since all
1592 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001593 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001594 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +00001595 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001596 if (Block) {
1597 if (!FinishBlock(BodyBlock))
1598 return 0;
1599 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001600
Mike Stump31feda52009-07-17 01:31:16 +00001601 // If we have no "default:" case, the default transition is to the code
1602 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001603 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001604
Ted Kremenek81e14852007-08-27 19:46:09 +00001605 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001606 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001607 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001608 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001609 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001610
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001611 // Finally, if the SwitchStmt contains a condition variable, add both the
1612 // SwitchStmt and the condition variable initialization to the CFG.
1613 if (VarDecl *VD = Terminator->getConditionVariable()) {
1614 if (Expr *Init = VD->getInit()) {
1615 autoCreateBlock();
1616 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1617 addStmt(Init);
1618 }
1619 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001620
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001621 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001622}
1623
Ted Kremenek93668002009-07-17 22:18:43 +00001624CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001625 // CaseStmts are essentially labels, so they are the first statement in a
1626 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00001627 CFGBlock *TopBlock = 0, *LastBlock = 0;
1628
1629 if (Stmt *Sub = CS->getSubStmt()) {
1630 // For deeply nested chains of CaseStmts, instead of doing a recursion
1631 // (which can blow out the stack), manually unroll and create blocks
1632 // along the way.
1633 while (isa<CaseStmt>(Sub)) {
1634 CFGBlock *CurrentBlock = createBlock(false);
1635 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00001636
Ted Kremenek60fa6572010-08-04 23:54:30 +00001637 if (TopBlock)
1638 AddSuccessor(LastBlock, CurrentBlock);
1639 else
1640 TopBlock = CurrentBlock;
1641
1642 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1643 LastBlock = CurrentBlock;
1644
1645 CS = cast<CaseStmt>(Sub);
1646 Sub = CS->getSubStmt();
1647 }
1648
1649 addStmt(Sub);
1650 }
Mike Stump11289f42009-09-09 15:08:12 +00001651
Ted Kremenek55e91e82007-08-30 18:48:11 +00001652 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001653 if (!CaseBlock)
1654 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001655
1656 // Cases statements partition blocks, so this is the top of the basic block we
1657 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001658 CaseBlock->setLabel(CS);
1659
Ted Kremenek55957a82009-05-02 00:13:27 +00001660 if (!FinishBlock(CaseBlock))
1661 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001662
1663 // Add this block to the list of successors for the block with the switch
1664 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001665 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001666 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001667
Ted Kremenek9aae5132007-08-23 21:42:29 +00001668 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1669 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001670
Ted Kremenek60fa6572010-08-04 23:54:30 +00001671 if (TopBlock) {
1672 AddSuccessor(LastBlock, CaseBlock);
1673 Succ = TopBlock;
1674 }
1675 else {
1676 // This block is now the implicit successor of other blocks.
1677 Succ = CaseBlock;
1678 }
Mike Stump31feda52009-07-17 01:31:16 +00001679
Ted Kremenek60fa6572010-08-04 23:54:30 +00001680 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001681}
Mike Stump31feda52009-07-17 01:31:16 +00001682
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001683CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001684 if (Terminator->getSubStmt())
1685 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001686
Ted Kremenek654c78f2008-02-13 22:05:39 +00001687 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001688
1689 if (!DefaultCaseBlock)
1690 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001691
1692 // Default statements partition blocks, so this is the top of the basic block
1693 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001694 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001695
Ted Kremenek55957a82009-05-02 00:13:27 +00001696 if (!FinishBlock(DefaultCaseBlock))
1697 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001698
Mike Stump31feda52009-07-17 01:31:16 +00001699 // Unlike case statements, we don't add the default block to the successors
1700 // for the switch statement immediately. This is done when we finish
1701 // processing the switch statement. This allows for the default case
1702 // (including a fall-through to the code after the switch statement) to always
1703 // be the last successor of a switch-terminated block.
1704
Ted Kremenek654c78f2008-02-13 22:05:39 +00001705 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1706 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001707
Ted Kremenek654c78f2008-02-13 22:05:39 +00001708 // This block is now the implicit successor of other blocks.
1709 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001710
1711 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001712}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001713
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001714CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1715 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1716 // current block.
1717 CFGBlock* TrySuccessor = NULL;
1718
1719 if (Block) {
1720 if (!FinishBlock(Block))
1721 return 0;
1722 TrySuccessor = Block;
1723 } else TrySuccessor = Succ;
1724
Mike Stump0bdba6c2010-01-20 01:15:34 +00001725 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001726
1727 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001728 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001729 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001730 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001731
Mike Stump0bdba6c2010-01-20 01:15:34 +00001732 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001733 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1734 // The code after the try is the implicit successor.
1735 Succ = TrySuccessor;
1736 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001737 if (CS->getExceptionDecl() == 0) {
1738 HasCatchAll = true;
1739 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001740 Block = NULL;
1741 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1742 if (CatchBlock == 0)
1743 return 0;
1744 // Add this block to the list of successors for the block with the try
1745 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001746 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001747 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001748 if (!HasCatchAll) {
1749 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001750 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001751 else
Mike Stump845384a2010-01-20 01:30:58 +00001752 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001753 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001754
1755 // The code after the try is the implicit successor.
1756 Succ = TrySuccessor;
1757
Mike Stump845384a2010-01-20 01:30:58 +00001758 // Save the current "try" context.
1759 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1760 TryTerminatedBlock = NewTryTerminatedBlock;
1761
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001762 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001763 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001764 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001765 return Block;
1766}
1767
1768CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1769 // CXXCatchStmt are treated like labels, so they are the first statement in a
1770 // block.
1771
1772 if (CS->getHandlerBlock())
1773 addStmt(CS->getHandlerBlock());
1774
1775 CFGBlock* CatchBlock = Block;
1776 if (!CatchBlock)
1777 CatchBlock = createBlock();
1778
1779 CatchBlock->setLabel(CS);
1780
1781 if (!FinishBlock(CatchBlock))
1782 return 0;
1783
1784 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1785 Block = NULL;
1786
1787 return CatchBlock;
1788}
1789
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001790CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00001791 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001792 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001793 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001794 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001795 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001796 return VisitChildren(C);
1797}
1798
Ted Kremenekeda180e22007-08-28 19:26:49 +00001799CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001800 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001801 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001802
Ted Kremenekeda180e22007-08-28 19:26:49 +00001803 if (!IBlock) {
1804 IBlock = createBlock(false);
1805 cfg->setIndirectGotoBlock(IBlock);
1806 }
Mike Stump31feda52009-07-17 01:31:16 +00001807
Ted Kremenekeda180e22007-08-28 19:26:49 +00001808 // IndirectGoto is a control-flow statement. Thus we stop processing the
1809 // current block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001810 if (Block && !FinishBlock(Block))
1811 return 0;
1812
Ted Kremenekeda180e22007-08-28 19:26:49 +00001813 Block = createBlock(false);
1814 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001815 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001816 return addStmt(I->getTarget());
1817}
1818
Ted Kremenek04cca642007-08-23 21:26:19 +00001819} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001820
Mike Stump31feda52009-07-17 01:31:16 +00001821/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1822/// no successors or predecessors. If this is the first block created in the
1823/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001824CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001825 bool first_block = begin() == end();
1826
1827 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001828 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1829 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1830 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001831
1832 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001833 if (first_block)
1834 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001835
1836 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001837 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001838}
1839
Ted Kremenek889073f2007-08-23 16:51:22 +00001840/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1841/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001842CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001843 bool PruneTriviallyFalse,
Mike Stump04c68512010-01-21 15:20:48 +00001844 bool AddEHEdges, bool AddScopes) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001845 CFGBuilder Builder;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001846 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
1847 AddEHEdges, AddScopes);
Ted Kremenek889073f2007-08-23 16:51:22 +00001848}
1849
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001850//===----------------------------------------------------------------------===//
1851// CFG: Queries for BlkExprs.
1852//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001853
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001854namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001855 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001856}
1857
Ted Kremenekbff98442009-12-23 23:37:10 +00001858static void FindSubExprAssignments(Stmt *S,
1859 llvm::SmallPtrSet<Expr*,50>& Set) {
1860 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001861 return;
Mike Stump31feda52009-07-17 01:31:16 +00001862
Ted Kremenekbff98442009-12-23 23:37:10 +00001863 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001864 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00001865 if (!child)
1866 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001867
Ted Kremenekbff98442009-12-23 23:37:10 +00001868 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001869 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001870
Ted Kremenekbff98442009-12-23 23:37:10 +00001871 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001872 }
1873}
1874
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001875static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1876 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001877
1878 // Look for assignments that are used as subexpressions. These are the only
1879 // assignments that we want to *possibly* register as a block-level
1880 // expression. Basically, if an assignment occurs both in a subexpression and
1881 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001882 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001883
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001884 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001885 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001886 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001887
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001888 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00001889
1890 // Iterate over the statements again on identify the Expr* and Stmt* at the
1891 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001892
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001893 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001894 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump31feda52009-07-17 01:31:16 +00001895
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001896 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001897 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00001898 // expression are really "statements" whose value is never used by
1899 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001900 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001901 continue;
Mike Stump31feda52009-07-17 01:31:16 +00001902 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1903 // Special handling for statement expressions. The last statement in
1904 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001905 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001906 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00001907 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001908 (*M)[C->body_back()] = x;
1909 }
1910 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00001911
Ted Kremenek95a123c2008-01-26 00:03:27 +00001912 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001913 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00001914 }
Mike Stump31feda52009-07-17 01:31:16 +00001915
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001916 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00001917
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001918 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00001919
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001920 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001921 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00001922 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001923 }
1924 }
Mike Stump31feda52009-07-17 01:31:16 +00001925
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001926 return M;
1927}
1928
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001929CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1930 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001931 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00001932
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001933 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001934 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00001935 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001936}
1937
1938unsigned CFG::getNumBlkExprs() {
1939 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1940 return M->size();
1941 else {
1942 // We assume callers interested in the number of BlkExprs will want
1943 // the map constructed if it doesn't already exist.
1944 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1945 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1946 }
1947}
1948
Ted Kremenek6065ef62008-04-28 18:00:46 +00001949//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00001950// Cleanup: CFG dstor.
1951//===----------------------------------------------------------------------===//
1952
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001953CFG::~CFG() {
1954 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1955}
Mike Stump31feda52009-07-17 01:31:16 +00001956
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001957//===----------------------------------------------------------------------===//
1958// CFG pretty printing
1959//===----------------------------------------------------------------------===//
1960
Ted Kremenek7e776b12007-08-22 18:22:34 +00001961namespace {
1962
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001963class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001964 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1965 StmtMapTy StmtMap;
1966 signed CurrentBlock;
1967 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00001968 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001969public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00001970
Chris Lattnerc61089a2009-06-30 01:26:17 +00001971 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1972 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001973 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1974 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001975 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001976 BI != BEnd; ++BI, ++j )
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001977 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001978 }
1979 }
Mike Stump31feda52009-07-17 01:31:16 +00001980
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001981 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00001982
Chris Lattnerc61089a2009-06-30 01:26:17 +00001983 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001984 void setBlockID(signed i) { CurrentBlock = i; }
1985 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenek2d470fc2008-09-13 05:16:45 +00001987 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump31feda52009-07-17 01:31:16 +00001988
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001989 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001990
1991 if (I == StmtMap.end())
1992 return false;
Mike Stump31feda52009-07-17 01:31:16 +00001993
1994 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00001995 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00001996 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001997 }
Mike Stump31feda52009-07-17 01:31:16 +00001998
Ted Kremenek60983dc2010-01-19 20:52:05 +00001999 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002000 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002001 }
2002};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002003} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002004
Chris Lattnerc61089a2009-06-30 01:26:17 +00002005
2006namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002007class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002008 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002009
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002010 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002011 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002012 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002013public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002014 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002015 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002016 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002017
Ted Kremenek9aae5132007-08-23 21:42:29 +00002018 void VisitIfStmt(IfStmt* I) {
2019 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002020 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002021 }
Mike Stump31feda52009-07-17 01:31:16 +00002022
Ted Kremenek9aae5132007-08-23 21:42:29 +00002023 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002024 void VisitStmt(Stmt* Terminator) {
2025 Terminator->printPretty(OS, Helper, Policy);
2026 }
2027
Ted Kremenek9aae5132007-08-23 21:42:29 +00002028 void VisitForStmt(ForStmt* F) {
2029 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002030 if (F->getInit())
2031 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002032 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002033 if (Stmt* C = F->getCond())
2034 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002035 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002036 if (F->getInc())
2037 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002038 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002039 }
Mike Stump31feda52009-07-17 01:31:16 +00002040
Ted Kremenek9aae5132007-08-23 21:42:29 +00002041 void VisitWhileStmt(WhileStmt* W) {
2042 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002043 if (Stmt* C = W->getCond())
2044 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002045 }
Mike Stump31feda52009-07-17 01:31:16 +00002046
Ted Kremenek9aae5132007-08-23 21:42:29 +00002047 void VisitDoStmt(DoStmt* D) {
2048 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002049 if (Stmt* C = D->getCond())
2050 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002051 }
Mike Stump31feda52009-07-17 01:31:16 +00002052
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002053 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002054 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002055 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002056 }
Mike Stump31feda52009-07-17 01:31:16 +00002057
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002058 void VisitCXXTryStmt(CXXTryStmt* CS) {
2059 OS << "try ...";
2060 }
2061
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002062 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002063 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002064 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002065 }
Mike Stump31feda52009-07-17 01:31:16 +00002066
Ted Kremenek391f94a2007-08-31 22:29:13 +00002067 void VisitChooseExpr(ChooseExpr* C) {
2068 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002069 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002070 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002071 }
Mike Stump31feda52009-07-17 01:31:16 +00002072
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002073 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2074 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002075 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002076 }
Mike Stump31feda52009-07-17 01:31:16 +00002077
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002078 void VisitBinaryOperator(BinaryOperator* B) {
2079 if (!B->isLogicalOp()) {
2080 VisitExpr(B);
2081 return;
2082 }
Mike Stump31feda52009-07-17 01:31:16 +00002083
Douglas Gregor7de59662009-05-29 20:38:28 +00002084 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002085
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002086 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002087 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002088 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002089 return;
John McCalle3027922010-08-25 11:45:40 +00002090 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002091 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002092 return;
2093 default:
2094 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002095 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002096 }
Mike Stump31feda52009-07-17 01:31:16 +00002097
Ted Kremenek12687ff2007-08-27 21:54:41 +00002098 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002099 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002100 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002101};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002102} // end anonymous namespace
2103
Mike Stump31feda52009-07-17 01:31:16 +00002104
Chris Lattnerc61089a2009-06-30 01:26:17 +00002105static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002106 const CFGElement &E) {
2107 Stmt *Terminator = E;
2108
2109 if (E.asStartScope()) {
2110 OS << "start scope\n";
2111 return;
2112 }
2113 if (E.asEndScope()) {
2114 OS << "end scope\n";
2115 return;
2116 }
2117
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002118 if (Helper) {
2119 // special printing for statement-expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002120 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002121 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump31feda52009-07-17 01:31:16 +00002122
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002123 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenekcc778062007-08-31 22:47:06 +00002124 OS << "({ ... ; ";
Ted Kremenek6d845f02007-10-29 20:41:04 +00002125 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenekcc778062007-08-31 22:47:06 +00002126 OS << " })\n";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002127 return;
2128 }
2129 }
Mike Stump31feda52009-07-17 01:31:16 +00002130
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002131 // special printing for comma expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002132 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
John McCalle3027922010-08-25 11:45:40 +00002133 if (B->getOpcode() == BO_Comma) {
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002134 OS << "... , ";
2135 Helper->handledStmt(B->getRHS(),OS);
2136 OS << '\n';
2137 return;
Mike Stump31feda52009-07-17 01:31:16 +00002138 }
2139 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002140 }
Mike Stump31feda52009-07-17 01:31:16 +00002141
Chris Lattnerc61089a2009-06-30 01:26:17 +00002142 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002143
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002144 // Expressions need a newline.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002145 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002146}
Mike Stump31feda52009-07-17 01:31:16 +00002147
Chris Lattnerc61089a2009-06-30 01:26:17 +00002148static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2149 const CFGBlock& B,
2150 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002151
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002152 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002153
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002154 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002155 OS << "\n [ B" << B.getBlockID();
2156
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002157 if (&B == &cfg->getEntry())
2158 OS << " (ENTRY) ]\n";
2159 else if (&B == &cfg->getExit())
2160 OS << " (EXIT) ]\n";
2161 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002162 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002163 else
2164 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002165
Ted Kremenek71eca012007-08-29 23:20:49 +00002166 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002167 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002168
2169 if (print_edges)
2170 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002171
Mike Stump92244b02010-01-19 22:00:14 +00002172 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002173 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002174 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002175 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002176 C->getLHS()->printPretty(OS, Helper,
2177 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002178 if (C->getRHS()) {
2179 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002180 C->getRHS()->printPretty(OS, Helper,
2181 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002182 }
Mike Stump92244b02010-01-19 22:00:14 +00002183 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002184 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002185 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002186 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002187 if (CS->getExceptionDecl())
2188 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2189 0);
2190 else
2191 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002192 OS << ")";
2193
2194 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002195 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002196
Ted Kremenek71eca012007-08-29 23:20:49 +00002197 OS << ":\n";
2198 }
Mike Stump31feda52009-07-17 01:31:16 +00002199
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002200 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002201 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002202
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002203 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2204 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002205
Ted Kremenek71eca012007-08-29 23:20:49 +00002206 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002207 if (print_edges)
2208 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002209
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002210 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002211
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002212 if (Helper)
2213 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002214
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002215 print_stmt(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002216 }
Mike Stump31feda52009-07-17 01:31:16 +00002217
Ted Kremenek71eca012007-08-29 23:20:49 +00002218 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002219 if (B.getTerminator()) {
2220 if (print_edges)
2221 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002222
Ted Kremenek71eca012007-08-29 23:20:49 +00002223 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002224
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002225 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002226
Chris Lattnerc61089a2009-06-30 01:26:17 +00002227 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2228 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002229 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002230 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002231 }
Mike Stump31feda52009-07-17 01:31:16 +00002232
Ted Kremenek71eca012007-08-29 23:20:49 +00002233 if (print_edges) {
2234 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002235 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002236 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002237
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002238 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2239 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002240
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002241 if (i == 8 || (i-8) == 0)
2242 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002243
Ted Kremenek71eca012007-08-29 23:20:49 +00002244 OS << " B" << (*I)->getBlockID();
2245 }
Mike Stump31feda52009-07-17 01:31:16 +00002246
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002247 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002248
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002249 // Print the successors of this block.
2250 OS << " Successors (" << B.succ_size() << "):";
2251 i = 0;
2252
2253 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2254 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002255
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002256 if (i == 8 || (i-8) % 10 == 0)
2257 OS << "\n ";
2258
Mike Stump0d76d072009-07-20 23:24:15 +00002259 if (*I)
2260 OS << " B" << (*I)->getBlockID();
2261 else
2262 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002263 }
Mike Stump31feda52009-07-17 01:31:16 +00002264
Ted Kremenek71eca012007-08-29 23:20:49 +00002265 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002266 }
Mike Stump31feda52009-07-17 01:31:16 +00002267}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002268
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002269
2270/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002271void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002272
2273/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002274void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2275 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002276
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002277 // Print the entry block.
2278 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002279
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002280 // Iterate through the CFGBlocks and print them one by one.
2281 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2282 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002283 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002284 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002285
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002286 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002287 }
Mike Stump31feda52009-07-17 01:31:16 +00002288
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002289 // Print the exit block.
2290 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002291 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002292}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002293
2294/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002295void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2296 print(llvm::errs(), cfg, LO);
2297}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002298
2299/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2300/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002301void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2302 const LangOptions &LO) const {
2303 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002304 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002305}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002306
Ted Kremenek15647632008-01-30 23:02:42 +00002307/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002308void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002309 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002310 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002311 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2312}
2313
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002314Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002315
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002316 if (!Terminator)
2317 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002318
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002319 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002320
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002321 switch (Terminator->getStmtClass()) {
2322 default:
2323 break;
Mike Stump31feda52009-07-17 01:31:16 +00002324
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002325 case Stmt::ForStmtClass:
2326 E = cast<ForStmt>(Terminator)->getCond();
2327 break;
Mike Stump31feda52009-07-17 01:31:16 +00002328
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002329 case Stmt::WhileStmtClass:
2330 E = cast<WhileStmt>(Terminator)->getCond();
2331 break;
Mike Stump31feda52009-07-17 01:31:16 +00002332
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002333 case Stmt::DoStmtClass:
2334 E = cast<DoStmt>(Terminator)->getCond();
2335 break;
Mike Stump31feda52009-07-17 01:31:16 +00002336
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002337 case Stmt::IfStmtClass:
2338 E = cast<IfStmt>(Terminator)->getCond();
2339 break;
Mike Stump31feda52009-07-17 01:31:16 +00002340
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002341 case Stmt::ChooseExprClass:
2342 E = cast<ChooseExpr>(Terminator)->getCond();
2343 break;
Mike Stump31feda52009-07-17 01:31:16 +00002344
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002345 case Stmt::IndirectGotoStmtClass:
2346 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2347 break;
Mike Stump31feda52009-07-17 01:31:16 +00002348
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002349 case Stmt::SwitchStmtClass:
2350 E = cast<SwitchStmt>(Terminator)->getCond();
2351 break;
Mike Stump31feda52009-07-17 01:31:16 +00002352
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002353 case Stmt::ConditionalOperatorClass:
2354 E = cast<ConditionalOperator>(Terminator)->getCond();
2355 break;
Mike Stump31feda52009-07-17 01:31:16 +00002356
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002357 case Stmt::BinaryOperatorClass: // '&&' and '||'
2358 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002359 break;
Mike Stump31feda52009-07-17 01:31:16 +00002360
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002361 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002362 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002363 }
Mike Stump31feda52009-07-17 01:31:16 +00002364
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002365 return E ? E->IgnoreParens() : NULL;
2366}
2367
Ted Kremenek92137a32008-05-16 16:06:00 +00002368bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002369
Ted Kremenek92137a32008-05-16 16:06:00 +00002370 if (!Terminator)
2371 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002372
Ted Kremenek92137a32008-05-16 16:06:00 +00002373 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002374
Ted Kremenek92137a32008-05-16 16:06:00 +00002375 switch (Terminator->getStmtClass()) {
2376 default:
2377 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002378
2379 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002380 case Stmt::WhileStmtClass:
2381 case Stmt::DoStmtClass:
2382 case Stmt::IfStmtClass:
2383 case Stmt::ChooseExprClass:
2384 case Stmt::ConditionalOperatorClass:
2385 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002386 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002387 }
Mike Stump31feda52009-07-17 01:31:16 +00002388
Ted Kremenek92137a32008-05-16 16:06:00 +00002389 return E ? E->IgnoreParens() : NULL;
2390}
2391
Ted Kremenek15647632008-01-30 23:02:42 +00002392
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002393//===----------------------------------------------------------------------===//
2394// CFG Graphviz Visualization
2395//===----------------------------------------------------------------------===//
2396
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002397
2398#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002399static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002400#endif
2401
Chris Lattnerc61089a2009-06-30 01:26:17 +00002402void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002403#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002404 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002405 GraphHelper = &H;
2406 llvm::ViewGraph(this,"CFG");
2407 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002408#endif
2409}
2410
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002411namespace llvm {
2412template<>
2413struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002414
2415 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2416
2417 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002418
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002419#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002420 std::string OutSStr;
2421 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002422 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002423 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002424
2425 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2426
2427 // Process string output to make it nicer...
2428 for (unsigned i = 0; i != OutStr.length(); ++i)
2429 if (OutStr[i] == '\n') { // Left justify
2430 OutStr[i] = '\\';
2431 OutStr.insert(OutStr.begin()+i+1, 'l');
2432 }
Mike Stump31feda52009-07-17 01:31:16 +00002433
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002434 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002435#else
2436 return "";
2437#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002438 }
2439};
2440} // end namespace llvm