blob: 6da47844052b1207aa098a15c09bd9a31f04af35 [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);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000293 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000294 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000295 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000296 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek21822592009-07-17 18:20:32 +0000297 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
298 CFGBlock *VisitDoStmt(DoStmt *D);
299 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000300 CFGBlock *VisitGotoStmt(GotoStmt* G);
301 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000302 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000303 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
304 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000305 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000306 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
307 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
308 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
309 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
310 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
311 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000312 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
313 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000314 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000315 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000316 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000317
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000318 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
319 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000320 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000321
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000322 // Visitors to walk an AST and generate destructors of temporaries in
323 // full expression.
324 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
325 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
326 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
327 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
328 bool BindToTemporary);
329 CFGBlock *VisitConditionalOperatorForTemporaryDtors(ConditionalOperator *E,
330 bool BindToTemporary);
331
Ted Kremenek6065ef62008-04-28 18:00:46 +0000332 // NYS == Not Yet Supported
333 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000334 badCFG = true;
335 return Block;
336 }
Mike Stump31feda52009-07-17 01:31:16 +0000337
Ted Kremenek93668002009-07-17 22:18:43 +0000338 void autoCreateBlock() { if (!Block) Block = createBlock(); }
339 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000340
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000341 CFGBlock *addStmt(Stmt *S) {
342 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000343 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000344 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000345 void addAutomaticObjDtors(LocalScope::const_iterator B,
346 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000347 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000348
Marcin Swiderski5e415732010-09-30 23:05:00 +0000349 // Local scopes creation.
350 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
351
Zhongxing Xu81714f22010-10-01 03:00:16 +0000352 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000353 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
354 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
355
356 void addLocalScopeAndDtors(Stmt* S);
357
358 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek8219b822010-12-16 07:46:53 +0000359 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000360 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000361 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000362 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000363 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000364 B->appendInitializer(I, cfg->getBumpVectorContext());
365 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000366 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
367 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
368 }
369 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
370 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
371 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000372 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
373 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
374 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000375
Marcin Swiderski321a7072010-09-30 22:54:37 +0000376 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
377 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
378 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
379 LocalScope::const_iterator E, Stmt* S);
380 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
381 LocalScope::const_iterator B, LocalScope::const_iterator E);
382
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000383 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000384 B->addSuccessor(S, cfg->getBumpVectorContext());
385 }
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenek963cc312009-07-24 06:55:42 +0000387 /// TryResult - a class representing a variant over the values
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000388 /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
Ted Kremenek963cc312009-07-24 06:55:42 +0000389 /// and is used by the CFGBuilder to decide if a branch condition
390 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000391 class TryResult {
392 int X;
393 public:
394 TryResult(bool b) : X(b ? 1 : 0) {}
395 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenek30754282009-07-24 04:47:11 +0000397 bool isTrue() const { return X == 1; }
398 bool isFalse() const { return X == 0; }
399 bool isKnown() const { return X >= 0; }
400 void negate() {
401 assert(isKnown());
402 X ^= 0x1;
403 }
404 };
Mike Stump11289f42009-09-09 15:08:12 +0000405
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000406 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump773582d2009-07-23 23:25:26 +0000407 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000408 TryResult tryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000409 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000410 return TryResult();
411
Mike Stump773582d2009-07-23 23:25:26 +0000412 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000413 if (!S->isTypeDependent() && !S->isValueDependent() &&
414 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenek963cc312009-07-24 06:55:42 +0000415 return Result.Val.getInt().getBoolValue();
Ted Kremenek30754282009-07-24 04:47:11 +0000416
417 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000418 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000419};
Mike Stump31feda52009-07-17 01:31:16 +0000420
Douglas Gregor4619e432008-12-05 23:32:09 +0000421// FIXME: Add support for dependent-sized array types in C++?
422// Does it even make sense to build a CFG for an uninstantiated template?
John McCall424cec92011-01-19 06:33:43 +0000423static const VariableArrayType *FindVA(const Type *t) {
424 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
425 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000426 if (vat->getSizeExpr())
427 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000428
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000429 t = vt->getElementType().getTypePtr();
430 }
Mike Stump31feda52009-07-17 01:31:16 +0000431
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000432 return 0;
433}
Mike Stump31feda52009-07-17 01:31:16 +0000434
435/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
436/// arbitrary statement. Examples include a single expression or a function
437/// body (compound statement). The ownership of the returned CFG is
438/// transferred to the caller. If CFG construction fails, this method returns
439/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000440CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000441 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000442
Mike Stump0d76d072009-07-20 23:24:15 +0000443 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000444 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000445 if (!Statement)
446 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000447
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000448 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000449
450 // Create an empty block that will serve as the exit block for the CFG. Since
451 // this is the first block added to the CFG, it will be implicitly registered
452 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000453 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000454 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000455 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000456
Marcin Swiderski20b88732010-10-05 05:37:00 +0000457 if (BuildOpts.AddImplicitDtors)
458 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
459 addImplicitDtorsForDestructor(DD);
460
Ted Kremenek9aae5132007-08-23 21:42:29 +0000461 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000462 CFGBlock *B = addStmt(Statement);
463
464 if (badCFG)
465 return NULL;
466
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000467 // For C++ constructor add initializers to CFG.
468 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
469 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
470 E = CD->init_rend(); I != E; ++I) {
471 B = addInitializer(*I);
472 if (badCFG)
473 return NULL;
474 }
475 }
476
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000477 if (B)
478 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000479
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000480 // Backpatch the gotos whose label -> block mappings we didn't know when we
481 // encountered them.
482 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
483 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000484
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000485 CFGBlock* B = I->block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000486 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
487 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000488
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000489 // If there is no target for the goto, then we are looking at an
490 // incomplete AST. Handle this by not registering a successor.
491 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000492
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000493 JumpTarget JT = LI->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000494 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
495 JT.scopePosition);
496 addSuccessor(B, JT.block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000497 }
498
499 // Add successors to the Indirect Goto Dispatch block (if we have one).
500 if (CFGBlock* B = cfg->getIndirectGotoBlock())
501 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
502 E = AddressTakenLabels.end(); I != E; ++I ) {
503
504 // Lookup the target block.
505 LabelMapTy::iterator LI = LabelMap.find(*I);
506
507 // If there is no target block that contains label, then we are looking
508 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000509 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000510
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000511 addSuccessor(B, LI->second.block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000512 }
Mike Stump31feda52009-07-17 01:31:16 +0000513
Mike Stump31feda52009-07-17 01:31:16 +0000514 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000515 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000516
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000517 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000518}
Mike Stump31feda52009-07-17 01:31:16 +0000519
Ted Kremenek9aae5132007-08-23 21:42:29 +0000520/// createBlock - Used to lazily create blocks that are connected
521/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000522CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000523 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000524 if (add_successor && Succ)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000525 addSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000526 return B;
527}
Mike Stump31feda52009-07-17 01:31:16 +0000528
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000529/// addInitializer - Add C++ base or member initializer element to CFG.
Alexis Hunt1d792652011-01-08 20:30:50 +0000530CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000531 if (!BuildOpts.AddInitializers)
532 return Block;
533
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000534 bool IsReference = false;
535 bool HasTemporaries = false;
536
537 // Destructors of temporaries in initialization expression should be called
538 // after initialization finishes.
539 Expr *Init = I->getInit();
540 if (Init) {
Francois Pichetd583da02010-12-04 09:14:42 +0000541 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000542 IsReference = FD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +0000543 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000544
545 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
546 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +0000547 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000548 IsReference);
549 }
550 }
551
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000552 autoCreateBlock();
553 appendInitializer(Block, I);
554
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000555 if (Init) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000556 if (HasTemporaries) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000557 // For expression with temporaries go directly to subexpression to omit
558 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +0000559 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
560 }
561 return Visit(Init);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000562 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000563
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000564 return Block;
565}
566
Marcin Swiderski5e415732010-09-30 23:05:00 +0000567/// addAutomaticObjDtors - Add to current block automatic objects destructors
568/// for objects in range of local scope positions. Use S as trigger statement
569/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000570void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
571 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000572 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000573 return;
574
Marcin Swiderski5e415732010-09-30 23:05:00 +0000575 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000576 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000577
578 autoCreateBlock();
579 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000580}
581
Marcin Swiderski20b88732010-10-05 05:37:00 +0000582/// addImplicitDtorsForDestructor - Add implicit destructors generated for
583/// base and member objects in destructor.
584void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
585 assert (BuildOpts.AddImplicitDtors
586 && "Can be called only when dtors should be added");
587 const CXXRecordDecl *RD = DD->getParent();
588
589 // At the end destroy virtual base objects.
590 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
591 VE = RD->vbases_end(); VI != VE; ++VI) {
592 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
593 if (!CD->hasTrivialDestructor()) {
594 autoCreateBlock();
595 appendBaseDtor(Block, VI);
596 }
597 }
598
599 // Before virtual bases destroy direct base objects.
600 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
601 BE = RD->bases_end(); BI != BE; ++BI) {
602 if (!BI->isVirtual()) {
603 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
604 if (!CD->hasTrivialDestructor()) {
605 autoCreateBlock();
606 appendBaseDtor(Block, BI);
607 }
608 }
609 }
610
611 // First destroy member objects.
612 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
613 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000614 // Check for constant size array. Set type to array element type.
615 QualType QT = FI->getType();
616 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
617 if (AT->getSize() == 0)
618 continue;
619 QT = AT->getElementType();
620 }
621
622 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000623 if (!CD->hasTrivialDestructor()) {
624 autoCreateBlock();
625 appendMemberDtor(Block, *FI);
626 }
627 }
628}
629
Marcin Swiderski5e415732010-09-30 23:05:00 +0000630/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
631/// way return valid LocalScope object.
632LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
633 if (!Scope) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000634 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
635 Scope = alloc.Allocate<LocalScope>();
636 BumpVectorContext ctx(alloc);
637 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000638 }
639 return Scope;
640}
641
642/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000643/// that should create implicit scope (e.g. if/else substatements).
644void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000645 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000646 return;
647
648 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000649
650 // For compound statement we will be creating explicit scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000651 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000652 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
653 ; BI != BE; ++BI) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000654 Stmt *SI = *BI;
655 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000656 SI = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000657 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000658 Scope = addLocalScopeForDeclStmt(DS, Scope);
659 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000660 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000661 }
662
663 // For any other statement scope will be implicit and as such will be
664 // interesting only for DeclStmt.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000665 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000666 S = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000667 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000668 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000669}
670
671/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
672/// reuse Scope if not NULL.
673LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000674 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000675 if (!BuildOpts.AddImplicitDtors)
676 return Scope;
677
678 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
679 ; DI != DE; ++DI) {
680 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
681 Scope = addLocalScopeForVarDecl(VD, Scope);
682 }
683 return Scope;
684}
685
686/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
687/// create add scope for automatic objects and temporary objects bound to
688/// const reference. Will reuse Scope if not NULL.
689LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000690 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000691 if (!BuildOpts.AddImplicitDtors)
692 return Scope;
693
694 // Check if variable is local.
695 switch (VD->getStorageClass()) {
696 case SC_None:
697 case SC_Auto:
698 case SC_Register:
699 break;
700 default: return Scope;
701 }
702
703 // Check for const references bound to temporary. Set type to pointee.
704 QualType QT = VD->getType();
705 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
706 QT = RT->getPointeeType();
707 if (!QT.isConstQualified())
708 return Scope;
709 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
710 return Scope;
711 }
712
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000713 // Check for constant size array. Set type to array element type.
714 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
715 if (AT->getSize() == 0)
716 return Scope;
717 QT = AT->getElementType();
718 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000719
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000720 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000721 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
722 if (!CD->hasTrivialDestructor()) {
723 // Add the variable to scope
724 Scope = createOrReuseLocalScope(Scope);
725 Scope->addVar(VD);
726 ScopePos = Scope->begin();
727 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000728 return Scope;
729}
730
731/// addLocalScopeAndDtors - For given statement add local scope for it and
732/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
733void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
734 if (!BuildOpts.AddImplicitDtors)
735 return;
736
737 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000738 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000739 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
740}
741
Marcin Swiderski321a7072010-09-30 22:54:37 +0000742/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
743/// automatic storage duration to CFGBlock's elements vector. Insertion will be
744/// performed in place specified with iterator.
745void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
746 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
747 BumpVectorContext& C = cfg->getBumpVectorContext();
748 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
749 while (B != E)
750 I = Blk->insertAutomaticObjDtor(I, *B++, S);
751}
752
753/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
754/// automatic storage duration to CFGBlock's elements vector. Elements will be
755/// appended to physical end of the vector which happens to be logical
756/// beginning.
757void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
758 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
759 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
760}
761
762/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
763/// variables with automatic storage duration to CFGBlock's elements vector.
764/// Elements will be prepended to physical beginning of the vector which
765/// happens to be logical end. Use blocks terminator as statement that specifies
766/// destructors call site.
767void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
768 LocalScope::const_iterator B, LocalScope::const_iterator E) {
769 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
770}
771
Ted Kremenek93668002009-07-17 22:18:43 +0000772/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000773/// blocks for ternary operators, &&, and ||. We also process "," and
774/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000775CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000776tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000777 if (!S) {
778 badCFG = true;
779 return 0;
780 }
Ted Kremenek93668002009-07-17 22:18:43 +0000781 switch (S->getStmtClass()) {
782 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000783 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000784
785 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000786 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000787
Ted Kremenek93668002009-07-17 22:18:43 +0000788 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000789 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000790
Ted Kremenek93668002009-07-17 22:18:43 +0000791 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000792 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000793
Ted Kremenek93668002009-07-17 22:18:43 +0000794 case Stmt::BreakStmtClass:
795 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000796
Ted Kremenek93668002009-07-17 22:18:43 +0000797 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000798 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000799 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek93668002009-07-17 22:18:43 +0000801 case Stmt::CaseStmtClass:
802 return VisitCaseStmt(cast<CaseStmt>(S));
803
804 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000805 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000806
Ted Kremenek93668002009-07-17 22:18:43 +0000807 case Stmt::CompoundStmtClass:
808 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000811 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000812
Ted Kremenek93668002009-07-17 22:18:43 +0000813 case Stmt::ContinueStmtClass:
814 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000815
Ted Kremenekb27378c2010-01-19 20:40:33 +0000816 case Stmt::CXXCatchStmtClass:
817 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
818
John McCall5d413782010-12-06 08:20:24 +0000819 case Stmt::ExprWithCleanupsClass:
820 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000821
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000822 case Stmt::CXXBindTemporaryExprClass:
823 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
824
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000825 case Stmt::CXXConstructExprClass:
826 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
827
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000828 case Stmt::CXXFunctionalCastExprClass:
829 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
830
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000831 case Stmt::CXXTemporaryObjectExprClass:
832 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
833
Zhongxing Xu7e612172010-04-13 09:38:01 +0000834 case Stmt::CXXMemberCallExprClass:
835 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
836
Ted Kremenekb27378c2010-01-19 20:40:33 +0000837 case Stmt::CXXThrowExprClass:
838 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000839
Ted Kremenekb27378c2010-01-19 20:40:33 +0000840 case Stmt::CXXTryStmtClass:
841 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000842
Ted Kremenek93668002009-07-17 22:18:43 +0000843 case Stmt::DeclStmtClass:
844 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000845
Ted Kremenek93668002009-07-17 22:18:43 +0000846 case Stmt::DefaultStmtClass:
847 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000848
Ted Kremenek93668002009-07-17 22:18:43 +0000849 case Stmt::DoStmtClass:
850 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000851
Ted Kremenek93668002009-07-17 22:18:43 +0000852 case Stmt::ForStmtClass:
853 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000854
Ted Kremenek93668002009-07-17 22:18:43 +0000855 case Stmt::GotoStmtClass:
856 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek93668002009-07-17 22:18:43 +0000858 case Stmt::IfStmtClass:
859 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000860
Ted Kremenek8219b822010-12-16 07:46:53 +0000861 case Stmt::ImplicitCastExprClass:
862 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000863
Ted Kremenek93668002009-07-17 22:18:43 +0000864 case Stmt::IndirectGotoStmtClass:
865 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000866
Ted Kremenek93668002009-07-17 22:18:43 +0000867 case Stmt::LabelStmtClass:
868 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000869
Ted Kremenek5868ec62010-04-11 17:02:10 +0000870 case Stmt::MemberExprClass:
871 return VisitMemberExpr(cast<MemberExpr>(S), asc);
872
Ted Kremenek93668002009-07-17 22:18:43 +0000873 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000874 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
875
Ted Kremenek93668002009-07-17 22:18:43 +0000876 case Stmt::ObjCAtSynchronizedStmtClass:
877 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000878
Ted Kremenek93668002009-07-17 22:18:43 +0000879 case Stmt::ObjCAtThrowStmtClass:
880 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000881
Ted Kremenek93668002009-07-17 22:18:43 +0000882 case Stmt::ObjCAtTryStmtClass:
883 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000884
Ted Kremenek93668002009-07-17 22:18:43 +0000885 case Stmt::ObjCForCollectionStmtClass:
886 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000887
Ted Kremenek93668002009-07-17 22:18:43 +0000888 case Stmt::ParenExprClass:
889 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000890 goto tryAgain;
891
Ted Kremenek93668002009-07-17 22:18:43 +0000892 case Stmt::NullStmtClass:
893 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000894
Ted Kremenek93668002009-07-17 22:18:43 +0000895 case Stmt::ReturnStmtClass:
896 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenek93668002009-07-17 22:18:43 +0000898 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000899 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000900
Ted Kremenek93668002009-07-17 22:18:43 +0000901 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000902 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000903
Ted Kremenek93668002009-07-17 22:18:43 +0000904 case Stmt::SwitchStmtClass:
905 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000906
Zhanyong Wan6dace612010-11-22 08:45:56 +0000907 case Stmt::UnaryOperatorClass:
908 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
909
Ted Kremenek93668002009-07-17 22:18:43 +0000910 case Stmt::WhileStmtClass:
911 return VisitWhileStmt(cast<WhileStmt>(S));
912 }
913}
Mike Stump11289f42009-09-09 15:08:12 +0000914
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000915CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
916 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000917 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000918 appendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000919 }
Mike Stump11289f42009-09-09 15:08:12 +0000920
Ted Kremenek93668002009-07-17 22:18:43 +0000921 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000922}
Mike Stump31feda52009-07-17 01:31:16 +0000923
Ted Kremenek93668002009-07-17 22:18:43 +0000924/// VisitChildren - Visit the children of a Stmt.
925CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
926 CFGBlock *B = Block;
John McCall8322c3a2011-02-13 04:07:26 +0000927 for (Stmt::child_range I = Terminator->children(); I; ++I) {
Ted Kremenek93668002009-07-17 22:18:43 +0000928 if (*I) B = Visit(*I);
929 }
Ted Kremenek9e248872007-08-27 21:27:44 +0000930 return B;
931}
Mike Stump11289f42009-09-09 15:08:12 +0000932
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000933CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
934 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000935 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000936
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000937 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000938 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000939 appendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000940 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000941
Ted Kremenek9aae5132007-08-23 21:42:29 +0000942 return Block;
943}
Mike Stump11289f42009-09-09 15:08:12 +0000944
Zhanyong Wan6dace612010-11-22 08:45:56 +0000945CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek8219b822010-12-16 07:46:53 +0000946 AddStmtChoice asc) {
Zhanyong Wan6dace612010-11-22 08:45:56 +0000947 if (asc.alwaysAdd()) {
948 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000949 appendStmt(Block, U, asc);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000950 }
951
Ted Kremenek8219b822010-12-16 07:46:53 +0000952 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan6dace612010-11-22 08:45:56 +0000953}
954
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000955CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
956 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000957 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000958 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000959 appendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000960
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000961 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000962 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000963
Ted Kremenek93668002009-07-17 22:18:43 +0000964 // create the block evaluating the LHS
965 CFGBlock* LHSBlock = createBlock(false);
966 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000967
Ted Kremenek93668002009-07-17 22:18:43 +0000968 // create the block evaluating the RHS
969 Succ = ConfluenceBlock;
970 Block = NULL;
971 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000972
973 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000974 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000975 return 0;
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000976 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +0000977 // Create an empty block for cases where the RHS doesn't require
978 // any explicit statements in the CFG.
979 RHSBlock = createBlock();
980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Mike Stump773582d2009-07-23 23:25:26 +0000982 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000983 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000984 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +0000985 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +0000986
Ted Kremenek93668002009-07-17 22:18:43 +0000987 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +0000988 if (B->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000989 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
990 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000991 } else {
John McCalle3027922010-08-25 11:45:40 +0000992 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000993 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
994 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +0000995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Ted Kremenek93668002009-07-17 22:18:43 +0000997 // Generate the blocks for evaluating the LHS.
998 Block = LHSBlock;
999 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +00001000 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001001
1002 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +00001003 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001004 appendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +00001005 addStmt(B->getRHS());
1006 return addStmt(B->getLHS());
1007 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001008
1009 if (B->isAssignmentOp()) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001010 if (asc.alwaysAdd()) {
1011 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001012 appendStmt(Block, B, asc);
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001013 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001014 Visit(B->getLHS());
Marcin Swiderski77232492010-10-24 08:21:40 +00001015 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001016 }
Mike Stump11289f42009-09-09 15:08:12 +00001017
Marcin Swiderski77232492010-10-24 08:21:40 +00001018 if (asc.alwaysAdd()) {
1019 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001020 appendStmt(Block, B, asc);
Marcin Swiderski77232492010-10-24 08:21:40 +00001021 }
1022
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001023 CFGBlock *RBlock = Visit(B->getRHS());
1024 CFGBlock *LBlock = Visit(B->getLHS());
1025 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1026 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1027 // return RBlock. Otherwise we'll incorrectly return NULL.
1028 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001029}
1030
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001031CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1032 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001033 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001034 appendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001035 }
1036 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001037}
1038
Ted Kremenek93668002009-07-17 22:18:43 +00001039CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1040 // "break" is a control-flow statement. Thus we stop processing the current
1041 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001042 if (badCFG)
1043 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001044
Ted Kremenek93668002009-07-17 22:18:43 +00001045 // Now create a new block that ends with the break statement.
1046 Block = createBlock(false);
1047 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001048
Ted Kremenek93668002009-07-17 22:18:43 +00001049 // If there is no target for the break, then we are looking at an incomplete
1050 // AST. This means that the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001051 if (BreakJumpTarget.block) {
1052 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1053 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001054 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001055 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001056
1057
Ted Kremenek9aae5132007-08-23 21:42:29 +00001058 return Block;
1059}
Mike Stump11289f42009-09-09 15:08:12 +00001060
Mike Stump04c68512010-01-21 15:20:48 +00001061static bool CanThrow(Expr *E) {
1062 QualType Ty = E->getType();
1063 if (Ty->isFunctionPointerType())
1064 Ty = Ty->getAs<PointerType>()->getPointeeType();
1065 else if (Ty->isBlockPointerType())
1066 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001067
Mike Stump04c68512010-01-21 15:20:48 +00001068 const FunctionType *FT = Ty->getAs<FunctionType>();
1069 if (FT) {
1070 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1071 if (Proto->hasEmptyExceptionSpec())
1072 return false;
1073 }
1074 return true;
1075}
1076
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001077CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001078 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001079 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001080 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001081 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001082 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001083
Mike Stump04c68512010-01-21 15:20:48 +00001084 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001085
1086 // Languages without exceptions are assumed to not throw.
1087 if (Context->getLangOptions().Exceptions) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001088 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001089 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001090 }
1091
1092 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001093 if (FD->hasAttr<NoReturnAttr>())
1094 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001095 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001096 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001097 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001098
Mike Stump04c68512010-01-21 15:20:48 +00001099 if (!CanThrow(C->getCallee()))
1100 AddEHEdge = false;
1101
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001102 if (!NoReturn && !AddEHEdge)
1103 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump11289f42009-09-09 15:08:12 +00001104
Mike Stump92244b02010-01-19 22:00:14 +00001105 if (Block) {
1106 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001107 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001108 return 0;
1109 }
Mike Stump11289f42009-09-09 15:08:12 +00001110
Mike Stump92244b02010-01-19 22:00:14 +00001111 Block = createBlock(!NoReturn);
Ted Kremenek8219b822010-12-16 07:46:53 +00001112 appendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001113
Mike Stump92244b02010-01-19 22:00:14 +00001114 if (NoReturn) {
1115 // Wire this to the exit block directly.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001116 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001117 }
Mike Stump04c68512010-01-21 15:20:48 +00001118 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001119 // Add exceptional edges.
1120 if (TryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001121 addSuccessor(Block, TryTerminatedBlock);
Mike Stump92244b02010-01-19 22:00:14 +00001122 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001123 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Mike Stump8c5d7992009-07-25 21:26:53 +00001126 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001127}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001128
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001129CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1130 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001131 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001132 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001133 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001134 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001136 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek21822592009-07-17 18:20:32 +00001137 Succ = ConfluenceBlock;
1138 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001139 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
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
Ted Kremenek21822592009-07-17 18:20:32 +00001143 Succ = ConfluenceBlock;
1144 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001145 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001146 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001147 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001148
Ted Kremenek21822592009-07-17 18:20:32 +00001149 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001150 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001151 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1152 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1153 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001154 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001155 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001156}
Mike Stump11289f42009-09-09 15:08:12 +00001157
1158
1159CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001160 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001161 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001162
1163 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1164 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001165 // If we hit a segment of code just containing ';' (NullStmts), we can
1166 // get a null block back. In such cases, just use the LastBlock
1167 if (CFGBlock *newBlock = addStmt(*I))
1168 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001169
Ted Kremenekce499c22009-08-27 23:16:26 +00001170 if (badCFG)
1171 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001172 }
Mike Stump92244b02010-01-19 22:00:14 +00001173
Ted Kremenek93668002009-07-17 22:18:43 +00001174 return LastBlock;
1175}
Mike Stump11289f42009-09-09 15:08:12 +00001176
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001177CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1178 AddStmtChoice asc) {
Ted Kremenek51d40b02009-07-17 18:15:54 +00001179 // Create the confluence block that will "merge" the results of the ternary
1180 // expression.
1181 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001182 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001183 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001184 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001185
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001186 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001187
Ted Kremenek51d40b02009-07-17 18:15:54 +00001188 // Create a block for the LHS expression if there is an LHS expression. A
1189 // GCC extension allows LHS to be NULL, causing the condition to be the
1190 // value that is returned instead.
1191 // e.g: x ?: y is shorthand for: x ? x : y;
1192 Succ = ConfluenceBlock;
1193 Block = NULL;
1194 CFGBlock* LHSBlock = NULL;
1195 if (C->getLHS()) {
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001196 LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001197 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001198 return 0;
1199 Block = NULL;
1200 }
Mike Stump11289f42009-09-09 15:08:12 +00001201
Ted Kremenek51d40b02009-07-17 18:15:54 +00001202 // Create the block for the RHS expression.
1203 Succ = ConfluenceBlock;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001204 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001205 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001206 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenek51d40b02009-07-17 18:15:54 +00001208 // Create the block that will contain the condition.
1209 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Mike Stump773582d2009-07-23 23:25:26 +00001211 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001212 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
Mike Stump0d76d072009-07-20 23:24:15 +00001213 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001214 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stump0d76d072009-07-20 23:24:15 +00001215 } else {
Ted Kremenek30754282009-07-24 04:47:11 +00001216 if (KnownVal.isFalse()) {
Mike Stump0d76d072009-07-20 23:24:15 +00001217 // If we know the condition is false, add NULL as the successor for
1218 // the block containing the condition. In this case, the confluence
1219 // block will have just one predecessor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001220 addSuccessor(Block, 0);
Ted Kremenek30754282009-07-24 04:47:11 +00001221 assert(ConfluenceBlock->pred_size() == 1);
Mike Stump0d76d072009-07-20 23:24:15 +00001222 } else {
1223 // If we have no LHS expression, add the ConfluenceBlock as a direct
1224 // successor for the block containing the condition. Moreover, we need to
1225 // reverse the order of the predecessors in the ConfluenceBlock because
1226 // the RHSBlock will have been added to the succcessors already, and we
1227 // want the first predecessor to the the block containing the expression
1228 // for the case when the ternary expression evaluates to true.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001229 addSuccessor(Block, ConfluenceBlock);
Ted Kremenek18fb1662010-11-15 22:59:22 +00001230 // Note that there can possibly only be one predecessor if one of the
1231 // subexpressions resulted in calling a noreturn function.
Mike Stump0d76d072009-07-20 23:24:15 +00001232 std::reverse(ConfluenceBlock->pred_begin(),
1233 ConfluenceBlock->pred_end());
1234 }
Ted Kremenek51d40b02009-07-17 18:15:54 +00001235 }
Mike Stump11289f42009-09-09 15:08:12 +00001236
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001237 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001238 Block->setTerminator(C);
1239 return addStmt(C->getCond());
1240}
1241
Ted Kremenek93668002009-07-17 22:18:43 +00001242CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001243 if (DS->isSingleDecl())
1244 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001245
Ted Kremenek93668002009-07-17 22:18:43 +00001246 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001247
Ted Kremenek93668002009-07-17 22:18:43 +00001248 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1249 typedef llvm::SmallVector<Decl*,10> BufTy;
1250 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001251
Ted Kremenek93668002009-07-17 22:18:43 +00001252 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1253 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1254 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1255 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001256
Ted Kremenek93668002009-07-17 22:18:43 +00001257 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1258 // automatically freed with the CFG.
1259 DeclGroupRef DG(*I);
1260 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001261 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001262 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001263
Ted Kremenek93668002009-07-17 22:18:43 +00001264 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001265 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001266 }
Mike Stump11289f42009-09-09 15:08:12 +00001267
1268 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001269}
Mike Stump11289f42009-09-09 15:08:12 +00001270
Ted Kremenek93668002009-07-17 22:18:43 +00001271/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001272/// DeclStmts and initializers in them.
1273CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1274 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001275
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001276 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001277
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001278 if (!VD) {
1279 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001280 appendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001281 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001282 }
Mike Stump11289f42009-09-09 15:08:12 +00001283
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001284 bool IsReference = false;
1285 bool HasTemporaries = false;
1286
1287 // Destructors of temporaries in initialization expression should be called
1288 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001289 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001290 if (Init) {
1291 IsReference = VD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +00001292 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001293
1294 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1295 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +00001296 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001297 IsReference);
1298 }
1299 }
1300
1301 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001302 appendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001303
Ted Kremenek93668002009-07-17 22:18:43 +00001304 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001305 if (HasTemporaries)
1306 // For expression with temporaries go directly to subexpression to omit
1307 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +00001308 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001309 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001310 Visit(Init);
Ted Kremenek93668002009-07-17 22:18:43 +00001311 }
Mike Stump11289f42009-09-09 15:08:12 +00001312
Ted Kremenek93668002009-07-17 22:18:43 +00001313 // If the type of VD is a VLA, then we must process its size expressions.
John McCall424cec92011-01-19 06:33:43 +00001314 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1315 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek93668002009-07-17 22:18:43 +00001316 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001317
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001318 // Remove variable from local scope.
1319 if (ScopePos && VD == *ScopePos)
1320 ++ScopePos;
1321
Ted Kremenek93668002009-07-17 22:18:43 +00001322 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001323}
1324
1325CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001326 // We may see an if statement in the middle of a basic block, or it may be the
1327 // first statement we are processing. In either case, we create a new basic
1328 // block. First, we create the blocks for the then...else statements, and
1329 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001330 // middle of a block, we stop processing that block. That block is then the
1331 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001332
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001333 // Save local scope position because in case of condition variable ScopePos
1334 // won't be restored when traversing AST.
1335 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1336
1337 // Create local scope for possible condition variable.
1338 // Store scope position. Add implicit destructor.
1339 if (VarDecl* VD = I->getConditionVariable()) {
1340 LocalScope::const_iterator BeginScopePos = ScopePos;
1341 addLocalScopeForVarDecl(VD);
1342 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1343 }
1344
Mike Stump31feda52009-07-17 01:31:16 +00001345 // The block we were proccessing is now finished. Make it the successor
1346 // block.
1347 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001348 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001349 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001350 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001351 }
Mike Stump31feda52009-07-17 01:31:16 +00001352
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001353 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001354 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001355
Ted Kremenek9aae5132007-08-23 21:42:29 +00001356 if (Stmt* Else = I->getElse()) {
1357 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001358
Ted Kremenek9aae5132007-08-23 21:42:29 +00001359 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001360 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001361 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001362
1363 // If branch is not a compound statement create implicit scope
1364 // and add destructors.
1365 if (!isa<CompoundStmt>(Else))
1366 addLocalScopeAndDtors(Else);
1367
Ted Kremenek93668002009-07-17 22:18:43 +00001368 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001369
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001370 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1371 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001372 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001373 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001374 return 0;
1375 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001376 }
Mike Stump31feda52009-07-17 01:31:16 +00001377
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001378 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001379 CFGBlock* ThenBlock;
1380 {
1381 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001382 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001383 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001384 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001385
1386 // If branch is not a compound statement create implicit scope
1387 // and add destructors.
1388 if (!isa<CompoundStmt>(Then))
1389 addLocalScopeAndDtors(Then);
1390
Ted Kremenek93668002009-07-17 22:18:43 +00001391 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001392
Ted Kremenek1b379512009-04-01 03:52:47 +00001393 if (!ThenBlock) {
1394 // We can reach here if the "then" body has all NullStmts.
1395 // Create an empty block so we can distinguish between true and false
1396 // branches in path-sensitive analyses.
1397 ThenBlock = createBlock(false);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001398 addSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001399 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001400 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001401 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001402 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001403 }
1404
Mike Stump31feda52009-07-17 01:31:16 +00001405 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001406 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001407
Ted Kremenek9aae5132007-08-23 21:42:29 +00001408 // Set the terminator of the new block to the If statement.
1409 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001410
Mike Stump773582d2009-07-23 23:25:26 +00001411 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001412 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001413
Ted Kremenek9aae5132007-08-23 21:42:29 +00001414 // Now add the successors.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001415 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1416 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001417
1418 // Add the condition as the last statement in the new block. This may create
1419 // new blocks as the condition may contain control-flow. Any newly created
1420 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001421 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001422
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001423 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1424 // and the condition variable initialization to the CFG.
1425 if (VarDecl *VD = I->getConditionVariable()) {
1426 if (Expr *Init = VD->getInit()) {
1427 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001428 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001429 addStmt(Init);
1430 }
1431 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001432
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001433 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001434}
Mike Stump31feda52009-07-17 01:31:16 +00001435
1436
Ted Kremenek9aae5132007-08-23 21:42:29 +00001437CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001438 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001439 //
Mike Stump31feda52009-07-17 01:31:16 +00001440 // NOTE: If a "return" appears in the middle of a block, this means that the
1441 // code afterwards is DEAD (unreachable). We still keep a basic block
1442 // for that code; a simple "mark-and-sweep" from the entry block will be
1443 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001444
1445 // Create the new block.
1446 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001447
Ted Kremenek9aae5132007-08-23 21:42:29 +00001448 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001449 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001450 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001451
1452 // Add the return statement to the block. This may create new blocks if R
1453 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001454 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001455}
1456
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001457CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001458 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001459 addStmt(L->getSubStmt());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001460 CFGBlock *LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001461
Ted Kremenek93668002009-07-17 22:18:43 +00001462 if (!LabelBlock) // This can happen when the body is empty, i.e.
1463 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001464
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001465 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1466 "label already in map");
1467 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001468
1469 // Labels partition blocks, so this is the end of the basic block we were
1470 // processing (L is the block's label). Because this is label (and we have
1471 // already processed the substatement) there is no extra control-flow to worry
1472 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001473 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001474 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001475 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001476
1477 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001478 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001479
Ted Kremenek9aae5132007-08-23 21:42:29 +00001480 // This block is now the implicit successor of other blocks.
1481 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001482
Ted Kremenek9aae5132007-08-23 21:42:29 +00001483 return LabelBlock;
1484}
1485
1486CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001487 // Goto is a control-flow statement. Thus we stop processing the current
1488 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001489
Ted Kremenek9aae5132007-08-23 21:42:29 +00001490 Block = createBlock(false);
1491 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001492
1493 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001494 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001495
Ted Kremenek9aae5132007-08-23 21:42:29 +00001496 if (I == LabelMap.end())
1497 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001498 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1499 else {
1500 JumpTarget JT = I->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001501 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1502 addSuccessor(Block, JT.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001503 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001504
Mike Stump31feda52009-07-17 01:31:16 +00001505 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001506}
1507
1508CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001509 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001510
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001511 // Save local scope position because in case of condition variable ScopePos
1512 // won't be restored when traversing AST.
1513 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1514
1515 // Create local scope for init statement and possible condition variable.
1516 // Add destructor for init statement and condition variable.
1517 // Store scope position for continue statement.
1518 if (Stmt* Init = F->getInit())
1519 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001520 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1521
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001522 if (VarDecl* VD = F->getConditionVariable())
1523 addLocalScopeForVarDecl(VD);
1524 LocalScope::const_iterator ContinueScopePos = ScopePos;
1525
1526 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1527
Mike Stump014b3ea2009-07-21 01:12:51 +00001528 // "for" is a control-flow statement. Thus we stop processing the current
1529 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001530 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001531 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001532 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001533 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001534 } else
1535 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001536
Ted Kremenek304a9532010-05-21 20:30:15 +00001537 // Save the current value for the break targets.
1538 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001539 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001540 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001541
Mike Stump31feda52009-07-17 01:31:16 +00001542 // Because of short-circuit evaluation, the condition of the loop can span
1543 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1544 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001545 CFGBlock* ExitConditionBlock = createBlock(false);
1546 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001547
Ted Kremenek81e14852007-08-27 19:46:09 +00001548 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001549 ExitConditionBlock->setTerminator(F);
1550
1551 // Now add the actual condition to the condition block. Because the condition
1552 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001553 if (Stmt* C = F->getCond()) {
1554 Block = ExitConditionBlock;
1555 EntryConditionBlock = addStmt(C);
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001556 if (badCFG)
1557 return 0;
Ted Kremenek7b31a612010-09-15 07:01:20 +00001558 assert(Block == EntryConditionBlock ||
1559 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001560
1561 // If this block contains a condition variable, add both the condition
1562 // variable and initializer to the CFG.
1563 if (VarDecl *VD = F->getConditionVariable()) {
1564 if (Expr *Init = VD->getInit()) {
1565 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001566 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenekec92f942009-12-24 01:49:06 +00001567 EntryConditionBlock = addStmt(Init);
1568 assert(Block == EntryConditionBlock);
1569 }
1570 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001571
Ted Kremenek55957a82009-05-02 00:13:27 +00001572 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001573 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001574 return 0;
1575 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001576 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001577
Mike Stump31feda52009-07-17 01:31:16 +00001578 // The condition block is the implicit successor for the loop body as well as
1579 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001580 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001581
Mike Stump773582d2009-07-23 23:25:26 +00001582 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001583 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001584
Mike Stump773582d2009-07-23 23:25:26 +00001585 if (F->getCond())
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001586 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001587
Ted Kremenek9aae5132007-08-23 21:42:29 +00001588 // Now create the loop body.
1589 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001590 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001591
Ted Kremenek304a9532010-05-21 20:30:15 +00001592 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001593 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1594 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001595
Ted Kremeneke9610502007-08-30 18:39:40 +00001596 // Create a new block to contain the (bottom) of the loop body.
1597 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001598
1599 // Loop body should end with destructor of Condition variable (if any).
1600 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001601
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001602 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001603 // Generate increment code in its own basic block. This is the target of
1604 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001605 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001606 } else {
1607 // No increment code. Create a special, empty, block that is used as the
1608 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001609 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001610 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001611 }
Mike Stump31feda52009-07-17 01:31:16 +00001612
Ted Kremenek902393b2009-04-28 00:51:56 +00001613 // Finish up the increment (or empty) block if it hasn't been already.
1614 if (Block) {
1615 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001616 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001617 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001618 Block = 0;
1619 }
Mike Stump31feda52009-07-17 01:31:16 +00001620
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001621 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001622
Ted Kremenek902393b2009-04-28 00:51:56 +00001623 // The starting block for the loop increment is the block that should
1624 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001625 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001626
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001627 // If body is not a compound statement create implicit scope
1628 // and add destructors.
1629 if (!isa<CompoundStmt>(F->getBody()))
1630 addLocalScopeAndDtors(F->getBody());
1631
Mike Stump31feda52009-07-17 01:31:16 +00001632 // Now populate the body block, and in the process create new blocks as we
1633 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001634 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001635
1636 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001637 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001638 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001639 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001640
Ted Kremenek30754282009-07-24 04:47:11 +00001641 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001642 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001643 }
Mike Stump31feda52009-07-17 01:31:16 +00001644
Ted Kremenek30754282009-07-24 04:47:11 +00001645 // Link up the condition block with the code that follows the loop. (the
1646 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001647 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001648
Ted Kremenek9aae5132007-08-23 21:42:29 +00001649 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001650 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001651 if (Stmt* I = F->getInit()) {
1652 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001653 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001654 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001655
1656 // There is no loop initialization. We are thus basically a while loop.
1657 // NULL out Block to force lazy block construction.
1658 Block = NULL;
1659 Succ = EntryConditionBlock;
1660 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001661}
1662
Ted Kremenek5868ec62010-04-11 17:02:10 +00001663CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1664 if (asc.alwaysAdd()) {
1665 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001666 appendStmt(Block, M, asc);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001667 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001668 return Visit(M->getBase());
Ted Kremenek5868ec62010-04-11 17:02:10 +00001669}
1670
Ted Kremenek9d56e642008-11-11 17:10:00 +00001671CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1672 // Objective-C fast enumeration 'for' statements:
1673 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1674 //
1675 // for ( Type newVariable in collection_expression ) { statements }
1676 //
1677 // becomes:
1678 //
1679 // prologue:
1680 // 1. collection_expression
1681 // T. jump to loop_entry
1682 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001683 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001684 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1685 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1686 // TB:
1687 // statements
1688 // T. jump to loop_entry
1689 // FB:
1690 // what comes after
1691 //
1692 // and
1693 //
1694 // Type existingItem;
1695 // for ( existingItem in expression ) { statements }
1696 //
1697 // becomes:
1698 //
Mike Stump31feda52009-07-17 01:31:16 +00001699 // the same with newVariable replaced with existingItem; the binding works
1700 // the same except that for one ObjCForCollectionStmt::getElement() returns
1701 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001702 //
Mike Stump31feda52009-07-17 01:31:16 +00001703
Ted Kremenek9d56e642008-11-11 17:10:00 +00001704 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001705
Ted Kremenek9d56e642008-11-11 17:10:00 +00001706 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001707 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001708 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001709 LoopSuccessor = Block;
1710 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001711 } else
1712 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001713
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001714 // Build the condition blocks.
1715 CFGBlock* ExitConditionBlock = createBlock(false);
1716 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001717
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001718 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001719 ExitConditionBlock->setTerminator(S);
1720
1721 // The last statement in the block should be the ObjCForCollectionStmt, which
1722 // performs the actual binding to 'element' and determines if there are any
1723 // more items in the collection.
Ted Kremenek8219b822010-12-16 07:46:53 +00001724 appendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001725 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001726
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001727 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001728 // generate new blocks as necesary. We DON'T add the statement by default to
1729 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001730 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001731 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001732 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001733 return 0;
1734 Block = 0;
1735 }
Mike Stump31feda52009-07-17 01:31:16 +00001736
1737 // The condition block is the implicit successor for the loop body as well as
1738 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001739 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001740
Ted Kremenek9d56e642008-11-11 17:10:00 +00001741 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001742 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001743 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001744 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1745 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1746 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001747
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001748 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1749 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001750
Ted Kremenek93668002009-07-17 22:18:43 +00001751 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001752
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001753 if (!BodyBlock)
1754 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001755 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001756 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001757 return 0;
1758 }
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001760 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001761 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001762 }
Mike Stump31feda52009-07-17 01:31:16 +00001763
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001764 // Link up the condition block with the code that follows the loop.
1765 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001766 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001767
Ted Kremenek9d56e642008-11-11 17:10:00 +00001768 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001769 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001770 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001771}
1772
Ted Kremenek49805452009-05-02 01:49:13 +00001773CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1774 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001775
Ted Kremenek49805452009-05-02 01:49:13 +00001776 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001777 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001778
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001779 // The sync body starts its own basic block. This makes it a little easier
1780 // for diagnostic clients.
1781 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001782 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001783 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001784
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001785 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001786 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001787 }
Mike Stump31feda52009-07-17 01:31:16 +00001788
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001789 // Add the @synchronized to the CFG.
1790 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001791 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001792
Ted Kremenek49805452009-05-02 01:49:13 +00001793 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001794 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001795}
Mike Stump31feda52009-07-17 01:31:16 +00001796
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001797CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001798 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001799 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001800}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001801
Ted Kremenek9aae5132007-08-23 21:42:29 +00001802CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001803 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001804
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001805 // Save local scope position because in case of condition variable ScopePos
1806 // won't be restored when traversing AST.
1807 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1808
1809 // Create local scope for possible condition variable.
1810 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001811 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001812 if (VarDecl* VD = W->getConditionVariable()) {
1813 addLocalScopeForVarDecl(VD);
1814 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1815 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001816
Mike Stump014b3ea2009-07-21 01:12:51 +00001817 // "while" is a control-flow statement. Thus we stop processing the current
1818 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001819 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001820 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001821 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001822 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001823 } else
1824 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001825
1826 // Because of short-circuit evaluation, the condition of the loop can span
1827 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1828 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001829 CFGBlock* ExitConditionBlock = createBlock(false);
1830 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001831
Ted Kremenek81e14852007-08-27 19:46:09 +00001832 // Set the terminator for the "exit" condition block.
1833 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001834
1835 // Now add the actual condition to the condition block. Because the condition
1836 // itself may contain control-flow, new blocks may be created. Thus we update
1837 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001838 if (Stmt* C = W->getCond()) {
1839 Block = ExitConditionBlock;
1840 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001841 // The condition might finish the current 'Block'.
1842 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001843
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001844 // If this block contains a condition variable, add both the condition
1845 // variable and initializer to the CFG.
1846 if (VarDecl *VD = W->getConditionVariable()) {
1847 if (Expr *Init = VD->getInit()) {
1848 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001849 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001850 EntryConditionBlock = addStmt(Init);
1851 assert(Block == EntryConditionBlock);
1852 }
1853 }
1854
Ted Kremenek55957a82009-05-02 00:13:27 +00001855 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001856 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001857 return 0;
1858 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001859 }
Mike Stump31feda52009-07-17 01:31:16 +00001860
1861 // The condition block is the implicit successor for the loop body as well as
1862 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001863 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001864
Mike Stump773582d2009-07-23 23:25:26 +00001865 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001866 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001867
Ted Kremenek9aae5132007-08-23 21:42:29 +00001868 // Process the loop body.
1869 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001870 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001871
1872 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001873 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1874 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1875 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001876
Mike Stump31feda52009-07-17 01:31:16 +00001877 // Create an empty block to represent the transition block for looping back
1878 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001879 Block = 0;
1880 assert(Succ == EntryConditionBlock);
1881 Succ = createBlock();
1882 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001883 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001884
Ted Kremenek9aae5132007-08-23 21:42:29 +00001885 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001886 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001887
Ted Kremenek9aae5132007-08-23 21:42:29 +00001888 // NULL out Block to force lazy instantiation of blocks for the body.
1889 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001890
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001891 // Loop body should end with destructor of Condition variable (if any).
1892 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1893
1894 // If body is not a compound statement create implicit scope
1895 // and add destructors.
1896 if (!isa<CompoundStmt>(W->getBody()))
1897 addLocalScopeAndDtors(W->getBody());
1898
Ted Kremenek9aae5132007-08-23 21:42:29 +00001899 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001900 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001901
Ted Kremeneke9610502007-08-30 18:39:40 +00001902 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001903 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001904 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001905 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001906 return 0;
1907 }
Mike Stump31feda52009-07-17 01:31:16 +00001908
Ted Kremenek30754282009-07-24 04:47:11 +00001909 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001910 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001911 }
Mike Stump31feda52009-07-17 01:31:16 +00001912
Ted Kremenek30754282009-07-24 04:47:11 +00001913 // Link up the condition block with the code that follows the loop. (the
1914 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001915 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001916
1917 // There can be no more statements in the condition block since we loop back
1918 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001919 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001920
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001921 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001922 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001923 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001924}
Mike Stump11289f42009-09-09 15:08:12 +00001925
1926
Ted Kremenek93668002009-07-17 22:18:43 +00001927CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1928 // FIXME: For now we pretend that @catch and the code it contains does not
1929 // exit.
1930 return Block;
1931}
Mike Stump31feda52009-07-17 01:31:16 +00001932
Ted Kremenek93041ba2008-12-09 20:20:09 +00001933CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1934 // FIXME: This isn't complete. We basically treat @throw like a return
1935 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001936
Ted Kremenek0868eea2009-09-24 18:45:41 +00001937 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001938 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001939 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001940
Ted Kremenek93041ba2008-12-09 20:20:09 +00001941 // Create the new block.
1942 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001943
Ted Kremenek93041ba2008-12-09 20:20:09 +00001944 // The Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001945 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001946
1947 // Add the statement to the block. This may create new blocks if S contains
1948 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001949 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001950}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001951
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001952CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001953 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001954 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001955 return 0;
1956
1957 // Create the new block.
1958 Block = createBlock(false);
1959
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001960 if (TryTerminatedBlock)
1961 // The current try statement is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001962 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001963 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001964 // otherwise the Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001965 addSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001966
1967 // Add the statement to the block. This may create new blocks if S contains
1968 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001969 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001970}
1971
Ted Kremenek93668002009-07-17 22:18:43 +00001972CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001973 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001974
Mike Stump8d50b6a2009-07-21 01:27:50 +00001975 // "do...while" is a control-flow statement. Thus we stop processing the
1976 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001977 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001978 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001979 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001980 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001981 } else
1982 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001983
1984 // Because of short-circuit evaluation, the condition of the loop can span
1985 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1986 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001987 CFGBlock* ExitConditionBlock = createBlock(false);
1988 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001989
Ted Kremenek81e14852007-08-27 19:46:09 +00001990 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001991 ExitConditionBlock->setTerminator(D);
1992
1993 // Now add the actual condition to the condition block. Because the condition
1994 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001995 if (Stmt* C = D->getCond()) {
1996 Block = ExitConditionBlock;
1997 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00001998 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001999 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002000 return 0;
2001 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002002 }
Mike Stump31feda52009-07-17 01:31:16 +00002003
Ted Kremeneka1523a32008-02-27 07:20:00 +00002004 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002005 Succ = EntryConditionBlock;
2006
Mike Stump773582d2009-07-23 23:25:26 +00002007 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002008 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002009
Ted Kremenek9aae5132007-08-23 21:42:29 +00002010 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002011 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002012 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002013 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002014
Ted Kremenek9aae5132007-08-23 21:42:29 +00002015 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002016 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2017 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2018 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002019
Ted Kremenek9aae5132007-08-23 21:42:29 +00002020 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002021 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002022
Ted Kremenek9aae5132007-08-23 21:42:29 +00002023 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002024 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002025
Ted Kremenek9aae5132007-08-23 21:42:29 +00002026 // NULL out Block to force lazy instantiation of blocks for the body.
2027 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002028
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002029 // If body is not a compound statement create implicit scope
2030 // and add destructors.
2031 if (!isa<CompoundStmt>(D->getBody()))
2032 addLocalScopeAndDtors(D->getBody());
2033
Ted Kremenek9aae5132007-08-23 21:42:29 +00002034 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002035 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002036
Ted Kremeneke9610502007-08-30 18:39:40 +00002037 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002038 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002039 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002040 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002041 return 0;
2042 }
Mike Stump31feda52009-07-17 01:31:16 +00002043
Ted Kremenek110974d2010-08-17 20:59:56 +00002044 if (!KnownVal.isFalse()) {
2045 // Add an intermediate block between the BodyBlock and the
2046 // ExitConditionBlock to represent the "loop back" transition. Create an
2047 // empty block to represent the transition block for looping back to the
2048 // head of the loop.
2049 // FIXME: Can we do this more efficiently without adding another block?
2050 Block = NULL;
2051 Succ = BodyBlock;
2052 CFGBlock *LoopBackBlock = createBlock();
2053 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002054
Ted Kremenek110974d2010-08-17 20:59:56 +00002055 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002056 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenek110974d2010-08-17 20:59:56 +00002057 }
2058 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002059 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002060 }
Mike Stump31feda52009-07-17 01:31:16 +00002061
Ted Kremenek30754282009-07-24 04:47:11 +00002062 // Link up the condition block with the code that follows the loop.
2063 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002064 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002065
2066 // There can be no more statements in the body block(s) since we loop back to
2067 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002068 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002069
Ted Kremenek9aae5132007-08-23 21:42:29 +00002070 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002071 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002072 return BodyBlock;
2073}
2074
2075CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2076 // "continue" is a control-flow statement. Thus we stop processing the
2077 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002078 if (badCFG)
2079 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002080
Ted Kremenek9aae5132007-08-23 21:42:29 +00002081 // Now create a new block that ends with the continue statement.
2082 Block = createBlock(false);
2083 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002084
Ted Kremenek9aae5132007-08-23 21:42:29 +00002085 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002086 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00002087 if (ContinueJumpTarget.block) {
2088 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2089 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002090 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002091 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002092
Ted Kremenek9aae5132007-08-23 21:42:29 +00002093 return Block;
2094}
Mike Stump11289f42009-09-09 15:08:12 +00002095
Ted Kremenek0747de62009-07-18 00:47:21 +00002096CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002097 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002098
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002099 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002100 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002101 appendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002102 }
Mike Stump11289f42009-09-09 15:08:12 +00002103
Ted Kremenek93668002009-07-17 22:18:43 +00002104 // VLA types have expressions that must be evaluated.
2105 if (E->isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002106 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek93668002009-07-17 22:18:43 +00002107 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2108 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002109 }
Mike Stump11289f42009-09-09 15:08:12 +00002110
Mike Stump31feda52009-07-17 01:31:16 +00002111 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002112}
Mike Stump11289f42009-09-09 15:08:12 +00002113
Ted Kremenek93668002009-07-17 22:18:43 +00002114/// VisitStmtExpr - Utility method to handle (nested) statement
2115/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002116CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2117 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002118 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002119 appendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002120 }
Ted Kremenek93668002009-07-17 22:18:43 +00002121 return VisitCompoundStmt(SE->getSubStmt());
2122}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002123
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002124CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002125 // "switch" is a control-flow statement. Thus we stop processing the current
2126 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002127 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002128
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002129 // Save local scope position because in case of condition variable ScopePos
2130 // won't be restored when traversing AST.
2131 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2132
2133 // Create local scope for possible condition variable.
2134 // Store scope position. Add implicit destructor.
2135 if (VarDecl* VD = Terminator->getConditionVariable()) {
2136 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2137 addLocalScopeForVarDecl(VD);
2138 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2139 }
2140
Ted Kremenek9aae5132007-08-23 21:42:29 +00002141 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002142 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002143 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002144 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002145 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002146
2147 // Save the current "switch" context.
2148 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002149 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002150 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002151
Mike Stump31feda52009-07-17 01:31:16 +00002152 // Set the "default" case to be the block after the switch statement. If the
2153 // switch statement contains a "default:", this value will be overwritten with
2154 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002155 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002156
Ted Kremenek9aae5132007-08-23 21:42:29 +00002157 // Create a new block that will contain the switch statement.
2158 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002159
Ted Kremenek9aae5132007-08-23 21:42:29 +00002160 // Now process the switch body. The code after the switch is the implicit
2161 // successor.
2162 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002163 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002164
2165 // When visiting the body, the case statements should automatically get linked
2166 // up to the switch. We also don't keep a pointer to the body, since all
2167 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002168 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002169 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002170
2171 // If body is not a compound statement create implicit scope
2172 // and add destructors.
2173 if (!isa<CompoundStmt>(Terminator->getBody()))
2174 addLocalScopeAndDtors(Terminator->getBody());
2175
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002176 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002177 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002178 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002179 return 0;
2180 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002181
Mike Stump31feda52009-07-17 01:31:16 +00002182 // If we have no "default:" case, the default transition is to the code
2183 // following the switch body.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002184 addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002185
Ted Kremenek81e14852007-08-27 19:46:09 +00002186 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002187 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002188 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002189 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002190 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002191
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002192 // Finally, if the SwitchStmt contains a condition variable, add both the
2193 // SwitchStmt and the condition variable initialization to the CFG.
2194 if (VarDecl *VD = Terminator->getConditionVariable()) {
2195 if (Expr *Init = VD->getInit()) {
2196 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002197 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002198 addStmt(Init);
2199 }
2200 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002201
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002202 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002203}
2204
Ted Kremenek93668002009-07-17 22:18:43 +00002205CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002206 // CaseStmts are essentially labels, so they are the first statement in a
2207 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002208 CFGBlock *TopBlock = 0, *LastBlock = 0;
2209
2210 if (Stmt *Sub = CS->getSubStmt()) {
2211 // For deeply nested chains of CaseStmts, instead of doing a recursion
2212 // (which can blow out the stack), manually unroll and create blocks
2213 // along the way.
2214 while (isa<CaseStmt>(Sub)) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002215 CFGBlock *currentBlock = createBlock(false);
2216 currentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002217
Ted Kremenek60fa6572010-08-04 23:54:30 +00002218 if (TopBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002219 addSuccessor(LastBlock, currentBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002220 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002221 TopBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002222
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002223 addSuccessor(SwitchTerminatedBlock, currentBlock);
2224 LastBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002225
2226 CS = cast<CaseStmt>(Sub);
2227 Sub = CS->getSubStmt();
2228 }
2229
2230 addStmt(Sub);
2231 }
Mike Stump11289f42009-09-09 15:08:12 +00002232
Ted Kremenek55e91e82007-08-30 18:48:11 +00002233 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002234 if (!CaseBlock)
2235 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002236
2237 // Cases statements partition blocks, so this is the top of the basic block we
2238 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002239 CaseBlock->setLabel(CS);
2240
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002241 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002242 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002243
2244 // Add this block to the list of successors for the block with the switch
2245 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002246 assert(SwitchTerminatedBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002247 addSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002248
Ted Kremenek9aae5132007-08-23 21:42:29 +00002249 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2250 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002251
Ted Kremenek60fa6572010-08-04 23:54:30 +00002252 if (TopBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002253 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002254 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002255 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002256 // This block is now the implicit successor of other blocks.
2257 Succ = CaseBlock;
2258 }
Mike Stump31feda52009-07-17 01:31:16 +00002259
Ted Kremenek60fa6572010-08-04 23:54:30 +00002260 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002261}
Mike Stump31feda52009-07-17 01:31:16 +00002262
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002263CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002264 if (Terminator->getSubStmt())
2265 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002266
Ted Kremenek654c78f2008-02-13 22:05:39 +00002267 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002268
2269 if (!DefaultCaseBlock)
2270 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002271
2272 // Default statements partition blocks, so this is the top of the basic block
2273 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002274 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002275
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002276 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002277 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002278
Mike Stump31feda52009-07-17 01:31:16 +00002279 // Unlike case statements, we don't add the default block to the successors
2280 // for the switch statement immediately. This is done when we finish
2281 // processing the switch statement. This allows for the default case
2282 // (including a fall-through to the code after the switch statement) to always
2283 // be the last successor of a switch-terminated block.
2284
Ted Kremenek654c78f2008-02-13 22:05:39 +00002285 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2286 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002287
Ted Kremenek654c78f2008-02-13 22:05:39 +00002288 // This block is now the implicit successor of other blocks.
2289 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002290
2291 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002292}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002293
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002294CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2295 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2296 // current block.
2297 CFGBlock* TrySuccessor = NULL;
2298
2299 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002300 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002301 return 0;
2302 TrySuccessor = Block;
2303 } else TrySuccessor = Succ;
2304
Mike Stump0bdba6c2010-01-20 01:15:34 +00002305 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002306
2307 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002308 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002309 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002310 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002311
Mike Stump0bdba6c2010-01-20 01:15:34 +00002312 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002313 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2314 // The code after the try is the implicit successor.
2315 Succ = TrySuccessor;
2316 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002317 if (CS->getExceptionDecl() == 0) {
2318 HasCatchAll = true;
2319 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002320 Block = NULL;
2321 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2322 if (CatchBlock == 0)
2323 return 0;
2324 // Add this block to the list of successors for the block with the try
2325 // statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002326 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002327 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002328 if (!HasCatchAll) {
2329 if (PrevTryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002330 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002331 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002332 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002333 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002334
2335 // The code after the try is the implicit successor.
2336 Succ = TrySuccessor;
2337
Mike Stump845384a2010-01-20 01:30:58 +00002338 // Save the current "try" context.
2339 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2340 TryTerminatedBlock = NewTryTerminatedBlock;
2341
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002342 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002343 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002344 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002345 return Block;
2346}
2347
2348CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2349 // CXXCatchStmt are treated like labels, so they are the first statement in a
2350 // block.
2351
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002352 // Save local scope position because in case of exception variable ScopePos
2353 // won't be restored when traversing AST.
2354 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2355
2356 // Create local scope for possible exception variable.
2357 // Store scope position. Add implicit destructor.
2358 if (VarDecl* VD = CS->getExceptionDecl()) {
2359 LocalScope::const_iterator BeginScopePos = ScopePos;
2360 addLocalScopeForVarDecl(VD);
2361 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2362 }
2363
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002364 if (CS->getHandlerBlock())
2365 addStmt(CS->getHandlerBlock());
2366
2367 CFGBlock* CatchBlock = Block;
2368 if (!CatchBlock)
2369 CatchBlock = createBlock();
2370
2371 CatchBlock->setLabel(CS);
2372
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002373 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002374 return 0;
2375
2376 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2377 Block = NULL;
2378
2379 return CatchBlock;
2380}
2381
John McCall5d413782010-12-06 08:20:24 +00002382CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002383 AddStmtChoice asc) {
2384 if (BuildOpts.AddImplicitDtors) {
2385 // If adding implicit destructors visit the full expression for adding
2386 // destructors of temporaries.
2387 VisitForTemporaryDtors(E->getSubExpr());
2388
2389 // Full expression has to be added as CFGStmt so it will be sequenced
2390 // before destructors of it's temporaries.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002391 asc = asc.withAlwaysAdd(true);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002392 }
2393 return Visit(E->getSubExpr(), asc);
2394}
2395
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002396CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2397 AddStmtChoice asc) {
2398 if (asc.alwaysAdd()) {
2399 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002400 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002401
2402 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002403 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002404 }
2405 return Visit(E->getSubExpr(), asc);
2406}
2407
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002408CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2409 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002410 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002411 if (!C->isElidable())
Ted Kremenek8219b822010-12-16 07:46:53 +00002412 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002413
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002414 return VisitChildren(C);
2415}
2416
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002417CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2418 AddStmtChoice asc) {
2419 if (asc.alwaysAdd()) {
2420 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002421 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002422 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002423 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002424 }
2425 return Visit(E->getSubExpr(), asc);
2426}
2427
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002428CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2429 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002430 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002431 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002432 return VisitChildren(C);
2433}
2434
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002435CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002436 AddStmtChoice asc) {
Zhongxing Xu7e612172010-04-13 09:38:01 +00002437 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002438 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002439 return VisitChildren(C);
2440}
2441
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002442CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2443 AddStmtChoice asc) {
2444 if (asc.alwaysAdd()) {
2445 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002446 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002447 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002448 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002449}
2450
Ted Kremenekeda180e22007-08-28 19:26:49 +00002451CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002452 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002453 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002454
Ted Kremenekeda180e22007-08-28 19:26:49 +00002455 if (!IBlock) {
2456 IBlock = createBlock(false);
2457 cfg->setIndirectGotoBlock(IBlock);
2458 }
Mike Stump31feda52009-07-17 01:31:16 +00002459
Ted Kremenekeda180e22007-08-28 19:26:49 +00002460 // IndirectGoto is a control-flow statement. Thus we stop processing the
2461 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002462 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002463 return 0;
2464
Ted Kremenekeda180e22007-08-28 19:26:49 +00002465 Block = createBlock(false);
2466 Block->setTerminator(I);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002467 addSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002468 return addStmt(I->getTarget());
2469}
2470
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002471CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2472tryAgain:
2473 if (!E) {
2474 badCFG = true;
2475 return NULL;
2476 }
2477 switch (E->getStmtClass()) {
2478 default:
2479 return VisitChildrenForTemporaryDtors(E);
2480
2481 case Stmt::BinaryOperatorClass:
2482 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2483
2484 case Stmt::CXXBindTemporaryExprClass:
2485 return VisitCXXBindTemporaryExprForTemporaryDtors(
2486 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2487
2488 case Stmt::ConditionalOperatorClass:
2489 return VisitConditionalOperatorForTemporaryDtors(
2490 cast<ConditionalOperator>(E), BindToTemporary);
2491
2492 case Stmt::ImplicitCastExprClass:
2493 // For implicit cast we want BindToTemporary to be passed further.
2494 E = cast<CastExpr>(E)->getSubExpr();
2495 goto tryAgain;
2496
2497 case Stmt::ParenExprClass:
2498 E = cast<ParenExpr>(E)->getSubExpr();
2499 goto tryAgain;
2500 }
2501}
2502
2503CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2504 // When visiting children for destructors we want to visit them in reverse
2505 // order. Because there's no reverse iterator for children must to reverse
2506 // them in helper vector.
2507 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2508 ChildrenVect ChildrenRev;
John McCall8322c3a2011-02-13 04:07:26 +00002509 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002510 if (*I) ChildrenRev.push_back(*I);
2511 }
2512
2513 CFGBlock *B = Block;
2514 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2515 L = ChildrenRev.rend(); I != L; ++I) {
2516 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2517 B = R;
2518 }
2519 return B;
2520}
2521
2522CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2523 if (E->isLogicalOp()) {
2524 // Destructors for temporaries in LHS expression should be called after
2525 // those for RHS expression. Even if this will unnecessarily create a block,
2526 // this block will be used at least by the full expression.
2527 autoCreateBlock();
2528 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2529 if (badCFG)
2530 return NULL;
2531
2532 Succ = ConfluenceBlock;
2533 Block = NULL;
2534 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2535
2536 if (RHSBlock) {
2537 if (badCFG)
2538 return NULL;
2539
2540 // If RHS expression did produce destructors we need to connect created
2541 // blocks to CFG in same manner as for binary operator itself.
2542 CFGBlock *LHSBlock = createBlock(false);
2543 LHSBlock->setTerminator(CFGTerminator(E, true));
2544
2545 // For binary operator LHS block is before RHS in list of predecessors
2546 // of ConfluenceBlock.
2547 std::reverse(ConfluenceBlock->pred_begin(),
2548 ConfluenceBlock->pred_end());
2549
2550 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002551 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002552 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2553 KnownVal.negate();
2554
2555 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2556 // itself.
2557 if (E->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002558 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2559 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002560 } else {
2561 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002562 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2563 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002564 }
2565
2566 Block = LHSBlock;
2567 return LHSBlock;
2568 }
2569
2570 Block = ConfluenceBlock;
2571 return ConfluenceBlock;
2572 }
2573
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002574 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002575 // For assignment operator (=) LHS expression is visited
2576 // before RHS expression. For destructors visit them in reverse order.
2577 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2578 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2579 return LHSBlock ? LHSBlock : RHSBlock;
2580 }
2581
2582 // For any other binary operator RHS expression is visited before
2583 // LHS expression (order of children). For destructors visit them in reverse
2584 // order.
2585 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2586 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2587 return RHSBlock ? RHSBlock : LHSBlock;
2588}
2589
2590CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2591 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2592 // First add destructors for temporaries in subexpression.
2593 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002594 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002595 // If lifetime of temporary is not prolonged (by assigning to constant
2596 // reference) add destructor for it.
2597 autoCreateBlock();
2598 appendTemporaryDtor(Block, E);
2599 B = Block;
2600 }
2601 return B;
2602}
2603
2604CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
2605 ConditionalOperator *E, bool BindToTemporary) {
2606 // First add destructors for condition expression. Even if this will
2607 // unnecessarily create a block, this block will be used at least by the full
2608 // expression.
2609 autoCreateBlock();
2610 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2611 if (badCFG)
2612 return NULL;
2613
2614 // Try to add block with destructors for LHS expression.
2615 CFGBlock *LHSBlock = NULL;
2616 if (E->getLHS()) {
2617 Succ = ConfluenceBlock;
2618 Block = NULL;
2619 LHSBlock = VisitForTemporaryDtors(E->getLHS(), BindToTemporary);
2620 if (badCFG)
2621 return NULL;
2622 }
2623
2624 // Try to add block with destructors for RHS expression;
2625 Succ = ConfluenceBlock;
2626 Block = NULL;
2627 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS(), BindToTemporary);
2628 if (badCFG)
2629 return NULL;
2630
2631 if (!RHSBlock && !LHSBlock) {
2632 // If neither LHS nor RHS expression had temporaries to destroy don't create
2633 // more blocks.
2634 Block = ConfluenceBlock;
2635 return Block;
2636 }
2637
2638 Block = createBlock(false);
2639 Block->setTerminator(CFGTerminator(E, true));
2640
2641 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002642 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002643
2644 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002645 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002646 } else if (KnownVal.isFalse()) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002647 addSuccessor(Block, NULL);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002648 } else {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002649 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002650 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2651 }
2652
2653 if (!RHSBlock)
2654 RHSBlock = ConfluenceBlock;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002655 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002656
2657 return Block;
2658}
2659
Ted Kremenek04cca642007-08-23 21:26:19 +00002660} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002661
Mike Stump31feda52009-07-17 01:31:16 +00002662/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2663/// no successors or predecessors. If this is the first block created in the
2664/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002665CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002666 bool first_block = begin() == end();
2667
2668 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002669 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2670 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2671 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002672
2673 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002674 if (first_block)
2675 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002676
2677 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002678 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002679}
2680
Ted Kremenek889073f2007-08-23 16:51:22 +00002681/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2682/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002683CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002684 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002685 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002686 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002687}
2688
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002689//===----------------------------------------------------------------------===//
2690// CFG: Queries for BlkExprs.
2691//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002692
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002693namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002694 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002695}
2696
Ted Kremenekbff98442009-12-23 23:37:10 +00002697static void FindSubExprAssignments(Stmt *S,
2698 llvm::SmallPtrSet<Expr*,50>& Set) {
2699 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002700 return;
Mike Stump31feda52009-07-17 01:31:16 +00002701
John McCall8322c3a2011-02-13 04:07:26 +00002702 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002703 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002704 if (!child)
2705 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002706
Ted Kremenekbff98442009-12-23 23:37:10 +00002707 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002708 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002709
Ted Kremenekbff98442009-12-23 23:37:10 +00002710 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002711 }
2712}
2713
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002714static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2715 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002716
2717 // Look for assignments that are used as subexpressions. These are the only
2718 // assignments that we want to *possibly* register as a block-level
2719 // expression. Basically, if an assignment occurs both in a subexpression and
2720 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002721 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002722
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002723 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002724 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002725 if (CFGStmt S = BI->getAs<CFGStmt>())
2726 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002727
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002728 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002729
2730 // Iterate over the statements again on identify the Expr* and Stmt* at the
2731 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002732
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002733 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2734 CFGStmt CS = BI->getAs<CFGStmt>();
2735 if (!CS.isValid())
2736 continue;
2737 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002738
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002739 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002740 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002741 // expression are really "statements" whose value is never used by
2742 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002743 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002744 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002745 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2746 // Special handling for statement expressions. The last statement in
2747 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002748 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002749 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002750 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002751 (*M)[C->body_back()] = x;
2752 }
2753 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002754
Ted Kremenek95a123c2008-01-26 00:03:27 +00002755 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002756 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002757 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002758 }
Mike Stump31feda52009-07-17 01:31:16 +00002759
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002760 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002761
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002762 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002763
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002764 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002765 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002766 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002767 }
2768 }
Mike Stump31feda52009-07-17 01:31:16 +00002769
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002770 return M;
2771}
2772
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002773CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2774 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002775 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002776
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002777 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002778 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002779 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002780}
2781
2782unsigned CFG::getNumBlkExprs() {
2783 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2784 return M->size();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002785
2786 // We assume callers interested in the number of BlkExprs will want
2787 // the map constructed if it doesn't already exist.
2788 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2789 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002790}
2791
Ted Kremenek6065ef62008-04-28 18:00:46 +00002792//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002793// Filtered walking of the CFG.
2794//===----------------------------------------------------------------------===//
2795
2796bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002797 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002798
2799 if (F.IgnoreDefaultsWithCoveredEnums) {
2800 // If the 'To' has no label or is labeled but the label isn't a
2801 // CaseStmt then filter this edge.
2802 if (const SwitchStmt *S =
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002803 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002804 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002805 const Stmt *L = To->getLabel();
2806 if (!L || !isa<CaseStmt>(L))
2807 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002808 }
2809 }
2810 }
2811
2812 return false;
2813}
2814
2815//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002816// Cleanup: CFG dstor.
2817//===----------------------------------------------------------------------===//
2818
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002819CFG::~CFG() {
2820 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2821}
Mike Stump31feda52009-07-17 01:31:16 +00002822
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002823//===----------------------------------------------------------------------===//
2824// CFG pretty printing
2825//===----------------------------------------------------------------------===//
2826
Ted Kremenek7e776b12007-08-22 18:22:34 +00002827namespace {
2828
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002829class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002830 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002831 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002832 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002833 DeclMapTy DeclMap;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002834 signed currentBlock;
2835 unsigned currentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002836 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002837public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002838
Chris Lattnerc61089a2009-06-30 01:26:17 +00002839 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002840 : currentBlock(0), currentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002841 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2842 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002843 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002844 BI != BEnd; ++BI, ++j ) {
2845 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2846 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2847 StmtMap[SE] = P;
2848
2849 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2850 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002851
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002852 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2853 if (VarDecl* VD = IS->getConditionVariable())
2854 DeclMap[VD] = P;
2855
2856 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2857 if (VarDecl* VD = FS->getConditionVariable())
2858 DeclMap[VD] = P;
2859
2860 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2861 if (VarDecl* VD = WS->getConditionVariable())
2862 DeclMap[VD] = P;
2863
2864 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2865 if (VarDecl* VD = SS->getConditionVariable())
2866 DeclMap[VD] = P;
2867
2868 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2869 if (VarDecl* VD = CS->getExceptionDecl())
2870 DeclMap[VD] = P;
2871 }
2872 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002873 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002874 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002875 }
Mike Stump31feda52009-07-17 01:31:16 +00002876
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002877 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002878
Chris Lattnerc61089a2009-06-30 01:26:17 +00002879 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002880 void setBlockID(signed i) { currentBlock = i; }
2881 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002882
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002883 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2884 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002885
2886 if (I == StmtMap.end())
2887 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002888
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002889 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2890 && I->second.second == currentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002891 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002892 }
Mike Stump31feda52009-07-17 01:31:16 +00002893
Ted Kremenek60983dc2010-01-19 20:52:05 +00002894 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002895 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002896 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002897
2898 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2899 DeclMapTy::iterator I = DeclMap.find(D);
2900
2901 if (I == DeclMap.end())
2902 return false;
2903
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002904 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2905 && I->second.second == currentStmt) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002906 return false;
2907 }
2908
2909 OS << "[B" << I->second.first << "." << I->second.second << "]";
2910 return true;
2911 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002912};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002913} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002914
Chris Lattnerc61089a2009-06-30 01:26:17 +00002915
2916namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002917class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002918 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002919
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002920 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002921 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002922 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002923public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002924 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002925 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002926 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002927
Ted Kremenek9aae5132007-08-23 21:42:29 +00002928 void VisitIfStmt(IfStmt* I) {
2929 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002930 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002931 }
Mike Stump31feda52009-07-17 01:31:16 +00002932
Ted Kremenek9aae5132007-08-23 21:42:29 +00002933 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002934 void VisitStmt(Stmt* Terminator) {
2935 Terminator->printPretty(OS, Helper, Policy);
2936 }
2937
Ted Kremenek9aae5132007-08-23 21:42:29 +00002938 void VisitForStmt(ForStmt* F) {
2939 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002940 if (F->getInit())
2941 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002942 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002943 if (Stmt* C = F->getCond())
2944 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002945 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002946 if (F->getInc())
2947 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002948 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002949 }
Mike Stump31feda52009-07-17 01:31:16 +00002950
Ted Kremenek9aae5132007-08-23 21:42:29 +00002951 void VisitWhileStmt(WhileStmt* W) {
2952 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002953 if (Stmt* C = W->getCond())
2954 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002955 }
Mike Stump31feda52009-07-17 01:31:16 +00002956
Ted Kremenek9aae5132007-08-23 21:42:29 +00002957 void VisitDoStmt(DoStmt* D) {
2958 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002959 if (Stmt* C = D->getCond())
2960 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002961 }
Mike Stump31feda52009-07-17 01:31:16 +00002962
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002963 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002964 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002965 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002966 }
Mike Stump31feda52009-07-17 01:31:16 +00002967
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002968 void VisitCXXTryStmt(CXXTryStmt* CS) {
2969 OS << "try ...";
2970 }
2971
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002972 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002973 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002974 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002975 }
Mike Stump31feda52009-07-17 01:31:16 +00002976
Ted Kremenek391f94a2007-08-31 22:29:13 +00002977 void VisitChooseExpr(ChooseExpr* C) {
2978 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002979 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002980 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002981 }
Mike Stump31feda52009-07-17 01:31:16 +00002982
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002983 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2984 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00002985 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002986 }
Mike Stump31feda52009-07-17 01:31:16 +00002987
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002988 void VisitBinaryOperator(BinaryOperator* B) {
2989 if (!B->isLogicalOp()) {
2990 VisitExpr(B);
2991 return;
2992 }
Mike Stump31feda52009-07-17 01:31:16 +00002993
Douglas Gregor7de59662009-05-29 20:38:28 +00002994 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002995
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002996 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00002997 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00002998 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002999 return;
John McCalle3027922010-08-25 11:45:40 +00003000 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003001 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003002 return;
3003 default:
3004 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003005 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003006 }
Mike Stump31feda52009-07-17 01:31:16 +00003007
Ted Kremenek12687ff2007-08-27 21:54:41 +00003008 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003009 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003010 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003011};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003012} // end anonymous namespace
3013
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003014static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003015 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003016 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3017 Stmt *S = CS;
3018
3019 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003020
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003021 // special printing for statement-expressions.
3022 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3023 CompoundStmt* Sub = SE->getSubStmt();
3024
John McCall8322c3a2011-02-13 04:07:26 +00003025 if (Sub->children()) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003026 OS << "({ ... ; ";
3027 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3028 OS << " })\n";
3029 return;
3030 }
3031 }
3032 // special printing for comma expressions.
3033 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3034 if (B->getOpcode() == BO_Comma) {
3035 OS << "... , ";
3036 Helper->handledStmt(B->getRHS(),OS);
3037 OS << '\n';
3038 return;
3039 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003040 }
3041 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003042 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003043
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003044 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003045 OS << " (OperatorCall)";
3046 } else if (isa<CXXBindTemporaryExpr>(S)) {
3047 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003048 }
Mike Stump31feda52009-07-17 01:31:16 +00003049
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003050 // Expressions need a newline.
3051 if (isa<Expr>(S))
3052 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003053
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003054 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
Alexis Hunt1d792652011-01-08 20:30:50 +00003055 CXXCtorInitializer* I = IE;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003056 if (I->isBaseInitializer())
3057 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichetd583da02010-12-04 09:14:42 +00003058 else OS << I->getAnyMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003059
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003060 OS << "(";
3061 if (Expr* IE = I->getInit())
3062 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3063 OS << ")";
3064
3065 if (I->isBaseInitializer())
3066 OS << " (Base initializer)\n";
3067 else OS << " (Member initializer)\n";
3068
3069 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3070 VarDecl* VD = DE.getVarDecl();
3071 Helper->handleDecl(VD, OS);
3072
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003073 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003074 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3075 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003076 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3077 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003078
3079 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3080 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003081
3082 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3083 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3084 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003085 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003086
3087 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3088 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003089
3090 const Type *T = FD->getType().getTypePtr();
3091 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3092 T = ET;
3093
Marcin Swiderski20b88732010-10-05 05:37:00 +00003094 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003095 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003096 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003097
3098 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3099 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3100 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3101 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003102 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003103}
Mike Stump31feda52009-07-17 01:31:16 +00003104
Chris Lattnerc61089a2009-06-30 01:26:17 +00003105static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3106 const CFGBlock& B,
3107 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003108
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003109 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003110
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003111 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003112 OS << "\n [ B" << B.getBlockID();
3113
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003114 if (&B == &cfg->getEntry())
3115 OS << " (ENTRY) ]\n";
3116 else if (&B == &cfg->getExit())
3117 OS << " (EXIT) ]\n";
3118 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003119 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003120 else
3121 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003122
Ted Kremenek71eca012007-08-29 23:20:49 +00003123 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003124 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003125
3126 if (print_edges)
3127 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003128
Mike Stump92244b02010-01-19 22:00:14 +00003129 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003130 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003131 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003132 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003133 C->getLHS()->printPretty(OS, Helper,
3134 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003135 if (C->getRHS()) {
3136 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003137 C->getRHS()->printPretty(OS, Helper,
3138 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003139 }
Mike Stump92244b02010-01-19 22:00:14 +00003140 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003141 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003142 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003143 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003144 if (CS->getExceptionDecl())
3145 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3146 0);
3147 else
3148 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003149 OS << ")";
3150
3151 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003152 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003153
Ted Kremenek71eca012007-08-29 23:20:49 +00003154 OS << ":\n";
3155 }
Mike Stump31feda52009-07-17 01:31:16 +00003156
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003157 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003158 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003159
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003160 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3161 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003162
Ted Kremenek71eca012007-08-29 23:20:49 +00003163 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003164 if (print_edges)
3165 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003166
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003167 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003168
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003169 if (Helper)
3170 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003171
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003172 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003173 }
Mike Stump31feda52009-07-17 01:31:16 +00003174
Ted Kremenek71eca012007-08-29 23:20:49 +00003175 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003176 if (B.getTerminator()) {
3177 if (print_edges)
3178 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003179
Ted Kremenek71eca012007-08-29 23:20:49 +00003180 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003181
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003182 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003183
Chris Lattnerc61089a2009-06-30 01:26:17 +00003184 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3185 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003186 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003187 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003188 }
Mike Stump31feda52009-07-17 01:31:16 +00003189
Ted Kremenek71eca012007-08-29 23:20:49 +00003190 if (print_edges) {
3191 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003192 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003193 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003194
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003195 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3196 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003197
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003198 if (i == 8 || (i-8) == 0)
3199 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003200
Ted Kremenek71eca012007-08-29 23:20:49 +00003201 OS << " B" << (*I)->getBlockID();
3202 }
Mike Stump31feda52009-07-17 01:31:16 +00003203
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003204 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003205
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003206 // Print the successors of this block.
3207 OS << " Successors (" << B.succ_size() << "):";
3208 i = 0;
3209
3210 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3211 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003212
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003213 if (i == 8 || (i-8) % 10 == 0)
3214 OS << "\n ";
3215
Mike Stump0d76d072009-07-20 23:24:15 +00003216 if (*I)
3217 OS << " B" << (*I)->getBlockID();
3218 else
3219 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003220 }
Mike Stump31feda52009-07-17 01:31:16 +00003221
Ted Kremenek71eca012007-08-29 23:20:49 +00003222 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003223 }
Mike Stump31feda52009-07-17 01:31:16 +00003224}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003225
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003226
3227/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003228void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003229
3230/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003231void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3232 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003233
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003234 // Print the entry block.
3235 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003236
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003237 // Iterate through the CFGBlocks and print them one by one.
3238 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3239 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003240 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003241 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003242
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003243 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003244 }
Mike Stump31feda52009-07-17 01:31:16 +00003245
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003246 // Print the exit block.
3247 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003248 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003249}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003250
3251/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003252void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3253 print(llvm::errs(), cfg, LO);
3254}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003255
3256/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3257/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003258void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3259 const LangOptions &LO) const {
3260 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003261 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003262}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003263
Ted Kremenek15647632008-01-30 23:02:42 +00003264/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003265void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003266 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003267 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003268 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003269}
3270
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003271Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003272 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003273 if (!Terminator)
3274 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003275
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003276 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003277
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003278 switch (Terminator->getStmtClass()) {
3279 default:
3280 break;
Mike Stump31feda52009-07-17 01:31:16 +00003281
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003282 case Stmt::ForStmtClass:
3283 E = cast<ForStmt>(Terminator)->getCond();
3284 break;
Mike Stump31feda52009-07-17 01:31:16 +00003285
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003286 case Stmt::WhileStmtClass:
3287 E = cast<WhileStmt>(Terminator)->getCond();
3288 break;
Mike Stump31feda52009-07-17 01:31:16 +00003289
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003290 case Stmt::DoStmtClass:
3291 E = cast<DoStmt>(Terminator)->getCond();
3292 break;
Mike Stump31feda52009-07-17 01:31:16 +00003293
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003294 case Stmt::IfStmtClass:
3295 E = cast<IfStmt>(Terminator)->getCond();
3296 break;
Mike Stump31feda52009-07-17 01:31:16 +00003297
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003298 case Stmt::ChooseExprClass:
3299 E = cast<ChooseExpr>(Terminator)->getCond();
3300 break;
Mike Stump31feda52009-07-17 01:31:16 +00003301
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003302 case Stmt::IndirectGotoStmtClass:
3303 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3304 break;
Mike Stump31feda52009-07-17 01:31:16 +00003305
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003306 case Stmt::SwitchStmtClass:
3307 E = cast<SwitchStmt>(Terminator)->getCond();
3308 break;
Mike Stump31feda52009-07-17 01:31:16 +00003309
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003310 case Stmt::ConditionalOperatorClass:
3311 E = cast<ConditionalOperator>(Terminator)->getCond();
3312 break;
Mike Stump31feda52009-07-17 01:31:16 +00003313
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003314 case Stmt::BinaryOperatorClass: // '&&' and '||'
3315 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003316 break;
Mike Stump31feda52009-07-17 01:31:16 +00003317
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003318 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003319 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003320 }
Mike Stump31feda52009-07-17 01:31:16 +00003321
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003322 return E ? E->IgnoreParens() : NULL;
3323}
3324
Ted Kremenek92137a32008-05-16 16:06:00 +00003325bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003326 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003327 if (!Terminator)
3328 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003329
Ted Kremenek92137a32008-05-16 16:06:00 +00003330 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003331
Ted Kremenek92137a32008-05-16 16:06:00 +00003332 switch (Terminator->getStmtClass()) {
3333 default:
3334 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003335
3336 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003337 case Stmt::WhileStmtClass:
3338 case Stmt::DoStmtClass:
3339 case Stmt::IfStmtClass:
3340 case Stmt::ChooseExprClass:
3341 case Stmt::ConditionalOperatorClass:
3342 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003343 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003344 }
Mike Stump31feda52009-07-17 01:31:16 +00003345
Ted Kremenek92137a32008-05-16 16:06:00 +00003346 return E ? E->IgnoreParens() : NULL;
3347}
3348
Ted Kremenek15647632008-01-30 23:02:42 +00003349
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003350//===----------------------------------------------------------------------===//
3351// CFG Graphviz Visualization
3352//===----------------------------------------------------------------------===//
3353
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003354
3355#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003356static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003357#endif
3358
Chris Lattnerc61089a2009-06-30 01:26:17 +00003359void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003360#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003361 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003362 GraphHelper = &H;
3363 llvm::ViewGraph(this,"CFG");
3364 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003365#endif
3366}
3367
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003368namespace llvm {
3369template<>
3370struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003371
3372 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3373
3374 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003375
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003376#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003377 std::string OutSStr;
3378 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003379 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003380 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003381
3382 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3383
3384 // Process string output to make it nicer...
3385 for (unsigned i = 0; i != OutStr.length(); ++i)
3386 if (OutStr[i] == '\n') { // Left justify
3387 OutStr[i] = '\\';
3388 OutStr.insert(OutStr.begin()+i+1, 'l');
3389 }
Mike Stump31feda52009-07-17 01:31:16 +00003390
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003391 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003392#else
3393 return "";
3394#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003395 }
3396};
3397} // end namespace llvm