blob: 0957875fd26866471113ff532b0d9b501d8b7158 [file] [log] [blame]
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb1c170e2009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremenek6796fbd2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stump6bf1c082010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenek1b8ac852007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek04f3cee2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000026
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor6e6ad602009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000035 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000036}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000037
Zhanyong Wanb5d11c12010-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 Kremenek4cad5fc2009-12-16 03:18:58 +000051class AddStmtChoice {
52public:
Ted Kremenek8219b822010-12-16 07:46:53 +000053 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000054
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000055 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000056
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000057 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wanb5d11c12010-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 Kremenek4cad5fc2009-12-16 03:18:58 +000066private:
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000067 Kind kind;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000068};
Mike Stump31feda52009-07-17 01:31:16 +000069
Marcin Swiderski8b99b8a2010-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 Swiderskie9862ce2010-09-30 22:42:32 +000080/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-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:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +000093 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000094
95 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000096 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-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 Swiderskie9862ce2010-09-30 22:42:32 +0000140 const_iterator operator++(int) {
141 const_iterator P = *this;
142 ++*this;
143 return P;
144 }
Marcin Swiderski8b99b8a2010-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 Swiderskie9862ce2010-09-30 22:42:32 +0000152
153 operator bool() const {
154 return *this != const_iterator();
155 }
156
157 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000158 };
159
160 friend class const_iterator;
161
162private:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000163 BumpVectorContext ctx;
164
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000165 /// Automatic variables in order of declaration.
166 AutomaticVarsTy Vars;
167 /// Iterator to variable in previous scope that was declared just before
168 /// begin of this scope.
169 const_iterator Prev;
170
171public:
172 /// Constructs empty scope linked to previous scope in specified place.
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000173 LocalScope(BumpVectorContext &ctx, const_iterator P)
174 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000175
176 /// Begin of scope in direction of CFG building (backwards).
177 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000178
179 void addVar(VarDecl* VD) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000180 Vars.push_back(VD, ctx);
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000181 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000182};
183
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000184/// distance - Calculates distance from this to L. L must be reachable from this
185/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
186/// number of scopes between this and L.
187int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
188 int D = 0;
189 const_iterator F = *this;
190 while (F.Scope != L.Scope) {
191 assert (F != const_iterator()
192 && "L iterator is not reachable from F iterator.");
193 D += F.VarIter;
194 F = F.Scope->Prev;
195 }
196 D += F.VarIter - L.VarIter;
197 return D;
198}
199
200/// BlockScopePosPair - Structure for specifying position in CFG during its
201/// build process. It consists of CFGBlock that specifies position in CFG graph
202/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000203struct BlockScopePosPair {
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000204 BlockScopePosPair() : block(0) {}
205 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
206 : block(b), scopePosition(scopePos) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000207
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000208 CFGBlock *block;
209 LocalScope::const_iterator scopePosition;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000210};
211
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000212/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000213/// The builder is stateful: an instance of the builder should be used to only
214/// construct a single CFG.
215///
216/// Example usage:
217///
218/// CFGBuilder builder;
219/// CFG* cfg = builder.BuildAST(stmt1);
220///
Mike Stump31feda52009-07-17 01:31:16 +0000221/// CFG construction is done via a recursive walk of an AST. We actually parse
222/// the AST in reverse order so that the successor of a basic block is
223/// constructed prior to its predecessor. This allows us to nicely capture
224/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000225///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000226class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000227 typedef BlockScopePosPair JumpTarget;
228 typedef BlockScopePosPair JumpSource;
229
Mike Stump0d76d072009-07-20 23:24:15 +0000230 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000231 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000232
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000233 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000234 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000235 JumpTarget ContinueJumpTarget;
236 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000237 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000238 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000239 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000240
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000241 // Current position in local scope.
242 LocalScope::const_iterator ScopePos;
243
244 // LabelMap records the mapping from Label expressions to their jump targets.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000245 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000246 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000247
248 // A list of blocks that end with a "goto" that must be backpatched to their
249 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000250 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000251 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000252
Ted Kremenekeda180e22007-08-28 19:26:49 +0000253 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000254 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenekeda180e22007-08-28 19:26:49 +0000255 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000256
Zhongxing Xud38fb842010-09-16 03:28:18 +0000257 bool badCFG;
258 CFG::BuildOptions BuildOpts;
259
Mike Stump31feda52009-07-17 01:31:16 +0000260public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000261 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
262 Block(NULL), Succ(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000263 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000264 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000265
Ted Kremenek9aae5132007-08-23 21:42:29 +0000266 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000267 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000268 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000269
Ted Kremenek93668002009-07-17 22:18:43 +0000270private:
271 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000272 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
273 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
274 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000275 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000276 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall5d413782010-12-06 08:20:24 +0000277 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000278 AddStmtChoice asc);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000279 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
280 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000281 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
282 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000283 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000284 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
285 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000286 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
287 AddStmtChoice asc);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000288 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000289 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000290 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000291 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000292 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCallc07a0c72011-02-17 10:25:35 +0000293 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
294 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000295 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000296 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000297 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek21822592009-07-17 18:20:32 +0000298 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
299 CFGBlock *VisitDoStmt(DoStmt *D);
300 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000301 CFGBlock *VisitGotoStmt(GotoStmt* G);
302 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000303 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000304 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
305 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000306 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000307 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
308 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
309 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
310 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
311 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
312 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000313 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
314 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000315 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000316 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000317 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000318
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000319 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
320 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000321 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000322
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000323 // Visitors to walk an AST and generate destructors of temporaries in
324 // full expression.
325 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
326 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
327 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
328 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
329 bool BindToTemporary);
John McCallc07a0c72011-02-17 10:25:35 +0000330 CFGBlock *
331 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
332 bool BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000333
Ted Kremenek6065ef62008-04-28 18:00:46 +0000334 // NYS == Not Yet Supported
335 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000336 badCFG = true;
337 return Block;
338 }
Mike Stump31feda52009-07-17 01:31:16 +0000339
Ted Kremenek93668002009-07-17 22:18:43 +0000340 void autoCreateBlock() { if (!Block) Block = createBlock(); }
341 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000342
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000343 CFGBlock *addStmt(Stmt *S) {
344 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000345 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000346 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000347 void addAutomaticObjDtors(LocalScope::const_iterator B,
348 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000349 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000350
Marcin Swiderski5e415732010-09-30 23:05:00 +0000351 // Local scopes creation.
352 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
353
Zhongxing Xu81714f22010-10-01 03:00:16 +0000354 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000355 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
356 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
357
358 void addLocalScopeAndDtors(Stmt* S);
359
360 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek8219b822010-12-16 07:46:53 +0000361 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000362 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000363 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000364 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000365 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000366 B->appendInitializer(I, cfg->getBumpVectorContext());
367 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000368 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
369 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
370 }
371 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
372 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
373 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000374 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
375 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
376 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000377
Marcin Swiderski321a7072010-09-30 22:54:37 +0000378 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
379 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
380 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
381 LocalScope::const_iterator E, Stmt* S);
382 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
383 LocalScope::const_iterator B, LocalScope::const_iterator E);
384
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000385 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000386 B->addSuccessor(S, cfg->getBumpVectorContext());
387 }
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenek963cc312009-07-24 06:55:42 +0000389 /// TryResult - a class representing a variant over the values
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000390 /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
Ted Kremenek963cc312009-07-24 06:55:42 +0000391 /// and is used by the CFGBuilder to decide if a branch condition
392 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000393 class TryResult {
394 int X;
395 public:
396 TryResult(bool b) : X(b ? 1 : 0) {}
397 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000398
Ted Kremenek30754282009-07-24 04:47:11 +0000399 bool isTrue() const { return X == 1; }
400 bool isFalse() const { return X == 0; }
401 bool isKnown() const { return X >= 0; }
402 void negate() {
403 assert(isKnown());
404 X ^= 0x1;
405 }
406 };
Mike Stump11289f42009-09-09 15:08:12 +0000407
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000408 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump773582d2009-07-23 23:25:26 +0000409 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000410 TryResult tryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000411 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000412 return TryResult();
413
Mike Stump773582d2009-07-23 23:25:26 +0000414 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000415 if (!S->isTypeDependent() && !S->isValueDependent() &&
416 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000417 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000418
419 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000420 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000421};
Mike Stump31feda52009-07-17 01:31:16 +0000422
Douglas Gregor4619e432008-12-05 23:32:09 +0000423// FIXME: Add support for dependent-sized array types in C++?
424// Does it even make sense to build a CFG for an uninstantiated template?
John McCall424cec92011-01-19 06:33:43 +0000425static const VariableArrayType *FindVA(const Type *t) {
426 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
427 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000428 if (vat->getSizeExpr())
429 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000430
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000431 t = vt->getElementType().getTypePtr();
432 }
Mike Stump31feda52009-07-17 01:31:16 +0000433
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000434 return 0;
435}
Mike Stump31feda52009-07-17 01:31:16 +0000436
437/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
438/// arbitrary statement. Examples include a single expression or a function
439/// body (compound statement). The ownership of the returned CFG is
440/// transferred to the caller. If CFG construction fails, this method returns
441/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000442CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000443 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000444
Mike Stump0d76d072009-07-20 23:24:15 +0000445 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000446 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000447 if (!Statement)
448 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000449
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000450 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000451
452 // Create an empty block that will serve as the exit block for the CFG. Since
453 // this is the first block added to the CFG, it will be implicitly registered
454 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000455 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000456 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000457 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000458
Marcin Swiderski20b88732010-10-05 05:37:00 +0000459 if (BuildOpts.AddImplicitDtors)
460 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
461 addImplicitDtorsForDestructor(DD);
462
Ted Kremenek9aae5132007-08-23 21:42:29 +0000463 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000464 CFGBlock *B = addStmt(Statement);
465
466 if (badCFG)
467 return NULL;
468
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000469 // For C++ constructor add initializers to CFG.
470 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
471 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
472 E = CD->init_rend(); I != E; ++I) {
473 B = addInitializer(*I);
474 if (badCFG)
475 return NULL;
476 }
477 }
478
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000479 if (B)
480 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000481
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000482 // Backpatch the gotos whose label -> block mappings we didn't know when we
483 // encountered them.
484 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
485 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000486
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000487 CFGBlock* B = I->block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000488 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
489 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000490
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000491 // If there is no target for the goto, then we are looking at an
492 // incomplete AST. Handle this by not registering a successor.
493 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000494
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000495 JumpTarget JT = LI->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000496 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
497 JT.scopePosition);
498 addSuccessor(B, JT.block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000499 }
500
501 // Add successors to the Indirect Goto Dispatch block (if we have one).
502 if (CFGBlock* B = cfg->getIndirectGotoBlock())
503 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
504 E = AddressTakenLabels.end(); I != E; ++I ) {
505
506 // Lookup the target block.
507 LabelMapTy::iterator LI = LabelMap.find(*I);
508
509 // If there is no target block that contains label, then we are looking
510 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000511 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000512
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000513 addSuccessor(B, LI->second.block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000514 }
Mike Stump31feda52009-07-17 01:31:16 +0000515
Mike Stump31feda52009-07-17 01:31:16 +0000516 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000517 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000518
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000519 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000520}
Mike Stump31feda52009-07-17 01:31:16 +0000521
Ted Kremenek9aae5132007-08-23 21:42:29 +0000522/// createBlock - Used to lazily create blocks that are connected
523/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000524CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000525 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000526 if (add_successor && Succ)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000527 addSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000528 return B;
529}
Mike Stump31feda52009-07-17 01:31:16 +0000530
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000531/// addInitializer - Add C++ base or member initializer element to CFG.
Alexis Hunt1d792652011-01-08 20:30:50 +0000532CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000533 if (!BuildOpts.AddInitializers)
534 return Block;
535
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000536 bool IsReference = false;
537 bool HasTemporaries = false;
538
539 // Destructors of temporaries in initialization expression should be called
540 // after initialization finishes.
541 Expr *Init = I->getInit();
542 if (Init) {
Francois Pichetd583da02010-12-04 09:14:42 +0000543 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000544 IsReference = FD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +0000545 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000546
547 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
548 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +0000549 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000550 IsReference);
551 }
552 }
553
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000554 autoCreateBlock();
555 appendInitializer(Block, I);
556
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000557 if (Init) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000558 if (HasTemporaries) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000559 // For expression with temporaries go directly to subexpression to omit
560 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +0000561 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
562 }
563 return Visit(Init);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000564 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000565
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000566 return Block;
567}
568
Marcin Swiderski5e415732010-09-30 23:05:00 +0000569/// addAutomaticObjDtors - Add to current block automatic objects destructors
570/// for objects in range of local scope positions. Use S as trigger statement
571/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000572void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
573 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000574 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000575 return;
576
Marcin Swiderski5e415732010-09-30 23:05:00 +0000577 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000578 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000579
580 autoCreateBlock();
581 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000582}
583
Marcin Swiderski20b88732010-10-05 05:37:00 +0000584/// addImplicitDtorsForDestructor - Add implicit destructors generated for
585/// base and member objects in destructor.
586void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
587 assert (BuildOpts.AddImplicitDtors
588 && "Can be called only when dtors should be added");
589 const CXXRecordDecl *RD = DD->getParent();
590
591 // At the end destroy virtual base objects.
592 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
593 VE = RD->vbases_end(); VI != VE; ++VI) {
594 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
595 if (!CD->hasTrivialDestructor()) {
596 autoCreateBlock();
597 appendBaseDtor(Block, VI);
598 }
599 }
600
601 // Before virtual bases destroy direct base objects.
602 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
603 BE = RD->bases_end(); BI != BE; ++BI) {
604 if (!BI->isVirtual()) {
605 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
606 if (!CD->hasTrivialDestructor()) {
607 autoCreateBlock();
608 appendBaseDtor(Block, BI);
609 }
610 }
611 }
612
613 // First destroy member objects.
614 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
615 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000616 // Check for constant size array. Set type to array element type.
617 QualType QT = FI->getType();
618 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
619 if (AT->getSize() == 0)
620 continue;
621 QT = AT->getElementType();
622 }
623
624 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000625 if (!CD->hasTrivialDestructor()) {
626 autoCreateBlock();
627 appendMemberDtor(Block, *FI);
628 }
629 }
630}
631
Marcin Swiderski5e415732010-09-30 23:05:00 +0000632/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
633/// way return valid LocalScope object.
634LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
635 if (!Scope) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000636 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
637 Scope = alloc.Allocate<LocalScope>();
638 BumpVectorContext ctx(alloc);
639 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000640 }
641 return Scope;
642}
643
644/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000645/// that should create implicit scope (e.g. if/else substatements).
646void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000647 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000648 return;
649
650 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000651
652 // For compound statement we will be creating explicit scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000653 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000654 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
655 ; BI != BE; ++BI) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000656 Stmt *SI = *BI;
657 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000658 SI = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000659 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000660 Scope = addLocalScopeForDeclStmt(DS, Scope);
661 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000662 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000663 }
664
665 // For any other statement scope will be implicit and as such will be
666 // interesting only for DeclStmt.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000667 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000668 S = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000669 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000670 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000671}
672
673/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
674/// reuse Scope if not NULL.
675LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000676 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000677 if (!BuildOpts.AddImplicitDtors)
678 return Scope;
679
680 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
681 ; DI != DE; ++DI) {
682 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
683 Scope = addLocalScopeForVarDecl(VD, Scope);
684 }
685 return Scope;
686}
687
688/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
689/// create add scope for automatic objects and temporary objects bound to
690/// const reference. Will reuse Scope if not NULL.
691LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000692 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000693 if (!BuildOpts.AddImplicitDtors)
694 return Scope;
695
696 // Check if variable is local.
697 switch (VD->getStorageClass()) {
698 case SC_None:
699 case SC_Auto:
700 case SC_Register:
701 break;
702 default: return Scope;
703 }
704
705 // Check for const references bound to temporary. Set type to pointee.
706 QualType QT = VD->getType();
707 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
708 QT = RT->getPointeeType();
709 if (!QT.isConstQualified())
710 return Scope;
711 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
712 return Scope;
713 }
714
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000715 // Check for constant size array. Set type to array element type.
716 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
717 if (AT->getSize() == 0)
718 return Scope;
719 QT = AT->getElementType();
720 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000721
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000722 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000723 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
724 if (!CD->hasTrivialDestructor()) {
725 // Add the variable to scope
726 Scope = createOrReuseLocalScope(Scope);
727 Scope->addVar(VD);
728 ScopePos = Scope->begin();
729 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000730 return Scope;
731}
732
733/// addLocalScopeAndDtors - For given statement add local scope for it and
734/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
735void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
736 if (!BuildOpts.AddImplicitDtors)
737 return;
738
739 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000740 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000741 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
742}
743
Marcin Swiderski321a7072010-09-30 22:54:37 +0000744/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
745/// automatic storage duration to CFGBlock's elements vector. Insertion will be
746/// performed in place specified with iterator.
747void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
748 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
749 BumpVectorContext& C = cfg->getBumpVectorContext();
750 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
751 while (B != E)
752 I = Blk->insertAutomaticObjDtor(I, *B++, S);
753}
754
755/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
756/// automatic storage duration to CFGBlock's elements vector. Elements will be
757/// appended to physical end of the vector which happens to be logical
758/// beginning.
759void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
760 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
761 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
762}
763
764/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
765/// variables with automatic storage duration to CFGBlock's elements vector.
766/// Elements will be prepended to physical beginning of the vector which
767/// happens to be logical end. Use blocks terminator as statement that specifies
768/// destructors call site.
769void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
770 LocalScope::const_iterator B, LocalScope::const_iterator E) {
771 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
772}
773
Ted Kremenek93668002009-07-17 22:18:43 +0000774/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000775/// blocks for ternary operators, &&, and ||. We also process "," and
776/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000777CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000778tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000779 if (!S) {
780 badCFG = true;
781 return 0;
782 }
Ted Kremenek93668002009-07-17 22:18:43 +0000783 switch (S->getStmtClass()) {
784 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000785 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000786
787 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000788 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000789
John McCallc07a0c72011-02-17 10:25:35 +0000790 case Stmt::BinaryConditionalOperatorClass:
791 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
792
Ted Kremenek93668002009-07-17 22:18:43 +0000793 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000794 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000795
Ted Kremenek93668002009-07-17 22:18:43 +0000796 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000797 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000798
Ted Kremenek93668002009-07-17 22:18:43 +0000799 case Stmt::BreakStmtClass:
800 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000801
Ted Kremenek93668002009-07-17 22:18:43 +0000802 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000803 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000804 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000805
Ted Kremenek93668002009-07-17 22:18:43 +0000806 case Stmt::CaseStmtClass:
807 return VisitCaseStmt(cast<CaseStmt>(S));
808
809 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000810 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000811
Ted Kremenek93668002009-07-17 22:18:43 +0000812 case Stmt::CompoundStmtClass:
813 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000814
Ted Kremenek93668002009-07-17 22:18:43 +0000815 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000816 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000817
Ted Kremenek93668002009-07-17 22:18:43 +0000818 case Stmt::ContinueStmtClass:
819 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000820
Ted Kremenekb27378c2010-01-19 20:40:33 +0000821 case Stmt::CXXCatchStmtClass:
822 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
823
John McCall5d413782010-12-06 08:20:24 +0000824 case Stmt::ExprWithCleanupsClass:
825 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000826
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000827 case Stmt::CXXBindTemporaryExprClass:
828 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
829
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000830 case Stmt::CXXConstructExprClass:
831 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
832
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000833 case Stmt::CXXFunctionalCastExprClass:
834 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
835
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000836 case Stmt::CXXTemporaryObjectExprClass:
837 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
838
Zhongxing Xu7e612172010-04-13 09:38:01 +0000839 case Stmt::CXXMemberCallExprClass:
840 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
841
Ted Kremenekb27378c2010-01-19 20:40:33 +0000842 case Stmt::CXXThrowExprClass:
843 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000844
Ted Kremenekb27378c2010-01-19 20:40:33 +0000845 case Stmt::CXXTryStmtClass:
846 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000847
Ted Kremenek93668002009-07-17 22:18:43 +0000848 case Stmt::DeclStmtClass:
849 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000850
Ted Kremenek93668002009-07-17 22:18:43 +0000851 case Stmt::DefaultStmtClass:
852 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000853
Ted Kremenek93668002009-07-17 22:18:43 +0000854 case Stmt::DoStmtClass:
855 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000856
Ted Kremenek93668002009-07-17 22:18:43 +0000857 case Stmt::ForStmtClass:
858 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000859
Ted Kremenek93668002009-07-17 22:18:43 +0000860 case Stmt::GotoStmtClass:
861 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000862
Ted Kremenek93668002009-07-17 22:18:43 +0000863 case Stmt::IfStmtClass:
864 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000865
Ted Kremenek8219b822010-12-16 07:46:53 +0000866 case Stmt::ImplicitCastExprClass:
867 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000868
Ted Kremenek93668002009-07-17 22:18:43 +0000869 case Stmt::IndirectGotoStmtClass:
870 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000871
Ted Kremenek93668002009-07-17 22:18:43 +0000872 case Stmt::LabelStmtClass:
873 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000874
Ted Kremenek5868ec62010-04-11 17:02:10 +0000875 case Stmt::MemberExprClass:
876 return VisitMemberExpr(cast<MemberExpr>(S), asc);
877
Ted Kremenek93668002009-07-17 22:18:43 +0000878 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000879 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
880
Ted Kremenek93668002009-07-17 22:18:43 +0000881 case Stmt::ObjCAtSynchronizedStmtClass:
882 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000883
Ted Kremenek93668002009-07-17 22:18:43 +0000884 case Stmt::ObjCAtThrowStmtClass:
885 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000886
Ted Kremenek93668002009-07-17 22:18:43 +0000887 case Stmt::ObjCAtTryStmtClass:
888 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000889
Ted Kremenek93668002009-07-17 22:18:43 +0000890 case Stmt::ObjCForCollectionStmtClass:
891 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000892
Ted Kremenek93668002009-07-17 22:18:43 +0000893 case Stmt::ParenExprClass:
894 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000895 goto tryAgain;
896
Ted Kremenek93668002009-07-17 22:18:43 +0000897 case Stmt::NullStmtClass:
898 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000899
Ted Kremenek93668002009-07-17 22:18:43 +0000900 case Stmt::ReturnStmtClass:
901 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000902
Ted Kremenek93668002009-07-17 22:18:43 +0000903 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000904 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000905
Ted Kremenek93668002009-07-17 22:18:43 +0000906 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000907 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenek93668002009-07-17 22:18:43 +0000909 case Stmt::SwitchStmtClass:
910 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000911
Zhanyong Wan6dace612010-11-22 08:45:56 +0000912 case Stmt::UnaryOperatorClass:
913 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
914
Ted Kremenek93668002009-07-17 22:18:43 +0000915 case Stmt::WhileStmtClass:
916 return VisitWhileStmt(cast<WhileStmt>(S));
917 }
918}
Mike Stump11289f42009-09-09 15:08:12 +0000919
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000920CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
921 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000922 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000923 appendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Ted Kremenek93668002009-07-17 22:18:43 +0000926 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000927}
Mike Stump31feda52009-07-17 01:31:16 +0000928
Ted Kremenek93668002009-07-17 22:18:43 +0000929/// VisitChildren - Visit the children of a Stmt.
930CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
Ted Kremenek828f6312011-02-21 22:11:26 +0000931 CFGBlock *lastBlock = Block;
932 for (Stmt::child_range I = Terminator->children(); I; ++I)
933 if (Stmt *child = *I)
934 if (CFGBlock *b = Visit(child))
935 lastBlock = b;
936
937 return lastBlock;
Ted Kremenek9e248872007-08-27 21:27:44 +0000938}
Mike Stump11289f42009-09-09 15:08:12 +0000939
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000940CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
941 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000942 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000943
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000944 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000945 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000946 appendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000947 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000948
Ted Kremenek9aae5132007-08-23 21:42:29 +0000949 return Block;
950}
Mike Stump11289f42009-09-09 15:08:12 +0000951
Zhanyong Wan6dace612010-11-22 08:45:56 +0000952CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek8219b822010-12-16 07:46:53 +0000953 AddStmtChoice asc) {
Zhanyong Wan6dace612010-11-22 08:45:56 +0000954 if (asc.alwaysAdd()) {
955 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000956 appendStmt(Block, U, asc);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000957 }
958
Ted Kremenek8219b822010-12-16 07:46:53 +0000959 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan6dace612010-11-22 08:45:56 +0000960}
961
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000962CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
963 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000964 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000965 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000966 appendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000967
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000968 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000969 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000970
Ted Kremenek93668002009-07-17 22:18:43 +0000971 // create the block evaluating the LHS
972 CFGBlock* LHSBlock = createBlock(false);
973 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000974
Ted Kremenek93668002009-07-17 22:18:43 +0000975 // create the block evaluating the RHS
976 Succ = ConfluenceBlock;
977 Block = NULL;
978 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000979
980 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000981 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000982 return 0;
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000983 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +0000984 // Create an empty block for cases where the RHS doesn't require
985 // any explicit statements in the CFG.
986 RHSBlock = createBlock();
987 }
Mike Stump11289f42009-09-09 15:08:12 +0000988
Mike Stump773582d2009-07-23 23:25:26 +0000989 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000990 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000991 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000992 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000993
Ted Kremenek93668002009-07-17 22:18:43 +0000994 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000995 if (B->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000996 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
997 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000998 } else {
John McCalle3027922010-08-25 11:45:40 +0000999 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001000 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1001 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001002 }
Mike Stump11289f42009-09-09 15:08:12 +00001003
Ted Kremenek93668002009-07-17 22:18:43 +00001004 // Generate the blocks for evaluating the LHS.
1005 Block = LHSBlock;
1006 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +00001007 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001008
1009 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +00001010 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001011 appendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +00001012 addStmt(B->getRHS());
1013 return addStmt(B->getLHS());
1014 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001015
1016 if (B->isAssignmentOp()) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001017 if (asc.alwaysAdd()) {
1018 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001019 appendStmt(Block, B, asc);
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001020 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001021 Visit(B->getLHS());
Marcin Swiderski77232492010-10-24 08:21:40 +00001022 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001023 }
Mike Stump11289f42009-09-09 15:08:12 +00001024
Marcin Swiderski77232492010-10-24 08:21:40 +00001025 if (asc.alwaysAdd()) {
1026 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001027 appendStmt(Block, B, asc);
Marcin Swiderski77232492010-10-24 08:21:40 +00001028 }
1029
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001030 CFGBlock *RBlock = Visit(B->getRHS());
1031 CFGBlock *LBlock = Visit(B->getLHS());
1032 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1033 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1034 // return RBlock. Otherwise we'll incorrectly return NULL.
1035 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001036}
1037
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001038CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1039 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001040 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001041 appendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001042 }
1043 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001044}
1045
Ted Kremenek93668002009-07-17 22:18:43 +00001046CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1047 // "break" is a control-flow statement. Thus we stop processing the current
1048 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001049 if (badCFG)
1050 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001051
Ted Kremenek93668002009-07-17 22:18:43 +00001052 // Now create a new block that ends with the break statement.
1053 Block = createBlock(false);
1054 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001055
Ted Kremenek93668002009-07-17 22:18:43 +00001056 // If there is no target for the break, then we are looking at an incomplete
1057 // AST. This means that the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001058 if (BreakJumpTarget.block) {
1059 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1060 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001061 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001062 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001063
1064
Ted Kremenek9aae5132007-08-23 21:42:29 +00001065 return Block;
1066}
Mike Stump11289f42009-09-09 15:08:12 +00001067
Mike Stump04c68512010-01-21 15:20:48 +00001068static bool CanThrow(Expr *E) {
1069 QualType Ty = E->getType();
1070 if (Ty->isFunctionPointerType())
1071 Ty = Ty->getAs<PointerType>()->getPointeeType();
1072 else if (Ty->isBlockPointerType())
1073 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001074
Mike Stump04c68512010-01-21 15:20:48 +00001075 const FunctionType *FT = Ty->getAs<FunctionType>();
1076 if (FT) {
1077 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1078 if (Proto->hasEmptyExceptionSpec())
1079 return false;
1080 }
1081 return true;
1082}
1083
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001084CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001085 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001086 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001087 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001088 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001089 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001090
Mike Stump04c68512010-01-21 15:20:48 +00001091 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001092
1093 // Languages without exceptions are assumed to not throw.
Anders Carlsson08ce5ed2011-02-20 00:20:27 +00001094 if (Context->getLangOptions().areExceptionsEnabled()) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001095 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001096 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001097 }
1098
1099 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001100 if (FD->hasAttr<NoReturnAttr>())
1101 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001102 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001103 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001104 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001105
Mike Stump04c68512010-01-21 15:20:48 +00001106 if (!CanThrow(C->getCallee()))
1107 AddEHEdge = false;
1108
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001109 if (!NoReturn && !AddEHEdge)
1110 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump11289f42009-09-09 15:08:12 +00001111
Mike Stump92244b02010-01-19 22:00:14 +00001112 if (Block) {
1113 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001114 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001115 return 0;
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Mike Stump92244b02010-01-19 22:00:14 +00001118 Block = createBlock(!NoReturn);
Ted Kremenek8219b822010-12-16 07:46:53 +00001119 appendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001120
Mike Stump92244b02010-01-19 22:00:14 +00001121 if (NoReturn) {
1122 // Wire this to the exit block directly.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001123 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001124 }
Mike Stump04c68512010-01-21 15:20:48 +00001125 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001126 // Add exceptional edges.
1127 if (TryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001128 addSuccessor(Block, TryTerminatedBlock);
Mike Stump92244b02010-01-19 22:00:14 +00001129 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001130 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
Mike Stump8c5d7992009-07-25 21:26:53 +00001133 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001134}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001135
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001136CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1137 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001138 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001139 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001140 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001141 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001142
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001143 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek21822592009-07-17 18:20:32 +00001144 Succ = ConfluenceBlock;
1145 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001146 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001147 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001148 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001149
Ted Kremenek21822592009-07-17 18:20:32 +00001150 Succ = ConfluenceBlock;
1151 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001152 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001153 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001154 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001155
Ted Kremenek21822592009-07-17 18:20:32 +00001156 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001157 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001158 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1159 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1160 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001161 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001162 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001163}
Mike Stump11289f42009-09-09 15:08:12 +00001164
1165
1166CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001167 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001168 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001169
1170 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1171 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001172 // If we hit a segment of code just containing ';' (NullStmts), we can
1173 // get a null block back. In such cases, just use the LastBlock
1174 if (CFGBlock *newBlock = addStmt(*I))
1175 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001176
Ted Kremenekce499c22009-08-27 23:16:26 +00001177 if (badCFG)
1178 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001179 }
Mike Stump92244b02010-01-19 22:00:14 +00001180
Ted Kremenek93668002009-07-17 22:18:43 +00001181 return LastBlock;
1182}
Mike Stump11289f42009-09-09 15:08:12 +00001183
John McCallc07a0c72011-02-17 10:25:35 +00001184CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001185 AddStmtChoice asc) {
John McCallc07a0c72011-02-17 10:25:35 +00001186 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1187 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1188
Ted Kremenek51d40b02009-07-17 18:15:54 +00001189 // Create the confluence block that will "merge" the results of the ternary
1190 // expression.
1191 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001192 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001193 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001194 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001195
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001196 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001197
Ted Kremenek51d40b02009-07-17 18:15:54 +00001198 // Create a block for the LHS expression if there is an LHS expression. A
1199 // GCC extension allows LHS to be NULL, causing the condition to be the
1200 // value that is returned instead.
1201 // e.g: x ?: y is shorthand for: x ? x : y;
1202 Succ = ConfluenceBlock;
1203 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00001204 CFGBlock* LHSBlock = 0;
1205 const Expr *trueExpr = C->getTrueExpr();
1206 if (trueExpr != opaqueValue) {
1207 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001208 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001209 return 0;
1210 Block = NULL;
1211 }
Mike Stump11289f42009-09-09 15:08:12 +00001212
Ted Kremenek51d40b02009-07-17 18:15:54 +00001213 // Create the block for the RHS expression.
1214 Succ = ConfluenceBlock;
John McCallc07a0c72011-02-17 10:25:35 +00001215 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001216 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001217 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001218
Ted Kremenek51d40b02009-07-17 18:15:54 +00001219 // Create the block that will contain the condition.
1220 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001221
Mike Stump773582d2009-07-23 23:25:26 +00001222 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001223 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001224 if (LHSBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001225 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001226 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001227 Block->setTerminator(C);
John McCallc07a0c72011-02-17 10:25:35 +00001228 Expr *condExpr = C->getCond();
John McCall68cc3352011-02-19 03:13:26 +00001229
1230 CFGBlock *result = 0;
1231
1232 // Run the condition expression if it's not trivially expressed in
1233 // terms of the opaque value (or if there is no opaque value).
John McCallc07a0c72011-02-17 10:25:35 +00001234 if (condExpr != opaqueValue) result = addStmt(condExpr);
John McCall68cc3352011-02-19 03:13:26 +00001235
1236 // Before that, run the common subexpression if there was one.
1237 // At least one of this or the above will be run.
1238 if (opaqueValue) result = addStmt(BCO->getCommon());
1239
John McCallc07a0c72011-02-17 10:25:35 +00001240 return result;
Ted Kremenek51d40b02009-07-17 18:15:54 +00001241}
1242
Ted Kremenek93668002009-07-17 22:18:43 +00001243CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001244 if (DS->isSingleDecl())
1245 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001246
Ted Kremenek93668002009-07-17 22:18:43 +00001247 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001248
Ted Kremenek93668002009-07-17 22:18:43 +00001249 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1250 typedef llvm::SmallVector<Decl*,10> BufTy;
1251 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001252
Ted Kremenek93668002009-07-17 22:18:43 +00001253 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1254 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1255 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1256 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001257
Ted Kremenek93668002009-07-17 22:18:43 +00001258 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1259 // automatically freed with the CFG.
1260 DeclGroupRef DG(*I);
1261 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001262 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001263 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001264
Ted Kremenek93668002009-07-17 22:18:43 +00001265 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001266 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001267 }
Mike Stump11289f42009-09-09 15:08:12 +00001268
1269 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001270}
Mike Stump11289f42009-09-09 15:08:12 +00001271
Ted Kremenek93668002009-07-17 22:18:43 +00001272/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001273/// DeclStmts and initializers in them.
1274CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1275 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001276
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001277 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001278
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001279 if (!VD) {
1280 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001281 appendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001282 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001283 }
Mike Stump11289f42009-09-09 15:08:12 +00001284
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001285 bool IsReference = false;
1286 bool HasTemporaries = false;
1287
1288 // Destructors of temporaries in initialization expression should be called
1289 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001290 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001291 if (Init) {
1292 IsReference = VD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +00001293 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001294
1295 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1296 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +00001297 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001298 IsReference);
1299 }
1300 }
1301
1302 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001303 appendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001304
Ted Kremenek93668002009-07-17 22:18:43 +00001305 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001306 if (HasTemporaries)
1307 // For expression with temporaries go directly to subexpression to omit
1308 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +00001309 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001310 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001311 Visit(Init);
Ted Kremenek93668002009-07-17 22:18:43 +00001312 }
Mike Stump11289f42009-09-09 15:08:12 +00001313
Ted Kremenek93668002009-07-17 22:18:43 +00001314 // If the type of VD is a VLA, then we must process its size expressions.
John McCall424cec92011-01-19 06:33:43 +00001315 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1316 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek93668002009-07-17 22:18:43 +00001317 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001318
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001319 // Remove variable from local scope.
1320 if (ScopePos && VD == *ScopePos)
1321 ++ScopePos;
1322
Ted Kremenek93668002009-07-17 22:18:43 +00001323 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001324}
1325
1326CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001327 // We may see an if statement in the middle of a basic block, or it may be the
1328 // first statement we are processing. In either case, we create a new basic
1329 // block. First, we create the blocks for the then...else statements, and
1330 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001331 // middle of a block, we stop processing that block. That block is then the
1332 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001333
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001334 // Save local scope position because in case of condition variable ScopePos
1335 // won't be restored when traversing AST.
1336 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1337
1338 // Create local scope for possible condition variable.
1339 // Store scope position. Add implicit destructor.
1340 if (VarDecl* VD = I->getConditionVariable()) {
1341 LocalScope::const_iterator BeginScopePos = ScopePos;
1342 addLocalScopeForVarDecl(VD);
1343 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1344 }
1345
Mike Stump31feda52009-07-17 01:31:16 +00001346 // The block we were proccessing is now finished. Make it the successor
1347 // block.
1348 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001349 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001350 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001351 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001352 }
Mike Stump31feda52009-07-17 01:31:16 +00001353
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001354 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001355 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001356
Ted Kremenek9aae5132007-08-23 21:42:29 +00001357 if (Stmt* Else = I->getElse()) {
1358 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001359
Ted Kremenek9aae5132007-08-23 21:42:29 +00001360 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001361 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001362 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001363
1364 // If branch is not a compound statement create implicit scope
1365 // and add destructors.
1366 if (!isa<CompoundStmt>(Else))
1367 addLocalScopeAndDtors(Else);
1368
Ted Kremenek93668002009-07-17 22:18:43 +00001369 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001370
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001371 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1372 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001373 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001374 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001375 return 0;
1376 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001377 }
Mike Stump31feda52009-07-17 01:31:16 +00001378
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001379 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001380 CFGBlock* ThenBlock;
1381 {
1382 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001383 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001384 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001385 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001386
1387 // If branch is not a compound statement create implicit scope
1388 // and add destructors.
1389 if (!isa<CompoundStmt>(Then))
1390 addLocalScopeAndDtors(Then);
1391
Ted Kremenek93668002009-07-17 22:18:43 +00001392 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001393
Ted Kremenek1b379512009-04-01 03:52:47 +00001394 if (!ThenBlock) {
1395 // We can reach here if the "then" body has all NullStmts.
1396 // Create an empty block so we can distinguish between true and false
1397 // branches in path-sensitive analyses.
1398 ThenBlock = createBlock(false);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001399 addSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001400 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001401 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001402 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001403 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001404 }
1405
Mike Stump31feda52009-07-17 01:31:16 +00001406 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001407 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001408
Ted Kremenek9aae5132007-08-23 21:42:29 +00001409 // Set the terminator of the new block to the If statement.
1410 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001411
Mike Stump773582d2009-07-23 23:25:26 +00001412 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001413 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001414
Ted Kremenek9aae5132007-08-23 21:42:29 +00001415 // Now add the successors.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001416 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1417 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001418
1419 // Add the condition as the last statement in the new block. This may create
1420 // new blocks as the condition may contain control-flow. Any newly created
1421 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001422 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001423
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001424 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1425 // and the condition variable initialization to the CFG.
1426 if (VarDecl *VD = I->getConditionVariable()) {
1427 if (Expr *Init = VD->getInit()) {
1428 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001429 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001430 addStmt(Init);
1431 }
1432 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001433
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001434 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001435}
Mike Stump31feda52009-07-17 01:31:16 +00001436
1437
Ted Kremenek9aae5132007-08-23 21:42:29 +00001438CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001439 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001440 //
Mike Stump31feda52009-07-17 01:31:16 +00001441 // NOTE: If a "return" appears in the middle of a block, this means that the
1442 // code afterwards is DEAD (unreachable). We still keep a basic block
1443 // for that code; a simple "mark-and-sweep" from the entry block will be
1444 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001445
1446 // Create the new block.
1447 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001448
Ted Kremenek9aae5132007-08-23 21:42:29 +00001449 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001450 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001451 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001452
1453 // Add the return statement to the block. This may create new blocks if R
1454 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001455 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001456}
1457
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001458CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001459 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001460 addStmt(L->getSubStmt());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001461 CFGBlock *LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001462
Ted Kremenek93668002009-07-17 22:18:43 +00001463 if (!LabelBlock) // This can happen when the body is empty, i.e.
1464 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001465
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001466 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1467 "label already in map");
1468 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001469
1470 // Labels partition blocks, so this is the end of the basic block we were
1471 // processing (L is the block's label). Because this is label (and we have
1472 // already processed the substatement) there is no extra control-flow to worry
1473 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001474 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001475 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001476 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001477
1478 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001479 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001480
Ted Kremenek9aae5132007-08-23 21:42:29 +00001481 // This block is now the implicit successor of other blocks.
1482 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001483
Ted Kremenek9aae5132007-08-23 21:42:29 +00001484 return LabelBlock;
1485}
1486
1487CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001488 // Goto is a control-flow statement. Thus we stop processing the current
1489 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001490
Ted Kremenek9aae5132007-08-23 21:42:29 +00001491 Block = createBlock(false);
1492 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001493
1494 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001495 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001496
Ted Kremenek9aae5132007-08-23 21:42:29 +00001497 if (I == LabelMap.end())
1498 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001499 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1500 else {
1501 JumpTarget JT = I->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001502 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1503 addSuccessor(Block, JT.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001504 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001505
Mike Stump31feda52009-07-17 01:31:16 +00001506 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001507}
1508
1509CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001510 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001511
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001512 // Save local scope position because in case of condition variable ScopePos
1513 // won't be restored when traversing AST.
1514 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1515
1516 // Create local scope for init statement and possible condition variable.
1517 // Add destructor for init statement and condition variable.
1518 // Store scope position for continue statement.
1519 if (Stmt* Init = F->getInit())
1520 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001521 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1522
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001523 if (VarDecl* VD = F->getConditionVariable())
1524 addLocalScopeForVarDecl(VD);
1525 LocalScope::const_iterator ContinueScopePos = ScopePos;
1526
1527 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1528
Mike Stump014b3ea2009-07-21 01:12:51 +00001529 // "for" is a control-flow statement. Thus we stop processing the current
1530 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001531 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001532 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001533 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001534 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001535 } else
1536 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001537
Ted Kremenek304a9532010-05-21 20:30:15 +00001538 // Save the current value for the break targets.
1539 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001540 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001541 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001542
Mike Stump31feda52009-07-17 01:31:16 +00001543 // Because of short-circuit evaluation, the condition of the loop can span
1544 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1545 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001546 CFGBlock* ExitConditionBlock = createBlock(false);
1547 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001548
Ted Kremenek81e14852007-08-27 19:46:09 +00001549 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001550 ExitConditionBlock->setTerminator(F);
1551
1552 // Now add the actual condition to the condition block. Because the condition
1553 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001554 if (Stmt* C = F->getCond()) {
1555 Block = ExitConditionBlock;
1556 EntryConditionBlock = addStmt(C);
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001557 if (badCFG)
1558 return 0;
Ted Kremenek7b31a612010-09-15 07:01:20 +00001559 assert(Block == EntryConditionBlock ||
1560 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001561
1562 // If this block contains a condition variable, add both the condition
1563 // variable and initializer to the CFG.
1564 if (VarDecl *VD = F->getConditionVariable()) {
1565 if (Expr *Init = VD->getInit()) {
1566 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001567 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenekec92f942009-12-24 01:49:06 +00001568 EntryConditionBlock = addStmt(Init);
1569 assert(Block == EntryConditionBlock);
1570 }
1571 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001572
Ted Kremenek55957a82009-05-02 00:13:27 +00001573 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001574 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001575 return 0;
1576 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001577 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001578
Mike Stump31feda52009-07-17 01:31:16 +00001579 // The condition block is the implicit successor for the loop body as well as
1580 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001581 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001582
Mike Stump773582d2009-07-23 23:25:26 +00001583 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001584 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001585
Mike Stump773582d2009-07-23 23:25:26 +00001586 if (F->getCond())
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001587 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001588
Ted Kremenek9aae5132007-08-23 21:42:29 +00001589 // Now create the loop body.
1590 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001591 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001592
Ted Kremenek304a9532010-05-21 20:30:15 +00001593 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001594 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1595 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001596
Ted Kremeneke9610502007-08-30 18:39:40 +00001597 // Create a new block to contain the (bottom) of the loop body.
1598 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001599
1600 // Loop body should end with destructor of Condition variable (if any).
1601 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001602
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001603 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001604 // Generate increment code in its own basic block. This is the target of
1605 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001606 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001607 } else {
1608 // No increment code. Create a special, empty, block that is used as the
1609 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001610 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001611 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001612 }
Mike Stump31feda52009-07-17 01:31:16 +00001613
Ted Kremenek902393b2009-04-28 00:51:56 +00001614 // Finish up the increment (or empty) block if it hasn't been already.
1615 if (Block) {
1616 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001617 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001618 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001619 Block = 0;
1620 }
Mike Stump31feda52009-07-17 01:31:16 +00001621
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001622 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001623
Ted Kremenek902393b2009-04-28 00:51:56 +00001624 // The starting block for the loop increment is the block that should
1625 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001626 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001627
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001628 // If body is not a compound statement create implicit scope
1629 // and add destructors.
1630 if (!isa<CompoundStmt>(F->getBody()))
1631 addLocalScopeAndDtors(F->getBody());
1632
Mike Stump31feda52009-07-17 01:31:16 +00001633 // Now populate the body block, and in the process create new blocks as we
1634 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001635 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001636
1637 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001638 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001639 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001640 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001641
Ted Kremenek30754282009-07-24 04:47:11 +00001642 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001643 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001644 }
Mike Stump31feda52009-07-17 01:31:16 +00001645
Ted Kremenek30754282009-07-24 04:47:11 +00001646 // Link up the condition block with the code that follows the loop. (the
1647 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001648 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001649
Ted Kremenek9aae5132007-08-23 21:42:29 +00001650 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001651 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001652 if (Stmt* I = F->getInit()) {
1653 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001654 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001655 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001656
1657 // There is no loop initialization. We are thus basically a while loop.
1658 // NULL out Block to force lazy block construction.
1659 Block = NULL;
1660 Succ = EntryConditionBlock;
1661 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001662}
1663
Ted Kremenek5868ec62010-04-11 17:02:10 +00001664CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1665 if (asc.alwaysAdd()) {
1666 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001667 appendStmt(Block, M, asc);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001668 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001669 return Visit(M->getBase());
Ted Kremenek5868ec62010-04-11 17:02:10 +00001670}
1671
Ted Kremenek9d56e642008-11-11 17:10:00 +00001672CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1673 // Objective-C fast enumeration 'for' statements:
1674 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1675 //
1676 // for ( Type newVariable in collection_expression ) { statements }
1677 //
1678 // becomes:
1679 //
1680 // prologue:
1681 // 1. collection_expression
1682 // T. jump to loop_entry
1683 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001684 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001685 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1686 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1687 // TB:
1688 // statements
1689 // T. jump to loop_entry
1690 // FB:
1691 // what comes after
1692 //
1693 // and
1694 //
1695 // Type existingItem;
1696 // for ( existingItem in expression ) { statements }
1697 //
1698 // becomes:
1699 //
Mike Stump31feda52009-07-17 01:31:16 +00001700 // the same with newVariable replaced with existingItem; the binding works
1701 // the same except that for one ObjCForCollectionStmt::getElement() returns
1702 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001703 //
Mike Stump31feda52009-07-17 01:31:16 +00001704
Ted Kremenek9d56e642008-11-11 17:10:00 +00001705 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001706
Ted Kremenek9d56e642008-11-11 17:10:00 +00001707 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001708 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001709 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001710 LoopSuccessor = Block;
1711 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001712 } else
1713 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001715 // Build the condition blocks.
1716 CFGBlock* ExitConditionBlock = createBlock(false);
1717 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001718
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001719 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001720 ExitConditionBlock->setTerminator(S);
1721
1722 // The last statement in the block should be the ObjCForCollectionStmt, which
1723 // performs the actual binding to 'element' and determines if there are any
1724 // more items in the collection.
Ted Kremenek8219b822010-12-16 07:46:53 +00001725 appendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001726 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001727
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001728 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001729 // generate new blocks as necesary. We DON'T add the statement by default to
1730 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001731 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001732 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001733 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001734 return 0;
1735 Block = 0;
1736 }
Mike Stump31feda52009-07-17 01:31:16 +00001737
1738 // The condition block is the implicit successor for the loop body as well as
1739 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001740 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001741
Ted Kremenek9d56e642008-11-11 17:10:00 +00001742 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001743 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001744 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001745 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1746 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1747 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001748
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001749 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1750 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001751
Ted Kremenek93668002009-07-17 22:18:43 +00001752 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001753
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001754 if (!BodyBlock)
1755 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001756 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001757 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001758 return 0;
1759 }
Mike Stump31feda52009-07-17 01:31:16 +00001760
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001761 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001762 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001763 }
Mike Stump31feda52009-07-17 01:31:16 +00001764
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001765 // Link up the condition block with the code that follows the loop.
1766 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001767 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001768
Ted Kremenek9d56e642008-11-11 17:10:00 +00001769 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001770 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001771 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001772}
1773
Ted Kremenek49805452009-05-02 01:49:13 +00001774CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1775 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001776
Ted Kremenek49805452009-05-02 01:49:13 +00001777 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001778 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001779
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001780 // The sync body starts its own basic block. This makes it a little easier
1781 // for diagnostic clients.
1782 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001783 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001784 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001785
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001786 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001787 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001788 }
Mike Stump31feda52009-07-17 01:31:16 +00001789
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001790 // Add the @synchronized to the CFG.
1791 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001792 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001793
Ted Kremenek49805452009-05-02 01:49:13 +00001794 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001795 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001796}
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001798CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001799 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001800 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001801}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001802
Ted Kremenek9aae5132007-08-23 21:42:29 +00001803CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001804 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001805
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001806 // Save local scope position because in case of condition variable ScopePos
1807 // won't be restored when traversing AST.
1808 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1809
1810 // Create local scope for possible condition variable.
1811 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001812 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001813 if (VarDecl* VD = W->getConditionVariable()) {
1814 addLocalScopeForVarDecl(VD);
1815 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1816 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001817
Mike Stump014b3ea2009-07-21 01:12:51 +00001818 // "while" is a control-flow statement. Thus we stop processing the current
1819 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001820 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001821 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001822 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001823 LoopSuccessor = Block;
Ted Kremenek828f6312011-02-21 22:11:26 +00001824 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001825 } else
1826 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001827
1828 // Because of short-circuit evaluation, the condition of the loop can span
1829 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1830 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001831 CFGBlock* ExitConditionBlock = createBlock(false);
1832 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001833
Ted Kremenek81e14852007-08-27 19:46:09 +00001834 // Set the terminator for the "exit" condition block.
1835 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001836
1837 // Now add the actual condition to the condition block. Because the condition
1838 // itself may contain control-flow, new blocks may be created. Thus we update
1839 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001840 if (Stmt* C = W->getCond()) {
1841 Block = ExitConditionBlock;
1842 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001843 // The condition might finish the current 'Block'.
1844 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001845
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001846 // If this block contains a condition variable, add both the condition
1847 // variable and initializer to the CFG.
1848 if (VarDecl *VD = W->getConditionVariable()) {
1849 if (Expr *Init = VD->getInit()) {
1850 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001851 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001852 EntryConditionBlock = addStmt(Init);
1853 assert(Block == EntryConditionBlock);
1854 }
1855 }
1856
Ted Kremenek55957a82009-05-02 00:13:27 +00001857 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001858 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001859 return 0;
1860 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001861 }
Mike Stump31feda52009-07-17 01:31:16 +00001862
1863 // The condition block is the implicit successor for the loop body as well as
1864 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001865 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001866
Mike Stump773582d2009-07-23 23:25:26 +00001867 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001868 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001869
Ted Kremenek9aae5132007-08-23 21:42:29 +00001870 // Process the loop body.
1871 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001872 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001873
1874 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001875 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1876 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1877 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001878
Mike Stump31feda52009-07-17 01:31:16 +00001879 // Create an empty block to represent the transition block for looping back
1880 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001881 Block = 0;
1882 assert(Succ == EntryConditionBlock);
1883 Succ = createBlock();
1884 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001885 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001886
Ted Kremenek9aae5132007-08-23 21:42:29 +00001887 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001888 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001889
Ted Kremenek9aae5132007-08-23 21:42:29 +00001890 // NULL out Block to force lazy instantiation of blocks for the body.
1891 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001892
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001893 // Loop body should end with destructor of Condition variable (if any).
1894 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1895
1896 // If body is not a compound statement create implicit scope
1897 // and add destructors.
1898 if (!isa<CompoundStmt>(W->getBody()))
1899 addLocalScopeAndDtors(W->getBody());
1900
Ted Kremenek9aae5132007-08-23 21:42:29 +00001901 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001902 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001903
Ted Kremeneke9610502007-08-30 18:39:40 +00001904 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001905 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001906 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001907 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001908 return 0;
1909 }
Mike Stump31feda52009-07-17 01:31:16 +00001910
Ted Kremenek30754282009-07-24 04:47:11 +00001911 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001912 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001913 }
Mike Stump31feda52009-07-17 01:31:16 +00001914
Ted Kremenek30754282009-07-24 04:47:11 +00001915 // Link up the condition block with the code that follows the loop. (the
1916 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001917 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001918
1919 // There can be no more statements in the condition block since we loop back
1920 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001921 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001923 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001924 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001925 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001926}
Mike Stump11289f42009-09-09 15:08:12 +00001927
1928
Ted Kremenek93668002009-07-17 22:18:43 +00001929CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1930 // FIXME: For now we pretend that @catch and the code it contains does not
1931 // exit.
1932 return Block;
1933}
Mike Stump31feda52009-07-17 01:31:16 +00001934
Ted Kremenek93041ba2008-12-09 20:20:09 +00001935CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1936 // FIXME: This isn't complete. We basically treat @throw like a return
1937 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001938
Ted Kremenek0868eea2009-09-24 18:45:41 +00001939 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001940 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001941 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001942
Ted Kremenek93041ba2008-12-09 20:20:09 +00001943 // Create the new block.
1944 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001945
Ted Kremenek93041ba2008-12-09 20:20:09 +00001946 // The Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001947 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001948
1949 // Add the statement to the block. This may create new blocks if S contains
1950 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001951 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001952}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001953
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001954CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001955 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001956 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001957 return 0;
1958
1959 // Create the new block.
1960 Block = createBlock(false);
1961
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001962 if (TryTerminatedBlock)
1963 // The current try statement is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001964 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001965 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001966 // otherwise the Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001967 addSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001968
1969 // Add the statement to the block. This may create new blocks if S contains
1970 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001971 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001972}
1973
Ted Kremenek93668002009-07-17 22:18:43 +00001974CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001975 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001976
Mike Stump8d50b6a2009-07-21 01:27:50 +00001977 // "do...while" is a control-flow statement. Thus we stop processing the
1978 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001979 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001980 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001981 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001982 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001983 } else
1984 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001985
1986 // Because of short-circuit evaluation, the condition of the loop can span
1987 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1988 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001989 CFGBlock* ExitConditionBlock = createBlock(false);
1990 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001991
Ted Kremenek81e14852007-08-27 19:46:09 +00001992 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001993 ExitConditionBlock->setTerminator(D);
1994
1995 // Now add the actual condition to the condition block. Because the condition
1996 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001997 if (Stmt* C = D->getCond()) {
1998 Block = ExitConditionBlock;
1999 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00002000 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002001 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002002 return 0;
2003 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002004 }
Mike Stump31feda52009-07-17 01:31:16 +00002005
Ted Kremeneka1523a32008-02-27 07:20:00 +00002006 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002007 Succ = EntryConditionBlock;
2008
Mike Stump773582d2009-07-23 23:25:26 +00002009 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002010 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002011
Ted Kremenek9aae5132007-08-23 21:42:29 +00002012 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002013 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002014 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002015 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002016
Ted Kremenek9aae5132007-08-23 21:42:29 +00002017 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002018 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2019 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2020 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002021
Ted Kremenek9aae5132007-08-23 21:42:29 +00002022 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002023 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002024
Ted Kremenek9aae5132007-08-23 21:42:29 +00002025 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002026 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002027
Ted Kremenek9aae5132007-08-23 21:42:29 +00002028 // NULL out Block to force lazy instantiation of blocks for the body.
2029 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002030
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002031 // If body is not a compound statement create implicit scope
2032 // and add destructors.
2033 if (!isa<CompoundStmt>(D->getBody()))
2034 addLocalScopeAndDtors(D->getBody());
2035
Ted Kremenek9aae5132007-08-23 21:42:29 +00002036 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002037 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002038
Ted Kremeneke9610502007-08-30 18:39:40 +00002039 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002040 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002041 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002042 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002043 return 0;
2044 }
Mike Stump31feda52009-07-17 01:31:16 +00002045
Ted Kremenek110974d2010-08-17 20:59:56 +00002046 if (!KnownVal.isFalse()) {
2047 // Add an intermediate block between the BodyBlock and the
2048 // ExitConditionBlock to represent the "loop back" transition. Create an
2049 // empty block to represent the transition block for looping back to the
2050 // head of the loop.
2051 // FIXME: Can we do this more efficiently without adding another block?
2052 Block = NULL;
2053 Succ = BodyBlock;
2054 CFGBlock *LoopBackBlock = createBlock();
2055 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002056
Ted Kremenek110974d2010-08-17 20:59:56 +00002057 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002058 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenek110974d2010-08-17 20:59:56 +00002059 }
2060 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002061 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002062 }
Mike Stump31feda52009-07-17 01:31:16 +00002063
Ted Kremenek30754282009-07-24 04:47:11 +00002064 // Link up the condition block with the code that follows the loop.
2065 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002066 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002067
2068 // There can be no more statements in the body block(s) since we loop back to
2069 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002070 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002071
Ted Kremenek9aae5132007-08-23 21:42:29 +00002072 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002073 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002074 return BodyBlock;
2075}
2076
2077CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2078 // "continue" is a control-flow statement. Thus we stop processing the
2079 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002080 if (badCFG)
2081 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002082
Ted Kremenek9aae5132007-08-23 21:42:29 +00002083 // Now create a new block that ends with the continue statement.
2084 Block = createBlock(false);
2085 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002086
Ted Kremenek9aae5132007-08-23 21:42:29 +00002087 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002088 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00002089 if (ContinueJumpTarget.block) {
2090 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2091 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002092 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002093 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002094
Ted Kremenek9aae5132007-08-23 21:42:29 +00002095 return Block;
2096}
Mike Stump11289f42009-09-09 15:08:12 +00002097
Ted Kremenek0747de62009-07-18 00:47:21 +00002098CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002099 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002100
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002101 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002102 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002103 appendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002104 }
Mike Stump11289f42009-09-09 15:08:12 +00002105
Ted Kremenek93668002009-07-17 22:18:43 +00002106 // VLA types have expressions that must be evaluated.
2107 if (E->isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002108 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek93668002009-07-17 22:18:43 +00002109 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2110 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002111 }
Mike Stump11289f42009-09-09 15:08:12 +00002112
Mike Stump31feda52009-07-17 01:31:16 +00002113 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002114}
Mike Stump11289f42009-09-09 15:08:12 +00002115
Ted Kremenek93668002009-07-17 22:18:43 +00002116/// VisitStmtExpr - Utility method to handle (nested) statement
2117/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002118CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2119 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002120 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002121 appendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002122 }
Ted Kremenek93668002009-07-17 22:18:43 +00002123 return VisitCompoundStmt(SE->getSubStmt());
2124}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002125
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002126CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002127 // "switch" is a control-flow statement. Thus we stop processing the current
2128 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002129 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002130
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002131 // Save local scope position because in case of condition variable ScopePos
2132 // won't be restored when traversing AST.
2133 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2134
2135 // Create local scope for possible condition variable.
2136 // Store scope position. Add implicit destructor.
2137 if (VarDecl* VD = Terminator->getConditionVariable()) {
2138 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2139 addLocalScopeForVarDecl(VD);
2140 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2141 }
2142
Ted Kremenek9aae5132007-08-23 21:42:29 +00002143 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002144 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002145 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002146 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002147 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002148
2149 // Save the current "switch" context.
2150 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002151 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002152 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002153
Mike Stump31feda52009-07-17 01:31:16 +00002154 // Set the "default" case to be the block after the switch statement. If the
2155 // switch statement contains a "default:", this value will be overwritten with
2156 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002157 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002158
Ted Kremenek9aae5132007-08-23 21:42:29 +00002159 // Create a new block that will contain the switch statement.
2160 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002161
Ted Kremenek9aae5132007-08-23 21:42:29 +00002162 // Now process the switch body. The code after the switch is the implicit
2163 // successor.
2164 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002165 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002166
2167 // When visiting the body, the case statements should automatically get linked
2168 // up to the switch. We also don't keep a pointer to the body, since all
2169 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002170 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002171 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002172
2173 // If body is not a compound statement create implicit scope
2174 // and add destructors.
2175 if (!isa<CompoundStmt>(Terminator->getBody()))
2176 addLocalScopeAndDtors(Terminator->getBody());
2177
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002178 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002179 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002180 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002181 return 0;
2182 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002183
Mike Stump31feda52009-07-17 01:31:16 +00002184 // If we have no "default:" case, the default transition is to the code
2185 // following the switch body.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002186 addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002187
Ted Kremenek81e14852007-08-27 19:46:09 +00002188 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002189 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002190 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002191 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002192 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002193
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002194 // Finally, if the SwitchStmt contains a condition variable, add both the
2195 // SwitchStmt and the condition variable initialization to the CFG.
2196 if (VarDecl *VD = Terminator->getConditionVariable()) {
2197 if (Expr *Init = VD->getInit()) {
2198 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002199 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002200 addStmt(Init);
2201 }
2202 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002203
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002204 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002205}
2206
Ted Kremenek93668002009-07-17 22:18:43 +00002207CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002208 // CaseStmts are essentially labels, so they are the first statement in a
2209 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002210 CFGBlock *TopBlock = 0, *LastBlock = 0;
2211
2212 if (Stmt *Sub = CS->getSubStmt()) {
2213 // For deeply nested chains of CaseStmts, instead of doing a recursion
2214 // (which can blow out the stack), manually unroll and create blocks
2215 // along the way.
2216 while (isa<CaseStmt>(Sub)) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002217 CFGBlock *currentBlock = createBlock(false);
2218 currentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002219
Ted Kremenek60fa6572010-08-04 23:54:30 +00002220 if (TopBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002221 addSuccessor(LastBlock, currentBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002222 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002223 TopBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002224
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002225 addSuccessor(SwitchTerminatedBlock, currentBlock);
2226 LastBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002227
2228 CS = cast<CaseStmt>(Sub);
2229 Sub = CS->getSubStmt();
2230 }
2231
2232 addStmt(Sub);
2233 }
Mike Stump11289f42009-09-09 15:08:12 +00002234
Ted Kremenek55e91e82007-08-30 18:48:11 +00002235 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002236 if (!CaseBlock)
2237 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002238
2239 // Cases statements partition blocks, so this is the top of the basic block we
2240 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002241 CaseBlock->setLabel(CS);
2242
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002243 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002244 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002245
2246 // Add this block to the list of successors for the block with the switch
2247 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002248 assert(SwitchTerminatedBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002249 addSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002250
Ted Kremenek9aae5132007-08-23 21:42:29 +00002251 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2252 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002253
Ted Kremenek60fa6572010-08-04 23:54:30 +00002254 if (TopBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002255 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002256 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002257 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002258 // This block is now the implicit successor of other blocks.
2259 Succ = CaseBlock;
2260 }
Mike Stump31feda52009-07-17 01:31:16 +00002261
Ted Kremenek60fa6572010-08-04 23:54:30 +00002262 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002263}
Mike Stump31feda52009-07-17 01:31:16 +00002264
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002265CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002266 if (Terminator->getSubStmt())
2267 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002268
Ted Kremenek654c78f2008-02-13 22:05:39 +00002269 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002270
2271 if (!DefaultCaseBlock)
2272 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002273
2274 // Default statements partition blocks, so this is the top of the basic block
2275 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002276 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002277
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002278 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002279 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002280
Mike Stump31feda52009-07-17 01:31:16 +00002281 // Unlike case statements, we don't add the default block to the successors
2282 // for the switch statement immediately. This is done when we finish
2283 // processing the switch statement. This allows for the default case
2284 // (including a fall-through to the code after the switch statement) to always
2285 // be the last successor of a switch-terminated block.
2286
Ted Kremenek654c78f2008-02-13 22:05:39 +00002287 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2288 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002289
Ted Kremenek654c78f2008-02-13 22:05:39 +00002290 // This block is now the implicit successor of other blocks.
2291 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002292
2293 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002294}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002295
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002296CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2297 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2298 // current block.
2299 CFGBlock* TrySuccessor = NULL;
2300
2301 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002302 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002303 return 0;
2304 TrySuccessor = Block;
2305 } else TrySuccessor = Succ;
2306
Mike Stump0bdba6c2010-01-20 01:15:34 +00002307 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002308
2309 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002310 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002311 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002312 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002313
Mike Stump0bdba6c2010-01-20 01:15:34 +00002314 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002315 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2316 // The code after the try is the implicit successor.
2317 Succ = TrySuccessor;
2318 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002319 if (CS->getExceptionDecl() == 0) {
2320 HasCatchAll = true;
2321 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002322 Block = NULL;
2323 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2324 if (CatchBlock == 0)
2325 return 0;
2326 // Add this block to the list of successors for the block with the try
2327 // statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002328 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002329 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002330 if (!HasCatchAll) {
2331 if (PrevTryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002332 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002333 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002334 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002335 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002336
2337 // The code after the try is the implicit successor.
2338 Succ = TrySuccessor;
2339
Mike Stump845384a2010-01-20 01:30:58 +00002340 // Save the current "try" context.
2341 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2342 TryTerminatedBlock = NewTryTerminatedBlock;
2343
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002344 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002345 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002346 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002347 return Block;
2348}
2349
2350CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2351 // CXXCatchStmt are treated like labels, so they are the first statement in a
2352 // block.
2353
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002354 // Save local scope position because in case of exception variable ScopePos
2355 // won't be restored when traversing AST.
2356 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2357
2358 // Create local scope for possible exception variable.
2359 // Store scope position. Add implicit destructor.
2360 if (VarDecl* VD = CS->getExceptionDecl()) {
2361 LocalScope::const_iterator BeginScopePos = ScopePos;
2362 addLocalScopeForVarDecl(VD);
2363 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2364 }
2365
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002366 if (CS->getHandlerBlock())
2367 addStmt(CS->getHandlerBlock());
2368
2369 CFGBlock* CatchBlock = Block;
2370 if (!CatchBlock)
2371 CatchBlock = createBlock();
2372
2373 CatchBlock->setLabel(CS);
2374
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002375 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002376 return 0;
2377
2378 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2379 Block = NULL;
2380
2381 return CatchBlock;
2382}
2383
John McCall5d413782010-12-06 08:20:24 +00002384CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002385 AddStmtChoice asc) {
2386 if (BuildOpts.AddImplicitDtors) {
2387 // If adding implicit destructors visit the full expression for adding
2388 // destructors of temporaries.
2389 VisitForTemporaryDtors(E->getSubExpr());
2390
2391 // Full expression has to be added as CFGStmt so it will be sequenced
2392 // before destructors of it's temporaries.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002393 asc = asc.withAlwaysAdd(true);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002394 }
2395 return Visit(E->getSubExpr(), asc);
2396}
2397
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002398CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2399 AddStmtChoice asc) {
2400 if (asc.alwaysAdd()) {
2401 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002402 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002403
2404 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002405 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002406 }
2407 return Visit(E->getSubExpr(), asc);
2408}
2409
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002410CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2411 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002412 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002413 if (!C->isElidable())
Ted Kremenek8219b822010-12-16 07:46:53 +00002414 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002415
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002416 return VisitChildren(C);
2417}
2418
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002419CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2420 AddStmtChoice asc) {
2421 if (asc.alwaysAdd()) {
2422 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002423 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002424 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002425 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002426 }
2427 return Visit(E->getSubExpr(), asc);
2428}
2429
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002430CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2431 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002432 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002433 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002434 return VisitChildren(C);
2435}
2436
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002437CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002438 AddStmtChoice asc) {
Zhongxing Xu7e612172010-04-13 09:38:01 +00002439 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002440 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002441 return VisitChildren(C);
2442}
2443
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002444CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2445 AddStmtChoice asc) {
2446 if (asc.alwaysAdd()) {
2447 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002448 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002449 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002450 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002451}
2452
Ted Kremenekeda180e22007-08-28 19:26:49 +00002453CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002454 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002455 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002456
Ted Kremenekeda180e22007-08-28 19:26:49 +00002457 if (!IBlock) {
2458 IBlock = createBlock(false);
2459 cfg->setIndirectGotoBlock(IBlock);
2460 }
Mike Stump31feda52009-07-17 01:31:16 +00002461
Ted Kremenekeda180e22007-08-28 19:26:49 +00002462 // IndirectGoto is a control-flow statement. Thus we stop processing the
2463 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002464 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002465 return 0;
2466
Ted Kremenekeda180e22007-08-28 19:26:49 +00002467 Block = createBlock(false);
2468 Block->setTerminator(I);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002469 addSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002470 return addStmt(I->getTarget());
2471}
2472
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002473CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2474tryAgain:
2475 if (!E) {
2476 badCFG = true;
2477 return NULL;
2478 }
2479 switch (E->getStmtClass()) {
2480 default:
2481 return VisitChildrenForTemporaryDtors(E);
2482
2483 case Stmt::BinaryOperatorClass:
2484 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2485
2486 case Stmt::CXXBindTemporaryExprClass:
2487 return VisitCXXBindTemporaryExprForTemporaryDtors(
2488 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2489
John McCallc07a0c72011-02-17 10:25:35 +00002490 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002491 case Stmt::ConditionalOperatorClass:
2492 return VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002493 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002494
2495 case Stmt::ImplicitCastExprClass:
2496 // For implicit cast we want BindToTemporary to be passed further.
2497 E = cast<CastExpr>(E)->getSubExpr();
2498 goto tryAgain;
2499
2500 case Stmt::ParenExprClass:
2501 E = cast<ParenExpr>(E)->getSubExpr();
2502 goto tryAgain;
2503 }
2504}
2505
2506CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2507 // When visiting children for destructors we want to visit them in reverse
2508 // order. Because there's no reverse iterator for children must to reverse
2509 // them in helper vector.
2510 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2511 ChildrenVect ChildrenRev;
John McCall8322c3a2011-02-13 04:07:26 +00002512 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002513 if (*I) ChildrenRev.push_back(*I);
2514 }
2515
2516 CFGBlock *B = Block;
2517 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2518 L = ChildrenRev.rend(); I != L; ++I) {
2519 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2520 B = R;
2521 }
2522 return B;
2523}
2524
2525CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2526 if (E->isLogicalOp()) {
2527 // Destructors for temporaries in LHS expression should be called after
2528 // those for RHS expression. Even if this will unnecessarily create a block,
2529 // this block will be used at least by the full expression.
2530 autoCreateBlock();
2531 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2532 if (badCFG)
2533 return NULL;
2534
2535 Succ = ConfluenceBlock;
2536 Block = NULL;
2537 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2538
2539 if (RHSBlock) {
2540 if (badCFG)
2541 return NULL;
2542
2543 // If RHS expression did produce destructors we need to connect created
2544 // blocks to CFG in same manner as for binary operator itself.
2545 CFGBlock *LHSBlock = createBlock(false);
2546 LHSBlock->setTerminator(CFGTerminator(E, true));
2547
2548 // For binary operator LHS block is before RHS in list of predecessors
2549 // of ConfluenceBlock.
2550 std::reverse(ConfluenceBlock->pred_begin(),
2551 ConfluenceBlock->pred_end());
2552
2553 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002554 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002555 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2556 KnownVal.negate();
2557
2558 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2559 // itself.
2560 if (E->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002561 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2562 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002563 } else {
2564 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002565 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2566 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002567 }
2568
2569 Block = LHSBlock;
2570 return LHSBlock;
2571 }
2572
2573 Block = ConfluenceBlock;
2574 return ConfluenceBlock;
2575 }
2576
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002577 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002578 // For assignment operator (=) LHS expression is visited
2579 // before RHS expression. For destructors visit them in reverse order.
2580 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2581 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2582 return LHSBlock ? LHSBlock : RHSBlock;
2583 }
2584
2585 // For any other binary operator RHS expression is visited before
2586 // LHS expression (order of children). For destructors visit them in reverse
2587 // order.
2588 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2589 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2590 return RHSBlock ? RHSBlock : LHSBlock;
2591}
2592
2593CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2594 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2595 // First add destructors for temporaries in subexpression.
2596 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002597 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002598 // If lifetime of temporary is not prolonged (by assigning to constant
2599 // reference) add destructor for it.
2600 autoCreateBlock();
2601 appendTemporaryDtor(Block, E);
2602 B = Block;
2603 }
2604 return B;
2605}
2606
2607CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002608 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002609 // First add destructors for condition expression. Even if this will
2610 // unnecessarily create a block, this block will be used at least by the full
2611 // expression.
2612 autoCreateBlock();
2613 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2614 if (badCFG)
2615 return NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002616 if (BinaryConditionalOperator *BCO
2617 = dyn_cast<BinaryConditionalOperator>(E)) {
2618 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002619 if (badCFG)
2620 return NULL;
2621 }
2622
John McCallc07a0c72011-02-17 10:25:35 +00002623 // Try to add block with destructors for LHS expression.
2624 CFGBlock *LHSBlock = NULL;
2625 Succ = ConfluenceBlock;
2626 Block = NULL;
2627 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2628 if (badCFG)
2629 return NULL;
2630
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002631 // Try to add block with destructors for RHS expression;
2632 Succ = ConfluenceBlock;
2633 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002634 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2635 BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002636 if (badCFG)
2637 return NULL;
2638
2639 if (!RHSBlock && !LHSBlock) {
2640 // If neither LHS nor RHS expression had temporaries to destroy don't create
2641 // more blocks.
2642 Block = ConfluenceBlock;
2643 return Block;
2644 }
2645
2646 Block = createBlock(false);
2647 Block->setTerminator(CFGTerminator(E, true));
2648
2649 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002650 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002651
2652 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002653 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002654 } else if (KnownVal.isFalse()) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002655 addSuccessor(Block, NULL);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002656 } else {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002657 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002658 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2659 }
2660
2661 if (!RHSBlock)
2662 RHSBlock = ConfluenceBlock;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002663 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002664
2665 return Block;
2666}
2667
Ted Kremenek04cca642007-08-23 21:26:19 +00002668} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002669
Mike Stump31feda52009-07-17 01:31:16 +00002670/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2671/// no successors or predecessors. If this is the first block created in the
2672/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002673CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002674 bool first_block = begin() == end();
2675
2676 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002677 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2678 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2679 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002680
2681 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002682 if (first_block)
2683 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002684
2685 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002686 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002687}
2688
Ted Kremenek889073f2007-08-23 16:51:22 +00002689/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2690/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002691CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002692 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002693 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002694 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002695}
2696
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002697//===----------------------------------------------------------------------===//
2698// CFG: Queries for BlkExprs.
2699//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002700
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002701namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002702 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002703}
2704
Ted Kremenekbff98442009-12-23 23:37:10 +00002705static void FindSubExprAssignments(Stmt *S,
2706 llvm::SmallPtrSet<Expr*,50>& Set) {
2707 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002708 return;
Mike Stump31feda52009-07-17 01:31:16 +00002709
John McCall8322c3a2011-02-13 04:07:26 +00002710 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002711 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002712 if (!child)
2713 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002714
Ted Kremenekbff98442009-12-23 23:37:10 +00002715 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002716 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002717
Ted Kremenekbff98442009-12-23 23:37:10 +00002718 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002719 }
2720}
2721
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002722static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2723 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002724
2725 // Look for assignments that are used as subexpressions. These are the only
2726 // assignments that we want to *possibly* register as a block-level
2727 // expression. Basically, if an assignment occurs both in a subexpression and
2728 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002729 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002730
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002731 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002732 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002733 if (CFGStmt S = BI->getAs<CFGStmt>())
2734 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002735
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002736 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002737
2738 // Iterate over the statements again on identify the Expr* and Stmt* at the
2739 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002740
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002741 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2742 CFGStmt CS = BI->getAs<CFGStmt>();
2743 if (!CS.isValid())
2744 continue;
2745 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002746
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002747 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002748 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002749 // expression are really "statements" whose value is never used by
2750 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002751 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002752 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002753 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2754 // Special handling for statement expressions. The last statement in
2755 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002756 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002757 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002758 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002759 (*M)[C->body_back()] = x;
2760 }
2761 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002762
Ted Kremenek95a123c2008-01-26 00:03:27 +00002763 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002764 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002765 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002766 }
Mike Stump31feda52009-07-17 01:31:16 +00002767
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002768 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002769
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002770 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002771
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002772 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002773 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002774 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002775 }
2776 }
Mike Stump31feda52009-07-17 01:31:16 +00002777
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002778 return M;
2779}
2780
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002781CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2782 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002783 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002784
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002785 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002786 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002787 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002788}
2789
2790unsigned CFG::getNumBlkExprs() {
2791 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2792 return M->size();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002793
2794 // We assume callers interested in the number of BlkExprs will want
2795 // the map constructed if it doesn't already exist.
2796 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2797 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002798}
2799
Ted Kremenek6065ef62008-04-28 18:00:46 +00002800//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002801// Filtered walking of the CFG.
2802//===----------------------------------------------------------------------===//
2803
2804bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002805 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002806
2807 if (F.IgnoreDefaultsWithCoveredEnums) {
2808 // If the 'To' has no label or is labeled but the label isn't a
2809 // CaseStmt then filter this edge.
2810 if (const SwitchStmt *S =
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002811 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002812 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002813 const Stmt *L = To->getLabel();
2814 if (!L || !isa<CaseStmt>(L))
2815 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002816 }
2817 }
2818 }
2819
2820 return false;
2821}
2822
2823//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002824// Cleanup: CFG dstor.
2825//===----------------------------------------------------------------------===//
2826
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002827CFG::~CFG() {
2828 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2829}
Mike Stump31feda52009-07-17 01:31:16 +00002830
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002831//===----------------------------------------------------------------------===//
2832// CFG pretty printing
2833//===----------------------------------------------------------------------===//
2834
Ted Kremenek7e776b12007-08-22 18:22:34 +00002835namespace {
2836
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002837class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002838 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002839 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002840 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002841 DeclMapTy DeclMap;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002842 signed currentBlock;
2843 unsigned currentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002844 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002845public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002846
Chris Lattnerc61089a2009-06-30 01:26:17 +00002847 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002848 : currentBlock(0), currentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002849 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2850 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002851 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002852 BI != BEnd; ++BI, ++j ) {
2853 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2854 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2855 StmtMap[SE] = P;
2856
2857 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2858 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002859
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002860 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2861 if (VarDecl* VD = IS->getConditionVariable())
2862 DeclMap[VD] = P;
2863
2864 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2865 if (VarDecl* VD = FS->getConditionVariable())
2866 DeclMap[VD] = P;
2867
2868 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2869 if (VarDecl* VD = WS->getConditionVariable())
2870 DeclMap[VD] = P;
2871
2872 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2873 if (VarDecl* VD = SS->getConditionVariable())
2874 DeclMap[VD] = P;
2875
2876 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2877 if (VarDecl* VD = CS->getExceptionDecl())
2878 DeclMap[VD] = P;
2879 }
2880 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002881 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002882 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002883 }
Mike Stump31feda52009-07-17 01:31:16 +00002884
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002885 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002886
Chris Lattnerc61089a2009-06-30 01:26:17 +00002887 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002888 void setBlockID(signed i) { currentBlock = i; }
2889 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002890
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002891 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2892 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002893
2894 if (I == StmtMap.end())
2895 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002896
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002897 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2898 && I->second.second == currentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002899 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002900 }
Mike Stump31feda52009-07-17 01:31:16 +00002901
Ted Kremenek60983dc2010-01-19 20:52:05 +00002902 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002903 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002904 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002905
2906 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2907 DeclMapTy::iterator I = DeclMap.find(D);
2908
2909 if (I == DeclMap.end())
2910 return false;
2911
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002912 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2913 && I->second.second == currentStmt) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002914 return false;
2915 }
2916
2917 OS << "[B" << I->second.first << "." << I->second.second << "]";
2918 return true;
2919 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002920};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002921} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002922
Chris Lattnerc61089a2009-06-30 01:26:17 +00002923
2924namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002925class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002926 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002927
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002928 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002929 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002930 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002931public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002932 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002933 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002934 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002935
Ted Kremenek9aae5132007-08-23 21:42:29 +00002936 void VisitIfStmt(IfStmt* I) {
2937 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002938 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002939 }
Mike Stump31feda52009-07-17 01:31:16 +00002940
Ted Kremenek9aae5132007-08-23 21:42:29 +00002941 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002942 void VisitStmt(Stmt* Terminator) {
2943 Terminator->printPretty(OS, Helper, Policy);
2944 }
2945
Ted Kremenek9aae5132007-08-23 21:42:29 +00002946 void VisitForStmt(ForStmt* F) {
2947 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002948 if (F->getInit())
2949 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002950 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002951 if (Stmt* C = F->getCond())
2952 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002953 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002954 if (F->getInc())
2955 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002956 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002957 }
Mike Stump31feda52009-07-17 01:31:16 +00002958
Ted Kremenek9aae5132007-08-23 21:42:29 +00002959 void VisitWhileStmt(WhileStmt* W) {
2960 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002961 if (Stmt* C = W->getCond())
2962 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002963 }
Mike Stump31feda52009-07-17 01:31:16 +00002964
Ted Kremenek9aae5132007-08-23 21:42:29 +00002965 void VisitDoStmt(DoStmt* D) {
2966 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002967 if (Stmt* C = D->getCond())
2968 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002969 }
Mike Stump31feda52009-07-17 01:31:16 +00002970
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002971 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002972 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002973 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002974 }
Mike Stump31feda52009-07-17 01:31:16 +00002975
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002976 void VisitCXXTryStmt(CXXTryStmt* CS) {
2977 OS << "try ...";
2978 }
2979
John McCallc07a0c72011-02-17 10:25:35 +00002980 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002981 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002982 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002983 }
Mike Stump31feda52009-07-17 01:31:16 +00002984
Ted Kremenek391f94a2007-08-31 22:29:13 +00002985 void VisitChooseExpr(ChooseExpr* C) {
2986 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002987 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002988 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002989 }
Mike Stump31feda52009-07-17 01:31:16 +00002990
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002991 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2992 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002993 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002994 }
Mike Stump31feda52009-07-17 01:31:16 +00002995
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002996 void VisitBinaryOperator(BinaryOperator* B) {
2997 if (!B->isLogicalOp()) {
2998 VisitExpr(B);
2999 return;
3000 }
Mike Stump31feda52009-07-17 01:31:16 +00003001
Douglas Gregor7de59662009-05-29 20:38:28 +00003002 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003003
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003004 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00003005 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00003006 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003007 return;
John McCalle3027922010-08-25 11:45:40 +00003008 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003009 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003010 return;
3011 default:
3012 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003013 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003014 }
Mike Stump31feda52009-07-17 01:31:16 +00003015
Ted Kremenek12687ff2007-08-27 21:54:41 +00003016 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003017 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003018 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003019};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003020} // end anonymous namespace
3021
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003022static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003023 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003024 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3025 Stmt *S = CS;
3026
3027 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003028
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003029 // special printing for statement-expressions.
3030 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3031 CompoundStmt* Sub = SE->getSubStmt();
3032
John McCall8322c3a2011-02-13 04:07:26 +00003033 if (Sub->children()) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003034 OS << "({ ... ; ";
3035 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3036 OS << " })\n";
3037 return;
3038 }
3039 }
3040 // special printing for comma expressions.
3041 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3042 if (B->getOpcode() == BO_Comma) {
3043 OS << "... , ";
3044 Helper->handledStmt(B->getRHS(),OS);
3045 OS << '\n';
3046 return;
3047 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003048 }
3049 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003050 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003051
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003052 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003053 OS << " (OperatorCall)";
3054 } else if (isa<CXXBindTemporaryExpr>(S)) {
3055 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003056 }
Mike Stump31feda52009-07-17 01:31:16 +00003057
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003058 // Expressions need a newline.
3059 if (isa<Expr>(S))
3060 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003061
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003062 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
Alexis Hunt1d792652011-01-08 20:30:50 +00003063 CXXCtorInitializer* I = IE;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003064 if (I->isBaseInitializer())
3065 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichetd583da02010-12-04 09:14:42 +00003066 else OS << I->getAnyMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003067
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003068 OS << "(";
3069 if (Expr* IE = I->getInit())
3070 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3071 OS << ")";
3072
3073 if (I->isBaseInitializer())
3074 OS << " (Base initializer)\n";
3075 else OS << " (Member initializer)\n";
3076
3077 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3078 VarDecl* VD = DE.getVarDecl();
3079 Helper->handleDecl(VD, OS);
3080
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003081 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003082 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3083 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003084 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3085 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003086
3087 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3088 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003089
3090 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3091 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3092 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003093 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003094
3095 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3096 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003097
3098 const Type *T = FD->getType().getTypePtr();
3099 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3100 T = ET;
3101
Marcin Swiderski20b88732010-10-05 05:37:00 +00003102 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003103 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003104 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003105
3106 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3107 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3108 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3109 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003110 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003111}
Mike Stump31feda52009-07-17 01:31:16 +00003112
Chris Lattnerc61089a2009-06-30 01:26:17 +00003113static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3114 const CFGBlock& B,
3115 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003116
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003117 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003118
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003119 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003120 OS << "\n [ B" << B.getBlockID();
3121
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003122 if (&B == &cfg->getEntry())
3123 OS << " (ENTRY) ]\n";
3124 else if (&B == &cfg->getExit())
3125 OS << " (EXIT) ]\n";
3126 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003127 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003128 else
3129 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003130
Ted Kremenek71eca012007-08-29 23:20:49 +00003131 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003132 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003133
3134 if (print_edges)
3135 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003136
Mike Stump92244b02010-01-19 22:00:14 +00003137 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003138 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003139 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003140 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003141 C->getLHS()->printPretty(OS, Helper,
3142 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003143 if (C->getRHS()) {
3144 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003145 C->getRHS()->printPretty(OS, Helper,
3146 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003147 }
Mike Stump92244b02010-01-19 22:00:14 +00003148 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003149 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003150 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003151 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003152 if (CS->getExceptionDecl())
3153 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3154 0);
3155 else
3156 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003157 OS << ")";
3158
3159 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003160 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003161
Ted Kremenek71eca012007-08-29 23:20:49 +00003162 OS << ":\n";
3163 }
Mike Stump31feda52009-07-17 01:31:16 +00003164
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003165 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003166 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003167
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003168 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3169 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003170
Ted Kremenek71eca012007-08-29 23:20:49 +00003171 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003172 if (print_edges)
3173 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003174
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003175 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003176
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003177 if (Helper)
3178 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003179
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003180 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003181 }
Mike Stump31feda52009-07-17 01:31:16 +00003182
Ted Kremenek71eca012007-08-29 23:20:49 +00003183 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003184 if (B.getTerminator()) {
3185 if (print_edges)
3186 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003187
Ted Kremenek71eca012007-08-29 23:20:49 +00003188 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003189
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003190 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003191
Chris Lattnerc61089a2009-06-30 01:26:17 +00003192 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3193 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003194 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003195 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003196 }
Mike Stump31feda52009-07-17 01:31:16 +00003197
Ted Kremenek71eca012007-08-29 23:20:49 +00003198 if (print_edges) {
3199 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003200 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003201 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003202
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003203 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3204 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003205
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003206 if (i == 8 || (i-8) == 0)
3207 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003208
Ted Kremenek71eca012007-08-29 23:20:49 +00003209 OS << " B" << (*I)->getBlockID();
3210 }
Mike Stump31feda52009-07-17 01:31:16 +00003211
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003212 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003213
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003214 // Print the successors of this block.
3215 OS << " Successors (" << B.succ_size() << "):";
3216 i = 0;
3217
3218 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3219 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003220
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003221 if (i == 8 || (i-8) % 10 == 0)
3222 OS << "\n ";
3223
Mike Stump0d76d072009-07-20 23:24:15 +00003224 if (*I)
3225 OS << " B" << (*I)->getBlockID();
3226 else
3227 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003228 }
Mike Stump31feda52009-07-17 01:31:16 +00003229
Ted Kremenek71eca012007-08-29 23:20:49 +00003230 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003231 }
Mike Stump31feda52009-07-17 01:31:16 +00003232}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003233
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003234
3235/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003236void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003237
3238/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003239void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3240 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003241
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003242 // Print the entry block.
3243 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003244
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003245 // Iterate through the CFGBlocks and print them one by one.
3246 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3247 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003248 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003249 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003250
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003251 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003252 }
Mike Stump31feda52009-07-17 01:31:16 +00003253
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003254 // Print the exit block.
3255 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003256 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003257}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003258
3259/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003260void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3261 print(llvm::errs(), cfg, LO);
3262}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003263
3264/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3265/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003266void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3267 const LangOptions &LO) const {
3268 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003269 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003270}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003271
Ted Kremenek15647632008-01-30 23:02:42 +00003272/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003273void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003274 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003275 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003276 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003277}
3278
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003279Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003280 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003281 if (!Terminator)
3282 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003283
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003284 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003285
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003286 switch (Terminator->getStmtClass()) {
3287 default:
3288 break;
Mike Stump31feda52009-07-17 01:31:16 +00003289
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003290 case Stmt::ForStmtClass:
3291 E = cast<ForStmt>(Terminator)->getCond();
3292 break;
Mike Stump31feda52009-07-17 01:31:16 +00003293
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003294 case Stmt::WhileStmtClass:
3295 E = cast<WhileStmt>(Terminator)->getCond();
3296 break;
Mike Stump31feda52009-07-17 01:31:16 +00003297
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003298 case Stmt::DoStmtClass:
3299 E = cast<DoStmt>(Terminator)->getCond();
3300 break;
Mike Stump31feda52009-07-17 01:31:16 +00003301
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003302 case Stmt::IfStmtClass:
3303 E = cast<IfStmt>(Terminator)->getCond();
3304 break;
Mike Stump31feda52009-07-17 01:31:16 +00003305
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003306 case Stmt::ChooseExprClass:
3307 E = cast<ChooseExpr>(Terminator)->getCond();
3308 break;
Mike Stump31feda52009-07-17 01:31:16 +00003309
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003310 case Stmt::IndirectGotoStmtClass:
3311 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3312 break;
Mike Stump31feda52009-07-17 01:31:16 +00003313
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003314 case Stmt::SwitchStmtClass:
3315 E = cast<SwitchStmt>(Terminator)->getCond();
3316 break;
Mike Stump31feda52009-07-17 01:31:16 +00003317
John McCallc07a0c72011-02-17 10:25:35 +00003318 case Stmt::BinaryConditionalOperatorClass:
3319 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3320 break;
3321
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003322 case Stmt::ConditionalOperatorClass:
3323 E = cast<ConditionalOperator>(Terminator)->getCond();
3324 break;
Mike Stump31feda52009-07-17 01:31:16 +00003325
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003326 case Stmt::BinaryOperatorClass: // '&&' and '||'
3327 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003328 break;
Mike Stump31feda52009-07-17 01:31:16 +00003329
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003330 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003331 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003332 }
Mike Stump31feda52009-07-17 01:31:16 +00003333
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003334 return E ? E->IgnoreParens() : NULL;
3335}
3336
Ted Kremenek92137a32008-05-16 16:06:00 +00003337bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003338 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003339 if (!Terminator)
3340 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003341
Ted Kremenek92137a32008-05-16 16:06:00 +00003342 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003343
Ted Kremenek92137a32008-05-16 16:06:00 +00003344 switch (Terminator->getStmtClass()) {
3345 default:
3346 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003347
3348 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003349 case Stmt::WhileStmtClass:
3350 case Stmt::DoStmtClass:
3351 case Stmt::IfStmtClass:
3352 case Stmt::ChooseExprClass:
John McCallc07a0c72011-02-17 10:25:35 +00003353 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003354 case Stmt::ConditionalOperatorClass:
3355 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003356 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003357 }
Mike Stump31feda52009-07-17 01:31:16 +00003358
Ted Kremenek92137a32008-05-16 16:06:00 +00003359 return E ? E->IgnoreParens() : NULL;
3360}
3361
Ted Kremenek15647632008-01-30 23:02:42 +00003362
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003363//===----------------------------------------------------------------------===//
3364// CFG Graphviz Visualization
3365//===----------------------------------------------------------------------===//
3366
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003367
3368#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003369static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003370#endif
3371
Chris Lattnerc61089a2009-06-30 01:26:17 +00003372void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003373#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003374 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003375 GraphHelper = &H;
3376 llvm::ViewGraph(this,"CFG");
3377 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003378#endif
3379}
3380
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003381namespace llvm {
3382template<>
3383struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003384
3385 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3386
3387 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003388
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003389#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003390 std::string OutSStr;
3391 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003392 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003393 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003394
3395 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3396
3397 // Process string output to make it nicer...
3398 for (unsigned i = 0; i != OutStr.length(); ++i)
3399 if (OutStr[i] == '\n') { // Left justify
3400 OutStr[i] = '\\';
3401 OutStr.insert(OutStr.begin()+i+1, 'l');
3402 }
Mike Stump31feda52009-07-17 01:31:16 +00003403
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003404 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003405#else
3406 return "";
3407#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003408 }
3409};
3410} // end namespace llvm