blob: 0f32614d2b38bd958652e5405da5a0c62ca15540 [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekfddd5182007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-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 Stump6d9828c2009-07-17 01:31:16 +000035 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000036}
Ted Kremenekad5a8942010-08-02 23:46:59 +000037
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000038/// The CFG builder uses a recursive algorithm to build the CFG. When
39/// we process an expression, sometimes we know that we must add the
40/// subexpressions as block-level expressions. For example:
41///
42/// exp1 || exp2
43///
44/// When processing the '||' expression, we know that exp1 and exp2
45/// need to be added as block-level expressions, even though they
46/// might not normally need to be. AddStmtChoice records this
47/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
48/// the builder has an option not to add a subexpression as a
49/// block-level expression.
50///
Ted Kremenek852274d2009-12-16 03:18:58 +000051class AddStmtChoice {
52public:
Ted Kremenek892697d2010-12-16 07:46:53 +000053 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5ba290a2010-03-02 21:43:54 +000054
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000055 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000056
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000057 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000058
59 /// Return a copy of this object, except with the 'always-add' bit
60 /// set as specified.
61 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
62 return AddStmtChoice(alwaysAdd ? Kind(kind | AlwaysAdd) :
63 Kind(kind & ~AlwaysAdd));
64 }
65
Ted Kremenek852274d2009-12-16 03:18:58 +000066private:
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +000067 Kind kind;
Ted Kremenek852274d2009-12-16 03:18:58 +000068};
Mike Stump6d9828c2009-07-17 01:31:16 +000069
Marcin Swiderskif1308c72010-09-25 11:05:21 +000070/// LocalScope - Node in tree of local scopes created for C++ implicit
71/// destructor calls generation. It contains list of automatic variables
72/// declared in the scope and link to position in previous scope this scope
73/// began in.
74///
75/// The process of creating local scopes is as follows:
76/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
77/// - Before processing statements in scope (e.g. CompoundStmt) create
78/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
79/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderski35387a02010-09-30 22:42:32 +000080/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-09-25 11:05:21 +000081/// at this VarDecl,
82/// - For every normal (without jump) end of scope add to CFGBlock destructors
83/// for objects in the current scope,
84/// - For every jump add to CFGBlock destructors for objects
85/// between CFGBuilder::ScopePos and local scope position saved for jump
86/// target. Thanks to C++ restrictions on goto jumps we can be sure that
87/// jump target position will be on the path to root from CFGBuilder::ScopePos
88/// (adding any variable that doesn't need constructor to be called to
89/// LocalScope can break this assumption),
90///
91class LocalScope {
92public:
93 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
94
95 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +000096 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-09-25 11:05:21 +000097 class const_iterator {
98 const LocalScope* Scope;
99
100 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
101 /// Invalid iterator (with null Scope) has VarIter equal to 0.
102 unsigned VarIter;
103
104 public:
105 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
106 /// Incrementing invalid iterator is allowed and will result in invalid
107 /// iterator.
108 const_iterator()
109 : Scope(NULL), VarIter(0) {}
110
111 /// Create valid iterator. In case when S.Prev is an invalid iterator and
112 /// I is equal to 0, this will create invalid iterator.
113 const_iterator(const LocalScope& S, unsigned I)
114 : Scope(&S), VarIter(I) {
115 // Iterator to "end" of scope is not allowed. Handle it by going up
116 // in scopes tree possibly up to invalid iterator in the root.
117 if (VarIter == 0 && Scope)
118 *this = Scope->Prev;
119 }
120
121 VarDecl* const* operator->() const {
122 assert (Scope && "Dereferencing invalid iterator is not allowed");
123 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
124 return &Scope->Vars[VarIter - 1];
125 }
126 VarDecl* operator*() const {
127 return *this->operator->();
128 }
129
130 const_iterator& operator++() {
131 if (!Scope)
132 return *this;
133
134 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
135 --VarIter;
136 if (VarIter == 0)
137 *this = Scope->Prev;
138 return *this;
139 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000140 const_iterator operator++(int) {
141 const_iterator P = *this;
142 ++*this;
143 return P;
144 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000145
146 bool operator==(const const_iterator& rhs) const {
147 return Scope == rhs.Scope && VarIter == rhs.VarIter;
148 }
149 bool operator!=(const const_iterator& rhs) const {
150 return !(*this == rhs);
151 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000152
153 operator bool() const {
154 return *this != const_iterator();
155 }
156
157 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000158 };
159
160 friend class const_iterator;
161
162private:
163 /// Automatic variables in order of declaration.
164 AutomaticVarsTy Vars;
165 /// Iterator to variable in previous scope that was declared just before
166 /// begin of this scope.
167 const_iterator Prev;
168
169public:
170 /// Constructs empty scope linked to previous scope in specified place.
171 LocalScope(const_iterator P)
172 : Vars()
173 , Prev(P) {}
174
175 /// Begin of scope in direction of CFG building (backwards).
176 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000177
178 void addVar(VarDecl* VD) {
179 Vars.push_back(VD);
180 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000181};
182
Marcin Swiderski35387a02010-09-30 22:42:32 +0000183/// distance - Calculates distance from this to L. L must be reachable from this
184/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
185/// number of scopes between this and L.
186int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
187 int D = 0;
188 const_iterator F = *this;
189 while (F.Scope != L.Scope) {
190 assert (F != const_iterator()
191 && "L iterator is not reachable from F iterator.");
192 D += F.VarIter;
193 F = F.Scope->Prev;
194 }
195 D += F.VarIter - L.VarIter;
196 return D;
197}
198
199/// BlockScopePosPair - Structure for specifying position in CFG during its
200/// build process. It consists of CFGBlock that specifies position in CFG graph
201/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000202struct BlockScopePosPair {
203 BlockScopePosPair() {}
204 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
205 : Block(B), ScopePos(S) {}
206
207 CFGBlock* Block;
208 LocalScope::const_iterator ScopePos;
209};
210
Ted Kremeneka34ea072008-08-04 22:51:42 +0000211/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +0000212/// The builder is stateful: an instance of the builder should be used to only
213/// construct a single CFG.
214///
215/// Example usage:
216///
217/// CFGBuilder builder;
218/// CFG* cfg = builder.BuildAST(stmt1);
219///
Mike Stump6d9828c2009-07-17 01:31:16 +0000220/// CFG construction is done via a recursive walk of an AST. We actually parse
221/// the AST in reverse order so that the successor of a basic block is
222/// constructed prior to its predecessor. This allows us to nicely capture
223/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +0000224///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000225class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000226 typedef BlockScopePosPair JumpTarget;
227 typedef BlockScopePosPair JumpSource;
228
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000229 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000230 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000231
Ted Kremenekfddd5182007-08-21 21:42:03 +0000232 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000233 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000234 JumpTarget ContinueJumpTarget;
235 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000236 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000237 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000238 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000239
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000240 // Current position in local scope.
241 LocalScope::const_iterator ScopePos;
242
243 // LabelMap records the mapping from Label expressions to their jump targets.
244 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000245 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +0000246
247 // A list of blocks that end with a "goto" that must be backpatched to their
248 // resolved targets upon completion of CFG construction.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000249 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000250 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000251
Ted Kremenek19bb3562007-08-28 19:26:49 +0000252 // A list of labels whose address has been taken (for indirect gotos).
253 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
254 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000255
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000256 bool badCFG;
257 CFG::BuildOptions BuildOpts;
258
Mike Stump6d9828c2009-07-17 01:31:16 +0000259public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000260 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
261 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000262 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000263 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000264
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000265 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000266 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000267 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000268
Ted Kremenek4f880632009-07-17 22:18:43 +0000269private:
270 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000271 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
272 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
273 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000274 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000275 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall4765fa02010-12-06 08:20:24 +0000276 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +0000277 AddStmtChoice asc);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000278 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
279 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000280 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
281 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000282 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000283 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
284 AddStmtChoice asc);
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000285 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
286 AddStmtChoice asc);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000287 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000288 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000289 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000290 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000291 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000292 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000293 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000294 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000295 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000296 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
297 CFGBlock *VisitDoStmt(DoStmt *D);
298 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000299 CFGBlock *VisitGotoStmt(GotoStmt* G);
300 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000301 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000302 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
303 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000304 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000305 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
306 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
307 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
308 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
309 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
310 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000311 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
312 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000313 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000314 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000315 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000316
Ted Kremenek852274d2009-12-16 03:18:58 +0000317 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
318 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000319 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000320
Marcin Swiderski8599e762010-11-03 06:19:35 +0000321 // Visitors to walk an AST and generate destructors of temporaries in
322 // full expression.
323 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
324 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
325 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
326 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
327 bool BindToTemporary);
328 CFGBlock *VisitConditionalOperatorForTemporaryDtors(ConditionalOperator *E,
329 bool BindToTemporary);
330
Ted Kremenek274f4332008-04-28 18:00:46 +0000331 // NYS == Not Yet Supported
332 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000333 badCFG = true;
334 return Block;
335 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000336
Ted Kremenek4f880632009-07-17 22:18:43 +0000337 void autoCreateBlock() { if (!Block) Block = createBlock(); }
338 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000339
Zhongxing Xudf119892010-06-03 06:43:23 +0000340 CFGBlock *addStmt(Stmt *S) {
341 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000342 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000343 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000344 void addAutomaticObjDtors(LocalScope::const_iterator B,
345 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000346 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000347
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000348 // Local scopes creation.
349 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
350
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000351 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000352 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
353 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
354
355 void addLocalScopeAndDtors(Stmt* S);
356
357 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek892697d2010-12-16 07:46:53 +0000358 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek852274d2009-12-16 03:18:58 +0000359 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000360 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000361 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000362 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
363 B->appendInitializer(I, cfg->getBumpVectorContext());
364 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000365 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
366 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
367 }
368 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
369 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
370 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000371 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
372 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
373 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000374
Marcin Swiderski53de1342010-09-30 22:54:37 +0000375 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
376 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
377 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
378 LocalScope::const_iterator E, Stmt* S);
379 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
380 LocalScope::const_iterator B, LocalScope::const_iterator E);
381
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000382 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
383 B->addSuccessor(S, cfg->getBumpVectorContext());
384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000386 /// TryResult - a class representing a variant over the values
387 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
388 /// and is used by the CFGBuilder to decide if a branch condition
389 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000390 class TryResult {
391 int X;
392 public:
393 TryResult(bool b) : X(b ? 1 : 0) {}
394 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek941fde82009-07-24 04:47:11 +0000396 bool isTrue() const { return X == 1; }
397 bool isFalse() const { return X == 0; }
398 bool isKnown() const { return X >= 0; }
399 void negate() {
400 assert(isKnown());
401 X ^= 0x1;
402 }
403 };
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Mike Stump00998a02009-07-23 23:25:26 +0000405 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
406 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000407 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000408 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000409 return TryResult();
410
Mike Stump00998a02009-07-23 23:25:26 +0000411 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000412 if (!S->isTypeDependent() && !S->isValueDependent() &&
413 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000414 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000415
416 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000417 }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000418};
Mike Stump6d9828c2009-07-17 01:31:16 +0000419
Douglas Gregor898574e2008-12-05 23:32:09 +0000420// FIXME: Add support for dependent-sized array types in C++?
421// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000422static VariableArrayType* FindVA(Type* t) {
423 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
424 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
425 if (vat->getSizeExpr())
426 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000427
Ted Kremenek610a09e2008-09-26 22:58:57 +0000428 t = vt->getElementType().getTypePtr();
429 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000430
Ted Kremenek610a09e2008-09-26 22:58:57 +0000431 return 0;
432}
Mike Stump6d9828c2009-07-17 01:31:16 +0000433
434/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
435/// arbitrary statement. Examples include a single expression or a function
436/// body (compound statement). The ownership of the returned CFG is
437/// transferred to the caller. If CFG construction fails, this method returns
438/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000439CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000440 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000441
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000442 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000443 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000444 if (!Statement)
445 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000446
Ted Kremenek6c52c782010-09-14 23:41:16 +0000447 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000448
449 // Create an empty block that will serve as the exit block for the CFG. Since
450 // this is the first block added to the CFG, it will be implicitly registered
451 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000452 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000453 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000454 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000455
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000456 if (BuildOpts.AddImplicitDtors)
457 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
458 addImplicitDtorsForDestructor(DD);
459
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000460 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000461 CFGBlock *B = addStmt(Statement);
462
463 if (badCFG)
464 return NULL;
465
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000466 // For C++ constructor add initializers to CFG.
467 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
468 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
469 E = CD->init_rend(); I != E; ++I) {
470 B = addInitializer(*I);
471 if (badCFG)
472 return NULL;
473 }
474 }
475
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000476 if (B)
477 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000478
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000479 // Backpatch the gotos whose label -> block mappings we didn't know when we
480 // encountered them.
481 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
482 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000483
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000484 CFGBlock* B = I->Block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000485 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
486 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000487
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000488 // If there is no target for the goto, then we are looking at an
489 // incomplete AST. Handle this by not registering a successor.
490 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000491
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000492 JumpTarget JT = LI->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +0000493 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000494 AddSuccessor(B, JT.Block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000495 }
496
497 // Add successors to the Indirect Goto Dispatch block (if we have one).
498 if (CFGBlock* B = cfg->getIndirectGotoBlock())
499 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
500 E = AddressTakenLabels.end(); I != E; ++I ) {
501
502 // Lookup the target block.
503 LabelMapTy::iterator LI = LabelMap.find(*I);
504
505 // If there is no target block that contains label, then we are looking
506 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000507 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000508
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000509 AddSuccessor(B, LI->second.Block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000510 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000511
Mike Stump6d9828c2009-07-17 01:31:16 +0000512 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000513 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000514
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000515 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000516}
Mike Stump6d9828c2009-07-17 01:31:16 +0000517
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000518/// createBlock - Used to lazily create blocks that are connected
519/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000520CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000521 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000522 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000523 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000524 return B;
525}
Mike Stump6d9828c2009-07-17 01:31:16 +0000526
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000527/// addInitializer - Add C++ base or member initializer element to CFG.
528CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
529 if (!BuildOpts.AddInitializers)
530 return Block;
531
Marcin Swiderski8599e762010-11-03 06:19:35 +0000532 bool IsReference = false;
533 bool HasTemporaries = false;
534
535 // Destructors of temporaries in initialization expression should be called
536 // after initialization finishes.
537 Expr *Init = I->getInit();
538 if (Init) {
Francois Pichet00eb3f92010-12-04 09:14:42 +0000539 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski8599e762010-11-03 06:19:35 +0000540 IsReference = FD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +0000541 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +0000542
543 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
544 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +0000545 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +0000546 IsReference);
547 }
548 }
549
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000550 autoCreateBlock();
551 appendInitializer(Block, I);
552
Marcin Swiderski8599e762010-11-03 06:19:35 +0000553 if (Init) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000554 if (HasTemporaries) {
Marcin Swiderski8599e762010-11-03 06:19:35 +0000555 // For expression with temporaries go directly to subexpression to omit
556 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +0000557 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
558 }
559 return Visit(Init);
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000560 }
Marcin Swiderski8599e762010-11-03 06:19:35 +0000561
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000562 return Block;
563}
564
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000565/// addAutomaticObjDtors - Add to current block automatic objects destructors
566/// for objects in range of local scope positions. Use S as trigger statement
567/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000568void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
569 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000570 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000571 return;
572
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000573 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000574 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000575
576 autoCreateBlock();
577 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000578}
579
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000580/// addImplicitDtorsForDestructor - Add implicit destructors generated for
581/// base and member objects in destructor.
582void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
583 assert (BuildOpts.AddImplicitDtors
584 && "Can be called only when dtors should be added");
585 const CXXRecordDecl *RD = DD->getParent();
586
587 // At the end destroy virtual base objects.
588 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
589 VE = RD->vbases_end(); VI != VE; ++VI) {
590 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
591 if (!CD->hasTrivialDestructor()) {
592 autoCreateBlock();
593 appendBaseDtor(Block, VI);
594 }
595 }
596
597 // Before virtual bases destroy direct base objects.
598 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
599 BE = RD->bases_end(); BI != BE; ++BI) {
600 if (!BI->isVirtual()) {
601 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
602 if (!CD->hasTrivialDestructor()) {
603 autoCreateBlock();
604 appendBaseDtor(Block, BI);
605 }
606 }
607 }
608
609 // First destroy member objects.
610 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
611 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +0000612 // Check for constant size array. Set type to array element type.
613 QualType QT = FI->getType();
614 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
615 if (AT->getSize() == 0)
616 continue;
617 QT = AT->getElementType();
618 }
619
620 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000621 if (!CD->hasTrivialDestructor()) {
622 autoCreateBlock();
623 appendMemberDtor(Block, *FI);
624 }
625 }
626}
627
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000628/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
629/// way return valid LocalScope object.
630LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
631 if (!Scope) {
632 Scope = cfg->getAllocator().Allocate<LocalScope>();
633 new (Scope) LocalScope(ScopePos);
634 }
635 return Scope;
636}
637
638/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000639/// that should create implicit scope (e.g. if/else substatements).
640void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000641 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000642 return;
643
644 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000645
646 // For compound statement we will be creating explicit scope.
647 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
648 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
649 ; BI != BE; ++BI) {
650 Stmt* SI = *BI;
651 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
652 SI = LS->getSubStmt();
653 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
654 Scope = addLocalScopeForDeclStmt(DS, Scope);
655 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000656 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000657 }
658
659 // For any other statement scope will be implicit and as such will be
660 // interesting only for DeclStmt.
661 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
662 S = LS->getSubStmt();
663 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000664 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000665}
666
667/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
668/// reuse Scope if not NULL.
669LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000670 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000671 if (!BuildOpts.AddImplicitDtors)
672 return Scope;
673
674 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
675 ; DI != DE; ++DI) {
676 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
677 Scope = addLocalScopeForVarDecl(VD, Scope);
678 }
679 return Scope;
680}
681
682/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
683/// create add scope for automatic objects and temporary objects bound to
684/// const reference. Will reuse Scope if not NULL.
685LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000686 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000687 if (!BuildOpts.AddImplicitDtors)
688 return Scope;
689
690 // Check if variable is local.
691 switch (VD->getStorageClass()) {
692 case SC_None:
693 case SC_Auto:
694 case SC_Register:
695 break;
696 default: return Scope;
697 }
698
699 // Check for const references bound to temporary. Set type to pointee.
700 QualType QT = VD->getType();
701 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
702 QT = RT->getPointeeType();
703 if (!QT.isConstQualified())
704 return Scope;
705 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
706 return Scope;
707 }
708
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000709 // Check for constant size array. Set type to array element type.
710 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
711 if (AT->getSize() == 0)
712 return Scope;
713 QT = AT->getElementType();
714 }
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000715
Marcin Swiderskib1c52872010-10-25 07:00:40 +0000716 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000717 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
718 if (!CD->hasTrivialDestructor()) {
719 // Add the variable to scope
720 Scope = createOrReuseLocalScope(Scope);
721 Scope->addVar(VD);
722 ScopePos = Scope->begin();
723 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000724 return Scope;
725}
726
727/// addLocalScopeAndDtors - For given statement add local scope for it and
728/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
729void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
730 if (!BuildOpts.AddImplicitDtors)
731 return;
732
733 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000734 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000735 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
736}
737
Marcin Swiderski53de1342010-09-30 22:54:37 +0000738/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
739/// automatic storage duration to CFGBlock's elements vector. Insertion will be
740/// performed in place specified with iterator.
741void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
742 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
743 BumpVectorContext& C = cfg->getBumpVectorContext();
744 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
745 while (B != E)
746 I = Blk->insertAutomaticObjDtor(I, *B++, S);
747}
748
749/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
750/// automatic storage duration to CFGBlock's elements vector. Elements will be
751/// appended to physical end of the vector which happens to be logical
752/// beginning.
753void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
754 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
755 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
756}
757
758/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
759/// variables with automatic storage duration to CFGBlock's elements vector.
760/// Elements will be prepended to physical beginning of the vector which
761/// happens to be logical end. Use blocks terminator as statement that specifies
762/// destructors call site.
763void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
764 LocalScope::const_iterator B, LocalScope::const_iterator E) {
765 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
766}
767
Ted Kremenek4f880632009-07-17 22:18:43 +0000768/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000769/// blocks for ternary operators, &&, and ||. We also process "," and
770/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000771CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000772tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000773 if (!S) {
774 badCFG = true;
775 return 0;
776 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000777 switch (S->getStmtClass()) {
778 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000779 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000780
781 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000782 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Ted Kremenek4f880632009-07-17 22:18:43 +0000784 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000785 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek4f880632009-07-17 22:18:43 +0000787 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000788 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000789
Ted Kremenek4f880632009-07-17 22:18:43 +0000790 case Stmt::BreakStmtClass:
791 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenek4f880632009-07-17 22:18:43 +0000793 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000794 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000795 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek4f880632009-07-17 22:18:43 +0000797 case Stmt::CaseStmtClass:
798 return VisitCaseStmt(cast<CaseStmt>(S));
799
800 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000801 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek4f880632009-07-17 22:18:43 +0000803 case Stmt::CompoundStmtClass:
804 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek4f880632009-07-17 22:18:43 +0000806 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000807 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek4f880632009-07-17 22:18:43 +0000809 case Stmt::ContinueStmtClass:
810 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek021c8af2010-01-19 20:40:33 +0000812 case Stmt::CXXCatchStmtClass:
813 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
814
John McCall4765fa02010-12-06 08:20:24 +0000815 case Stmt::ExprWithCleanupsClass:
816 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000817
Zhongxing Xua725ed42010-11-01 13:04:58 +0000818 case Stmt::CXXBindTemporaryExprClass:
819 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
820
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000821 case Stmt::CXXConstructExprClass:
822 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
823
Zhongxing Xua725ed42010-11-01 13:04:58 +0000824 case Stmt::CXXFunctionalCastExprClass:
825 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
826
Zhongxing Xu81bc7d02010-11-01 06:46:05 +0000827 case Stmt::CXXTemporaryObjectExprClass:
828 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
829
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000830 case Stmt::CXXMemberCallExprClass:
831 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
832
Ted Kremenek021c8af2010-01-19 20:40:33 +0000833 case Stmt::CXXThrowExprClass:
834 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000835
Ted Kremenek021c8af2010-01-19 20:40:33 +0000836 case Stmt::CXXTryStmtClass:
837 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000838
Ted Kremenek4f880632009-07-17 22:18:43 +0000839 case Stmt::DeclStmtClass:
840 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek4f880632009-07-17 22:18:43 +0000842 case Stmt::DefaultStmtClass:
843 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek4f880632009-07-17 22:18:43 +0000845 case Stmt::DoStmtClass:
846 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek4f880632009-07-17 22:18:43 +0000848 case Stmt::ForStmtClass:
849 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Ted Kremenek4f880632009-07-17 22:18:43 +0000851 case Stmt::GotoStmtClass:
852 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Ted Kremenek4f880632009-07-17 22:18:43 +0000854 case Stmt::IfStmtClass:
855 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek892697d2010-12-16 07:46:53 +0000857 case Stmt::ImplicitCastExprClass:
858 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +0000859
Ted Kremenek4f880632009-07-17 22:18:43 +0000860 case Stmt::IndirectGotoStmtClass:
861 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Ted Kremenek4f880632009-07-17 22:18:43 +0000863 case Stmt::LabelStmtClass:
864 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Ted Kremenek115c1b92010-04-11 17:02:10 +0000866 case Stmt::MemberExprClass:
867 return VisitMemberExpr(cast<MemberExpr>(S), asc);
868
Ted Kremenek4f880632009-07-17 22:18:43 +0000869 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000870 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
871
Ted Kremenek4f880632009-07-17 22:18:43 +0000872 case Stmt::ObjCAtSynchronizedStmtClass:
873 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenek4f880632009-07-17 22:18:43 +0000875 case Stmt::ObjCAtThrowStmtClass:
876 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek4f880632009-07-17 22:18:43 +0000878 case Stmt::ObjCAtTryStmtClass:
879 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenek4f880632009-07-17 22:18:43 +0000881 case Stmt::ObjCForCollectionStmtClass:
882 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Ted Kremenek4f880632009-07-17 22:18:43 +0000884 case Stmt::ParenExprClass:
885 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000886 goto tryAgain;
887
Ted Kremenek4f880632009-07-17 22:18:43 +0000888 case Stmt::NullStmtClass:
889 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek4f880632009-07-17 22:18:43 +0000891 case Stmt::ReturnStmtClass:
892 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek4f880632009-07-17 22:18:43 +0000894 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000895 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenek4f880632009-07-17 22:18:43 +0000897 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000898 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremenek4f880632009-07-17 22:18:43 +0000900 case Stmt::SwitchStmtClass:
901 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000903 case Stmt::UnaryOperatorClass:
904 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
905
Ted Kremenek4f880632009-07-17 22:18:43 +0000906 case Stmt::WhileStmtClass:
907 return VisitWhileStmt(cast<WhileStmt>(S));
908 }
909}
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Ted Kremenek852274d2009-12-16 03:18:58 +0000911CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
912 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000913 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000914 appendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Ted Kremenek4f880632009-07-17 22:18:43 +0000917 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000918}
Mike Stump6d9828c2009-07-17 01:31:16 +0000919
Ted Kremenek4f880632009-07-17 22:18:43 +0000920/// VisitChildren - Visit the children of a Stmt.
921CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
922 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000923 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000924 E = Terminator->child_end(); I != E; ++I) {
925 if (*I) B = Visit(*I);
926 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000927 return B;
928}
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Ted Kremenek852274d2009-12-16 03:18:58 +0000930CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
931 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000932 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000933
Ted Kremenek852274d2009-12-16 03:18:58 +0000934 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000935 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000936 appendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000937 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000938
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000939 return Block;
940}
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000942CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek892697d2010-12-16 07:46:53 +0000943 AddStmtChoice asc) {
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000944 if (asc.alwaysAdd()) {
945 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000946 appendStmt(Block, U, asc);
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000947 }
948
Ted Kremenek892697d2010-12-16 07:46:53 +0000949 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan99cae5b2010-11-22 08:45:56 +0000950}
951
Ted Kremenek852274d2009-12-16 03:18:58 +0000952CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
953 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000954 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000955 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +0000956 appendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000958 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000959 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Ted Kremenek4f880632009-07-17 22:18:43 +0000961 // create the block evaluating the LHS
962 CFGBlock* LHSBlock = createBlock(false);
963 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Ted Kremenek4f880632009-07-17 22:18:43 +0000965 // create the block evaluating the RHS
966 Succ = ConfluenceBlock;
967 Block = NULL;
968 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000969
970 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000971 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000972 return 0;
Zhanyong Wan36f327c2010-11-22 19:32:14 +0000973 } else {
Ted Kremenek862b24f2010-04-29 01:10:26 +0000974 // Create an empty block for cases where the RHS doesn't require
975 // any explicit statements in the CFG.
976 RHSBlock = createBlock();
977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Mike Stump00998a02009-07-23 23:25:26 +0000979 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000980 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000981 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000982 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000983
Ted Kremenek4f880632009-07-17 22:18:43 +0000984 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000985 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000986 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
987 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000988 } else {
John McCall2de56d12010-08-25 11:45:40 +0000989 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000990 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
991 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000992 }
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Ted Kremenek4f880632009-07-17 22:18:43 +0000994 // Generate the blocks for evaluating the LHS.
995 Block = LHSBlock;
996 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000997 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +0000998
999 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +00001000 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001001 appendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +00001002 addStmt(B->getRHS());
1003 return addStmt(B->getLHS());
1004 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001005
1006 if (B->isAssignmentOp()) {
Zhongxing Xufc61d942010-06-03 06:23:18 +00001007 if (asc.alwaysAdd()) {
1008 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001009 appendStmt(Block, B, asc);
Zhongxing Xufc61d942010-06-03 06:23:18 +00001010 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001011 Visit(B->getLHS());
Marcin Swiderskie1667192010-10-24 08:21:40 +00001012 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +00001013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Marcin Swiderskie1667192010-10-24 08:21:40 +00001015 if (asc.alwaysAdd()) {
1016 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001017 appendStmt(Block, B, asc);
Marcin Swiderskie1667192010-10-24 08:21:40 +00001018 }
1019
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001020 CFGBlock *RBlock = Visit(B->getRHS());
1021 CFGBlock *LBlock = Visit(B->getLHS());
1022 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1023 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1024 // return RBlock. Otherwise we'll incorrectly return NULL.
1025 return (LBlock ? LBlock : RBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +00001026}
1027
Ted Kremenek852274d2009-12-16 03:18:58 +00001028CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1029 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +00001030 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001031 appendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +00001032 }
1033 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001034}
1035
Ted Kremenek4f880632009-07-17 22:18:43 +00001036CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1037 // "break" is a control-flow statement. Thus we stop processing the current
1038 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001039 if (badCFG)
1040 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Ted Kremenek4f880632009-07-17 22:18:43 +00001042 // Now create a new block that ends with the break statement.
1043 Block = createBlock(false);
1044 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenek4f880632009-07-17 22:18:43 +00001046 // If there is no target for the break, then we are looking at an incomplete
1047 // AST. This means that the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001048 if (BreakJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001049 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001050 AddSuccessor(Block, BreakJumpTarget.Block);
1051 } else
Ted Kremenek4f880632009-07-17 22:18:43 +00001052 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001053
1054
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001055 return Block;
1056}
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Mike Stump4c45aa12010-01-21 15:20:48 +00001058static bool CanThrow(Expr *E) {
1059 QualType Ty = E->getType();
1060 if (Ty->isFunctionPointerType())
1061 Ty = Ty->getAs<PointerType>()->getPointeeType();
1062 else if (Ty->isBlockPointerType())
1063 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +00001064
Mike Stump4c45aa12010-01-21 15:20:48 +00001065 const FunctionType *FT = Ty->getAs<FunctionType>();
1066 if (FT) {
1067 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1068 if (Proto->hasEmptyExceptionSpec())
1069 return false;
1070 }
1071 return true;
1072}
1073
Ted Kremenek852274d2009-12-16 03:18:58 +00001074CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001075 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +00001076 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +00001077 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +00001078 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +00001079 }
Mike Stump24556362009-07-25 21:26:53 +00001080
Mike Stump4c45aa12010-01-21 15:20:48 +00001081 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001082
1083 // Languages without exceptions are assumed to not throw.
1084 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +00001085 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +00001086 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +00001087 }
1088
1089 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +00001090 if (FD->hasAttr<NoReturnAttr>())
1091 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +00001092 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +00001093 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +00001094 }
Mike Stump24556362009-07-25 21:26:53 +00001095
Mike Stump4c45aa12010-01-21 15:20:48 +00001096 if (!CanThrow(C->getCallee()))
1097 AddEHEdge = false;
1098
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001099 if (!NoReturn && !AddEHEdge)
1100 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Mike Stump079bd722010-01-19 22:00:14 +00001102 if (Block) {
1103 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001104 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001105 return 0;
1106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Mike Stump079bd722010-01-19 22:00:14 +00001108 Block = createBlock(!NoReturn);
Ted Kremenek892697d2010-12-16 07:46:53 +00001109 appendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001110
Mike Stump079bd722010-01-19 22:00:14 +00001111 if (NoReturn) {
1112 // Wire this to the exit block directly.
1113 AddSuccessor(Block, &cfg->getExit());
1114 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001115 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001116 // Add exceptional edges.
1117 if (TryTerminatedBlock)
1118 AddSuccessor(Block, TryTerminatedBlock);
1119 else
1120 AddSuccessor(Block, &cfg->getExit());
1121 }
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Mike Stump24556362009-07-25 21:26:53 +00001123 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001124}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001125
Ted Kremenek852274d2009-12-16 03:18:58 +00001126CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1127 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001128 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001129 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001130 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001131 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001133 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001134 Succ = ConfluenceBlock;
1135 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001136 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001137 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001138 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001140 Succ = ConfluenceBlock;
1141 Block = NULL;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001142 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001143 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001144 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001146 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001147 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001148 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001149 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1150 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001151 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001152 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001153}
Mike Stump1eb44332009-09-09 15:08:12 +00001154
1155
1156CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001157 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001158 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001159
1160 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1161 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001162 // If we hit a segment of code just containing ';' (NullStmts), we can
1163 // get a null block back. In such cases, just use the LastBlock
1164 if (CFGBlock *newBlock = addStmt(*I))
1165 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001167 if (badCFG)
1168 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001169 }
Mike Stump079bd722010-01-19 22:00:14 +00001170
Ted Kremenek4f880632009-07-17 22:18:43 +00001171 return LastBlock;
1172}
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Ted Kremenek852274d2009-12-16 03:18:58 +00001174CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1175 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001176 // Create the confluence block that will "merge" the results of the ternary
1177 // expression.
1178 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001179 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001180 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001181 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001183 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001184
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001185 // Create a block for the LHS expression if there is an LHS expression. A
1186 // GCC extension allows LHS to be NULL, causing the condition to be the
1187 // value that is returned instead.
1188 // e.g: x ?: y is shorthand for: x ? x : y;
1189 Succ = ConfluenceBlock;
1190 Block = NULL;
1191 CFGBlock* LHSBlock = NULL;
1192 if (C->getLHS()) {
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001193 LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001194 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001195 return 0;
1196 Block = NULL;
1197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001199 // Create the block for the RHS expression.
1200 Succ = ConfluenceBlock;
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00001201 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001202 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001203 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001205 // Create the block that will contain the condition.
1206 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Mike Stump00998a02009-07-23 23:25:26 +00001208 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001209 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001210 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001211 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001212 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +00001213 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001214 // If we know the condition is false, add NULL as the successor for
1215 // the block containing the condition. In this case, the confluence
1216 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001217 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +00001218 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001219 } else {
1220 // If we have no LHS expression, add the ConfluenceBlock as a direct
1221 // successor for the block containing the condition. Moreover, we need to
1222 // reverse the order of the predecessors in the ConfluenceBlock because
1223 // the RHSBlock will have been added to the succcessors already, and we
1224 // want the first predecessor to the the block containing the expression
1225 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001226 AddSuccessor(Block, ConfluenceBlock);
Ted Kremeneke4ae4dc2010-11-15 22:59:22 +00001227 // Note that there can possibly only be one predecessor if one of the
1228 // subexpressions resulted in calling a noreturn function.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001229 std::reverse(ConfluenceBlock->pred_begin(),
1230 ConfluenceBlock->pred_end());
1231 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001232 }
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001234 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001235 Block->setTerminator(C);
1236 return addStmt(C->getCond());
1237}
1238
Ted Kremenek4f880632009-07-17 22:18:43 +00001239CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001240 if (DS->isSingleDecl())
1241 return VisitDeclSubExpr(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Ted Kremenek4f880632009-07-17 22:18:43 +00001243 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenek4f880632009-07-17 22:18:43 +00001245 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1246 typedef llvm::SmallVector<Decl*,10> BufTy;
1247 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Ted Kremenek4f880632009-07-17 22:18:43 +00001249 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1250 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1251 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1252 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek4f880632009-07-17 22:18:43 +00001254 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1255 // automatically freed with the CFG.
1256 DeclGroupRef DG(*I);
1257 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001258 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001259 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenek4f880632009-07-17 22:18:43 +00001261 // Append the fake DeclStmt to block.
Marcin Swiderski8599e762010-11-03 06:19:35 +00001262 B = VisitDeclSubExpr(DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001263 }
Mike Stump1eb44332009-09-09 15:08:12 +00001264
1265 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001266}
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek4f880632009-07-17 22:18:43 +00001268/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski8599e762010-11-03 06:19:35 +00001269/// DeclStmts and initializers in them.
1270CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1271 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekd34066c2008-02-26 00:22:58 +00001272
Marcin Swiderski8599e762010-11-03 06:19:35 +00001273 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Marcin Swiderski8599e762010-11-03 06:19:35 +00001275 if (!VD) {
1276 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001277 appendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001278 return Block;
Marcin Swiderski8599e762010-11-03 06:19:35 +00001279 }
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Marcin Swiderski8599e762010-11-03 06:19:35 +00001281 bool IsReference = false;
1282 bool HasTemporaries = false;
1283
1284 // Destructors of temporaries in initialization expression should be called
1285 // after initialization finishes.
Ted Kremenek4f880632009-07-17 22:18:43 +00001286 Expr *Init = VD->getInit();
Marcin Swiderski8599e762010-11-03 06:19:35 +00001287 if (Init) {
1288 IsReference = VD->getType()->isReferenceType();
John McCall4765fa02010-12-06 08:20:24 +00001289 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski8599e762010-11-03 06:19:35 +00001290
1291 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1292 // Generate destructors for temporaries in initialization expression.
John McCall4765fa02010-12-06 08:20:24 +00001293 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski8599e762010-11-03 06:19:35 +00001294 IsReference);
1295 }
1296 }
1297
1298 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001299 appendStmt(Block, DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Ted Kremenek4f880632009-07-17 22:18:43 +00001301 if (Init) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00001302 if (HasTemporaries)
1303 // For expression with temporaries go directly to subexpression to omit
1304 // generating destructors for the second time.
Ted Kremenek892697d2010-12-16 07:46:53 +00001305 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski8599e762010-11-03 06:19:35 +00001306 else
Ted Kremenek892697d2010-12-16 07:46:53 +00001307 Visit(Init);
Ted Kremenek4f880632009-07-17 22:18:43 +00001308 }
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Ted Kremenek4f880632009-07-17 22:18:43 +00001310 // If the type of VD is a VLA, then we must process its size expressions.
1311 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1312 VA = FindVA(VA->getElementType().getTypePtr()))
1313 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001315 // Remove variable from local scope.
1316 if (ScopePos && VD == *ScopePos)
1317 ++ScopePos;
1318
Ted Kremenek4f880632009-07-17 22:18:43 +00001319 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001320}
1321
1322CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001323 // We may see an if statement in the middle of a basic block, or it may be the
1324 // first statement we are processing. In either case, we create a new basic
1325 // block. First, we create the blocks for the then...else statements, and
1326 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001327 // middle of a block, we stop processing that block. That block is then the
1328 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001329
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001330 // Save local scope position because in case of condition variable ScopePos
1331 // won't be restored when traversing AST.
1332 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1333
1334 // Create local scope for possible condition variable.
1335 // Store scope position. Add implicit destructor.
1336 if (VarDecl* VD = I->getConditionVariable()) {
1337 LocalScope::const_iterator BeginScopePos = ScopePos;
1338 addLocalScopeForVarDecl(VD);
1339 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1340 }
1341
Mike Stump6d9828c2009-07-17 01:31:16 +00001342 // The block we were proccessing is now finished. Make it the successor
1343 // block.
1344 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001345 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001346 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001347 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001348 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001349
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001350 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001351 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001352
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001353 if (Stmt* Else = I->getElse()) {
1354 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001355
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001356 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001357 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001358 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001359
1360 // If branch is not a compound statement create implicit scope
1361 // and add destructors.
1362 if (!isa<CompoundStmt>(Else))
1363 addLocalScopeAndDtors(Else);
1364
Ted Kremenek4f880632009-07-17 22:18:43 +00001365 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001366
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001367 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1368 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001369 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001370 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001371 return 0;
1372 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001374
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001375 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001376 CFGBlock* ThenBlock;
1377 {
1378 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001379 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001380 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001381 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001382
1383 // If branch is not a compound statement create implicit scope
1384 // and add destructors.
1385 if (!isa<CompoundStmt>(Then))
1386 addLocalScopeAndDtors(Then);
1387
Ted Kremenek4f880632009-07-17 22:18:43 +00001388 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001389
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001390 if (!ThenBlock) {
1391 // We can reach here if the "then" body has all NullStmts.
1392 // Create an empty block so we can distinguish between true and false
1393 // branches in path-sensitive analyses.
1394 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001395 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001396 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001397 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001398 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001399 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001400 }
1401
Mike Stump6d9828c2009-07-17 01:31:16 +00001402 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001403 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001404
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001405 // Set the terminator of the new block to the If statement.
1406 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001407
Mike Stump00998a02009-07-23 23:25:26 +00001408 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001409 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001410
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001411 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001412 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1413 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001414
1415 // Add the condition as the last statement in the new block. This may create
1416 // new blocks as the condition may contain control-flow. Any newly created
1417 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001418 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001419
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001420 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1421 // and the condition variable initialization to the CFG.
1422 if (VarDecl *VD = I->getConditionVariable()) {
1423 if (Expr *Init = VD->getInit()) {
1424 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001425 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001426 addStmt(Init);
1427 }
1428 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001429
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001430 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001431}
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
1433
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001434CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001435 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001436 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001437 // NOTE: If a "return" appears in the middle of a block, this means that the
1438 // code afterwards is DEAD (unreachable). We still keep a basic block
1439 // for that code; a simple "mark-and-sweep" from the entry block will be
1440 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001441
1442 // Create the new block.
1443 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001445 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001446 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001447 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001448
1449 // Add the return statement to the block. This may create new blocks if R
1450 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001451 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001452}
1453
1454CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1455 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001456 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +00001457 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001458
Ted Kremenek4f880632009-07-17 22:18:43 +00001459 if (!LabelBlock) // This can happen when the body is empty, i.e.
1460 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001461
Ted Kremenek4f880632009-07-17 22:18:43 +00001462 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001463 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001464
1465 // Labels partition blocks, so this is the end of the basic block we were
1466 // processing (L is the block's label). Because this is label (and we have
1467 // already processed the substatement) there is no extra control-flow to worry
1468 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001469 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001470 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001471 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001472
1473 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001474 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001476 // This block is now the implicit successor of other blocks.
1477 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001478
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001479 return LabelBlock;
1480}
1481
1482CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001483 // Goto is a control-flow statement. Thus we stop processing the current
1484 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001485
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001486 Block = createBlock(false);
1487 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001488
1489 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001490 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001491
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001492 if (I == LabelMap.end())
1493 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001494 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1495 else {
1496 JumpTarget JT = I->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001497 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001498 AddSuccessor(Block, JT.Block);
1499 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001500
Mike Stump6d9828c2009-07-17 01:31:16 +00001501 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001502}
1503
1504CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001505 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001506
Marcin Swiderski47575f12010-10-01 01:38:14 +00001507 // Save local scope position because in case of condition variable ScopePos
1508 // won't be restored when traversing AST.
1509 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1510
1511 // Create local scope for init statement and possible condition variable.
1512 // Add destructor for init statement and condition variable.
1513 // Store scope position for continue statement.
1514 if (Stmt* Init = F->getInit())
1515 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001516 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1517
Marcin Swiderski47575f12010-10-01 01:38:14 +00001518 if (VarDecl* VD = F->getConditionVariable())
1519 addLocalScopeForVarDecl(VD);
1520 LocalScope::const_iterator ContinueScopePos = ScopePos;
1521
1522 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1523
Mike Stumpfefb9f72009-07-21 01:12:51 +00001524 // "for" is a control-flow statement. Thus we stop processing the current
1525 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001526 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001527 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001528 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001529 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001530 } else
1531 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001532
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001533 // Save the current value for the break targets.
1534 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001535 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001536 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001537
Mike Stump6d9828c2009-07-17 01:31:16 +00001538 // Because of short-circuit evaluation, the condition of the loop can span
1539 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1540 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001541 CFGBlock* ExitConditionBlock = createBlock(false);
1542 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001543
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001544 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001545 ExitConditionBlock->setTerminator(F);
1546
1547 // Now add the actual condition to the condition block. Because the condition
1548 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001549 if (Stmt* C = F->getCond()) {
1550 Block = ExitConditionBlock;
1551 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001552 assert(Block == EntryConditionBlock ||
1553 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001554
1555 // If this block contains a condition variable, add both the condition
1556 // variable and initializer to the CFG.
1557 if (VarDecl *VD = F->getConditionVariable()) {
1558 if (Expr *Init = VD->getInit()) {
1559 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001560 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001561 EntryConditionBlock = addStmt(Init);
1562 assert(Block == EntryConditionBlock);
1563 }
1564 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001565
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001566 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001567 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001568 return 0;
1569 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001570 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001571
Mike Stump6d9828c2009-07-17 01:31:16 +00001572 // The condition block is the implicit successor for the loop body as well as
1573 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001574 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001575
Mike Stump00998a02009-07-23 23:25:26 +00001576 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001577 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Mike Stump00998a02009-07-23 23:25:26 +00001579 if (F->getCond())
1580 KnownVal = TryEvaluateBool(F->getCond());
1581
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001582 // Now create the loop body.
1583 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001584 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001585
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001586 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001587 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1588 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Ted Kremenekaf603f72007-08-30 18:39:40 +00001590 // Create a new block to contain the (bottom) of the loop body.
1591 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001592
1593 // Loop body should end with destructor of Condition variable (if any).
1594 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001595
Ted Kremeneke9334502008-09-04 21:48:47 +00001596 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001597 // Generate increment code in its own basic block. This is the target of
1598 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001599 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001600 } else {
1601 // No increment code. Create a special, empty, block that is used as the
1602 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001603 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001604 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001605 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001606
Ted Kremenek3575f842009-04-28 00:51:56 +00001607 // Finish up the increment (or empty) block if it hasn't been already.
1608 if (Block) {
1609 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001610 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001611 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001612 Block = 0;
1613 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001614
Marcin Swiderski47575f12010-10-01 01:38:14 +00001615 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001616
Ted Kremenek3575f842009-04-28 00:51:56 +00001617 // The starting block for the loop increment is the block that should
1618 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001619 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001620
Marcin Swiderski47575f12010-10-01 01:38:14 +00001621 // If body is not a compound statement create implicit scope
1622 // and add destructors.
1623 if (!isa<CompoundStmt>(F->getBody()))
1624 addLocalScopeAndDtors(F->getBody());
1625
Mike Stump6d9828c2009-07-17 01:31:16 +00001626 // Now populate the body block, and in the process create new blocks as we
1627 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001628 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001629
1630 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001631 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001632 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001633 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001634
Ted Kremenek941fde82009-07-24 04:47:11 +00001635 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001636 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001637 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001638
Ted Kremenek941fde82009-07-24 04:47:11 +00001639 // Link up the condition block with the code that follows the loop. (the
1640 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001641 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001642
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001643 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001644 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001645 if (Stmt* I = F->getInit()) {
1646 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001647 return addStmt(I);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001648 }
Zhanyong Wan36f327c2010-11-22 19:32:14 +00001649
1650 // There is no loop initialization. We are thus basically a while loop.
1651 // NULL out Block to force lazy block construction.
1652 Block = NULL;
1653 Succ = EntryConditionBlock;
1654 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001655}
1656
Ted Kremenek115c1b92010-04-11 17:02:10 +00001657CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1658 if (asc.alwaysAdd()) {
1659 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001660 appendStmt(Block, M, asc);
Ted Kremenek115c1b92010-04-11 17:02:10 +00001661 }
Ted Kremenek892697d2010-12-16 07:46:53 +00001662 return Visit(M->getBase());
Ted Kremenek115c1b92010-04-11 17:02:10 +00001663}
1664
Ted Kremenek514de5a2008-11-11 17:10:00 +00001665CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1666 // Objective-C fast enumeration 'for' statements:
1667 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1668 //
1669 // for ( Type newVariable in collection_expression ) { statements }
1670 //
1671 // becomes:
1672 //
1673 // prologue:
1674 // 1. collection_expression
1675 // T. jump to loop_entry
1676 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001677 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001678 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1679 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1680 // TB:
1681 // statements
1682 // T. jump to loop_entry
1683 // FB:
1684 // what comes after
1685 //
1686 // and
1687 //
1688 // Type existingItem;
1689 // for ( existingItem in expression ) { statements }
1690 //
1691 // becomes:
1692 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001693 // the same with newVariable replaced with existingItem; the binding works
1694 // the same except that for one ObjCForCollectionStmt::getElement() returns
1695 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001696 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001697
Ted Kremenek514de5a2008-11-11 17:10:00 +00001698 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001699
Ted Kremenek514de5a2008-11-11 17:10:00 +00001700 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001701 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001702 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001703 LoopSuccessor = Block;
1704 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001705 } else
1706 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001707
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001708 // Build the condition blocks.
1709 CFGBlock* ExitConditionBlock = createBlock(false);
1710 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001711
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001712 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001713 ExitConditionBlock->setTerminator(S);
1714
1715 // The last statement in the block should be the ObjCForCollectionStmt, which
1716 // performs the actual binding to 'element' and determines if there are any
1717 // more items in the collection.
Ted Kremenek892697d2010-12-16 07:46:53 +00001718 appendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001719 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001720
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001721 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001722 // generate new blocks as necesary. We DON'T add the statement by default to
1723 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001724 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001725 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001726 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001727 return 0;
1728 Block = 0;
1729 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001730
1731 // The condition block is the implicit successor for the loop body as well as
1732 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001733 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001734
Ted Kremenek514de5a2008-11-11 17:10:00 +00001735 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001736 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001737 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001738 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1739 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1740 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001742 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1743 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001744
Ted Kremenek4f880632009-07-17 22:18:43 +00001745 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001746
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001747 if (!BodyBlock)
1748 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001749 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001750 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001751 return 0;
1752 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001753
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001754 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001755 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001756 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001757
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001758 // Link up the condition block with the code that follows the loop.
1759 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001760 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001761
Ted Kremenek514de5a2008-11-11 17:10:00 +00001762 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001763 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001764 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001765}
1766
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001767CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1768 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001769
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001770 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001771 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001772
Ted Kremenekda5348e2009-05-05 23:11:51 +00001773 // The sync body starts its own basic block. This makes it a little easier
1774 // for diagnostic clients.
1775 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001776 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001777 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001778
Ted Kremenekda5348e2009-05-05 23:11:51 +00001779 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001780 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001781 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001782
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001783 // Add the @synchronized to the CFG.
1784 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001785 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001786
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001787 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001788 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001789}
Mike Stump6d9828c2009-07-17 01:31:16 +00001790
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001791CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001792 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001793 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001794}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001795
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001796CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001797 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001799 // Save local scope position because in case of condition variable ScopePos
1800 // won't be restored when traversing AST.
1801 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1802
1803 // Create local scope for possible condition variable.
1804 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001805 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001806 if (VarDecl* VD = W->getConditionVariable()) {
1807 addLocalScopeForVarDecl(VD);
1808 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1809 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001810
Mike Stumpfefb9f72009-07-21 01:12:51 +00001811 // "while" is a control-flow statement. Thus we stop processing the current
1812 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001813 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001814 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001815 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001816 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001817 } else
1818 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001819
1820 // Because of short-circuit evaluation, the condition of the loop can span
1821 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1822 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001823 CFGBlock* ExitConditionBlock = createBlock(false);
1824 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001825
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001826 // Set the terminator for the "exit" condition block.
1827 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001828
1829 // Now add the actual condition to the condition block. Because the condition
1830 // itself may contain control-flow, new blocks may be created. Thus we update
1831 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001832 if (Stmt* C = W->getCond()) {
1833 Block = ExitConditionBlock;
1834 EntryConditionBlock = addStmt(C);
Zhongxing Xua1898dd2010-10-27 03:23:10 +00001835 // The condition might finish the current 'Block'.
1836 Block = EntryConditionBlock;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001837
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001838 // If this block contains a condition variable, add both the condition
1839 // variable and initializer to the CFG.
1840 if (VarDecl *VD = W->getConditionVariable()) {
1841 if (Expr *Init = VD->getInit()) {
1842 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00001843 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001844 EntryConditionBlock = addStmt(Init);
1845 assert(Block == EntryConditionBlock);
1846 }
1847 }
1848
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001849 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001850 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001851 return 0;
1852 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001853 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001854
1855 // The condition block is the implicit successor for the loop body as well as
1856 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001857 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001858
Mike Stump00998a02009-07-23 23:25:26 +00001859 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001860 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001861
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001862 // Process the loop body.
1863 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001864 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001865
1866 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001867 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1868 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1869 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001870
Mike Stump6d9828c2009-07-17 01:31:16 +00001871 // Create an empty block to represent the transition block for looping back
1872 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001873 Block = 0;
1874 assert(Succ == EntryConditionBlock);
1875 Succ = createBlock();
1876 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001877 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001878
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001879 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001880 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001882 // NULL out Block to force lazy instantiation of blocks for the body.
1883 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001884
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001885 // Loop body should end with destructor of Condition variable (if any).
1886 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1887
1888 // If body is not a compound statement create implicit scope
1889 // and add destructors.
1890 if (!isa<CompoundStmt>(W->getBody()))
1891 addLocalScopeAndDtors(W->getBody());
1892
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001893 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001894 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001895
Ted Kremenekaf603f72007-08-30 18:39:40 +00001896 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001897 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001898 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001899 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001900 return 0;
1901 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
Ted Kremenek941fde82009-07-24 04:47:11 +00001903 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001904 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001905 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001906
Ted Kremenek941fde82009-07-24 04:47:11 +00001907 // Link up the condition block with the code that follows the loop. (the
1908 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001909 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001910
1911 // There can be no more statements in the condition block since we loop back
1912 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001913 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001914
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001915 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001916 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001917 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001918}
Mike Stump1eb44332009-09-09 15:08:12 +00001919
1920
Ted Kremenek4f880632009-07-17 22:18:43 +00001921CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1922 // FIXME: For now we pretend that @catch and the code it contains does not
1923 // exit.
1924 return Block;
1925}
Mike Stump6d9828c2009-07-17 01:31:16 +00001926
Ted Kremenek2fda5042008-12-09 20:20:09 +00001927CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1928 // FIXME: This isn't complete. We basically treat @throw like a return
1929 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001930
Ted Kremenek6c249722009-09-24 18:45:41 +00001931 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001932 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001933 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001934
Ted Kremenek2fda5042008-12-09 20:20:09 +00001935 // Create the new block.
1936 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001937
Ted Kremenek2fda5042008-12-09 20:20:09 +00001938 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001939 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001940
1941 // Add the statement to the block. This may create new blocks if S contains
1942 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001943 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001944}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001945
Mike Stump0979d802009-07-22 22:56:04 +00001946CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001947 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001948 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001949 return 0;
1950
1951 // Create the new block.
1952 Block = createBlock(false);
1953
Mike Stump5d1d2022010-01-19 02:20:09 +00001954 if (TryTerminatedBlock)
1955 // The current try statement is the only successor.
1956 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001957 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001958 // otherwise the Exit block is the only successor.
1959 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001960
1961 // Add the statement to the block. This may create new blocks if S contains
1962 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001963 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001964}
1965
Ted Kremenek4f880632009-07-17 22:18:43 +00001966CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001967 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Mike Stump8f9893a2009-07-21 01:27:50 +00001969 // "do...while" is a control-flow statement. Thus we stop processing the
1970 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001971 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001972 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001973 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001974 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001975 } else
1976 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001977
1978 // Because of short-circuit evaluation, the condition of the loop can span
1979 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1980 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001981 CFGBlock* ExitConditionBlock = createBlock(false);
1982 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001983
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001984 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001985 ExitConditionBlock->setTerminator(D);
1986
1987 // Now add the actual condition to the condition block. Because the condition
1988 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001989 if (Stmt* C = D->getCond()) {
1990 Block = ExitConditionBlock;
1991 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001992 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001993 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001994 return 0;
1995 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001996 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001997
Ted Kremenek54827132008-02-27 07:20:00 +00001998 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001999 Succ = EntryConditionBlock;
2000
Mike Stump00998a02009-07-23 23:25:26 +00002001 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00002002 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00002003
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002004 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002005 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002006 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002007 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002008
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002009 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002010 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2011 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2012 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00002013
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002014 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002015 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002016
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002017 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002018 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002019
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002020 // NULL out Block to force lazy instantiation of blocks for the body.
2021 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002022
Marcin Swiderski05adedc2010-10-01 01:14:17 +00002023 // If body is not a compound statement create implicit scope
2024 // and add destructors.
2025 if (!isa<CompoundStmt>(D->getBody()))
2026 addLocalScopeAndDtors(D->getBody());
2027
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002028 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00002029 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00002030
Ted Kremenekaf603f72007-08-30 18:39:40 +00002031 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00002032 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002033 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002034 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002035 return 0;
2036 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002037
Ted Kremenekd173dc72010-08-17 20:59:56 +00002038 if (!KnownVal.isFalse()) {
2039 // Add an intermediate block between the BodyBlock and the
2040 // ExitConditionBlock to represent the "loop back" transition. Create an
2041 // empty block to represent the transition block for looping back to the
2042 // head of the loop.
2043 // FIXME: Can we do this more efficiently without adding another block?
2044 Block = NULL;
2045 Succ = BodyBlock;
2046 CFGBlock *LoopBackBlock = createBlock();
2047 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00002048
Ted Kremenekd173dc72010-08-17 20:59:56 +00002049 // Add the loop body entry as a successor to the condition.
2050 AddSuccessor(ExitConditionBlock, LoopBackBlock);
2051 }
2052 else
2053 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002054 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002055
Ted Kremenek941fde82009-07-24 04:47:11 +00002056 // Link up the condition block with the code that follows the loop.
2057 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002058 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00002059
2060 // There can be no more statements in the body block(s) since we loop back to
2061 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002062 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002063
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002064 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00002065 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002066 return BodyBlock;
2067}
2068
2069CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2070 // "continue" is a control-flow statement. Thus we stop processing the
2071 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002072 if (badCFG)
2073 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002074
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002075 // Now create a new block that ends with the continue statement.
2076 Block = createBlock(false);
2077 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00002078
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002079 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002080 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002081 if (ContinueJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00002082 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002083 AddSuccessor(Block, ContinueJumpTarget.Block);
2084 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00002085 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00002086
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002087 return Block;
2088}
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002090CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00002091 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002092
Ted Kremenek852274d2009-12-16 03:18:58 +00002093 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002094 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002095 appendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002096 }
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Ted Kremenek4f880632009-07-17 22:18:43 +00002098 // VLA types have expressions that must be evaluated.
2099 if (E->isArgumentType()) {
2100 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
2101 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2102 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002103 }
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Mike Stump6d9828c2009-07-17 01:31:16 +00002105 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002106}
Mike Stump1eb44332009-09-09 15:08:12 +00002107
Ted Kremenek4f880632009-07-17 22:18:43 +00002108/// VisitStmtExpr - Utility method to handle (nested) statement
2109/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00002110CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2111 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002112 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002113 appendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002114 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002115 return VisitCompoundStmt(SE->getSubStmt());
2116}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002117
Ted Kremenek411cdee2008-04-16 21:10:48 +00002118CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002119 // "switch" is a control-flow statement. Thus we stop processing the current
2120 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002121 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002122
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002123 // Save local scope position because in case of condition variable ScopePos
2124 // won't be restored when traversing AST.
2125 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2126
2127 // Create local scope for possible condition variable.
2128 // Store scope position. Add implicit destructor.
2129 if (VarDecl* VD = Terminator->getConditionVariable()) {
2130 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2131 addLocalScopeForVarDecl(VD);
2132 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2133 }
2134
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002135 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002136 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002137 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002138 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002139 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002140
2141 // Save the current "switch" context.
2142 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002143 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002144 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002145
Mike Stump6d9828c2009-07-17 01:31:16 +00002146 // Set the "default" case to be the block after the switch statement. If the
2147 // switch statement contains a "default:", this value will be overwritten with
2148 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002149 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002150
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002151 // Create a new block that will contain the switch statement.
2152 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002153
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002154 // Now process the switch body. The code after the switch is the implicit
2155 // successor.
2156 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002157 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002158
2159 // When visiting the body, the case statements should automatically get linked
2160 // up to the switch. We also don't keep a pointer to the body, since all
2161 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002162 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002163 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002164
2165 // If body is not a compound statement create implicit scope
2166 // and add destructors.
2167 if (!isa<CompoundStmt>(Terminator->getBody()))
2168 addLocalScopeAndDtors(Terminator->getBody());
2169
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002170 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002171 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002172 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002173 return 0;
2174 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002175
Mike Stump6d9828c2009-07-17 01:31:16 +00002176 // If we have no "default:" case, the default transition is to the code
2177 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002178 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002179
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002180 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002181 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002182 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002183 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002184 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002185
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002186 // Finally, if the SwitchStmt contains a condition variable, add both the
2187 // SwitchStmt and the condition variable initialization to the CFG.
2188 if (VarDecl *VD = Terminator->getConditionVariable()) {
2189 if (Expr *Init = VD->getInit()) {
2190 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002191 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002192 addStmt(Init);
2193 }
2194 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002195
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002196 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002197}
2198
Ted Kremenek4f880632009-07-17 22:18:43 +00002199CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002200 // CaseStmts are essentially labels, so they are the first statement in a
2201 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002202 CFGBlock *TopBlock = 0, *LastBlock = 0;
2203
2204 if (Stmt *Sub = CS->getSubStmt()) {
2205 // For deeply nested chains of CaseStmts, instead of doing a recursion
2206 // (which can blow out the stack), manually unroll and create blocks
2207 // along the way.
2208 while (isa<CaseStmt>(Sub)) {
2209 CFGBlock *CurrentBlock = createBlock(false);
2210 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002211
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002212 if (TopBlock)
2213 AddSuccessor(LastBlock, CurrentBlock);
2214 else
2215 TopBlock = CurrentBlock;
2216
2217 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2218 LastBlock = CurrentBlock;
2219
2220 CS = cast<CaseStmt>(Sub);
2221 Sub = CS->getSubStmt();
2222 }
2223
2224 addStmt(Sub);
2225 }
Mike Stump1eb44332009-09-09 15:08:12 +00002226
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002227 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002228 if (!CaseBlock)
2229 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002230
2231 // Cases statements partition blocks, so this is the top of the basic block we
2232 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002233 CaseBlock->setLabel(CS);
2234
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002235 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002236 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002237
2238 // Add this block to the list of successors for the block with the switch
2239 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002240 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002241 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002242
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002243 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2244 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002245
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002246 if (TopBlock) {
2247 AddSuccessor(LastBlock, CaseBlock);
2248 Succ = TopBlock;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002249 } else {
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002250 // This block is now the implicit successor of other blocks.
2251 Succ = CaseBlock;
2252 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002253
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002254 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002255}
Mike Stump6d9828c2009-07-17 01:31:16 +00002256
Ted Kremenek411cdee2008-04-16 21:10:48 +00002257CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002258 if (Terminator->getSubStmt())
2259 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002261 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002262
2263 if (!DefaultCaseBlock)
2264 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002265
2266 // Default statements partition blocks, so this is the top of the basic block
2267 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002268 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002270 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002271 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002272
Mike Stump6d9828c2009-07-17 01:31:16 +00002273 // Unlike case statements, we don't add the default block to the successors
2274 // for the switch statement immediately. This is done when we finish
2275 // processing the switch statement. This allows for the default case
2276 // (including a fall-through to the code after the switch statement) to always
2277 // be the last successor of a switch-terminated block.
2278
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002279 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2280 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002281
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002282 // This block is now the implicit successor of other blocks.
2283 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002284
2285 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002286}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002287
Mike Stump5d1d2022010-01-19 02:20:09 +00002288CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2289 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2290 // current block.
2291 CFGBlock* TrySuccessor = NULL;
2292
2293 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002294 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002295 return 0;
2296 TrySuccessor = Block;
2297 } else TrySuccessor = Succ;
2298
Mike Stumpa1f93632010-01-20 01:15:34 +00002299 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002300
2301 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002302 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002303 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002304 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002305
Mike Stumpa1f93632010-01-20 01:15:34 +00002306 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002307 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2308 // The code after the try is the implicit successor.
2309 Succ = TrySuccessor;
2310 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002311 if (CS->getExceptionDecl() == 0) {
2312 HasCatchAll = true;
2313 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002314 Block = NULL;
2315 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2316 if (CatchBlock == 0)
2317 return 0;
2318 // Add this block to the list of successors for the block with the try
2319 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002320 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002321 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002322 if (!HasCatchAll) {
2323 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00002324 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002325 else
Mike Stumpf00cca52010-01-20 01:30:58 +00002326 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002327 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002328
2329 // The code after the try is the implicit successor.
2330 Succ = TrySuccessor;
2331
Mike Stumpf00cca52010-01-20 01:30:58 +00002332 // Save the current "try" context.
2333 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2334 TryTerminatedBlock = NewTryTerminatedBlock;
2335
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002336 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002337 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002338 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002339 return Block;
2340}
2341
2342CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2343 // CXXCatchStmt are treated like labels, so they are the first statement in a
2344 // block.
2345
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002346 // Save local scope position because in case of exception variable ScopePos
2347 // won't be restored when traversing AST.
2348 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2349
2350 // Create local scope for possible exception variable.
2351 // Store scope position. Add implicit destructor.
2352 if (VarDecl* VD = CS->getExceptionDecl()) {
2353 LocalScope::const_iterator BeginScopePos = ScopePos;
2354 addLocalScopeForVarDecl(VD);
2355 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2356 }
2357
Mike Stump5d1d2022010-01-19 02:20:09 +00002358 if (CS->getHandlerBlock())
2359 addStmt(CS->getHandlerBlock());
2360
2361 CFGBlock* CatchBlock = Block;
2362 if (!CatchBlock)
2363 CatchBlock = createBlock();
2364
2365 CatchBlock->setLabel(CS);
2366
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002367 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002368 return 0;
2369
2370 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2371 Block = NULL;
2372
2373 return CatchBlock;
2374}
2375
John McCall4765fa02010-12-06 08:20:24 +00002376CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski8599e762010-11-03 06:19:35 +00002377 AddStmtChoice asc) {
2378 if (BuildOpts.AddImplicitDtors) {
2379 // If adding implicit destructors visit the full expression for adding
2380 // destructors of temporaries.
2381 VisitForTemporaryDtors(E->getSubExpr());
2382
2383 // Full expression has to be added as CFGStmt so it will be sequenced
2384 // before destructors of it's temporaries.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002385 asc = asc.withAlwaysAdd(true);
Marcin Swiderski8599e762010-11-03 06:19:35 +00002386 }
2387 return Visit(E->getSubExpr(), asc);
2388}
2389
Zhongxing Xua725ed42010-11-01 13:04:58 +00002390CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2391 AddStmtChoice asc) {
2392 if (asc.alwaysAdd()) {
2393 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002394 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002395
2396 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002397 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002398 }
2399 return Visit(E->getSubExpr(), asc);
2400}
2401
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002402CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2403 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002404 autoCreateBlock();
Zhongxing Xu3ff5b262010-11-03 11:14:06 +00002405 if (!C->isElidable())
Ted Kremenek892697d2010-12-16 07:46:53 +00002406 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002407
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002408 return VisitChildren(C);
2409}
2410
Zhongxing Xua725ed42010-11-01 13:04:58 +00002411CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2412 AddStmtChoice asc) {
2413 if (asc.alwaysAdd()) {
2414 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002415 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002416 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wan94a3dcf2010-11-24 03:28:53 +00002417 asc = asc.withAlwaysAdd(false);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002418 }
2419 return Visit(E->getSubExpr(), asc);
2420}
2421
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002422CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2423 AddStmtChoice asc) {
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002424 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002425 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00002426 return VisitChildren(C);
2427}
2428
Ted Kremenekad5a8942010-08-02 23:46:59 +00002429CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002430 AddStmtChoice asc) {
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002431 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002432 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002433 return VisitChildren(C);
2434}
2435
Zhongxing Xua725ed42010-11-01 13:04:58 +00002436CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2437 AddStmtChoice asc) {
2438 if (asc.alwaysAdd()) {
2439 autoCreateBlock();
Ted Kremenek892697d2010-12-16 07:46:53 +00002440 appendStmt(Block, E, asc);
Zhongxing Xua725ed42010-11-01 13:04:58 +00002441 }
Ted Kremenek892697d2010-12-16 07:46:53 +00002442 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xua725ed42010-11-01 13:04:58 +00002443}
2444
Ted Kremenek19bb3562007-08-28 19:26:49 +00002445CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002446 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002447 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002448
Ted Kremenek19bb3562007-08-28 19:26:49 +00002449 if (!IBlock) {
2450 IBlock = createBlock(false);
2451 cfg->setIndirectGotoBlock(IBlock);
2452 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002453
Ted Kremenek19bb3562007-08-28 19:26:49 +00002454 // IndirectGoto is a control-flow statement. Thus we stop processing the
2455 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002456 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002457 return 0;
2458
Ted Kremenek19bb3562007-08-28 19:26:49 +00002459 Block = createBlock(false);
2460 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002461 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002462 return addStmt(I->getTarget());
2463}
2464
Marcin Swiderski8599e762010-11-03 06:19:35 +00002465CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2466tryAgain:
2467 if (!E) {
2468 badCFG = true;
2469 return NULL;
2470 }
2471 switch (E->getStmtClass()) {
2472 default:
2473 return VisitChildrenForTemporaryDtors(E);
2474
2475 case Stmt::BinaryOperatorClass:
2476 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2477
2478 case Stmt::CXXBindTemporaryExprClass:
2479 return VisitCXXBindTemporaryExprForTemporaryDtors(
2480 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2481
2482 case Stmt::ConditionalOperatorClass:
2483 return VisitConditionalOperatorForTemporaryDtors(
2484 cast<ConditionalOperator>(E), BindToTemporary);
2485
2486 case Stmt::ImplicitCastExprClass:
2487 // For implicit cast we want BindToTemporary to be passed further.
2488 E = cast<CastExpr>(E)->getSubExpr();
2489 goto tryAgain;
2490
2491 case Stmt::ParenExprClass:
2492 E = cast<ParenExpr>(E)->getSubExpr();
2493 goto tryAgain;
2494 }
2495}
2496
2497CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2498 // When visiting children for destructors we want to visit them in reverse
2499 // order. Because there's no reverse iterator for children must to reverse
2500 // them in helper vector.
2501 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2502 ChildrenVect ChildrenRev;
2503 for (Stmt::child_iterator I = E->child_begin(), L = E->child_end();
2504 I != L; ++I) {
2505 if (*I) ChildrenRev.push_back(*I);
2506 }
2507
2508 CFGBlock *B = Block;
2509 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2510 L = ChildrenRev.rend(); I != L; ++I) {
2511 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2512 B = R;
2513 }
2514 return B;
2515}
2516
2517CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2518 if (E->isLogicalOp()) {
2519 // Destructors for temporaries in LHS expression should be called after
2520 // those for RHS expression. Even if this will unnecessarily create a block,
2521 // this block will be used at least by the full expression.
2522 autoCreateBlock();
2523 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2524 if (badCFG)
2525 return NULL;
2526
2527 Succ = ConfluenceBlock;
2528 Block = NULL;
2529 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2530
2531 if (RHSBlock) {
2532 if (badCFG)
2533 return NULL;
2534
2535 // If RHS expression did produce destructors we need to connect created
2536 // blocks to CFG in same manner as for binary operator itself.
2537 CFGBlock *LHSBlock = createBlock(false);
2538 LHSBlock->setTerminator(CFGTerminator(E, true));
2539
2540 // For binary operator LHS block is before RHS in list of predecessors
2541 // of ConfluenceBlock.
2542 std::reverse(ConfluenceBlock->pred_begin(),
2543 ConfluenceBlock->pred_end());
2544
2545 // See if this is a known constant.
2546 TryResult KnownVal = TryEvaluateBool(E->getLHS());
2547 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2548 KnownVal.negate();
2549
2550 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2551 // itself.
2552 if (E->getOpcode() == BO_LOr) {
2553 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2554 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2555 } else {
2556 assert (E->getOpcode() == BO_LAnd);
2557 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2558 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2559 }
2560
2561 Block = LHSBlock;
2562 return LHSBlock;
2563 }
2564
2565 Block = ConfluenceBlock;
2566 return ConfluenceBlock;
2567 }
2568
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002569 if (E->isAssignmentOp()) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002570 // For assignment operator (=) LHS expression is visited
2571 // before RHS expression. For destructors visit them in reverse order.
2572 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2573 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2574 return LHSBlock ? LHSBlock : RHSBlock;
2575 }
2576
2577 // For any other binary operator RHS expression is visited before
2578 // LHS expression (order of children). For destructors visit them in reverse
2579 // order.
2580 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2581 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2582 return RHSBlock ? RHSBlock : LHSBlock;
2583}
2584
2585CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2586 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2587 // First add destructors for temporaries in subexpression.
2588 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xu249c9452010-11-14 15:23:50 +00002589 if (!BindToTemporary) {
Marcin Swiderski8599e762010-11-03 06:19:35 +00002590 // If lifetime of temporary is not prolonged (by assigning to constant
2591 // reference) add destructor for it.
2592 autoCreateBlock();
2593 appendTemporaryDtor(Block, E);
2594 B = Block;
2595 }
2596 return B;
2597}
2598
2599CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2600 ConditionalOperator *E, bool BindToTemporary) {
2601 // First add destructors for condition expression. Even if this will
2602 // unnecessarily create a block, this block will be used at least by the full
2603 // expression.
2604 autoCreateBlock();
2605 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2606 if (badCFG)
2607 return NULL;
2608
2609 // Try to add block with destructors for LHS expression.
2610 CFGBlock *LHSBlock = NULL;
2611 if (E->getLHS()) {
2612 Succ = ConfluenceBlock;
2613 Block = NULL;
2614 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2615 if (badCFG)
2616 return NULL;
2617 }
2618
2619 // Try to add block with destructors for RHS expression;
2620 Succ = ConfluenceBlock;
2621 Block = NULL;
2622 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2623 if (badCFG)
2624 return NULL;
2625
2626 if (!RHSBlock && !LHSBlock) {
2627 // If neither LHS nor RHS expression had temporaries to destroy don't create
2628 // more blocks.
2629 Block = ConfluenceBlock;
2630 return Block;
2631 }
2632
2633 Block = createBlock(false);
2634 Block->setTerminator(CFGTerminator(E, true));
2635
2636 // See if this is a known constant.
2637 const TryResult &KnownVal = TryEvaluateBool(E->getCond());
2638
2639 if (LHSBlock) {
2640 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
2641 } else if (KnownVal.isFalse()) {
2642 AddSuccessor(Block, NULL);
2643 } else {
2644 AddSuccessor(Block, ConfluenceBlock);
2645 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2646 }
2647
2648 if (!RHSBlock)
2649 RHSBlock = ConfluenceBlock;
2650 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
2651
2652 return Block;
2653}
2654
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002655} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002656
Mike Stump6d9828c2009-07-17 01:31:16 +00002657/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2658/// no successors or predecessors. If this is the first block created in the
2659/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002660CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002661 bool first_block = begin() == end();
2662
2663 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002664 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2665 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2666 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002667
2668 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002669 if (first_block)
2670 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002671
2672 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002673 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002674}
2675
Ted Kremenek026473c2007-08-23 16:51:22 +00002676/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2677/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002678CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002679 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002680 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002681 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002682}
2683
Ted Kremenek63f58872007-10-01 19:33:33 +00002684//===----------------------------------------------------------------------===//
2685// CFG: Queries for BlkExprs.
2686//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002687
Ted Kremenek63f58872007-10-01 19:33:33 +00002688namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002689 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002690}
2691
Ted Kremenek8a693662009-12-23 23:37:10 +00002692static void FindSubExprAssignments(Stmt *S,
2693 llvm::SmallPtrSet<Expr*,50>& Set) {
2694 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002695 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002696
Ted Kremenek8a693662009-12-23 23:37:10 +00002697 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002698 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002699 if (!child)
2700 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002701
Ted Kremenek8a693662009-12-23 23:37:10 +00002702 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002703 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002704
Ted Kremenek8a693662009-12-23 23:37:10 +00002705 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002706 }
2707}
2708
Ted Kremenek63f58872007-10-01 19:33:33 +00002709static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2710 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002711
2712 // Look for assignments that are used as subexpressions. These are the only
2713 // assignments that we want to *possibly* register as a block-level
2714 // expression. Basically, if an assignment occurs both in a subexpression and
2715 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002716 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002717
Ted Kremenek63f58872007-10-01 19:33:33 +00002718 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002719 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002720 if (CFGStmt S = BI->getAs<CFGStmt>())
2721 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002722
Ted Kremenek411cdee2008-04-16 21:10:48 +00002723 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002724
2725 // Iterate over the statements again on identify the Expr* and Stmt* at the
2726 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002727
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002728 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2729 CFGStmt CS = BI->getAs<CFGStmt>();
2730 if (!CS.isValid())
2731 continue;
2732 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002733
Ted Kremenek411cdee2008-04-16 21:10:48 +00002734 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002735 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002736 // expression are really "statements" whose value is never used by
2737 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002738 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002739 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002740 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2741 // Special handling for statement expressions. The last statement in
2742 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002743 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002744 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002745 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002746 (*M)[C->body_back()] = x;
2747 }
2748 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002749
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002750 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002751 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002752 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002753 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002754
Ted Kremenek411cdee2008-04-16 21:10:48 +00002755 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002756
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002757 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002758
Ted Kremenek390e48b2008-11-12 21:11:49 +00002759 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002760 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002761 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002762 }
2763 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002764
Ted Kremenek63f58872007-10-01 19:33:33 +00002765 return M;
2766}
2767
Ted Kremenek86946742008-01-17 20:48:37 +00002768CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2769 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002770 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002771
Ted Kremenek63f58872007-10-01 19:33:33 +00002772 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002773 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002774 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002775}
2776
2777unsigned CFG::getNumBlkExprs() {
2778 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2779 return M->size();
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002780
2781 // We assume callers interested in the number of BlkExprs will want
2782 // the map constructed if it doesn't already exist.
2783 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2784 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenek63f58872007-10-01 19:33:33 +00002785}
2786
Ted Kremenek274f4332008-04-28 18:00:46 +00002787//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002788// Filtered walking of the CFG.
2789//===----------------------------------------------------------------------===//
2790
2791bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002792 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002793
2794 if (F.IgnoreDefaultsWithCoveredEnums) {
2795 // If the 'To' has no label or is labeled but the label isn't a
2796 // CaseStmt then filter this edge.
2797 if (const SwitchStmt *S =
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00002798 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002799 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002800 const Stmt *L = To->getLabel();
2801 if (!L || !isa<CaseStmt>(L))
2802 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002803 }
2804 }
2805 }
2806
2807 return false;
2808}
2809
2810//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002811// Cleanup: CFG dstor.
2812//===----------------------------------------------------------------------===//
2813
Ted Kremenek63f58872007-10-01 19:33:33 +00002814CFG::~CFG() {
2815 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2816}
Mike Stump6d9828c2009-07-17 01:31:16 +00002817
Ted Kremenek7dba8602007-08-29 21:56:09 +00002818//===----------------------------------------------------------------------===//
2819// CFG pretty printing
2820//===----------------------------------------------------------------------===//
2821
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002822namespace {
2823
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002824class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002825 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002826 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002827 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002828 DeclMapTy DeclMap;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002829 signed CurrentBlock;
2830 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002831 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002832public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002833
Chris Lattnere4f21422009-06-30 01:26:17 +00002834 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2835 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002836 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2837 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002838 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002839 BI != BEnd; ++BI, ++j ) {
2840 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2841 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2842 StmtMap[SE] = P;
2843
2844 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2845 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan36f327c2010-11-22 19:32:14 +00002846
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002847 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2848 if (VarDecl* VD = IS->getConditionVariable())
2849 DeclMap[VD] = P;
2850
2851 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2852 if (VarDecl* VD = FS->getConditionVariable())
2853 DeclMap[VD] = P;
2854
2855 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2856 if (VarDecl* VD = WS->getConditionVariable())
2857 DeclMap[VD] = P;
2858
2859 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2860 if (VarDecl* VD = SS->getConditionVariable())
2861 DeclMap[VD] = P;
2862
2863 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2864 if (VarDecl* VD = CS->getExceptionDecl())
2865 DeclMap[VD] = P;
2866 }
2867 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002868 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002869 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002870 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002871
Ted Kremenek42a509f2007-08-31 21:30:12 +00002872 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002873
Chris Lattnere4f21422009-06-30 01:26:17 +00002874 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002875 void setBlockID(signed i) { CurrentBlock = i; }
2876 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002877
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002878 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2879 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002880
2881 if (I == StmtMap.end())
2882 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002883
2884 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002885 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002886 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002887 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002888
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002889 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002890 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002891 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002892
2893 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2894 DeclMapTy::iterator I = DeclMap.find(D);
2895
2896 if (I == DeclMap.end())
2897 return false;
2898
2899 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2900 && I->second.second == CurrentStmt) {
2901 return false;
2902 }
2903
2904 OS << "[B" << I->second.first << "." << I->second.second << "]";
2905 return true;
2906 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002907};
Chris Lattnere4f21422009-06-30 01:26:17 +00002908} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002909
Chris Lattnere4f21422009-06-30 01:26:17 +00002910
2911namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002912class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002913 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002914
Ted Kremeneka95d3752008-09-13 05:16:45 +00002915 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002916 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002917 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002918public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002919 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002920 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002921 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002922
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002923 void VisitIfStmt(IfStmt* I) {
2924 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002925 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002926 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002927
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002928 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002929 void VisitStmt(Stmt* Terminator) {
2930 Terminator->printPretty(OS, Helper, Policy);
2931 }
2932
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002933 void VisitForStmt(ForStmt* F) {
2934 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002935 if (F->getInit())
2936 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002937 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002938 if (Stmt* C = F->getCond())
2939 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002940 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002941 if (F->getInc())
2942 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002943 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002944 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002945
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002946 void VisitWhileStmt(WhileStmt* W) {
2947 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002948 if (Stmt* C = W->getCond())
2949 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002950 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002951
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002952 void VisitDoStmt(DoStmt* D) {
2953 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002954 if (Stmt* C = D->getCond())
2955 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002956 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002957
Ted Kremenek411cdee2008-04-16 21:10:48 +00002958 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002959 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002960 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002961 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002962
Mike Stump5d1d2022010-01-19 02:20:09 +00002963 void VisitCXXTryStmt(CXXTryStmt* CS) {
2964 OS << "try ...";
2965 }
2966
Ted Kremenek805e9a82007-08-31 21:49:40 +00002967 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002968 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002969 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002970 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002971
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002972 void VisitChooseExpr(ChooseExpr* C) {
2973 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002974 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002975 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002976 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002977
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002978 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2979 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002980 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002981 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002982
Ted Kremenek805e9a82007-08-31 21:49:40 +00002983 void VisitBinaryOperator(BinaryOperator* B) {
2984 if (!B->isLogicalOp()) {
2985 VisitExpr(B);
2986 return;
2987 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002988
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002989 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002990
Ted Kremenek805e9a82007-08-31 21:49:40 +00002991 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002992 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002993 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002994 return;
John McCall2de56d12010-08-25 11:45:40 +00002995 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002996 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002997 return;
2998 default:
2999 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003000 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00003001 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003002
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00003003 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00003004 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00003005 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00003006};
Chris Lattnere4f21422009-06-30 01:26:17 +00003007} // end anonymous namespace
3008
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003009static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00003010 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003011 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3012 Stmt *S = CS;
3013
3014 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003015
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003016 // special printing for statement-expressions.
3017 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3018 CompoundStmt* Sub = SE->getSubStmt();
3019
3020 if (Sub->child_begin() != Sub->child_end()) {
3021 OS << "({ ... ; ";
3022 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3023 OS << " })\n";
3024 return;
3025 }
3026 }
3027 // special printing for comma expressions.
3028 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3029 if (B->getOpcode() == BO_Comma) {
3030 OS << "... , ";
3031 Helper->handledStmt(B->getRHS(),OS);
3032 OS << '\n';
3033 return;
3034 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00003035 }
3036 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003037 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00003038
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003039 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan36f327c2010-11-22 19:32:14 +00003040 OS << " (OperatorCall)";
3041 } else if (isa<CXXBindTemporaryExpr>(S)) {
3042 OS << " (BindTemporary)";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003043 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003044
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003045 // Expressions need a newline.
3046 if (isa<Expr>(S))
3047 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00003048
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003049 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
3050 CXXBaseOrMemberInitializer* I = IE;
3051 if (I->isBaseInitializer())
3052 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichet00eb3f92010-12-04 09:14:42 +00003053 else OS << I->getAnyMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00003054
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003055 OS << "(";
3056 if (Expr* IE = I->getInit())
3057 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3058 OS << ")";
3059
3060 if (I->isBaseInitializer())
3061 OS << " (Base initializer)\n";
3062 else OS << " (Member initializer)\n";
3063
3064 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3065 VarDecl* VD = DE.getVarDecl();
3066 Helper->handleDecl(VD, OS);
3067
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003068 const Type* T = VD->getType().getTypePtr();
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003069 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3070 T = RT->getPointeeType().getTypePtr();
Marcin Swiderskib1c52872010-10-25 07:00:40 +00003071 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3072 T = ET;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003073
3074 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3075 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003076
3077 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3078 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3079 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003080 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003081
3082 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3083 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003084
3085 const Type *T = FD->getType().getTypePtr();
3086 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3087 T = ET;
3088
Marcin Swiderski7c625d82010-10-05 05:37:00 +00003089 OS << "this->" << FD->getName();
Marcin Swiderski8c5e5d62010-10-25 07:05:54 +00003090 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00003091 OS << " (Member object destructor)\n";
Marcin Swiderski8599e762010-11-03 06:19:35 +00003092
3093 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3094 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3095 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3096 OS << " (Temporary object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003097 }
Zhongxing Xu81bc7d02010-11-01 06:46:05 +00003098}
Mike Stump6d9828c2009-07-17 01:31:16 +00003099
Chris Lattnere4f21422009-06-30 01:26:17 +00003100static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3101 const CFGBlock& B,
3102 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003103
Ted Kremenek42a509f2007-08-31 21:30:12 +00003104 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00003105
Ted Kremenek7dba8602007-08-29 21:56:09 +00003106 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00003107 OS << "\n [ B" << B.getBlockID();
3108
Ted Kremenek42a509f2007-08-31 21:30:12 +00003109 if (&B == &cfg->getEntry())
3110 OS << " (ENTRY) ]\n";
3111 else if (&B == &cfg->getExit())
3112 OS << " (EXIT) ]\n";
3113 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00003114 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003115 else
3116 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00003117
Ted Kremenek9cffe732007-08-29 23:20:49 +00003118 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00003119 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003120
3121 if (print_edges)
3122 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003123
Mike Stump079bd722010-01-19 22:00:14 +00003124 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003125 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00003126 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00003127 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003128 C->getLHS()->printPretty(OS, Helper,
3129 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003130 if (C->getRHS()) {
3131 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00003132 C->getRHS()->printPretty(OS, Helper,
3133 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00003134 }
Mike Stump079bd722010-01-19 22:00:14 +00003135 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00003136 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00003137 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00003138 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00003139 if (CS->getExceptionDecl())
3140 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3141 0);
3142 else
3143 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00003144 OS << ")";
3145
3146 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00003147 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00003148
Ted Kremenek9cffe732007-08-29 23:20:49 +00003149 OS << ":\n";
3150 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003151
Ted Kremenekfddd5182007-08-21 21:42:03 +00003152 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00003153 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00003154
Ted Kremenek42a509f2007-08-31 21:30:12 +00003155 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3156 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003157
Ted Kremenek9cffe732007-08-29 23:20:49 +00003158 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003159 if (print_edges)
3160 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003161
Ted Kremeneka95d3752008-09-13 05:16:45 +00003162 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003163
Ted Kremenek42a509f2007-08-31 21:30:12 +00003164 if (Helper)
3165 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00003166
Marcin Swiderski1cff1322010-09-21 05:58:15 +00003167 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00003168 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003169
Ted Kremenek9cffe732007-08-29 23:20:49 +00003170 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003171 if (B.getTerminator()) {
3172 if (print_edges)
3173 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003174
Ted Kremenek9cffe732007-08-29 23:20:49 +00003175 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003176
Ted Kremenek42a509f2007-08-31 21:30:12 +00003177 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00003178
Chris Lattnere4f21422009-06-30 01:26:17 +00003179 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3180 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003181 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003182 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003183 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003184
Ted Kremenek9cffe732007-08-29 23:20:49 +00003185 if (print_edges) {
3186 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00003187 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00003188 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00003189
Ted Kremenek42a509f2007-08-31 21:30:12 +00003190 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3191 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003192
Ted Kremenek42a509f2007-08-31 21:30:12 +00003193 if (i == 8 || (i-8) == 0)
3194 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00003195
Ted Kremenek9cffe732007-08-29 23:20:49 +00003196 OS << " B" << (*I)->getBlockID();
3197 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003198
Ted Kremenek42a509f2007-08-31 21:30:12 +00003199 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00003200
Ted Kremenek42a509f2007-08-31 21:30:12 +00003201 // Print the successors of this block.
3202 OS << " Successors (" << B.succ_size() << "):";
3203 i = 0;
3204
3205 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3206 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00003207
Ted Kremenek42a509f2007-08-31 21:30:12 +00003208 if (i == 8 || (i-8) % 10 == 0)
3209 OS << "\n ";
3210
Mike Stumpe5af3ce2009-07-20 23:24:15 +00003211 if (*I)
3212 OS << " B" << (*I)->getBlockID();
3213 else
3214 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00003215 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003216
Ted Kremenek9cffe732007-08-29 23:20:49 +00003217 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00003218 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003219}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003220
Ted Kremenek42a509f2007-08-31 21:30:12 +00003221
3222/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003223void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00003224
3225/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00003226void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3227 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00003228
Ted Kremenek42a509f2007-08-31 21:30:12 +00003229 // Print the entry block.
3230 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00003231
Ted Kremenek42a509f2007-08-31 21:30:12 +00003232 // Iterate through the CFGBlocks and print them one by one.
3233 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3234 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003235 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00003236 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00003237
Ted Kremenekee82d9b2009-10-12 20:55:07 +00003238 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003239 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003240
Ted Kremenek42a509f2007-08-31 21:30:12 +00003241 // Print the exit block.
3242 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00003243 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00003244}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003245
3246/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00003247void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3248 print(llvm::errs(), cfg, LO);
3249}
Ted Kremenek42a509f2007-08-31 21:30:12 +00003250
3251/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3252/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00003253void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3254 const LangOptions &LO) const {
3255 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003256 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00003257}
Ted Kremenek7dba8602007-08-29 21:56:09 +00003258
Ted Kremeneka2925852008-01-30 23:02:42 +00003259/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00003260void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00003261 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00003262 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003263 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremeneka2925852008-01-30 23:02:42 +00003264}
3265
Ted Kremenek390e48b2008-11-12 21:11:49 +00003266Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003267 Stmt *Terminator = this->Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003268 if (!Terminator)
3269 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003270
Ted Kremenek411cdee2008-04-16 21:10:48 +00003271 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003272
Ted Kremenek411cdee2008-04-16 21:10:48 +00003273 switch (Terminator->getStmtClass()) {
3274 default:
3275 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003276
Ted Kremenek411cdee2008-04-16 21:10:48 +00003277 case Stmt::ForStmtClass:
3278 E = cast<ForStmt>(Terminator)->getCond();
3279 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003280
Ted Kremenek411cdee2008-04-16 21:10:48 +00003281 case Stmt::WhileStmtClass:
3282 E = cast<WhileStmt>(Terminator)->getCond();
3283 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003284
Ted Kremenek411cdee2008-04-16 21:10:48 +00003285 case Stmt::DoStmtClass:
3286 E = cast<DoStmt>(Terminator)->getCond();
3287 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003288
Ted Kremenek411cdee2008-04-16 21:10:48 +00003289 case Stmt::IfStmtClass:
3290 E = cast<IfStmt>(Terminator)->getCond();
3291 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003292
Ted Kremenek411cdee2008-04-16 21:10:48 +00003293 case Stmt::ChooseExprClass:
3294 E = cast<ChooseExpr>(Terminator)->getCond();
3295 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003296
Ted Kremenek411cdee2008-04-16 21:10:48 +00003297 case Stmt::IndirectGotoStmtClass:
3298 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3299 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003300
Ted Kremenek411cdee2008-04-16 21:10:48 +00003301 case Stmt::SwitchStmtClass:
3302 E = cast<SwitchStmt>(Terminator)->getCond();
3303 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003304
Ted Kremenek411cdee2008-04-16 21:10:48 +00003305 case Stmt::ConditionalOperatorClass:
3306 E = cast<ConditionalOperator>(Terminator)->getCond();
3307 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003308
Ted Kremenek411cdee2008-04-16 21:10:48 +00003309 case Stmt::BinaryOperatorClass: // '&&' and '||'
3310 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00003311 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00003312
Ted Kremenek390e48b2008-11-12 21:11:49 +00003313 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003314 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00003315 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003316
Ted Kremenek411cdee2008-04-16 21:10:48 +00003317 return E ? E->IgnoreParens() : NULL;
3318}
3319
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003320bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderski4ba72a02010-10-29 05:21:47 +00003321 const Stmt *Terminator = this->Terminator;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003322 if (!Terminator)
3323 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003324
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003325 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00003326
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003327 switch (Terminator->getStmtClass()) {
3328 default:
3329 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00003330
3331 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003332 case Stmt::WhileStmtClass:
3333 case Stmt::DoStmtClass:
3334 case Stmt::IfStmtClass:
3335 case Stmt::ChooseExprClass:
3336 case Stmt::ConditionalOperatorClass:
3337 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00003338 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003339 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003340
Ted Kremenek9c2535a2008-05-16 16:06:00 +00003341 return E ? E->IgnoreParens() : NULL;
3342}
3343
Ted Kremeneka2925852008-01-30 23:02:42 +00003344
Ted Kremenek7dba8602007-08-29 21:56:09 +00003345//===----------------------------------------------------------------------===//
3346// CFG Graphviz Visualization
3347//===----------------------------------------------------------------------===//
3348
Ted Kremenek42a509f2007-08-31 21:30:12 +00003349
3350#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00003351static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003352#endif
3353
Chris Lattnere4f21422009-06-30 01:26:17 +00003354void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00003355#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00003356 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003357 GraphHelper = &H;
3358 llvm::ViewGraph(this,"CFG");
3359 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00003360#endif
3361}
3362
Ted Kremenek7dba8602007-08-29 21:56:09 +00003363namespace llvm {
3364template<>
3365struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00003366
3367 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3368
3369 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003370
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003371#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003372 std::string OutSStr;
3373 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003374 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003375 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003376
3377 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3378
3379 // Process string output to make it nicer...
3380 for (unsigned i = 0; i != OutStr.length(); ++i)
3381 if (OutStr[i] == '\n') { // Left justify
3382 OutStr[i] = '\\';
3383 OutStr.insert(OutStr.begin()+i+1, 'l');
3384 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003385
Ted Kremenek7dba8602007-08-29 21:56:09 +00003386 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003387#else
3388 return "";
3389#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003390 }
3391};
3392} // end namespace llvm