blob: 912d1edc3c0d94d22303193bfb97e21bcaea3c4f [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
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000055/// LocalScope - Node in tree of local scopes created for C++ implicit
56/// destructor calls generation. It contains list of automatic variables
57/// declared in the scope and link to position in previous scope this scope
58/// began in.
59///
60/// The process of creating local scopes is as follows:
61/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
62/// - Before processing statements in scope (e.g. CompoundStmt) create
63/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
64/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000065/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000066/// at this VarDecl,
67/// - For every normal (without jump) end of scope add to CFGBlock destructors
68/// for objects in the current scope,
69/// - For every jump add to CFGBlock destructors for objects
70/// between CFGBuilder::ScopePos and local scope position saved for jump
71/// target. Thanks to C++ restrictions on goto jumps we can be sure that
72/// jump target position will be on the path to root from CFGBuilder::ScopePos
73/// (adding any variable that doesn't need constructor to be called to
74/// LocalScope can break this assumption),
75///
76class LocalScope {
77public:
78 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
79
80 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000081 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000082 class const_iterator {
83 const LocalScope* Scope;
84
85 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
86 /// Invalid iterator (with null Scope) has VarIter equal to 0.
87 unsigned VarIter;
88
89 public:
90 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
91 /// Incrementing invalid iterator is allowed and will result in invalid
92 /// iterator.
93 const_iterator()
94 : Scope(NULL), VarIter(0) {}
95
96 /// Create valid iterator. In case when S.Prev is an invalid iterator and
97 /// I is equal to 0, this will create invalid iterator.
98 const_iterator(const LocalScope& S, unsigned I)
99 : Scope(&S), VarIter(I) {
100 // Iterator to "end" of scope is not allowed. Handle it by going up
101 // in scopes tree possibly up to invalid iterator in the root.
102 if (VarIter == 0 && Scope)
103 *this = Scope->Prev;
104 }
105
106 VarDecl* const* operator->() const {
107 assert (Scope && "Dereferencing invalid iterator is not allowed");
108 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
109 return &Scope->Vars[VarIter - 1];
110 }
111 VarDecl* operator*() const {
112 return *this->operator->();
113 }
114
115 const_iterator& operator++() {
116 if (!Scope)
117 return *this;
118
119 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
120 --VarIter;
121 if (VarIter == 0)
122 *this = Scope->Prev;
123 return *this;
124 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000125 const_iterator operator++(int) {
126 const_iterator P = *this;
127 ++*this;
128 return P;
129 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000130
131 bool operator==(const const_iterator& rhs) const {
132 return Scope == rhs.Scope && VarIter == rhs.VarIter;
133 }
134 bool operator!=(const const_iterator& rhs) const {
135 return !(*this == rhs);
136 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000137
138 operator bool() const {
139 return *this != const_iterator();
140 }
141
142 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000143 };
144
145 friend class const_iterator;
146
147private:
148 /// Automatic variables in order of declaration.
149 AutomaticVarsTy Vars;
150 /// Iterator to variable in previous scope that was declared just before
151 /// begin of this scope.
152 const_iterator Prev;
153
154public:
155 /// Constructs empty scope linked to previous scope in specified place.
156 LocalScope(const_iterator P)
157 : Vars()
158 , Prev(P) {}
159
160 /// Begin of scope in direction of CFG building (backwards).
161 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000162
163 void addVar(VarDecl* VD) {
164 Vars.push_back(VD);
165 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000166};
167
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000168/// distance - Calculates distance from this to L. L must be reachable from this
169/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
170/// number of scopes between this and L.
171int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
172 int D = 0;
173 const_iterator F = *this;
174 while (F.Scope != L.Scope) {
175 assert (F != const_iterator()
176 && "L iterator is not reachable from F iterator.");
177 D += F.VarIter;
178 F = F.Scope->Prev;
179 }
180 D += F.VarIter - L.VarIter;
181 return D;
182}
183
184/// BlockScopePosPair - Structure for specifying position in CFG during its
185/// build process. It consists of CFGBlock that specifies position in CFG graph
186/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000187struct BlockScopePosPair {
188 BlockScopePosPair() {}
189 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
190 : Block(B), ScopePos(S) {}
191
192 CFGBlock* Block;
193 LocalScope::const_iterator ScopePos;
194};
195
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000196/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000197/// The builder is stateful: an instance of the builder should be used to only
198/// construct a single CFG.
199///
200/// Example usage:
201///
202/// CFGBuilder builder;
203/// CFG* cfg = builder.BuildAST(stmt1);
204///
Mike Stump31feda52009-07-17 01:31:16 +0000205/// CFG construction is done via a recursive walk of an AST. We actually parse
206/// the AST in reverse order so that the successor of a basic block is
207/// constructed prior to its predecessor. This allows us to nicely capture
208/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000209///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000210class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000211 typedef BlockScopePosPair JumpTarget;
212 typedef BlockScopePosPair JumpSource;
213
Mike Stump0d76d072009-07-20 23:24:15 +0000214 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000215 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000216
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000217 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000218 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000219 JumpTarget ContinueJumpTarget;
220 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000221 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000222 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000223 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000224
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000225 // Current position in local scope.
226 LocalScope::const_iterator ScopePos;
227
228 // LabelMap records the mapping from Label expressions to their jump targets.
229 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000230 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000231
232 // A list of blocks that end with a "goto" that must be backpatched to their
233 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000234 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000235 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000236
Ted Kremenekeda180e22007-08-28 19:26:49 +0000237 // A list of labels whose address has been taken (for indirect gotos).
238 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
239 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000240
Zhongxing Xud38fb842010-09-16 03:28:18 +0000241 bool badCFG;
242 CFG::BuildOptions BuildOpts;
243
Mike Stump31feda52009-07-17 01:31:16 +0000244public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000245 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
246 Block(NULL), Succ(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000247 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000248 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000249
Ted Kremenek9aae5132007-08-23 21:42:29 +0000250 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000251 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000252 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000253
Ted Kremenek93668002009-07-17 22:18:43 +0000254private:
255 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000256 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
257 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
258 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000259 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000260 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
261 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
262 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000263 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000264 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000265 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000266 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000267 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000268 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000269 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000270 CFGBlock *VisitDeclStmt(DeclStmt *DS);
271 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek21822592009-07-17 18:20:32 +0000272 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
273 CFGBlock *VisitDoStmt(DoStmt *D);
274 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000275 CFGBlock *VisitGotoStmt(GotoStmt* G);
276 CFGBlock *VisitIfStmt(IfStmt *I);
277 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
278 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000279 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000280 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
281 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
282 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
283 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
284 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
285 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000286 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
287 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000288 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
289 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000290
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000291 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
292 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000293 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000294
Ted Kremenek6065ef62008-04-28 18:00:46 +0000295 // NYS == Not Yet Supported
296 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000297 badCFG = true;
298 return Block;
299 }
Mike Stump31feda52009-07-17 01:31:16 +0000300
Ted Kremenek93668002009-07-17 22:18:43 +0000301 void autoCreateBlock() { if (!Block) Block = createBlock(); }
302 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000303
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000304 CFGBlock *addStmt(Stmt *S) {
305 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000306 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000307
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000308 void AppendStmt(CFGBlock *B, Stmt *S,
309 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
310 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000311 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000312
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000313 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
314 B->addSuccessor(S, cfg->getBumpVectorContext());
315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek963cc312009-07-24 06:55:42 +0000317 /// TryResult - a class representing a variant over the values
318 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
319 /// and is used by the CFGBuilder to decide if a branch condition
320 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000321 class TryResult {
322 int X;
323 public:
324 TryResult(bool b) : X(b ? 1 : 0) {}
325 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000326
Ted Kremenek30754282009-07-24 04:47:11 +0000327 bool isTrue() const { return X == 1; }
328 bool isFalse() const { return X == 0; }
329 bool isKnown() const { return X >= 0; }
330 void negate() {
331 assert(isKnown());
332 X ^= 0x1;
333 }
334 };
Mike Stump11289f42009-09-09 15:08:12 +0000335
Mike Stump773582d2009-07-23 23:25:26 +0000336 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
337 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek30754282009-07-24 04:47:11 +0000338 TryResult TryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000339 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000340 return TryResult();
341
Mike Stump773582d2009-07-23 23:25:26 +0000342 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000343 if (!S->isTypeDependent() && !S->isValueDependent() &&
344 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000345 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000346
347 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000348 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000349};
Mike Stump31feda52009-07-17 01:31:16 +0000350
Douglas Gregor4619e432008-12-05 23:32:09 +0000351// FIXME: Add support for dependent-sized array types in C++?
352// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000353static VariableArrayType* FindVA(Type* t) {
354 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
355 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
356 if (vat->getSizeExpr())
357 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000358
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000359 t = vt->getElementType().getTypePtr();
360 }
Mike Stump31feda52009-07-17 01:31:16 +0000361
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000362 return 0;
363}
Mike Stump31feda52009-07-17 01:31:16 +0000364
365/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
366/// arbitrary statement. Examples include a single expression or a function
367/// body (compound statement). The ownership of the returned CFG is
368/// transferred to the caller. If CFG construction fails, this method returns
369/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000370CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000371 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000372
Mike Stump0d76d072009-07-20 23:24:15 +0000373 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000374 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000375 if (!Statement)
376 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000377
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000378 BuildOpts = BO;
379 if (!C->getLangOptions().CPlusPlus)
380 BuildOpts.AddImplicitDtors = false;
Mike Stump31feda52009-07-17 01:31:16 +0000381
382 // Create an empty block that will serve as the exit block for the CFG. Since
383 // this is the first block added to the CFG, it will be implicitly registered
384 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000385 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000386 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000387 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000388
Ted Kremenek9aae5132007-08-23 21:42:29 +0000389 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000390 CFGBlock *B = addStmt(Statement);
391
392 if (badCFG)
393 return NULL;
394
395 if (B)
396 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000397
398 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
399 // FIXME: Add code for base initializers and member initializers.
400 (void)CD;
401 }
Mike Stump31feda52009-07-17 01:31:16 +0000402
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000403 // Backpatch the gotos whose label -> block mappings we didn't know when we
404 // encountered them.
405 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
406 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000407
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000408 CFGBlock* B = I->Block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000409 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
410 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000411
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000412 // If there is no target for the goto, then we are looking at an
413 // incomplete AST. Handle this by not registering a successor.
414 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000415
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000416 JumpTarget JT = LI->second;
417 AddSuccessor(B, JT.Block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000418 }
419
420 // Add successors to the Indirect Goto Dispatch block (if we have one).
421 if (CFGBlock* B = cfg->getIndirectGotoBlock())
422 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
423 E = AddressTakenLabels.end(); I != E; ++I ) {
424
425 // Lookup the target block.
426 LabelMapTy::iterator LI = LabelMap.find(*I);
427
428 // If there is no target block that contains label, then we are looking
429 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000430 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000431
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000432 AddSuccessor(B, LI->second.Block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000433 }
Mike Stump31feda52009-07-17 01:31:16 +0000434
Mike Stump31feda52009-07-17 01:31:16 +0000435 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000436 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000437
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000438 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000439}
Mike Stump31feda52009-07-17 01:31:16 +0000440
Ted Kremenek9aae5132007-08-23 21:42:29 +0000441/// createBlock - Used to lazily create blocks that are connected
442/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000443CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000444 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000445 if (add_successor && Succ)
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000446 AddSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000447 return B;
448}
Mike Stump31feda52009-07-17 01:31:16 +0000449
Ted Kremenek93668002009-07-17 22:18:43 +0000450/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000451/// blocks for ternary operators, &&, and ||. We also process "," and
452/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000453CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000454tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000455 if (!S) {
456 badCFG = true;
457 return 0;
458 }
Ted Kremenek93668002009-07-17 22:18:43 +0000459 switch (S->getStmtClass()) {
460 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000461 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000462
463 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000464 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000465
Ted Kremenek93668002009-07-17 22:18:43 +0000466 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000467 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000468
Ted Kremenek93668002009-07-17 22:18:43 +0000469 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000470 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000471
Ted Kremenek93668002009-07-17 22:18:43 +0000472 case Stmt::BreakStmtClass:
473 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000474
Ted Kremenek93668002009-07-17 22:18:43 +0000475 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000476 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000477 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000478
Ted Kremenek93668002009-07-17 22:18:43 +0000479 case Stmt::CaseStmtClass:
480 return VisitCaseStmt(cast<CaseStmt>(S));
481
482 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000483 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000484
Ted Kremenek93668002009-07-17 22:18:43 +0000485 case Stmt::CompoundStmtClass:
486 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000487
Ted Kremenek93668002009-07-17 22:18:43 +0000488 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000489 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000490
Ted Kremenek93668002009-07-17 22:18:43 +0000491 case Stmt::ContinueStmtClass:
492 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenekb27378c2010-01-19 20:40:33 +0000494 case Stmt::CXXCatchStmtClass:
495 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
496
Ted Kremenek82bfc862010-08-28 00:19:02 +0000497 case Stmt::CXXExprWithTemporariesClass: {
498 // FIXME: Handle temporaries. For now, just visit the subexpression
499 // so we don't artificially create extra blocks.
Ted Kremenek128d04d2010-08-31 18:47:34 +0000500 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000501 }
502
Zhongxing Xu7e612172010-04-13 09:38:01 +0000503 case Stmt::CXXMemberCallExprClass:
504 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
505
Ted Kremenekb27378c2010-01-19 20:40:33 +0000506 case Stmt::CXXThrowExprClass:
507 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000508
Ted Kremenekb27378c2010-01-19 20:40:33 +0000509 case Stmt::CXXTryStmtClass:
510 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000511
Ted Kremenek93668002009-07-17 22:18:43 +0000512 case Stmt::DeclStmtClass:
513 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000514
Ted Kremenek93668002009-07-17 22:18:43 +0000515 case Stmt::DefaultStmtClass:
516 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000517
Ted Kremenek93668002009-07-17 22:18:43 +0000518 case Stmt::DoStmtClass:
519 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000520
Ted Kremenek93668002009-07-17 22:18:43 +0000521 case Stmt::ForStmtClass:
522 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000523
Ted Kremenek93668002009-07-17 22:18:43 +0000524 case Stmt::GotoStmtClass:
525 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000526
Ted Kremenek93668002009-07-17 22:18:43 +0000527 case Stmt::IfStmtClass:
528 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000529
Ted Kremenek93668002009-07-17 22:18:43 +0000530 case Stmt::IndirectGotoStmtClass:
531 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000532
Ted Kremenek93668002009-07-17 22:18:43 +0000533 case Stmt::LabelStmtClass:
534 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000535
Ted Kremenek5868ec62010-04-11 17:02:10 +0000536 case Stmt::MemberExprClass:
537 return VisitMemberExpr(cast<MemberExpr>(S), asc);
538
Ted Kremenek93668002009-07-17 22:18:43 +0000539 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000540 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
541
Ted Kremenek93668002009-07-17 22:18:43 +0000542 case Stmt::ObjCAtSynchronizedStmtClass:
543 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000544
Ted Kremenek93668002009-07-17 22:18:43 +0000545 case Stmt::ObjCAtThrowStmtClass:
546 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000547
Ted Kremenek93668002009-07-17 22:18:43 +0000548 case Stmt::ObjCAtTryStmtClass:
549 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000550
Ted Kremenek93668002009-07-17 22:18:43 +0000551 case Stmt::ObjCForCollectionStmtClass:
552 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000553
Ted Kremenek93668002009-07-17 22:18:43 +0000554 case Stmt::ParenExprClass:
555 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000556 goto tryAgain;
557
Ted Kremenek93668002009-07-17 22:18:43 +0000558 case Stmt::NullStmtClass:
559 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000560
Ted Kremenek93668002009-07-17 22:18:43 +0000561 case Stmt::ReturnStmtClass:
562 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000563
Ted Kremenek93668002009-07-17 22:18:43 +0000564 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000565 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000566
Ted Kremenek93668002009-07-17 22:18:43 +0000567 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000568 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000569
Ted Kremenek93668002009-07-17 22:18:43 +0000570 case Stmt::SwitchStmtClass:
571 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000572
Ted Kremenek93668002009-07-17 22:18:43 +0000573 case Stmt::WhileStmtClass:
574 return VisitWhileStmt(cast<WhileStmt>(S));
575 }
576}
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000578CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
579 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000580 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000581 AppendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000582 }
Mike Stump11289f42009-09-09 15:08:12 +0000583
Ted Kremenek93668002009-07-17 22:18:43 +0000584 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000585}
Mike Stump31feda52009-07-17 01:31:16 +0000586
Ted Kremenek93668002009-07-17 22:18:43 +0000587/// VisitChildren - Visit the children of a Stmt.
588CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
589 CFGBlock *B = Block;
Mike Stumpd8ba7a22009-02-26 08:00:25 +0000590 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek93668002009-07-17 22:18:43 +0000591 E = Terminator->child_end(); I != E; ++I) {
592 if (*I) B = Visit(*I);
593 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000594 return B;
595}
Mike Stump11289f42009-09-09 15:08:12 +0000596
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000597CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
598 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000599 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000600
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000601 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000602 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000603 AppendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000604 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000605
Ted Kremenek9aae5132007-08-23 21:42:29 +0000606 return Block;
607}
Mike Stump11289f42009-09-09 15:08:12 +0000608
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000609CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
610 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000611 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000612 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000613 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000615 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000616 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000617
Ted Kremenek93668002009-07-17 22:18:43 +0000618 // create the block evaluating the LHS
619 CFGBlock* LHSBlock = createBlock(false);
620 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Ted Kremenek93668002009-07-17 22:18:43 +0000622 // create the block evaluating the RHS
623 Succ = ConfluenceBlock;
624 Block = NULL;
625 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000626
627 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000628 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000629 return 0;
630 }
631 else {
632 // Create an empty block for cases where the RHS doesn't require
633 // any explicit statements in the CFG.
634 RHSBlock = createBlock();
635 }
Mike Stump11289f42009-09-09 15:08:12 +0000636
Mike Stump773582d2009-07-23 23:25:26 +0000637 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000638 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000639 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000640 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000641
Ted Kremenek93668002009-07-17 22:18:43 +0000642 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000643 if (B->getOpcode() == BO_LOr) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000644 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
645 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000646 } else {
John McCalle3027922010-08-25 11:45:40 +0000647 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000648 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
649 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000650 }
Mike Stump11289f42009-09-09 15:08:12 +0000651
Ted Kremenek93668002009-07-17 22:18:43 +0000652 // Generate the blocks for evaluating the LHS.
653 Block = LHSBlock;
654 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +0000655 }
John McCalle3027922010-08-25 11:45:40 +0000656 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +0000657 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000658 AppendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000659 addStmt(B->getRHS());
660 return addStmt(B->getLHS());
661 }
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000662 else if (B->isAssignmentOp()) {
663 if (asc.alwaysAdd()) {
664 autoCreateBlock();
665 AppendStmt(Block, B, asc);
666 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000667
Ted Kremenek8abff772010-09-14 01:13:32 +0000668 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
669 // create a new block, then we should return RBlock. Otherwise
670 // we'll incorrectly return NULL.
671 CFGBlock *RBlock = Visit(B->getRHS());
672 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
673 return LBlock ? LBlock : RBlock;
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000674 }
Mike Stump11289f42009-09-09 15:08:12 +0000675
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000676 return VisitStmt(B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000677}
678
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000679CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
680 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +0000681 autoCreateBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000682 AppendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +0000683 }
684 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000685}
686
Ted Kremenek93668002009-07-17 22:18:43 +0000687CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
688 // "break" is a control-flow statement. Thus we stop processing the current
689 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000690 if (badCFG)
691 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000692
Ted Kremenek93668002009-07-17 22:18:43 +0000693 // Now create a new block that ends with the break statement.
694 Block = createBlock(false);
695 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000696
Ted Kremenek93668002009-07-17 22:18:43 +0000697 // If there is no target for the break, then we are looking at an incomplete
698 // AST. This means that the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000699 if (BreakJumpTarget.Block) {
700 AddSuccessor(Block, BreakJumpTarget.Block);
701 } else
Ted Kremenek93668002009-07-17 22:18:43 +0000702 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +0000703
704
Ted Kremenek9aae5132007-08-23 21:42:29 +0000705 return Block;
706}
Mike Stump11289f42009-09-09 15:08:12 +0000707
Mike Stump04c68512010-01-21 15:20:48 +0000708static bool CanThrow(Expr *E) {
709 QualType Ty = E->getType();
710 if (Ty->isFunctionPointerType())
711 Ty = Ty->getAs<PointerType>()->getPointeeType();
712 else if (Ty->isBlockPointerType())
713 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000714
Mike Stump04c68512010-01-21 15:20:48 +0000715 const FunctionType *FT = Ty->getAs<FunctionType>();
716 if (FT) {
717 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
718 if (Proto->hasEmptyExceptionSpec())
719 return false;
720 }
721 return true;
722}
723
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000724CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000725 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +0000726 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000727 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000728 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +0000729 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000730
Mike Stump04c68512010-01-21 15:20:48 +0000731 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000732
733 // Languages without exceptions are assumed to not throw.
734 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000735 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +0000736 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +0000737 }
738
739 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +0000740 if (FD->hasAttr<NoReturnAttr>())
741 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +0000742 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +0000743 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +0000744 }
Mike Stump8c5d7992009-07-25 21:26:53 +0000745
Mike Stump04c68512010-01-21 15:20:48 +0000746 if (!CanThrow(C->getCallee()))
747 AddEHEdge = false;
748
Zhongxing Xu41cdf582010-06-03 06:23:18 +0000749 if (!NoReturn && !AddEHEdge) {
750 if (asc.asLValue())
751 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
752 else
753 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Mike Stump92244b02010-01-19 22:00:14 +0000756 if (Block) {
757 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000758 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +0000759 return 0;
760 }
Mike Stump11289f42009-09-09 15:08:12 +0000761
Mike Stump92244b02010-01-19 22:00:14 +0000762 Block = createBlock(!NoReturn);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000763 AppendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +0000764
Mike Stump92244b02010-01-19 22:00:14 +0000765 if (NoReturn) {
766 // Wire this to the exit block directly.
767 AddSuccessor(Block, &cfg->getExit());
768 }
Mike Stump04c68512010-01-21 15:20:48 +0000769 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +0000770 // Add exceptional edges.
771 if (TryTerminatedBlock)
772 AddSuccessor(Block, TryTerminatedBlock);
773 else
774 AddSuccessor(Block, &cfg->getExit());
775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Mike Stump8c5d7992009-07-25 21:26:53 +0000777 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +0000778}
Ted Kremenek9aae5132007-08-23 21:42:29 +0000779
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000780CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
781 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +0000782 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000783 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000784 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000785 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000786
Ted Kremenek5868ec62010-04-11 17:02:10 +0000787 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
788 : AddStmtChoice::AlwaysAdd;
789
Ted Kremenek21822592009-07-17 18:20:32 +0000790 Succ = ConfluenceBlock;
791 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000792 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000793 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000794 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000795
Ted Kremenek21822592009-07-17 18:20:32 +0000796 Succ = ConfluenceBlock;
797 Block = NULL;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000798 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000799 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +0000800 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000801
Ted Kremenek21822592009-07-17 18:20:32 +0000802 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +0000803 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000804 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000805 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
806 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +0000807 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +0000808 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +0000809}
Mike Stump11289f42009-09-09 15:08:12 +0000810
811
812CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
813 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +0000814
815 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
816 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +0000817 // If we hit a segment of code just containing ';' (NullStmts), we can
818 // get a null block back. In such cases, just use the LastBlock
819 if (CFGBlock *newBlock = addStmt(*I))
820 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +0000821
Ted Kremenekce499c22009-08-27 23:16:26 +0000822 if (badCFG)
823 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000824 }
Mike Stump92244b02010-01-19 22:00:14 +0000825
Ted Kremenek93668002009-07-17 22:18:43 +0000826 return LastBlock;
827}
Mike Stump11289f42009-09-09 15:08:12 +0000828
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000829CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
830 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +0000831 // Create the confluence block that will "merge" the results of the ternary
832 // expression.
833 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000834 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000835 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000836 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Ted Kremenek5868ec62010-04-11 17:02:10 +0000838 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
839 : AddStmtChoice::AlwaysAdd;
840
Ted Kremenek51d40b02009-07-17 18:15:54 +0000841 // Create a block for the LHS expression if there is an LHS expression. A
842 // GCC extension allows LHS to be NULL, causing the condition to be the
843 // value that is returned instead.
844 // e.g: x ?: y is shorthand for: x ? x : y;
845 Succ = ConfluenceBlock;
846 Block = NULL;
847 CFGBlock* LHSBlock = NULL;
848 if (C->getLHS()) {
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000849 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000850 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000851 return 0;
852 Block = NULL;
853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Ted Kremenek51d40b02009-07-17 18:15:54 +0000855 // Create the block for the RHS expression.
856 Succ = ConfluenceBlock;
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000857 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000858 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +0000859 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Ted Kremenek51d40b02009-07-17 18:15:54 +0000861 // Create the block that will contain the condition.
862 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +0000863
Mike Stump773582d2009-07-23 23:25:26 +0000864 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +0000865 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +0000866 if (LHSBlock) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000867 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +0000868 } else {
Ted Kremenek30754282009-07-24 04:47:11 +0000869 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +0000870 // If we know the condition is false, add NULL as the successor for
871 // the block containing the condition. In this case, the confluence
872 // block will have just one predecessor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000873 AddSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +0000874 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +0000875 } else {
876 // If we have no LHS expression, add the ConfluenceBlock as a direct
877 // successor for the block containing the condition. Moreover, we need to
878 // reverse the order of the predecessors in the ConfluenceBlock because
879 // the RHSBlock will have been added to the succcessors already, and we
880 // want the first predecessor to the the block containing the expression
881 // for the case when the ternary expression evaluates to true.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000882 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek30754282009-07-24 04:47:11 +0000883 assert(ConfluenceBlock->pred_size() == 2);
Mike Stump0d76d072009-07-20 23:24:15 +0000884 std::reverse(ConfluenceBlock->pred_begin(),
885 ConfluenceBlock->pred_end());
886 }
Ted Kremenek51d40b02009-07-17 18:15:54 +0000887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000889 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +0000890 Block->setTerminator(C);
891 return addStmt(C->getCond());
892}
893
Ted Kremenek93668002009-07-17 22:18:43 +0000894CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
895 autoCreateBlock();
Mike Stump31feda52009-07-17 01:31:16 +0000896
Ted Kremenek93668002009-07-17 22:18:43 +0000897 if (DS->isSingleDecl()) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000898 AppendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +0000899 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekf6998822008-02-26 00:22:58 +0000900 }
Mike Stump11289f42009-09-09 15:08:12 +0000901
Ted Kremenek93668002009-07-17 22:18:43 +0000902 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000903
Ted Kremenek93668002009-07-17 22:18:43 +0000904 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
905 typedef llvm::SmallVector<Decl*,10> BufTy;
906 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +0000907
Ted Kremenek93668002009-07-17 22:18:43 +0000908 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
909 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
910 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
911 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +0000912
Ted Kremenek93668002009-07-17 22:18:43 +0000913 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
914 // automatically freed with the CFG.
915 DeclGroupRef DG(*I);
916 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +0000917 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +0000918 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +0000919
Ted Kremenek93668002009-07-17 22:18:43 +0000920 // Append the fake DeclStmt to block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000921 AppendStmt(Block, DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +0000922 B = VisitDeclSubExpr(D);
923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
925 return B;
Ted Kremenek93668002009-07-17 22:18:43 +0000926}
Mike Stump11289f42009-09-09 15:08:12 +0000927
Ted Kremenek93668002009-07-17 22:18:43 +0000928/// VisitDeclSubExpr - Utility method to add block-level expressions for
929/// initializers in Decls.
930CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
931 assert(Block);
Ted Kremenekf6998822008-02-26 00:22:58 +0000932
Ted Kremenek93668002009-07-17 22:18:43 +0000933 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump11289f42009-09-09 15:08:12 +0000934
Ted Kremenek93668002009-07-17 22:18:43 +0000935 if (!VD)
936 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000937
Ted Kremenek93668002009-07-17 22:18:43 +0000938 Expr *Init = VD->getInit();
Mike Stump11289f42009-09-09 15:08:12 +0000939
Ted Kremenek93668002009-07-17 22:18:43 +0000940 if (Init) {
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +0000941 AddStmtChoice::Kind k =
942 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
943 : AddStmtChoice::NotAlwaysAdd;
944 Visit(Init, AddStmtChoice(k));
Ted Kremenek93668002009-07-17 22:18:43 +0000945 }
Mike Stump11289f42009-09-09 15:08:12 +0000946
Ted Kremenek93668002009-07-17 22:18:43 +0000947 // If the type of VD is a VLA, then we must process its size expressions.
948 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
949 VA = FindVA(VA->getElementType().getTypePtr()))
950 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000951
Ted Kremenek93668002009-07-17 22:18:43 +0000952 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000953}
954
955CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +0000956 // We may see an if statement in the middle of a basic block, or it may be the
957 // first statement we are processing. In either case, we create a new basic
958 // block. First, we create the blocks for the then...else statements, and
959 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +0000960 // middle of a block, we stop processing that block. That block is then the
961 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +0000962
963 // The block we were proccessing is now finished. Make it the successor
964 // block.
965 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +0000966 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000967 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000968 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000969 }
Mike Stump31feda52009-07-17 01:31:16 +0000970
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000971 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000972 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +0000973
Ted Kremenek9aae5132007-08-23 21:42:29 +0000974 if (Stmt* Else = I->getElse()) {
975 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000976
Ted Kremenek9aae5132007-08-23 21:42:29 +0000977 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +0000978 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000979 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000980 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +0000981
Ted Kremenekbbad8ce2007-08-30 18:13:31 +0000982 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
983 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +0000984 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000985 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +0000986 return 0;
987 }
Ted Kremenek9aae5132007-08-23 21:42:29 +0000988 }
Mike Stump31feda52009-07-17 01:31:16 +0000989
Ted Kremenek0bcdc982009-07-17 18:04:55 +0000990 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000991 CFGBlock* ThenBlock;
992 {
993 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +0000994 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000995 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +0000996 Block = NULL;
Ted Kremenek93668002009-07-17 22:18:43 +0000997 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +0000998
Ted Kremenek1b379512009-04-01 03:52:47 +0000999 if (!ThenBlock) {
1000 // We can reach here if the "then" body has all NullStmts.
1001 // Create an empty block so we can distinguish between true and false
1002 // branches in path-sensitive analyses.
1003 ThenBlock = createBlock(false);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001004 AddSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001005 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001006 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001007 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001008 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001009 }
1010
Mike Stump31feda52009-07-17 01:31:16 +00001011 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001012 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001013
Ted Kremenek9aae5132007-08-23 21:42:29 +00001014 // Set the terminator of the new block to the If statement.
1015 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001016
Mike Stump773582d2009-07-23 23:25:26 +00001017 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001018 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001019
Ted Kremenek9aae5132007-08-23 21:42:29 +00001020 // Now add the successors.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001021 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1022 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001023
1024 // Add the condition as the last statement in the new block. This may create
1025 // new blocks as the condition may contain control-flow. Any newly created
1026 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001027 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001028
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001029 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1030 // and the condition variable initialization to the CFG.
1031 if (VarDecl *VD = I->getConditionVariable()) {
1032 if (Expr *Init = VD->getInit()) {
1033 autoCreateBlock();
1034 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1035 addStmt(Init);
1036 }
1037 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001038
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001039 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001040}
Mike Stump31feda52009-07-17 01:31:16 +00001041
1042
Ted Kremenek9aae5132007-08-23 21:42:29 +00001043CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001044 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001045 //
Mike Stump31feda52009-07-17 01:31:16 +00001046 // NOTE: If a "return" appears in the middle of a block, this means that the
1047 // code afterwards is DEAD (unreachable). We still keep a basic block
1048 // for that code; a simple "mark-and-sweep" from the entry block will be
1049 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001050
1051 // Create the new block.
1052 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001053
Ted Kremenek9aae5132007-08-23 21:42:29 +00001054 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001055 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001056
1057 // Add the return statement to the block. This may create new blocks if R
1058 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001059 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001060}
1061
1062CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1063 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001064 addStmt(L->getSubStmt());
Ted Kremenekcab47bd2008-03-15 07:45:02 +00001065 CFGBlock* LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001066
Ted Kremenek93668002009-07-17 22:18:43 +00001067 if (!LabelBlock) // This can happen when the body is empty, i.e.
1068 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001069
Ted Kremenek93668002009-07-17 22:18:43 +00001070 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001071 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001072
1073 // Labels partition blocks, so this is the end of the basic block we were
1074 // processing (L is the block's label). Because this is label (and we have
1075 // already processed the substatement) there is no extra control-flow to worry
1076 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001077 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001078 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001079 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001080
1081 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001082 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001083
Ted Kremenek9aae5132007-08-23 21:42:29 +00001084 // This block is now the implicit successor of other blocks.
1085 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001086
Ted Kremenek9aae5132007-08-23 21:42:29 +00001087 return LabelBlock;
1088}
1089
1090CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001091 // Goto is a control-flow statement. Thus we stop processing the current
1092 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001093
Ted Kremenek9aae5132007-08-23 21:42:29 +00001094 Block = createBlock(false);
1095 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001096
1097 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001098 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001099
Ted Kremenek9aae5132007-08-23 21:42:29 +00001100 if (I == LabelMap.end())
1101 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001102 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1103 else {
1104 JumpTarget JT = I->second;
1105 AddSuccessor(Block, JT.Block);
1106 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001107
Mike Stump31feda52009-07-17 01:31:16 +00001108 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001109}
1110
1111CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001112 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001113
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001114 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1115
Mike Stump014b3ea2009-07-21 01:12:51 +00001116 // "for" is a control-flow statement. Thus we stop processing the current
1117 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001118 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001119 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001120 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001121 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001122 } else
1123 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001124
Ted Kremenek304a9532010-05-21 20:30:15 +00001125 // Save the current value for the break targets.
1126 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001127 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
1128 BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001129
Mike Stump31feda52009-07-17 01:31:16 +00001130 // Because of short-circuit evaluation, the condition of the loop can span
1131 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1132 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001133 CFGBlock* ExitConditionBlock = createBlock(false);
1134 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001135
Ted Kremenek81e14852007-08-27 19:46:09 +00001136 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001137 ExitConditionBlock->setTerminator(F);
1138
1139 // Now add the actual condition to the condition block. Because the condition
1140 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001141 if (Stmt* C = F->getCond()) {
1142 Block = ExitConditionBlock;
1143 EntryConditionBlock = addStmt(C);
Ted Kremenek7b31a612010-09-15 07:01:20 +00001144 assert(Block == EntryConditionBlock ||
1145 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001146
1147 // If this block contains a condition variable, add both the condition
1148 // variable and initializer to the CFG.
1149 if (VarDecl *VD = F->getConditionVariable()) {
1150 if (Expr *Init = VD->getInit()) {
1151 autoCreateBlock();
1152 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1153 EntryConditionBlock = addStmt(Init);
1154 assert(Block == EntryConditionBlock);
1155 }
1156 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001157
Ted Kremenek55957a82009-05-02 00:13:27 +00001158 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001159 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001160 return 0;
1161 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001162 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001163
Mike Stump31feda52009-07-17 01:31:16 +00001164 // The condition block is the implicit successor for the loop body as well as
1165 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001166 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001167
Mike Stump773582d2009-07-23 23:25:26 +00001168 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001169 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001170
Mike Stump773582d2009-07-23 23:25:26 +00001171 if (F->getCond())
1172 KnownVal = TryEvaluateBool(F->getCond());
1173
Ted Kremenek9aae5132007-08-23 21:42:29 +00001174 // Now create the loop body.
1175 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001176 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001177
Ted Kremenek304a9532010-05-21 20:30:15 +00001178 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001179 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1180 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001181
Ted Kremeneke9610502007-08-30 18:39:40 +00001182 // Create a new block to contain the (bottom) of the loop body.
1183 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001184
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001185 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001186 // Generate increment code in its own basic block. This is the target of
1187 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001188 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001189 } else {
1190 // No increment code. Create a special, empty, block that is used as the
1191 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001192 assert(Succ == EntryConditionBlock);
1193 Succ = createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001194 }
Mike Stump31feda52009-07-17 01:31:16 +00001195
Ted Kremenek902393b2009-04-28 00:51:56 +00001196 // Finish up the increment (or empty) block if it hasn't been already.
1197 if (Block) {
1198 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001199 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001200 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001201 Block = 0;
1202 }
Mike Stump31feda52009-07-17 01:31:16 +00001203
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001204 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001205
Ted Kremenek902393b2009-04-28 00:51:56 +00001206 // The starting block for the loop increment is the block that should
1207 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001208 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001209
Mike Stump31feda52009-07-17 01:31:16 +00001210 // Now populate the body block, and in the process create new blocks as we
1211 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001212 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001213
1214 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001215 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001216 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001217 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001218
Ted Kremenek30754282009-07-24 04:47:11 +00001219 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001220 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001221 }
Mike Stump31feda52009-07-17 01:31:16 +00001222
Ted Kremenek30754282009-07-24 04:47:11 +00001223 // Link up the condition block with the code that follows the loop. (the
1224 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001225 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001226
Ted Kremenek9aae5132007-08-23 21:42:29 +00001227 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001228 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001229 if (Stmt* I = F->getInit()) {
1230 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001231 return addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001232 } else {
1233 // There is no loop initialization. We are thus basically a while loop.
1234 // NULL out Block to force lazy block construction.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001235 Block = NULL;
Ted Kremeneka1523a32008-02-27 07:20:00 +00001236 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001237 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001238 }
1239}
1240
Ted Kremenek5868ec62010-04-11 17:02:10 +00001241CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1242 if (asc.alwaysAdd()) {
1243 autoCreateBlock();
1244 AppendStmt(Block, M, asc);
1245 }
1246 return Visit(M->getBase(),
1247 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1248 : AddStmtChoice::AsLValueNotAlwaysAdd);
1249}
1250
Ted Kremenek9d56e642008-11-11 17:10:00 +00001251CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1252 // Objective-C fast enumeration 'for' statements:
1253 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1254 //
1255 // for ( Type newVariable in collection_expression ) { statements }
1256 //
1257 // becomes:
1258 //
1259 // prologue:
1260 // 1. collection_expression
1261 // T. jump to loop_entry
1262 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001263 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001264 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1265 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1266 // TB:
1267 // statements
1268 // T. jump to loop_entry
1269 // FB:
1270 // what comes after
1271 //
1272 // and
1273 //
1274 // Type existingItem;
1275 // for ( existingItem in expression ) { statements }
1276 //
1277 // becomes:
1278 //
Mike Stump31feda52009-07-17 01:31:16 +00001279 // the same with newVariable replaced with existingItem; the binding works
1280 // the same except that for one ObjCForCollectionStmt::getElement() returns
1281 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001282 //
Mike Stump31feda52009-07-17 01:31:16 +00001283
Ted Kremenek9d56e642008-11-11 17:10:00 +00001284 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001285
Ted Kremenek9d56e642008-11-11 17:10:00 +00001286 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001287 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001288 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001289 LoopSuccessor = Block;
1290 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001291 } else
1292 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001293
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001294 // Build the condition blocks.
1295 CFGBlock* ExitConditionBlock = createBlock(false);
1296 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001297
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001298 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001299 ExitConditionBlock->setTerminator(S);
1300
1301 // The last statement in the block should be the ObjCForCollectionStmt, which
1302 // performs the actual binding to 'element' and determines if there are any
1303 // more items in the collection.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001304 AppendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001305 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001306
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001307 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001308 // generate new blocks as necesary. We DON'T add the statement by default to
1309 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001310 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001311 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001312 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001313 return 0;
1314 Block = 0;
1315 }
Mike Stump31feda52009-07-17 01:31:16 +00001316
1317 // The condition block is the implicit successor for the loop body as well as
1318 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001319 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001320
Ted Kremenek9d56e642008-11-11 17:10:00 +00001321 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001322 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001323 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001324 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1325 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1326 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001327
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001328 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1329 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001330
Ted Kremenek93668002009-07-17 22:18:43 +00001331 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001332
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001333 if (!BodyBlock)
1334 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001335 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001336 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001337 return 0;
1338 }
Mike Stump31feda52009-07-17 01:31:16 +00001339
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001340 // This new body block is a successor to our "exit" condition block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001341 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001342 }
Mike Stump31feda52009-07-17 01:31:16 +00001343
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001344 // Link up the condition block with the code that follows the loop.
1345 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001346 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001347
Ted Kremenek9d56e642008-11-11 17:10:00 +00001348 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001349 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001350 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001351}
1352
Ted Kremenek49805452009-05-02 01:49:13 +00001353CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1354 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek49805452009-05-02 01:49:13 +00001356 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001357 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001358
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001359 // The sync body starts its own basic block. This makes it a little easier
1360 // for diagnostic clients.
1361 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001362 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001363 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001364
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001365 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001366 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001367 }
Mike Stump31feda52009-07-17 01:31:16 +00001368
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001369 // Add the @synchronized to the CFG.
1370 autoCreateBlock();
1371 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1372
Ted Kremenek49805452009-05-02 01:49:13 +00001373 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001374 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001375}
Mike Stump31feda52009-07-17 01:31:16 +00001376
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001377CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001378 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001379 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001380}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001381
Ted Kremenek9aae5132007-08-23 21:42:29 +00001382CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001383 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001384
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001385 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1386
Mike Stump014b3ea2009-07-21 01:12:51 +00001387 // "while" is a control-flow statement. Thus we stop processing the current
1388 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001389 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001390 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001391 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001392 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001393 } else
1394 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001395
1396 // Because of short-circuit evaluation, the condition of the loop can span
1397 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1398 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001399 CFGBlock* ExitConditionBlock = createBlock(false);
1400 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001401
Ted Kremenek81e14852007-08-27 19:46:09 +00001402 // Set the terminator for the "exit" condition block.
1403 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001404
1405 // Now add the actual condition to the condition block. Because the condition
1406 // itself may contain control-flow, new blocks may be created. Thus we update
1407 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001408 if (Stmt* C = W->getCond()) {
1409 Block = ExitConditionBlock;
1410 EntryConditionBlock = addStmt(C);
Ted Kremenek49936f72009-04-28 03:09:44 +00001411 assert(Block == EntryConditionBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001412
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001413 // If this block contains a condition variable, add both the condition
1414 // variable and initializer to the CFG.
1415 if (VarDecl *VD = W->getConditionVariable()) {
1416 if (Expr *Init = VD->getInit()) {
1417 autoCreateBlock();
1418 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1419 EntryConditionBlock = addStmt(Init);
1420 assert(Block == EntryConditionBlock);
1421 }
1422 }
1423
Ted Kremenek55957a82009-05-02 00:13:27 +00001424 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001425 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001426 return 0;
1427 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001428 }
Mike Stump31feda52009-07-17 01:31:16 +00001429
1430 // The condition block is the implicit successor for the loop body as well as
1431 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001432 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001433
Mike Stump773582d2009-07-23 23:25:26 +00001434 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001435 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001436
Ted Kremenek9aae5132007-08-23 21:42:29 +00001437 // Process the loop body.
1438 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001439 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001440
1441 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001442 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1443 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1444 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001445
Mike Stump31feda52009-07-17 01:31:16 +00001446 // Create an empty block to represent the transition block for looping back
1447 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001448 Block = 0;
1449 assert(Succ == EntryConditionBlock);
1450 Succ = createBlock();
1451 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001452 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001453
Ted Kremenek9aae5132007-08-23 21:42:29 +00001454 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001455 BreakJumpTarget = JumpTarget(LoopSuccessor, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001456
Ted Kremenek9aae5132007-08-23 21:42:29 +00001457 // NULL out Block to force lazy instantiation of blocks for the body.
1458 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001459
Ted Kremenek9aae5132007-08-23 21:42:29 +00001460 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001461 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremeneke9610502007-08-30 18:39:40 +00001463 if (!BodyBlock)
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001464 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001465 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001466 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001467 return 0;
1468 }
Mike Stump31feda52009-07-17 01:31:16 +00001469
Ted Kremenek30754282009-07-24 04:47:11 +00001470 // Add the loop body entry as a successor to the condition.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001471 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001472 }
Mike Stump31feda52009-07-17 01:31:16 +00001473
Ted Kremenek30754282009-07-24 04:47:11 +00001474 // Link up the condition block with the code that follows the loop. (the
1475 // false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001476 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001477
1478 // There can be no more statements in the condition block since we loop back
1479 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001480 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001481
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001482 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001483 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001484 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001485}
Mike Stump11289f42009-09-09 15:08:12 +00001486
1487
Ted Kremenek93668002009-07-17 22:18:43 +00001488CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1489 // FIXME: For now we pretend that @catch and the code it contains does not
1490 // exit.
1491 return Block;
1492}
Mike Stump31feda52009-07-17 01:31:16 +00001493
Ted Kremenek93041ba2008-12-09 20:20:09 +00001494CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1495 // FIXME: This isn't complete. We basically treat @throw like a return
1496 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001497
Ted Kremenek0868eea2009-09-24 18:45:41 +00001498 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001499 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001500 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001501
Ted Kremenek93041ba2008-12-09 20:20:09 +00001502 // Create the new block.
1503 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001504
Ted Kremenek93041ba2008-12-09 20:20:09 +00001505 // The Exit block is the only successor.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001506 AddSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001507
1508 // Add the statement to the block. This may create new blocks if S contains
1509 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001510 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001511}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001512
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001513CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001514 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001515 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001516 return 0;
1517
1518 // Create the new block.
1519 Block = createBlock(false);
1520
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001521 if (TryTerminatedBlock)
1522 // The current try statement is the only successor.
1523 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001524 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001525 // otherwise the Exit block is the only successor.
1526 AddSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001527
1528 // Add the statement to the block. This may create new blocks if S contains
1529 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001530 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001531}
1532
Ted Kremenek93668002009-07-17 22:18:43 +00001533CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001534 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001535
Mike Stump8d50b6a2009-07-21 01:27:50 +00001536 // "do...while" is a control-flow statement. Thus we stop processing the
1537 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001538 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001539 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001540 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001541 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001542 } else
1543 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001544
1545 // Because of short-circuit evaluation, the condition of the loop can span
1546 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1547 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001548 CFGBlock* ExitConditionBlock = createBlock(false);
1549 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001550
Ted Kremenek81e14852007-08-27 19:46:09 +00001551 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001552 ExitConditionBlock->setTerminator(D);
1553
1554 // Now add the actual condition to the condition block. Because the condition
1555 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001556 if (Stmt* C = D->getCond()) {
1557 Block = ExitConditionBlock;
1558 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001559 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001560 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001561 return 0;
1562 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001563 }
Mike Stump31feda52009-07-17 01:31:16 +00001564
Ted Kremeneka1523a32008-02-27 07:20:00 +00001565 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001566 Succ = EntryConditionBlock;
1567
Mike Stump773582d2009-07-23 23:25:26 +00001568 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001569 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001570
Ted Kremenek9aae5132007-08-23 21:42:29 +00001571 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00001572 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001573 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001574 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001575
Ted Kremenek9aae5132007-08-23 21:42:29 +00001576 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001577 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1578 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1579 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001580
Ted Kremenek9aae5132007-08-23 21:42:29 +00001581 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001582 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001583
Ted Kremenek9aae5132007-08-23 21:42:29 +00001584 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001585 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001586
Ted Kremenek9aae5132007-08-23 21:42:29 +00001587 // NULL out Block to force lazy instantiation of blocks for the body.
1588 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001589
Ted Kremenek9aae5132007-08-23 21:42:29 +00001590 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001591 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001592
Ted Kremeneke9610502007-08-30 18:39:40 +00001593 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00001594 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00001595 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001596 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001597 return 0;
1598 }
Mike Stump31feda52009-07-17 01:31:16 +00001599
Ted Kremenek110974d2010-08-17 20:59:56 +00001600 if (!KnownVal.isFalse()) {
1601 // Add an intermediate block between the BodyBlock and the
1602 // ExitConditionBlock to represent the "loop back" transition. Create an
1603 // empty block to represent the transition block for looping back to the
1604 // head of the loop.
1605 // FIXME: Can we do this more efficiently without adding another block?
1606 Block = NULL;
1607 Succ = BodyBlock;
1608 CFGBlock *LoopBackBlock = createBlock();
1609 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00001610
Ted Kremenek110974d2010-08-17 20:59:56 +00001611 // Add the loop body entry as a successor to the condition.
1612 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1613 }
1614 else
1615 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001616 }
Mike Stump31feda52009-07-17 01:31:16 +00001617
Ted Kremenek30754282009-07-24 04:47:11 +00001618 // Link up the condition block with the code that follows the loop.
1619 // (the false branch).
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001620 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001621
1622 // There can be no more statements in the body block(s) since we loop back to
1623 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001624 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001625
Ted Kremenek9aae5132007-08-23 21:42:29 +00001626 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001627 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001628 return BodyBlock;
1629}
1630
1631CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1632 // "continue" is a control-flow statement. Thus we stop processing the
1633 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001634 if (badCFG)
1635 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001636
Ted Kremenek9aae5132007-08-23 21:42:29 +00001637 // Now create a new block that ends with the continue statement.
1638 Block = createBlock(false);
1639 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00001640
Ted Kremenek9aae5132007-08-23 21:42:29 +00001641 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00001642 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001643 if (ContinueJumpTarget.Block) {
1644 AddSuccessor(Block, ContinueJumpTarget.Block);
1645 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00001646 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00001647
Ted Kremenek9aae5132007-08-23 21:42:29 +00001648 return Block;
1649}
Mike Stump11289f42009-09-09 15:08:12 +00001650
Ted Kremenek0747de62009-07-18 00:47:21 +00001651CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001652 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001653
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001654 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001655 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001656 AppendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00001657 }
Mike Stump11289f42009-09-09 15:08:12 +00001658
Ted Kremenek93668002009-07-17 22:18:43 +00001659 // VLA types have expressions that must be evaluated.
1660 if (E->isArgumentType()) {
1661 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1662 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1663 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00001664 }
Mike Stump11289f42009-09-09 15:08:12 +00001665
Mike Stump31feda52009-07-17 01:31:16 +00001666 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001667}
Mike Stump11289f42009-09-09 15:08:12 +00001668
Ted Kremenek93668002009-07-17 22:18:43 +00001669/// VisitStmtExpr - Utility method to handle (nested) statement
1670/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001671CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1672 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00001673 autoCreateBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001674 AppendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00001675 }
Ted Kremenek93668002009-07-17 22:18:43 +00001676 return VisitCompoundStmt(SE->getSubStmt());
1677}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001678
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001679CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00001680 // "switch" is a control-flow statement. Thus we stop processing the current
1681 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001682 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001683
Ted Kremenek9aae5132007-08-23 21:42:29 +00001684 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001685 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001686 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001687 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001688 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001689
1690 // Save the current "switch" context.
1691 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00001692 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001693 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00001694
Mike Stump31feda52009-07-17 01:31:16 +00001695 // Set the "default" case to be the block after the switch statement. If the
1696 // switch statement contains a "default:", this value will be overwritten with
1697 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00001698 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00001699
Ted Kremenek9aae5132007-08-23 21:42:29 +00001700 // Create a new block that will contain the switch statement.
1701 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001702
Ted Kremenek9aae5132007-08-23 21:42:29 +00001703 // Now process the switch body. The code after the switch is the implicit
1704 // successor.
1705 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001706 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001707
1708 // When visiting the body, the case statements should automatically get linked
1709 // up to the switch. We also don't keep a pointer to the body, since all
1710 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001711 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00001712 Block = NULL;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001713 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00001714 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001715 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001716 return 0;
1717 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001718
Mike Stump31feda52009-07-17 01:31:16 +00001719 // If we have no "default:" case, the default transition is to the code
1720 // following the switch body.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001721 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenek81e14852007-08-27 19:46:09 +00001723 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001724 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001725 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00001726 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001727 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001728
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001729 // Finally, if the SwitchStmt contains a condition variable, add both the
1730 // SwitchStmt and the condition variable initialization to the CFG.
1731 if (VarDecl *VD = Terminator->getConditionVariable()) {
1732 if (Expr *Init = VD->getInit()) {
1733 autoCreateBlock();
1734 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1735 addStmt(Init);
1736 }
1737 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001738
Ted Kremenek8b5dc122009-12-24 00:39:26 +00001739 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001740}
1741
Ted Kremenek93668002009-07-17 22:18:43 +00001742CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00001743 // CaseStmts are essentially labels, so they are the first statement in a
1744 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00001745 CFGBlock *TopBlock = 0, *LastBlock = 0;
1746
1747 if (Stmt *Sub = CS->getSubStmt()) {
1748 // For deeply nested chains of CaseStmts, instead of doing a recursion
1749 // (which can blow out the stack), manually unroll and create blocks
1750 // along the way.
1751 while (isa<CaseStmt>(Sub)) {
1752 CFGBlock *CurrentBlock = createBlock(false);
1753 CurrentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00001754
Ted Kremenek60fa6572010-08-04 23:54:30 +00001755 if (TopBlock)
1756 AddSuccessor(LastBlock, CurrentBlock);
1757 else
1758 TopBlock = CurrentBlock;
1759
1760 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1761 LastBlock = CurrentBlock;
1762
1763 CS = cast<CaseStmt>(Sub);
1764 Sub = CS->getSubStmt();
1765 }
1766
1767 addStmt(Sub);
1768 }
Mike Stump11289f42009-09-09 15:08:12 +00001769
Ted Kremenek55e91e82007-08-30 18:48:11 +00001770 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001771 if (!CaseBlock)
1772 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001773
1774 // Cases statements partition blocks, so this is the top of the basic block we
1775 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00001776 CaseBlock->setLabel(CS);
1777
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001778 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001779 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001780
1781 // Add this block to the list of successors for the block with the switch
1782 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00001783 assert(SwitchTerminatedBlock);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001784 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001785
Ted Kremenek9aae5132007-08-23 21:42:29 +00001786 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1787 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001788
Ted Kremenek60fa6572010-08-04 23:54:30 +00001789 if (TopBlock) {
1790 AddSuccessor(LastBlock, CaseBlock);
1791 Succ = TopBlock;
1792 }
1793 else {
1794 // This block is now the implicit successor of other blocks.
1795 Succ = CaseBlock;
1796 }
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremenek60fa6572010-08-04 23:54:30 +00001798 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001799}
Mike Stump31feda52009-07-17 01:31:16 +00001800
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001801CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00001802 if (Terminator->getSubStmt())
1803 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00001804
Ted Kremenek654c78f2008-02-13 22:05:39 +00001805 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001806
1807 if (!DefaultCaseBlock)
1808 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001809
1810 // Default statements partition blocks, so this is the top of the basic block
1811 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00001812 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00001813
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001814 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001815 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00001816
Mike Stump31feda52009-07-17 01:31:16 +00001817 // Unlike case statements, we don't add the default block to the successors
1818 // for the switch statement immediately. This is done when we finish
1819 // processing the switch statement. This allows for the default case
1820 // (including a fall-through to the code after the switch statement) to always
1821 // be the last successor of a switch-terminated block.
1822
Ted Kremenek654c78f2008-02-13 22:05:39 +00001823 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1824 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001825
Ted Kremenek654c78f2008-02-13 22:05:39 +00001826 // This block is now the implicit successor of other blocks.
1827 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001828
1829 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00001830}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001831
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001832CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1833 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1834 // current block.
1835 CFGBlock* TrySuccessor = NULL;
1836
1837 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001838 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001839 return 0;
1840 TrySuccessor = Block;
1841 } else TrySuccessor = Succ;
1842
Mike Stump0bdba6c2010-01-20 01:15:34 +00001843 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001844
1845 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00001846 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001847 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00001848 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001849
Mike Stump0bdba6c2010-01-20 01:15:34 +00001850 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001851 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1852 // The code after the try is the implicit successor.
1853 Succ = TrySuccessor;
1854 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001855 if (CS->getExceptionDecl() == 0) {
1856 HasCatchAll = true;
1857 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001858 Block = NULL;
1859 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1860 if (CatchBlock == 0)
1861 return 0;
1862 // Add this block to the list of successors for the block with the try
1863 // statement.
Mike Stump845384a2010-01-20 01:30:58 +00001864 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001865 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00001866 if (!HasCatchAll) {
1867 if (PrevTryTerminatedBlock)
Mike Stump845384a2010-01-20 01:30:58 +00001868 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00001869 else
Mike Stump845384a2010-01-20 01:30:58 +00001870 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00001871 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001872
1873 // The code after the try is the implicit successor.
1874 Succ = TrySuccessor;
1875
Mike Stump845384a2010-01-20 01:30:58 +00001876 // Save the current "try" context.
1877 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1878 TryTerminatedBlock = NewTryTerminatedBlock;
1879
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001880 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001881 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00001882 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001883 return Block;
1884}
1885
1886CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1887 // CXXCatchStmt are treated like labels, so they are the first statement in a
1888 // block.
1889
1890 if (CS->getHandlerBlock())
1891 addStmt(CS->getHandlerBlock());
1892
1893 CFGBlock* CatchBlock = Block;
1894 if (!CatchBlock)
1895 CatchBlock = createBlock();
1896
1897 CatchBlock->setLabel(CS);
1898
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001899 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001900 return 0;
1901
1902 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1903 Block = NULL;
1904
1905 return CatchBlock;
1906}
1907
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001908CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00001909 AddStmtChoice asc) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001910 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001911 : AddStmtChoice::AlwaysAdd;
Zhongxing Xu7e612172010-04-13 09:38:01 +00001912 autoCreateBlock();
Zhongxing Xub5e94ac2010-04-14 05:50:04 +00001913 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xu7e612172010-04-13 09:38:01 +00001914 return VisitChildren(C);
1915}
1916
Ted Kremenekeda180e22007-08-28 19:26:49 +00001917CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001918 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00001919 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00001920
Ted Kremenekeda180e22007-08-28 19:26:49 +00001921 if (!IBlock) {
1922 IBlock = createBlock(false);
1923 cfg->setIndirectGotoBlock(IBlock);
1924 }
Mike Stump31feda52009-07-17 01:31:16 +00001925
Ted Kremenekeda180e22007-08-28 19:26:49 +00001926 // IndirectGoto is a control-flow statement. Thus we stop processing the
1927 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001928 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001929 return 0;
1930
Ted Kremenekeda180e22007-08-28 19:26:49 +00001931 Block = createBlock(false);
1932 Block->setTerminator(I);
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001933 AddSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00001934 return addStmt(I->getTarget());
1935}
1936
Ted Kremenek04cca642007-08-23 21:26:19 +00001937} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00001938
Mike Stump31feda52009-07-17 01:31:16 +00001939/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1940/// no successors or predecessors. If this is the first block created in the
1941/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00001942CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00001943 bool first_block = begin() == end();
1944
1945 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001946 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1947 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1948 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00001949
1950 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001951 if (first_block)
1952 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00001953
1954 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00001955 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001956}
1957
Ted Kremenek889073f2007-08-23 16:51:22 +00001958/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1959/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00001960CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001961 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00001962 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001963 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00001964}
1965
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001966//===----------------------------------------------------------------------===//
1967// CFG: Queries for BlkExprs.
1968//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00001969
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001970namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00001971 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001972}
1973
Ted Kremenekbff98442009-12-23 23:37:10 +00001974static void FindSubExprAssignments(Stmt *S,
1975 llvm::SmallPtrSet<Expr*,50>& Set) {
1976 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00001977 return;
Mike Stump31feda52009-07-17 01:31:16 +00001978
Ted Kremenekbff98442009-12-23 23:37:10 +00001979 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001980 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00001981 if (!child)
1982 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001983
Ted Kremenekbff98442009-12-23 23:37:10 +00001984 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00001985 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00001986
Ted Kremenekbff98442009-12-23 23:37:10 +00001987 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00001988 }
1989}
1990
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00001991static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1992 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00001993
1994 // Look for assignments that are used as subexpressions. These are the only
1995 // assignments that we want to *possibly* register as a block-level
1996 // expression. Basically, if an assignment occurs both in a subexpression and
1997 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00001998 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00001999
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002000 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002001 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002002 if (CFGStmt S = BI->getAs<CFGStmt>())
2003 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002004
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002005 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002006
2007 // Iterate over the statements again on identify the Expr* and Stmt* at the
2008 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002009
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002010 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2011 CFGStmt CS = BI->getAs<CFGStmt>();
2012 if (!CS.isValid())
2013 continue;
2014 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002015
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002016 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002017 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002018 // expression are really "statements" whose value is never used by
2019 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002020 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002021 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002022 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2023 // Special handling for statement expressions. The last statement in
2024 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002025 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002026 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002027 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002028 (*M)[C->body_back()] = x;
2029 }
2030 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002031
Ted Kremenek95a123c2008-01-26 00:03:27 +00002032 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002033 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002034 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002035 }
Mike Stump31feda52009-07-17 01:31:16 +00002036
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002037 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002038
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002039 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002040
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002041 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002042 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002043 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002044 }
2045 }
Mike Stump31feda52009-07-17 01:31:16 +00002046
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002047 return M;
2048}
2049
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002050CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2051 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002052 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002053
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002054 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002055 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002056 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002057}
2058
2059unsigned CFG::getNumBlkExprs() {
2060 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2061 return M->size();
2062 else {
2063 // We assume callers interested in the number of BlkExprs will want
2064 // the map constructed if it doesn't already exist.
2065 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2066 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2067 }
2068}
2069
Ted Kremenek6065ef62008-04-28 18:00:46 +00002070//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002071// Filtered walking of the CFG.
2072//===----------------------------------------------------------------------===//
2073
2074bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002075 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002076
2077 if (F.IgnoreDefaultsWithCoveredEnums) {
2078 // If the 'To' has no label or is labeled but the label isn't a
2079 // CaseStmt then filter this edge.
2080 if (const SwitchStmt *S =
Ted Kremenekf146cd12010-09-09 02:57:48 +00002081 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002082 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002083 const Stmt *L = To->getLabel();
2084 if (!L || !isa<CaseStmt>(L))
2085 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002086 }
2087 }
2088 }
2089
2090 return false;
2091}
2092
2093//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002094// Cleanup: CFG dstor.
2095//===----------------------------------------------------------------------===//
2096
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002097CFG::~CFG() {
2098 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2099}
Mike Stump31feda52009-07-17 01:31:16 +00002100
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002101//===----------------------------------------------------------------------===//
2102// CFG pretty printing
2103//===----------------------------------------------------------------------===//
2104
Ted Kremenek7e776b12007-08-22 18:22:34 +00002105namespace {
2106
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002107class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002108 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002109 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002110 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002111 DeclMapTy DeclMap;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002112 signed CurrentBlock;
2113 unsigned CurrentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002114 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002115public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002116
Chris Lattnerc61089a2009-06-30 01:26:17 +00002117 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2118 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002119 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2120 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002121 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002122 BI != BEnd; ++BI, ++j ) {
2123 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2124 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2125 StmtMap[SE] = P;
2126
2127 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2128 DeclMap[DS->getSingleDecl()] = P;
2129
2130 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2131 if (VarDecl* VD = IS->getConditionVariable())
2132 DeclMap[VD] = P;
2133
2134 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2135 if (VarDecl* VD = FS->getConditionVariable())
2136 DeclMap[VD] = P;
2137
2138 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2139 if (VarDecl* VD = WS->getConditionVariable())
2140 DeclMap[VD] = P;
2141
2142 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2143 if (VarDecl* VD = SS->getConditionVariable())
2144 DeclMap[VD] = P;
2145
2146 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2147 if (VarDecl* VD = CS->getExceptionDecl())
2148 DeclMap[VD] = P;
2149 }
2150 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002151 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002152 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002153 }
Mike Stump31feda52009-07-17 01:31:16 +00002154
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002155 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002156
Chris Lattnerc61089a2009-06-30 01:26:17 +00002157 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002158 void setBlockID(signed i) { CurrentBlock = i; }
2159 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002160
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002161 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2162 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002163
2164 if (I == StmtMap.end())
2165 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002166
2167 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek60983dc2010-01-19 20:52:05 +00002168 && I->second.second == CurrentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002169 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002170 }
Mike Stump31feda52009-07-17 01:31:16 +00002171
Ted Kremenek60983dc2010-01-19 20:52:05 +00002172 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002173 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002174 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002175
2176 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2177 DeclMapTy::iterator I = DeclMap.find(D);
2178
2179 if (I == DeclMap.end())
2180 return false;
2181
2182 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2183 && I->second.second == CurrentStmt) {
2184 return false;
2185 }
2186
2187 OS << "[B" << I->second.first << "." << I->second.second << "]";
2188 return true;
2189 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002190};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002191} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002192
Chris Lattnerc61089a2009-06-30 01:26:17 +00002193
2194namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002195class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002196 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002197
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002198 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002199 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002200 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002201public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002202 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002203 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002204 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002205
Ted Kremenek9aae5132007-08-23 21:42:29 +00002206 void VisitIfStmt(IfStmt* I) {
2207 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002208 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002209 }
Mike Stump31feda52009-07-17 01:31:16 +00002210
Ted Kremenek9aae5132007-08-23 21:42:29 +00002211 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002212 void VisitStmt(Stmt* Terminator) {
2213 Terminator->printPretty(OS, Helper, Policy);
2214 }
2215
Ted Kremenek9aae5132007-08-23 21:42:29 +00002216 void VisitForStmt(ForStmt* F) {
2217 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002218 if (F->getInit())
2219 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002220 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002221 if (Stmt* C = F->getCond())
2222 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002223 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002224 if (F->getInc())
2225 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002226 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002227 }
Mike Stump31feda52009-07-17 01:31:16 +00002228
Ted Kremenek9aae5132007-08-23 21:42:29 +00002229 void VisitWhileStmt(WhileStmt* W) {
2230 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002231 if (Stmt* C = W->getCond())
2232 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002233 }
Mike Stump31feda52009-07-17 01:31:16 +00002234
Ted Kremenek9aae5132007-08-23 21:42:29 +00002235 void VisitDoStmt(DoStmt* D) {
2236 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002237 if (Stmt* C = D->getCond())
2238 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002239 }
Mike Stump31feda52009-07-17 01:31:16 +00002240
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002241 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002242 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002243 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002244 }
Mike Stump31feda52009-07-17 01:31:16 +00002245
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002246 void VisitCXXTryStmt(CXXTryStmt* CS) {
2247 OS << "try ...";
2248 }
2249
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002250 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002251 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002252 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002253 }
Mike Stump31feda52009-07-17 01:31:16 +00002254
Ted Kremenek391f94a2007-08-31 22:29:13 +00002255 void VisitChooseExpr(ChooseExpr* C) {
2256 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002257 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002258 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002259 }
Mike Stump31feda52009-07-17 01:31:16 +00002260
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002261 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2262 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002263 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002264 }
Mike Stump31feda52009-07-17 01:31:16 +00002265
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002266 void VisitBinaryOperator(BinaryOperator* B) {
2267 if (!B->isLogicalOp()) {
2268 VisitExpr(B);
2269 return;
2270 }
Mike Stump31feda52009-07-17 01:31:16 +00002271
Douglas Gregor7de59662009-05-29 20:38:28 +00002272 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002273
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002274 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002275 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002276 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002277 return;
John McCalle3027922010-08-25 11:45:40 +00002278 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00002279 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002280 return;
2281 default:
2282 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00002283 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002284 }
Mike Stump31feda52009-07-17 01:31:16 +00002285
Ted Kremenek12687ff2007-08-27 21:54:41 +00002286 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002287 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002288 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00002289};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002290} // end anonymous namespace
2291
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002292static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00002293 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002294 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2295 Stmt *S = CS;
2296
2297 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00002298
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002299 // special printing for statement-expressions.
2300 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2301 CompoundStmt* Sub = SE->getSubStmt();
2302
2303 if (Sub->child_begin() != Sub->child_end()) {
2304 OS << "({ ... ; ";
2305 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2306 OS << " })\n";
2307 return;
2308 }
2309 }
2310 // special printing for comma expressions.
2311 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2312 if (B->getOpcode() == BO_Comma) {
2313 OS << "... , ";
2314 Helper->handledStmt(B->getRHS(),OS);
2315 OS << '\n';
2316 return;
2317 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002318 }
2319 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002320 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00002321
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002322 if (isa<CXXOperatorCallExpr>(S)) {
2323 OS << " (OperatorCall)";
Mike Stump31feda52009-07-17 01:31:16 +00002324 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002325 else if (isa<CXXBindTemporaryExpr>(S)) {
2326 OS << " (BindTemporary)";
2327 }
Mike Stump31feda52009-07-17 01:31:16 +00002328
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002329 // Expressions need a newline.
2330 if (isa<Expr>(S))
2331 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00002332
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002333 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2334 CXXBaseOrMemberInitializer* I = IE;
2335 if (I->isBaseInitializer())
2336 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2337 else OS << I->getMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00002338
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002339 OS << "(";
2340 if (Expr* IE = I->getInit())
2341 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2342 OS << ")";
2343
2344 if (I->isBaseInitializer())
2345 OS << " (Base initializer)\n";
2346 else OS << " (Member initializer)\n";
2347
2348 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2349 VarDecl* VD = DE.getVarDecl();
2350 Helper->handleDecl(VD, OS);
2351
2352 Type* T = VD->getType().getTypePtr();
2353 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2354 T = RT->getPointeeType().getTypePtr();
2355
2356 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2357 OS << " (Implicit destructor)\n";
2358 }
2359 }
Mike Stump31feda52009-07-17 01:31:16 +00002360
Chris Lattnerc61089a2009-06-30 01:26:17 +00002361static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2362 const CFGBlock& B,
2363 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00002364
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002365 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00002366
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002367 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00002368 OS << "\n [ B" << B.getBlockID();
2369
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002370 if (&B == &cfg->getEntry())
2371 OS << " (ENTRY) ]\n";
2372 else if (&B == &cfg->getExit())
2373 OS << " (EXIT) ]\n";
2374 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002375 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002376 else
2377 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00002378
Ted Kremenek71eca012007-08-29 23:20:49 +00002379 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00002380 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002381
2382 if (print_edges)
2383 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002384
Mike Stump92244b02010-01-19 22:00:14 +00002385 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002386 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00002387 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00002388 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002389 C->getLHS()->printPretty(OS, Helper,
2390 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002391 if (C->getRHS()) {
2392 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00002393 C->getRHS()->printPretty(OS, Helper,
2394 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00002395 }
Mike Stump92244b02010-01-19 22:00:14 +00002396 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00002397 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00002398 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002399 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00002400 if (CS->getExceptionDecl())
2401 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2402 0);
2403 else
2404 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002405 OS << ")";
2406
2407 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002408 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00002409
Ted Kremenek71eca012007-08-29 23:20:49 +00002410 OS << ":\n";
2411 }
Mike Stump31feda52009-07-17 01:31:16 +00002412
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002413 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002414 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00002415
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002416 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2417 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00002418
Ted Kremenek71eca012007-08-29 23:20:49 +00002419 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002420 if (print_edges)
2421 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002422
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002423 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00002424
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002425 if (Helper)
2426 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00002427
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002428 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002429 }
Mike Stump31feda52009-07-17 01:31:16 +00002430
Ted Kremenek71eca012007-08-29 23:20:49 +00002431 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002432 if (B.getTerminator()) {
2433 if (print_edges)
2434 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00002435
Ted Kremenek71eca012007-08-29 23:20:49 +00002436 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00002437
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002438 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00002439
Chris Lattnerc61089a2009-06-30 01:26:17 +00002440 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2441 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002442 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremenek15647632008-01-30 23:02:42 +00002443 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002444 }
Mike Stump31feda52009-07-17 01:31:16 +00002445
Ted Kremenek71eca012007-08-29 23:20:49 +00002446 if (print_edges) {
2447 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002448 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00002449 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00002450
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002451 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2452 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002453
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002454 if (i == 8 || (i-8) == 0)
2455 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00002456
Ted Kremenek71eca012007-08-29 23:20:49 +00002457 OS << " B" << (*I)->getBlockID();
2458 }
Mike Stump31feda52009-07-17 01:31:16 +00002459
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002460 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00002461
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002462 // Print the successors of this block.
2463 OS << " Successors (" << B.succ_size() << "):";
2464 i = 0;
2465
2466 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2467 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00002468
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002469 if (i == 8 || (i-8) % 10 == 0)
2470 OS << "\n ";
2471
Mike Stump0d76d072009-07-20 23:24:15 +00002472 if (*I)
2473 OS << " B" << (*I)->getBlockID();
2474 else
2475 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002476 }
Mike Stump31feda52009-07-17 01:31:16 +00002477
Ted Kremenek71eca012007-08-29 23:20:49 +00002478 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002479 }
Mike Stump31feda52009-07-17 01:31:16 +00002480}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002481
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002482
2483/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002484void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002485
2486/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002487void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2488 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00002489
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002490 // Print the entry block.
2491 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00002492
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002493 // Iterate through the CFGBlocks and print them one by one.
2494 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2495 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002496 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002497 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002498
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002499 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002500 }
Mike Stump31feda52009-07-17 01:31:16 +00002501
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002502 // Print the exit block.
2503 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00002504 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00002505}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002506
2507/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002508void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2509 print(llvm::errs(), cfg, LO);
2510}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002511
2512/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2513/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002514void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2515 const LangOptions &LO) const {
2516 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002517 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00002518}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002519
Ted Kremenek15647632008-01-30 23:02:42 +00002520/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00002521void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00002522 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00002523 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremenek15647632008-01-30 23:02:42 +00002524 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2525}
2526
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002527Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump31feda52009-07-17 01:31:16 +00002528
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002529 if (!Terminator)
2530 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002531
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002532 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002533
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002534 switch (Terminator->getStmtClass()) {
2535 default:
2536 break;
Mike Stump31feda52009-07-17 01:31:16 +00002537
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002538 case Stmt::ForStmtClass:
2539 E = cast<ForStmt>(Terminator)->getCond();
2540 break;
Mike Stump31feda52009-07-17 01:31:16 +00002541
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002542 case Stmt::WhileStmtClass:
2543 E = cast<WhileStmt>(Terminator)->getCond();
2544 break;
Mike Stump31feda52009-07-17 01:31:16 +00002545
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002546 case Stmt::DoStmtClass:
2547 E = cast<DoStmt>(Terminator)->getCond();
2548 break;
Mike Stump31feda52009-07-17 01:31:16 +00002549
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002550 case Stmt::IfStmtClass:
2551 E = cast<IfStmt>(Terminator)->getCond();
2552 break;
Mike Stump31feda52009-07-17 01:31:16 +00002553
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002554 case Stmt::ChooseExprClass:
2555 E = cast<ChooseExpr>(Terminator)->getCond();
2556 break;
Mike Stump31feda52009-07-17 01:31:16 +00002557
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002558 case Stmt::IndirectGotoStmtClass:
2559 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2560 break;
Mike Stump31feda52009-07-17 01:31:16 +00002561
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002562 case Stmt::SwitchStmtClass:
2563 E = cast<SwitchStmt>(Terminator)->getCond();
2564 break;
Mike Stump31feda52009-07-17 01:31:16 +00002565
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002566 case Stmt::ConditionalOperatorClass:
2567 E = cast<ConditionalOperator>(Terminator)->getCond();
2568 break;
Mike Stump31feda52009-07-17 01:31:16 +00002569
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002570 case Stmt::BinaryOperatorClass: // '&&' and '||'
2571 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002572 break;
Mike Stump31feda52009-07-17 01:31:16 +00002573
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002574 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00002575 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002576 }
Mike Stump31feda52009-07-17 01:31:16 +00002577
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002578 return E ? E->IgnoreParens() : NULL;
2579}
2580
Ted Kremenek92137a32008-05-16 16:06:00 +00002581bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump31feda52009-07-17 01:31:16 +00002582
Ted Kremenek92137a32008-05-16 16:06:00 +00002583 if (!Terminator)
2584 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002585
Ted Kremenek92137a32008-05-16 16:06:00 +00002586 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002587
Ted Kremenek92137a32008-05-16 16:06:00 +00002588 switch (Terminator->getStmtClass()) {
2589 default:
2590 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002591
2592 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00002593 case Stmt::WhileStmtClass:
2594 case Stmt::DoStmtClass:
2595 case Stmt::IfStmtClass:
2596 case Stmt::ChooseExprClass:
2597 case Stmt::ConditionalOperatorClass:
2598 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00002599 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00002600 }
Mike Stump31feda52009-07-17 01:31:16 +00002601
Ted Kremenek92137a32008-05-16 16:06:00 +00002602 return E ? E->IgnoreParens() : NULL;
2603}
2604
Ted Kremenek15647632008-01-30 23:02:42 +00002605
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002606//===----------------------------------------------------------------------===//
2607// CFG Graphviz Visualization
2608//===----------------------------------------------------------------------===//
2609
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002610
2611#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00002612static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002613#endif
2614
Chris Lattnerc61089a2009-06-30 01:26:17 +00002615void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002616#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00002617 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002618 GraphHelper = &H;
2619 llvm::ViewGraph(this,"CFG");
2620 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002621#endif
2622}
2623
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002624namespace llvm {
2625template<>
2626struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00002627
2628 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2629
2630 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002631
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002632#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002633 std::string OutSStr;
2634 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002635 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002636 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002637
2638 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2639
2640 // Process string output to make it nicer...
2641 for (unsigned i = 0; i != OutStr.length(); ++i)
2642 if (OutStr[i] == '\n') { // Left justify
2643 OutStr[i] = '\\';
2644 OutStr.insert(OutStr.begin()+i+1, 'l');
2645 }
Mike Stump31feda52009-07-17 01:31:16 +00002646
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002647 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00002648#else
2649 return "";
2650#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002651 }
2652};
2653} // end namespace llvm