blob: 90b3120cd22fa631172f6b8ed966315845f80f61 [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"
Ted Kremenek1a241d12011-02-23 05:11:46 +000020#include "clang/AST/CharUnits.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000021#include "llvm/Support/GraphWriter.h"
Benjamin Kramer89b422c2009-08-23 12:08:50 +000022#include "llvm/Support/Allocator.h"
23#include "llvm/Support/Format.h"
Ted Kremenek8a632182007-08-21 23:26:17 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenekeda180e22007-08-28 19:26:49 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek8aed4902009-10-20 23:46:25 +000026#include "llvm/ADT/OwningPtr.h"
Ted Kremeneke5ccf9a2008-01-11 00:40:29 +000027
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +000028using namespace clang;
29
30namespace {
31
Douglas Gregor6e6ad602009-01-20 01:17:11 +000032static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenek8889bb32008-08-06 23:20:50 +000033 if (VarDecl* VD = dyn_cast<VarDecl>(D))
34 if (Expr* Ex = VD->getInit())
35 return Ex->getSourceRange().getEnd();
Mike Stump31feda52009-07-17 01:31:16 +000036 return D->getLocation();
Ted Kremenek8889bb32008-08-06 23:20:50 +000037}
Ted Kremenekdc03bd02010-08-02 23:46:59 +000038
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000039/// The CFG builder uses a recursive algorithm to build the CFG. When
40/// we process an expression, sometimes we know that we must add the
41/// subexpressions as block-level expressions. For example:
42///
43/// exp1 || exp2
44///
45/// When processing the '||' expression, we know that exp1 and exp2
46/// need to be added as block-level expressions, even though they
47/// might not normally need to be. AddStmtChoice records this
48/// contextual information. If AddStmtChoice is 'NotAlwaysAdd', then
49/// the builder has an option not to add a subexpression as a
50/// block-level expression.
51///
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000052class AddStmtChoice {
53public:
Ted Kremenek8219b822010-12-16 07:46:53 +000054 enum Kind { NotAlwaysAdd = 0, AlwaysAdd = 1 };
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000055
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000056 AddStmtChoice(Kind a_kind = NotAlwaysAdd) : kind(a_kind) {}
Ted Kremenek5d2bb1b2010-03-02 21:43:54 +000057
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000058 bool alwaysAdd() const { return kind & AlwaysAdd; }
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000059
60 /// Return a copy of this object, except with the 'always-add' bit
61 /// set as specified.
62 AddStmtChoice withAlwaysAdd(bool alwaysAdd) const {
63 return AddStmtChoice(alwaysAdd ? Kind(kind | AlwaysAdd) :
64 Kind(kind & ~AlwaysAdd));
65 }
66
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000067private:
Zhanyong Wanb5d11c12010-11-24 03:28:53 +000068 Kind kind;
Ted Kremenek4cad5fc2009-12-16 03:18:58 +000069};
Mike Stump31feda52009-07-17 01:31:16 +000070
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000071/// LocalScope - Node in tree of local scopes created for C++ implicit
72/// destructor calls generation. It contains list of automatic variables
73/// declared in the scope and link to position in previous scope this scope
74/// began in.
75///
76/// The process of creating local scopes is as follows:
77/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
78/// - Before processing statements in scope (e.g. CompoundStmt) create
79/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
80/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000081/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000082/// at this VarDecl,
83/// - For every normal (without jump) end of scope add to CFGBlock destructors
84/// for objects in the current scope,
85/// - For every jump add to CFGBlock destructors for objects
86/// between CFGBuilder::ScopePos and local scope position saved for jump
87/// target. Thanks to C++ restrictions on goto jumps we can be sure that
88/// jump target position will be on the path to root from CFGBuilder::ScopePos
89/// (adding any variable that doesn't need constructor to be called to
90/// LocalScope can break this assumption),
91///
92class LocalScope {
93public:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +000094 typedef BumpVector<VarDecl*> AutomaticVarsTy;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000095
96 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderskie9862ce2010-09-30 22:42:32 +000097 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +000098 class const_iterator {
99 const LocalScope* Scope;
100
101 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
102 /// Invalid iterator (with null Scope) has VarIter equal to 0.
103 unsigned VarIter;
104
105 public:
106 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
107 /// Incrementing invalid iterator is allowed and will result in invalid
108 /// iterator.
109 const_iterator()
110 : Scope(NULL), VarIter(0) {}
111
112 /// Create valid iterator. In case when S.Prev is an invalid iterator and
113 /// I is equal to 0, this will create invalid iterator.
114 const_iterator(const LocalScope& S, unsigned I)
115 : Scope(&S), VarIter(I) {
116 // Iterator to "end" of scope is not allowed. Handle it by going up
117 // in scopes tree possibly up to invalid iterator in the root.
118 if (VarIter == 0 && Scope)
119 *this = Scope->Prev;
120 }
121
122 VarDecl* const* operator->() const {
123 assert (Scope && "Dereferencing invalid iterator is not allowed");
124 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
125 return &Scope->Vars[VarIter - 1];
126 }
127 VarDecl* operator*() const {
128 return *this->operator->();
129 }
130
131 const_iterator& operator++() {
132 if (!Scope)
133 return *this;
134
135 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
136 --VarIter;
137 if (VarIter == 0)
138 *this = Scope->Prev;
139 return *this;
140 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000141 const_iterator operator++(int) {
142 const_iterator P = *this;
143 ++*this;
144 return P;
145 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000146
147 bool operator==(const const_iterator& rhs) const {
148 return Scope == rhs.Scope && VarIter == rhs.VarIter;
149 }
150 bool operator!=(const const_iterator& rhs) const {
151 return !(*this == rhs);
152 }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000153
154 operator bool() const {
155 return *this != const_iterator();
156 }
157
158 int distance(const_iterator L);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000159 };
160
161 friend class const_iterator;
162
163private:
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000164 BumpVectorContext ctx;
165
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000166 /// Automatic variables in order of declaration.
167 AutomaticVarsTy Vars;
168 /// Iterator to variable in previous scope that was declared just before
169 /// begin of this scope.
170 const_iterator Prev;
171
172public:
173 /// Constructs empty scope linked to previous scope in specified place.
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000174 LocalScope(BumpVectorContext &ctx, const_iterator P)
175 : ctx(ctx), Vars(ctx, 4), Prev(P) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000176
177 /// Begin of scope in direction of CFG building (backwards).
178 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000179
180 void addVar(VarDecl* VD) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000181 Vars.push_back(VD, ctx);
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000182 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000183};
184
Marcin Swiderskie9862ce2010-09-30 22:42:32 +0000185/// distance - Calculates distance from this to L. L must be reachable from this
186/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
187/// number of scopes between this and L.
188int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
189 int D = 0;
190 const_iterator F = *this;
191 while (F.Scope != L.Scope) {
192 assert (F != const_iterator()
193 && "L iterator is not reachable from F iterator.");
194 D += F.VarIter;
195 F = F.Scope->Prev;
196 }
197 D += F.VarIter - L.VarIter;
198 return D;
199}
200
201/// BlockScopePosPair - Structure for specifying position in CFG during its
202/// build process. It consists of CFGBlock that specifies position in CFG graph
203/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000204struct BlockScopePosPair {
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000205 BlockScopePosPair() : block(0) {}
206 BlockScopePosPair(CFGBlock* b, LocalScope::const_iterator scopePos)
207 : block(b), scopePosition(scopePos) {}
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000208
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000209 CFGBlock *block;
210 LocalScope::const_iterator scopePosition;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000211};
212
Ted Kremenekbe9b33b2008-08-04 22:51:42 +0000213/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000214/// The builder is stateful: an instance of the builder should be used to only
215/// construct a single CFG.
216///
217/// Example usage:
218///
219/// CFGBuilder builder;
220/// CFG* cfg = builder.BuildAST(stmt1);
221///
Mike Stump31feda52009-07-17 01:31:16 +0000222/// CFG construction is done via a recursive walk of an AST. We actually parse
223/// the AST in reverse order so that the successor of a basic block is
224/// constructed prior to its predecessor. This allows us to nicely capture
225/// implicit fall-throughs without extra basic blocks.
Ted Kremenek1b8ac852007-08-21 22:06:14 +0000226///
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000227class CFGBuilder {
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000228 typedef BlockScopePosPair JumpTarget;
229 typedef BlockScopePosPair JumpSource;
230
Mike Stump0d76d072009-07-20 23:24:15 +0000231 ASTContext *Context;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000232 llvm::OwningPtr<CFG> cfg;
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000233
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000234 CFGBlock* Block;
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000235 CFGBlock* Succ;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000236 JumpTarget ContinueJumpTarget;
237 JumpTarget BreakJumpTarget;
Ted Kremenek879d8e12007-08-23 18:43:24 +0000238 CFGBlock* SwitchTerminatedBlock;
Ted Kremenek654c78f2008-02-13 22:05:39 +0000239 CFGBlock* DefaultCaseBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000240 CFGBlock* TryTerminatedBlock;
Mike Stump31feda52009-07-17 01:31:16 +0000241
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000242 // Current position in local scope.
243 LocalScope::const_iterator ScopePos;
244
245 // LabelMap records the mapping from Label expressions to their jump targets.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000246 typedef llvm::DenseMap<LabelDecl*, JumpTarget> LabelMapTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000247 LabelMapTy LabelMap;
Mike Stump31feda52009-07-17 01:31:16 +0000248
249 // A list of blocks that end with a "goto" that must be backpatched to their
250 // resolved targets upon completion of CFG construction.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000251 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek8a632182007-08-21 23:26:17 +0000252 BackpatchBlocksTy BackpatchBlocks;
Mike Stump31feda52009-07-17 01:31:16 +0000253
Ted Kremenekeda180e22007-08-28 19:26:49 +0000254 // A list of labels whose address has been taken (for indirect gotos).
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000255 typedef llvm::SmallPtrSet<LabelDecl*, 5> LabelSetTy;
Ted Kremenekeda180e22007-08-28 19:26:49 +0000256 LabelSetTy AddressTakenLabels;
Mike Stump31feda52009-07-17 01:31:16 +0000257
Zhongxing Xud38fb842010-09-16 03:28:18 +0000258 bool badCFG;
259 CFG::BuildOptions BuildOpts;
260
Mike Stump31feda52009-07-17 01:31:16 +0000261public:
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000262 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
263 Block(NULL), Succ(NULL),
Mike Stumpbbf5ba62010-01-19 02:20:09 +0000264 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xud38fb842010-09-16 03:28:18 +0000265 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump31feda52009-07-17 01:31:16 +0000266
Ted Kremenek9aae5132007-08-23 21:42:29 +0000267 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000268 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000269 CFG::BuildOptions BO);
Mike Stump31feda52009-07-17 01:31:16 +0000270
Ted Kremenek93668002009-07-17 22:18:43 +0000271private:
272 // Visitors to walk an AST and construct the CFG.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000273 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
274 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
275 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000276 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000277 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
John McCall5d413782010-12-06 08:20:24 +0000278 CFGBlock *VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000279 AddStmtChoice asc);
Ted Kremenekd2ba1f92010-04-11 17:01:59 +0000280 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
281 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000282 CFGBlock *VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
283 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000284 CFGBlock *VisitCXXConstructExpr(CXXConstructExpr *C, AddStmtChoice asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000285 CFGBlock *VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
286 AddStmtChoice asc);
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000287 CFGBlock *VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
288 AddStmtChoice asc);
Zhongxing Xu7e612172010-04-13 09:38:01 +0000289 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000290 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000291 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000292 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000293 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
John McCallc07a0c72011-02-17 10:25:35 +0000294 CFGBlock *VisitConditionalOperator(AbstractConditionalOperator *C,
295 AddStmtChoice asc);
Ted Kremenek21822592009-07-17 18:20:32 +0000296 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek93668002009-07-17 22:18:43 +0000297 CFGBlock *VisitDeclStmt(DeclStmt *DS);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000298 CFGBlock *VisitDeclSubExpr(DeclStmt* DS);
Ted Kremenek21822592009-07-17 18:20:32 +0000299 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
300 CFGBlock *VisitDoStmt(DoStmt *D);
301 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek93668002009-07-17 22:18:43 +0000302 CFGBlock *VisitGotoStmt(GotoStmt* G);
303 CFGBlock *VisitIfStmt(IfStmt *I);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000304 CFGBlock *VisitImplicitCastExpr(ImplicitCastExpr *E, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000305 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
306 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek5868ec62010-04-11 17:02:10 +0000307 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000308 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
309 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
310 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
311 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
312 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
313 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000314 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
315 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000316 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000317 CFGBlock *VisitUnaryOperator(UnaryOperator *U, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000318 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stump48871a22009-07-17 01:04:31 +0000319
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000320 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
321 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000322 CFGBlock *VisitChildren(Stmt* S);
Mike Stump48871a22009-07-17 01:04:31 +0000323
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000324 // Visitors to walk an AST and generate destructors of temporaries in
325 // full expression.
326 CFGBlock *VisitForTemporaryDtors(Stmt *E, bool BindToTemporary = false);
327 CFGBlock *VisitChildrenForTemporaryDtors(Stmt *E);
328 CFGBlock *VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E);
329 CFGBlock *VisitCXXBindTemporaryExprForTemporaryDtors(CXXBindTemporaryExpr *E,
330 bool BindToTemporary);
John McCallc07a0c72011-02-17 10:25:35 +0000331 CFGBlock *
332 VisitConditionalOperatorForTemporaryDtors(AbstractConditionalOperator *E,
333 bool BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000334
Ted Kremenek6065ef62008-04-28 18:00:46 +0000335 // NYS == Not Yet Supported
336 CFGBlock* NYS() {
Ted Kremenekb64d1832008-03-13 03:04:22 +0000337 badCFG = true;
338 return Block;
339 }
Mike Stump31feda52009-07-17 01:31:16 +0000340
Ted Kremenek93668002009-07-17 22:18:43 +0000341 void autoCreateBlock() { if (!Block) Block = createBlock(); }
342 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000343
Zhongxing Xuea9fcff2010-06-03 06:43:23 +0000344 CFGBlock *addStmt(Stmt *S) {
345 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000346 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000347 CFGBlock *addInitializer(CXXCtorInitializer *I);
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000348 void addAutomaticObjDtors(LocalScope::const_iterator B,
349 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski20b88732010-10-05 05:37:00 +0000350 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000351
Marcin Swiderski5e415732010-09-30 23:05:00 +0000352 // Local scopes creation.
353 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
354
Zhongxing Xu81714f22010-10-01 03:00:16 +0000355 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000356 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
357 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
358
359 void addLocalScopeAndDtors(Stmt* S);
360
361 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek8219b822010-12-16 07:46:53 +0000362 void appendStmt(CFGBlock *B, Stmt *S,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000363 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000364 B->appendStmt(S, cfg->getBumpVectorContext());
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000365 }
Alexis Hunt1d792652011-01-08 20:30:50 +0000366 void appendInitializer(CFGBlock *B, CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000367 B->appendInitializer(I, cfg->getBumpVectorContext());
368 }
Marcin Swiderski20b88732010-10-05 05:37:00 +0000369 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
370 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
371 }
372 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
373 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
374 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000375 void appendTemporaryDtor(CFGBlock *B, CXXBindTemporaryExpr *E) {
376 B->appendTemporaryDtor(E, cfg->getBumpVectorContext());
377 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000378
Marcin Swiderski321a7072010-09-30 22:54:37 +0000379 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
380 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
381 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
382 LocalScope::const_iterator E, Stmt* S);
383 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
384 LocalScope::const_iterator B, LocalScope::const_iterator E);
385
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000386 void addSuccessor(CFGBlock *B, CFGBlock *S) {
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000387 B->addSuccessor(S, cfg->getBumpVectorContext());
388 }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek963cc312009-07-24 06:55:42 +0000390 /// TryResult - a class representing a variant over the values
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000391 /// 'true', 'false', or 'unknown'. This is returned by tryEvaluateBool,
Ted Kremenek963cc312009-07-24 06:55:42 +0000392 /// and is used by the CFGBuilder to decide if a branch condition
393 /// can be decided up front during CFG construction.
Ted Kremenek30754282009-07-24 04:47:11 +0000394 class TryResult {
395 int X;
396 public:
397 TryResult(bool b) : X(b ? 1 : 0) {}
398 TryResult() : X(-1) {}
Mike Stump11289f42009-09-09 15:08:12 +0000399
Ted Kremenek30754282009-07-24 04:47:11 +0000400 bool isTrue() const { return X == 1; }
401 bool isFalse() const { return X == 0; }
402 bool isKnown() const { return X >= 0; }
403 void negate() {
404 assert(isKnown());
405 X ^= 0x1;
406 }
407 };
Mike Stump11289f42009-09-09 15:08:12 +0000408
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000409 /// tryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
Mike Stump773582d2009-07-23 23:25:26 +0000410 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000411 TryResult tryEvaluateBool(Expr *S) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000412 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000413 return TryResult();
414
Mike Stump773582d2009-07-23 23:25:26 +0000415 Expr::EvalResult Result;
Douglas Gregor4c952882009-08-24 21:39:56 +0000416 if (!S->isTypeDependent() && !S->isValueDependent() &&
Ted Kremenek1a241d12011-02-23 05:11:46 +0000417 S->Evaluate(Result, *Context)) {
418 if (Result.Val.isInt())
419 return Result.Val.getInt().getBoolValue();
420 if (Result.Val.isLValue()) {
421 Expr *e = Result.Val.getLValueBase();
422 const CharUnits &c = Result.Val.getLValueOffset();
423 if (!e && c.isZero())
424 return false;
425 }
426 }
Ted Kremenek30754282009-07-24 04:47:11 +0000427 return TryResult();
Mike Stump773582d2009-07-23 23:25:26 +0000428 }
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +0000429};
Mike Stump31feda52009-07-17 01:31:16 +0000430
Douglas Gregor4619e432008-12-05 23:32:09 +0000431// FIXME: Add support for dependent-sized array types in C++?
432// Does it even make sense to build a CFG for an uninstantiated template?
John McCall424cec92011-01-19 06:33:43 +0000433static const VariableArrayType *FindVA(const Type *t) {
434 while (const ArrayType *vt = dyn_cast<ArrayType>(t)) {
435 if (const VariableArrayType *vat = dyn_cast<VariableArrayType>(vt))
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000436 if (vat->getSizeExpr())
437 return vat;
Mike Stump31feda52009-07-17 01:31:16 +0000438
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000439 t = vt->getElementType().getTypePtr();
440 }
Mike Stump31feda52009-07-17 01:31:16 +0000441
Ted Kremenekd86d39c2008-09-26 22:58:57 +0000442 return 0;
443}
Mike Stump31feda52009-07-17 01:31:16 +0000444
445/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
446/// arbitrary statement. Examples include a single expression or a function
447/// body (compound statement). The ownership of the returned CFG is
448/// transferred to the caller. If CFG construction fails, this method returns
449/// NULL.
Mike Stump6bf1c082010-01-21 02:21:40 +0000450CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000451 CFG::BuildOptions BO) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000452
Mike Stump0d76d072009-07-20 23:24:15 +0000453 Context = C;
Ted Kremenek8aed4902009-10-20 23:46:25 +0000454 assert(cfg.get());
Ted Kremenek93668002009-07-17 22:18:43 +0000455 if (!Statement)
456 return NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000457
Ted Kremeneke97b1eb2010-09-14 23:41:16 +0000458 BuildOpts = BO;
Mike Stump31feda52009-07-17 01:31:16 +0000459
460 // Create an empty block that will serve as the exit block for the CFG. Since
461 // this is the first block added to the CFG, it will be implicitly registered
462 // as the exit block.
Ted Kremenek81e14852007-08-27 19:46:09 +0000463 Succ = createBlock();
Ted Kremenek289ae4f2009-10-12 20:55:07 +0000464 assert(Succ == &cfg->getExit());
Ted Kremenek81e14852007-08-27 19:46:09 +0000465 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump31feda52009-07-17 01:31:16 +0000466
Marcin Swiderski20b88732010-10-05 05:37:00 +0000467 if (BuildOpts.AddImplicitDtors)
468 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
469 addImplicitDtorsForDestructor(DD);
470
Ted Kremenek9aae5132007-08-23 21:42:29 +0000471 // Visit the statements and create the CFG.
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000472 CFGBlock *B = addStmt(Statement);
473
474 if (badCFG)
475 return NULL;
476
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000477 // For C++ constructor add initializers to CFG.
478 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
479 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
480 E = CD->init_rend(); I != E; ++I) {
481 B = addInitializer(*I);
482 if (badCFG)
483 return NULL;
484 }
485 }
486
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000487 if (B)
488 Succ = B;
Mike Stump6bf1c082010-01-21 02:21:40 +0000489
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000490 // Backpatch the gotos whose label -> block mappings we didn't know when we
491 // encountered them.
492 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
493 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump31feda52009-07-17 01:31:16 +0000494
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000495 CFGBlock* B = I->block;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000496 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
497 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +0000498
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000499 // If there is no target for the goto, then we are looking at an
500 // incomplete AST. Handle this by not registering a successor.
501 if (LI == LabelMap.end()) continue;
Ted Kremenek9aae5132007-08-23 21:42:29 +0000502
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +0000503 JumpTarget JT = LI->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000504 prependAutomaticObjDtorsWithTerminator(B, I->scopePosition,
505 JT.scopePosition);
506 addSuccessor(B, JT.block);
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000507 }
508
509 // Add successors to the Indirect Goto Dispatch block (if we have one).
510 if (CFGBlock* B = cfg->getIndirectGotoBlock())
511 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
512 E = AddressTakenLabels.end(); I != E; ++I ) {
513
514 // Lookup the target block.
515 LabelMapTy::iterator LI = LabelMap.find(*I);
516
517 // If there is no target block that contains label, then we are looking
518 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenek9aae5132007-08-23 21:42:29 +0000519 if (LI == LabelMap.end()) continue;
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000520
Ted Kremenekef81e9e2011-01-07 19:37:16 +0000521 addSuccessor(B, LI->second.block);
Ted Kremenekeda180e22007-08-28 19:26:49 +0000522 }
Mike Stump31feda52009-07-17 01:31:16 +0000523
Mike Stump31feda52009-07-17 01:31:16 +0000524 // Create an empty entry block that has no predecessors.
Ted Kremenek5c50fd12007-09-26 21:23:31 +0000525 cfg->setEntry(createBlock());
Mike Stump31feda52009-07-17 01:31:16 +0000526
Zhongxing Xub1e10aa2010-09-06 07:04:06 +0000527 return cfg.take();
Ted Kremenek9aae5132007-08-23 21:42:29 +0000528}
Mike Stump31feda52009-07-17 01:31:16 +0000529
Ted Kremenek9aae5132007-08-23 21:42:29 +0000530/// createBlock - Used to lazily create blocks that are connected
531/// to the current (global) succcessor.
Mike Stump31feda52009-07-17 01:31:16 +0000532CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek813dd672007-09-05 20:02:05 +0000533 CFGBlock* B = cfg->createBlock();
Ted Kremenek93668002009-07-17 22:18:43 +0000534 if (add_successor && Succ)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000535 addSuccessor(B, Succ);
Ted Kremenek9aae5132007-08-23 21:42:29 +0000536 return B;
537}
Mike Stump31feda52009-07-17 01:31:16 +0000538
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000539/// addInitializer - Add C++ base or member initializer element to CFG.
Alexis Hunt1d792652011-01-08 20:30:50 +0000540CFGBlock *CFGBuilder::addInitializer(CXXCtorInitializer *I) {
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000541 if (!BuildOpts.AddInitializers)
542 return Block;
543
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000544 bool IsReference = false;
545 bool HasTemporaries = false;
546
547 // Destructors of temporaries in initialization expression should be called
548 // after initialization finishes.
549 Expr *Init = I->getInit();
550 if (Init) {
Francois Pichetd583da02010-12-04 09:14:42 +0000551 if (FieldDecl *FD = I->getAnyMember())
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000552 IsReference = FD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +0000553 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000554
555 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
556 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +0000557 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000558 IsReference);
559 }
560 }
561
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000562 autoCreateBlock();
563 appendInitializer(Block, I);
564
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000565 if (Init) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000566 if (HasTemporaries) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000567 // For expression with temporaries go directly to subexpression to omit
568 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +0000569 return Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
570 }
571 return Visit(Init);
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000572 }
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +0000573
Marcin Swiderski87b1bb62010-10-04 03:38:22 +0000574 return Block;
575}
576
Marcin Swiderski5e415732010-09-30 23:05:00 +0000577/// addAutomaticObjDtors - Add to current block automatic objects destructors
578/// for objects in range of local scope positions. Use S as trigger statement
579/// for destructors.
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000580void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
581 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000582 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000583 return;
584
Marcin Swiderski5e415732010-09-30 23:05:00 +0000585 if (B == E)
Zhongxing Xu6d372f72010-10-01 03:22:39 +0000586 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000587
588 autoCreateBlock();
589 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000590}
591
Marcin Swiderski20b88732010-10-05 05:37:00 +0000592/// addImplicitDtorsForDestructor - Add implicit destructors generated for
593/// base and member objects in destructor.
594void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
595 assert (BuildOpts.AddImplicitDtors
596 && "Can be called only when dtors should be added");
597 const CXXRecordDecl *RD = DD->getParent();
598
599 // At the end destroy virtual base objects.
600 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
601 VE = RD->vbases_end(); VI != VE; ++VI) {
602 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
603 if (!CD->hasTrivialDestructor()) {
604 autoCreateBlock();
605 appendBaseDtor(Block, VI);
606 }
607 }
608
609 // Before virtual bases destroy direct base objects.
610 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
611 BE = RD->bases_end(); BI != BE; ++BI) {
612 if (!BI->isVirtual()) {
613 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
614 if (!CD->hasTrivialDestructor()) {
615 autoCreateBlock();
616 appendBaseDtor(Block, BI);
617 }
618 }
619 }
620
621 // First destroy member objects.
622 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
623 FE = RD->field_end(); FI != FE; ++FI) {
Marcin Swiderski01769902010-10-25 07:05:54 +0000624 // Check for constant size array. Set type to array element type.
625 QualType QT = FI->getType();
626 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
627 if (AT->getSize() == 0)
628 continue;
629 QT = AT->getElementType();
630 }
631
632 if (const CXXRecordDecl *CD = QT->getAsCXXRecordDecl())
Marcin Swiderski20b88732010-10-05 05:37:00 +0000633 if (!CD->hasTrivialDestructor()) {
634 autoCreateBlock();
635 appendMemberDtor(Block, *FI);
636 }
637 }
638}
639
Marcin Swiderski5e415732010-09-30 23:05:00 +0000640/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
641/// way return valid LocalScope object.
642LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
643 if (!Scope) {
Ted Kremenekc7bfdcd2011-02-15 02:47:45 +0000644 llvm::BumpPtrAllocator &alloc = cfg->getAllocator();
645 Scope = alloc.Allocate<LocalScope>();
646 BumpVectorContext ctx(alloc);
647 new (Scope) LocalScope(ctx, ScopePos);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000648 }
649 return Scope;
650}
651
652/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu81714f22010-10-01 03:00:16 +0000653/// that should create implicit scope (e.g. if/else substatements).
654void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000655 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu81714f22010-10-01 03:00:16 +0000656 return;
657
658 LocalScope *Scope = 0;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000659
660 // For compound statement we will be creating explicit scope.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000661 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(S)) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000662 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
663 ; BI != BE; ++BI) {
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000664 Stmt *SI = *BI;
665 if (LabelStmt *LS = dyn_cast<LabelStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000666 SI = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000667 if (DeclStmt *DS = dyn_cast<DeclStmt>(SI))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000668 Scope = addLocalScopeForDeclStmt(DS, Scope);
669 }
Zhongxing Xu81714f22010-10-01 03:00:16 +0000670 return;
Marcin Swiderski5e415732010-09-30 23:05:00 +0000671 }
672
673 // For any other statement scope will be implicit and as such will be
674 // interesting only for DeclStmt.
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000675 if (LabelStmt *LS = dyn_cast<LabelStmt>(S))
Marcin Swiderski5e415732010-09-30 23:05:00 +0000676 S = LS->getSubStmt();
Chris Lattnerc8e630e2011-02-17 07:39:24 +0000677 if (DeclStmt *DS = dyn_cast<DeclStmt>(S))
Zhongxing Xu307701e2010-10-01 03:09:09 +0000678 addLocalScopeForDeclStmt(DS);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000679}
680
681/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
682/// reuse Scope if not NULL.
683LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000684 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000685 if (!BuildOpts.AddImplicitDtors)
686 return Scope;
687
688 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
689 ; DI != DE; ++DI) {
690 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
691 Scope = addLocalScopeForVarDecl(VD, Scope);
692 }
693 return Scope;
694}
695
696/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
697/// create add scope for automatic objects and temporary objects bound to
698/// const reference. Will reuse Scope if not NULL.
699LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xu307701e2010-10-01 03:09:09 +0000700 LocalScope* Scope) {
Marcin Swiderski5e415732010-09-30 23:05:00 +0000701 if (!BuildOpts.AddImplicitDtors)
702 return Scope;
703
704 // Check if variable is local.
705 switch (VD->getStorageClass()) {
706 case SC_None:
707 case SC_Auto:
708 case SC_Register:
709 break;
710 default: return Scope;
711 }
712
713 // Check for const references bound to temporary. Set type to pointee.
714 QualType QT = VD->getType();
715 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
716 QT = RT->getPointeeType();
717 if (!QT.isConstQualified())
718 return Scope;
719 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
720 return Scope;
721 }
722
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000723 // Check for constant size array. Set type to array element type.
724 if (const ConstantArrayType *AT = Context->getAsConstantArrayType(QT)) {
725 if (AT->getSize() == 0)
726 return Scope;
727 QT = AT->getElementType();
728 }
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000729
Marcin Swiderski52e4bc12010-10-25 07:00:40 +0000730 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu614e17d2010-10-05 08:38:06 +0000731 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
732 if (!CD->hasTrivialDestructor()) {
733 // Add the variable to scope
734 Scope = createOrReuseLocalScope(Scope);
735 Scope->addVar(VD);
736 ScopePos = Scope->begin();
737 }
Marcin Swiderski5e415732010-09-30 23:05:00 +0000738 return Scope;
739}
740
741/// addLocalScopeAndDtors - For given statement add local scope for it and
742/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
743void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
744 if (!BuildOpts.AddImplicitDtors)
745 return;
746
747 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu81714f22010-10-01 03:00:16 +0000748 addLocalScopeForStmt(S);
Marcin Swiderski5e415732010-09-30 23:05:00 +0000749 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
750}
751
Marcin Swiderski321a7072010-09-30 22:54:37 +0000752/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
753/// automatic storage duration to CFGBlock's elements vector. Insertion will be
754/// performed in place specified with iterator.
755void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
756 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
757 BumpVectorContext& C = cfg->getBumpVectorContext();
758 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
759 while (B != E)
760 I = Blk->insertAutomaticObjDtor(I, *B++, S);
761}
762
763/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
764/// automatic storage duration to CFGBlock's elements vector. Elements will be
765/// appended to physical end of the vector which happens to be logical
766/// beginning.
767void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
768 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
769 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
770}
771
772/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
773/// variables with automatic storage duration to CFGBlock's elements vector.
774/// Elements will be prepended to physical beginning of the vector which
775/// happens to be logical end. Use blocks terminator as statement that specifies
776/// destructors call site.
777void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
778 LocalScope::const_iterator B, LocalScope::const_iterator E) {
779 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
780}
781
Ted Kremenek93668002009-07-17 22:18:43 +0000782/// Visit - Walk the subtree of a statement and add extra
Mike Stump31feda52009-07-17 01:31:16 +0000783/// blocks for ternary operators, &&, and ||. We also process "," and
784/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000785CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000786tryAgain:
Ted Kremenekbc1416d2010-04-30 22:25:53 +0000787 if (!S) {
788 badCFG = true;
789 return 0;
790 }
Ted Kremenek93668002009-07-17 22:18:43 +0000791 switch (S->getStmtClass()) {
792 default:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000793 return VisitStmt(S, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000794
795 case Stmt::AddrLabelExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000796 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000797
John McCallc07a0c72011-02-17 10:25:35 +0000798 case Stmt::BinaryConditionalOperatorClass:
799 return VisitConditionalOperator(cast<BinaryConditionalOperator>(S), asc);
800
Ted Kremenek93668002009-07-17 22:18:43 +0000801 case Stmt::BinaryOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000802 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000803
Ted Kremenek93668002009-07-17 22:18:43 +0000804 case Stmt::BlockExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000805 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000806
Ted Kremenek93668002009-07-17 22:18:43 +0000807 case Stmt::BreakStmtClass:
808 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000809
Ted Kremenek93668002009-07-17 22:18:43 +0000810 case Stmt::CallExprClass:
Ted Kremenek128d04d2010-08-31 18:47:34 +0000811 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000812 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000813
Ted Kremenek93668002009-07-17 22:18:43 +0000814 case Stmt::CaseStmtClass:
815 return VisitCaseStmt(cast<CaseStmt>(S));
816
817 case Stmt::ChooseExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000818 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000819
Ted Kremenek93668002009-07-17 22:18:43 +0000820 case Stmt::CompoundStmtClass:
821 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000822
Ted Kremenek93668002009-07-17 22:18:43 +0000823 case Stmt::ConditionalOperatorClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000824 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000825
Ted Kremenek93668002009-07-17 22:18:43 +0000826 case Stmt::ContinueStmtClass:
827 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000828
Ted Kremenekb27378c2010-01-19 20:40:33 +0000829 case Stmt::CXXCatchStmtClass:
830 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
831
John McCall5d413782010-12-06 08:20:24 +0000832 case Stmt::ExprWithCleanupsClass:
833 return VisitExprWithCleanups(cast<ExprWithCleanups>(S), asc);
Ted Kremenek82bfc862010-08-28 00:19:02 +0000834
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000835 case Stmt::CXXBindTemporaryExprClass:
836 return VisitCXXBindTemporaryExpr(cast<CXXBindTemporaryExpr>(S), asc);
837
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000838 case Stmt::CXXConstructExprClass:
839 return VisitCXXConstructExpr(cast<CXXConstructExpr>(S), asc);
840
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000841 case Stmt::CXXFunctionalCastExprClass:
842 return VisitCXXFunctionalCastExpr(cast<CXXFunctionalCastExpr>(S), asc);
843
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +0000844 case Stmt::CXXTemporaryObjectExprClass:
845 return VisitCXXTemporaryObjectExpr(cast<CXXTemporaryObjectExpr>(S), asc);
846
Zhongxing Xu7e612172010-04-13 09:38:01 +0000847 case Stmt::CXXMemberCallExprClass:
848 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
849
Ted Kremenekb27378c2010-01-19 20:40:33 +0000850 case Stmt::CXXThrowExprClass:
851 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000852
Ted Kremenekb27378c2010-01-19 20:40:33 +0000853 case Stmt::CXXTryStmtClass:
854 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000855
Ted Kremenek93668002009-07-17 22:18:43 +0000856 case Stmt::DeclStmtClass:
857 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000858
Ted Kremenek93668002009-07-17 22:18:43 +0000859 case Stmt::DefaultStmtClass:
860 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000861
Ted Kremenek93668002009-07-17 22:18:43 +0000862 case Stmt::DoStmtClass:
863 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000864
Ted Kremenek93668002009-07-17 22:18:43 +0000865 case Stmt::ForStmtClass:
866 return VisitForStmt(cast<ForStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000867
Ted Kremenek93668002009-07-17 22:18:43 +0000868 case Stmt::GotoStmtClass:
869 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000870
Ted Kremenek93668002009-07-17 22:18:43 +0000871 case Stmt::IfStmtClass:
872 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000873
Ted Kremenek8219b822010-12-16 07:46:53 +0000874 case Stmt::ImplicitCastExprClass:
875 return VisitImplicitCastExpr(cast<ImplicitCastExpr>(S), asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +0000876
Ted Kremenek93668002009-07-17 22:18:43 +0000877 case Stmt::IndirectGotoStmtClass:
878 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000879
Ted Kremenek93668002009-07-17 22:18:43 +0000880 case Stmt::LabelStmtClass:
881 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000882
Ted Kremenek5868ec62010-04-11 17:02:10 +0000883 case Stmt::MemberExprClass:
884 return VisitMemberExpr(cast<MemberExpr>(S), asc);
885
Ted Kremenek93668002009-07-17 22:18:43 +0000886 case Stmt::ObjCAtCatchStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000887 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
888
Ted Kremenek93668002009-07-17 22:18:43 +0000889 case Stmt::ObjCAtSynchronizedStmtClass:
890 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000891
Ted Kremenek93668002009-07-17 22:18:43 +0000892 case Stmt::ObjCAtThrowStmtClass:
893 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000894
Ted Kremenek93668002009-07-17 22:18:43 +0000895 case Stmt::ObjCAtTryStmtClass:
896 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenek93668002009-07-17 22:18:43 +0000898 case Stmt::ObjCForCollectionStmtClass:
899 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000900
Ted Kremenek93668002009-07-17 22:18:43 +0000901 case Stmt::ParenExprClass:
902 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +0000903 goto tryAgain;
904
Ted Kremenek93668002009-07-17 22:18:43 +0000905 case Stmt::NullStmtClass:
906 return Block;
Mike Stump11289f42009-09-09 15:08:12 +0000907
Ted Kremenek93668002009-07-17 22:18:43 +0000908 case Stmt::ReturnStmtClass:
909 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000910
Ted Kremenek93668002009-07-17 22:18:43 +0000911 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000912 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000913
Ted Kremenek93668002009-07-17 22:18:43 +0000914 case Stmt::StmtExprClass:
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000915 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump11289f42009-09-09 15:08:12 +0000916
Ted Kremenek93668002009-07-17 22:18:43 +0000917 case Stmt::SwitchStmtClass:
918 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump11289f42009-09-09 15:08:12 +0000919
Zhanyong Wan6dace612010-11-22 08:45:56 +0000920 case Stmt::UnaryOperatorClass:
921 return VisitUnaryOperator(cast<UnaryOperator>(S), asc);
922
Ted Kremenek93668002009-07-17 22:18:43 +0000923 case Stmt::WhileStmtClass:
924 return VisitWhileStmt(cast<WhileStmt>(S));
925 }
926}
Mike Stump11289f42009-09-09 15:08:12 +0000927
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000928CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
929 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000930 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000931 appendStmt(Block, S, asc);
Mike Stump31feda52009-07-17 01:31:16 +0000932 }
Mike Stump11289f42009-09-09 15:08:12 +0000933
Ted Kremenek93668002009-07-17 22:18:43 +0000934 return VisitChildren(S);
Ted Kremenek9e248872007-08-27 21:27:44 +0000935}
Mike Stump31feda52009-07-17 01:31:16 +0000936
Ted Kremenek93668002009-07-17 22:18:43 +0000937/// VisitChildren - Visit the children of a Stmt.
938CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
Ted Kremenek828f6312011-02-21 22:11:26 +0000939 CFGBlock *lastBlock = Block;
940 for (Stmt::child_range I = Terminator->children(); I; ++I)
941 if (Stmt *child = *I)
942 if (CFGBlock *b = Visit(child))
943 lastBlock = b;
944
945 return lastBlock;
Ted Kremenek9e248872007-08-27 21:27:44 +0000946}
Mike Stump11289f42009-09-09 15:08:12 +0000947
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000948CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
949 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000950 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9e248872007-08-27 21:27:44 +0000951
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000952 if (asc.alwaysAdd()) {
Ted Kremenek93668002009-07-17 22:18:43 +0000953 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000954 appendStmt(Block, A, asc);
Ted Kremenek93668002009-07-17 22:18:43 +0000955 }
Ted Kremenek81e14852007-08-27 19:46:09 +0000956
Ted Kremenek9aae5132007-08-23 21:42:29 +0000957 return Block;
958}
Mike Stump11289f42009-09-09 15:08:12 +0000959
Zhanyong Wan6dace612010-11-22 08:45:56 +0000960CFGBlock *CFGBuilder::VisitUnaryOperator(UnaryOperator *U,
Ted Kremenek8219b822010-12-16 07:46:53 +0000961 AddStmtChoice asc) {
Zhanyong Wan6dace612010-11-22 08:45:56 +0000962 if (asc.alwaysAdd()) {
963 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000964 appendStmt(Block, U, asc);
Zhanyong Wan6dace612010-11-22 08:45:56 +0000965 }
966
Ted Kremenek8219b822010-12-16 07:46:53 +0000967 return Visit(U->getSubExpr(), AddStmtChoice());
Zhanyong Wan6dace612010-11-22 08:45:56 +0000968}
969
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000970CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
971 AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +0000972 if (B->isLogicalOp()) { // && or ||
Ted Kremenek93668002009-07-17 22:18:43 +0000973 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +0000974 appendStmt(ConfluenceBlock, B, asc);
Mike Stump11289f42009-09-09 15:08:12 +0000975
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000976 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +0000977 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000978
Ted Kremenek93668002009-07-17 22:18:43 +0000979 // create the block evaluating the LHS
980 CFGBlock* LHSBlock = createBlock(false);
981 LHSBlock->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +0000982
Ted Kremenek93668002009-07-17 22:18:43 +0000983 // create the block evaluating the RHS
984 Succ = ConfluenceBlock;
985 Block = NULL;
986 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek989da5e2010-04-29 01:10:26 +0000987
988 if (RHSBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +0000989 if (badCFG)
Ted Kremenek989da5e2010-04-29 01:10:26 +0000990 return 0;
Zhanyong Wan59f09c72010-11-22 19:32:14 +0000991 } else {
Ted Kremenek989da5e2010-04-29 01:10:26 +0000992 // Create an empty block for cases where the RHS doesn't require
993 // any explicit statements in the CFG.
994 RHSBlock = createBlock();
995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Mike Stump773582d2009-07-23 23:25:26 +0000997 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +0000998 TryResult KnownVal = tryEvaluateBool(B->getLHS());
John McCalle3027922010-08-25 11:45:40 +0000999 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek30754282009-07-24 04:47:11 +00001000 KnownVal.negate();
Mike Stump773582d2009-07-23 23:25:26 +00001001
Ted Kremenek93668002009-07-17 22:18:43 +00001002 // Now link the LHSBlock with RHSBlock.
John McCalle3027922010-08-25 11:45:40 +00001003 if (B->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001004 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
1005 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +00001006 } else {
John McCalle3027922010-08-25 11:45:40 +00001007 assert(B->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001008 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
1009 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001010 }
Mike Stump11289f42009-09-09 15:08:12 +00001011
Ted Kremenek93668002009-07-17 22:18:43 +00001012 // Generate the blocks for evaluating the LHS.
1013 Block = LHSBlock;
1014 return addStmt(B->getLHS());
Mike Stump11289f42009-09-09 15:08:12 +00001015 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001016
1017 if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenekfe9b7682009-07-17 22:57:50 +00001018 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001019 appendStmt(Block, B, asc);
Ted Kremenek93668002009-07-17 22:18:43 +00001020 addStmt(B->getRHS());
1021 return addStmt(B->getLHS());
1022 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001023
1024 if (B->isAssignmentOp()) {
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001025 if (asc.alwaysAdd()) {
1026 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001027 appendStmt(Block, B, asc);
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001028 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001029 Visit(B->getLHS());
Marcin Swiderski77232492010-10-24 08:21:40 +00001030 return Visit(B->getRHS());
Zhongxing Xu41cdf582010-06-03 06:23:18 +00001031 }
Mike Stump11289f42009-09-09 15:08:12 +00001032
Marcin Swiderski77232492010-10-24 08:21:40 +00001033 if (asc.alwaysAdd()) {
1034 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001035 appendStmt(Block, B, asc);
Marcin Swiderski77232492010-10-24 08:21:40 +00001036 }
1037
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001038 CFGBlock *RBlock = Visit(B->getRHS());
1039 CFGBlock *LBlock = Visit(B->getLHS());
1040 // If visiting RHS causes us to finish 'Block', e.g. the RHS is a StmtExpr
1041 // containing a DoStmt, and the LHS doesn't create a new block, then we should
1042 // return RBlock. Otherwise we'll incorrectly return NULL.
1043 return (LBlock ? LBlock : RBlock);
Ted Kremenek93668002009-07-17 22:18:43 +00001044}
1045
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001046CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
1047 if (asc.alwaysAdd()) {
Ted Kremenek470bfa42009-11-25 01:34:30 +00001048 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001049 appendStmt(Block, E, asc);
Ted Kremenek470bfa42009-11-25 01:34:30 +00001050 }
1051 return Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001052}
1053
Ted Kremenek93668002009-07-17 22:18:43 +00001054CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
1055 // "break" is a control-flow statement. Thus we stop processing the current
1056 // block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001057 if (badCFG)
1058 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001059
Ted Kremenek93668002009-07-17 22:18:43 +00001060 // Now create a new block that ends with the break statement.
1061 Block = createBlock(false);
1062 Block->setTerminator(B);
Mike Stump11289f42009-09-09 15:08:12 +00001063
Ted Kremenek93668002009-07-17 22:18:43 +00001064 // If there is no target for the break, then we are looking at an incomplete
1065 // AST. This means that the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001066 if (BreakJumpTarget.block) {
1067 addAutomaticObjDtors(ScopePos, BreakJumpTarget.scopePosition, B);
1068 addSuccessor(Block, BreakJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001069 } else
Ted Kremenek93668002009-07-17 22:18:43 +00001070 badCFG = true;
Mike Stump11289f42009-09-09 15:08:12 +00001071
1072
Ted Kremenek9aae5132007-08-23 21:42:29 +00001073 return Block;
1074}
Mike Stump11289f42009-09-09 15:08:12 +00001075
Mike Stump04c68512010-01-21 15:20:48 +00001076static bool CanThrow(Expr *E) {
1077 QualType Ty = E->getType();
1078 if (Ty->isFunctionPointerType())
1079 Ty = Ty->getAs<PointerType>()->getPointeeType();
1080 else if (Ty->isBlockPointerType())
1081 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001082
Mike Stump04c68512010-01-21 15:20:48 +00001083 const FunctionType *FT = Ty->getAs<FunctionType>();
1084 if (FT) {
1085 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
1086 if (Proto->hasEmptyExceptionSpec())
1087 return false;
1088 }
1089 return true;
1090}
1091
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001092CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek93668002009-07-17 22:18:43 +00001093 // If this is a call to a no-return function, this stops the block here.
Mike Stump8c5d7992009-07-25 21:26:53 +00001094 bool NoReturn = false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001095 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001096 NoReturn = true;
Ted Kremenek93668002009-07-17 22:18:43 +00001097 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001098
Mike Stump04c68512010-01-21 15:20:48 +00001099 bool AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001100
1101 // Languages without exceptions are assumed to not throw.
Anders Carlsson08ce5ed2011-02-20 00:20:27 +00001102 if (Context->getLangOptions().areExceptionsEnabled()) {
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00001103 if (BuildOpts.AddEHEdges)
Mike Stump04c68512010-01-21 15:20:48 +00001104 AddEHEdge = true;
Mike Stump92244b02010-01-19 22:00:14 +00001105 }
1106
1107 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump8c5d7992009-07-25 21:26:53 +00001108 if (FD->hasAttr<NoReturnAttr>())
1109 NoReturn = true;
Mike Stump92244b02010-01-19 22:00:14 +00001110 if (FD->hasAttr<NoThrowAttr>())
Mike Stump04c68512010-01-21 15:20:48 +00001111 AddEHEdge = false;
Mike Stump92244b02010-01-19 22:00:14 +00001112 }
Mike Stump8c5d7992009-07-25 21:26:53 +00001113
Mike Stump04c68512010-01-21 15:20:48 +00001114 if (!CanThrow(C->getCallee()))
1115 AddEHEdge = false;
1116
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001117 if (!NoReturn && !AddEHEdge)
1118 return VisitStmt(C, asc.withAlwaysAdd(true));
Mike Stump11289f42009-09-09 15:08:12 +00001119
Mike Stump92244b02010-01-19 22:00:14 +00001120 if (Block) {
1121 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001122 if (badCFG)
Mike Stump92244b02010-01-19 22:00:14 +00001123 return 0;
1124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Mike Stump92244b02010-01-19 22:00:14 +00001126 Block = createBlock(!NoReturn);
Ted Kremenek8219b822010-12-16 07:46:53 +00001127 appendStmt(Block, C, asc);
Mike Stump8c5d7992009-07-25 21:26:53 +00001128
Mike Stump92244b02010-01-19 22:00:14 +00001129 if (NoReturn) {
1130 // Wire this to the exit block directly.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001131 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001132 }
Mike Stump04c68512010-01-21 15:20:48 +00001133 if (AddEHEdge) {
Mike Stump92244b02010-01-19 22:00:14 +00001134 // Add exceptional edges.
1135 if (TryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001136 addSuccessor(Block, TryTerminatedBlock);
Mike Stump92244b02010-01-19 22:00:14 +00001137 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001138 addSuccessor(Block, &cfg->getExit());
Mike Stump92244b02010-01-19 22:00:14 +00001139 }
Mike Stump11289f42009-09-09 15:08:12 +00001140
Mike Stump8c5d7992009-07-25 21:26:53 +00001141 return VisitChildren(C);
Ted Kremenek93668002009-07-17 22:18:43 +00001142}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001143
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001144CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1145 AddStmtChoice asc) {
Ted Kremenek21822592009-07-17 18:20:32 +00001146 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001147 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001148 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001149 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001150
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001151 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek21822592009-07-17 18:20:32 +00001152 Succ = ConfluenceBlock;
1153 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001154 CFGBlock* LHSBlock = Visit(C->getLHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001155 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001156 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001157
Ted Kremenek21822592009-07-17 18:20:32 +00001158 Succ = ConfluenceBlock;
1159 Block = NULL;
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001160 CFGBlock* RHSBlock = Visit(C->getRHS(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001161 if (badCFG)
Ted Kremenek21822592009-07-17 18:20:32 +00001162 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001163
Ted Kremenek21822592009-07-17 18:20:32 +00001164 Block = createBlock(false);
Mike Stump773582d2009-07-23 23:25:26 +00001165 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001166 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
1167 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1168 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek21822592009-07-17 18:20:32 +00001169 Block->setTerminator(C);
Mike Stump11289f42009-09-09 15:08:12 +00001170 return addStmt(C->getCond());
Ted Kremenek21822592009-07-17 18:20:32 +00001171}
Mike Stump11289f42009-09-09 15:08:12 +00001172
1173
1174CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001175 addLocalScopeAndDtors(C);
Mike Stump11289f42009-09-09 15:08:12 +00001176 CFGBlock* LastBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001177
1178 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1179 I != E; ++I ) {
Ted Kremenek4f2ab5a2010-08-17 21:00:06 +00001180 // If we hit a segment of code just containing ';' (NullStmts), we can
1181 // get a null block back. In such cases, just use the LastBlock
1182 if (CFGBlock *newBlock = addStmt(*I))
1183 LastBlock = newBlock;
Mike Stump11289f42009-09-09 15:08:12 +00001184
Ted Kremenekce499c22009-08-27 23:16:26 +00001185 if (badCFG)
1186 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001187 }
Mike Stump92244b02010-01-19 22:00:14 +00001188
Ted Kremenek93668002009-07-17 22:18:43 +00001189 return LastBlock;
1190}
Mike Stump11289f42009-09-09 15:08:12 +00001191
John McCallc07a0c72011-02-17 10:25:35 +00001192CFGBlock *CFGBuilder::VisitConditionalOperator(AbstractConditionalOperator *C,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001193 AddStmtChoice asc) {
John McCallc07a0c72011-02-17 10:25:35 +00001194 const BinaryConditionalOperator *BCO = dyn_cast<BinaryConditionalOperator>(C);
1195 const OpaqueValueExpr *opaqueValue = (BCO ? BCO->getOpaqueValue() : NULL);
1196
Ted Kremenek51d40b02009-07-17 18:15:54 +00001197 // Create the confluence block that will "merge" the results of the ternary
1198 // expression.
1199 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001200 appendStmt(ConfluenceBlock, C, asc);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001201 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001202 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00001204 AddStmtChoice alwaysAdd = asc.withAlwaysAdd(true);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001205
Ted Kremenek51d40b02009-07-17 18:15:54 +00001206 // Create a block for the LHS expression if there is an LHS expression. A
1207 // GCC extension allows LHS to be NULL, causing the condition to be the
1208 // value that is returned instead.
1209 // e.g: x ?: y is shorthand for: x ? x : y;
1210 Succ = ConfluenceBlock;
1211 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00001212 CFGBlock* LHSBlock = 0;
1213 const Expr *trueExpr = C->getTrueExpr();
1214 if (trueExpr != opaqueValue) {
1215 LHSBlock = Visit(C->getTrueExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001216 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001217 return 0;
1218 Block = NULL;
1219 }
Mike Stump11289f42009-09-09 15:08:12 +00001220
Ted Kremenek51d40b02009-07-17 18:15:54 +00001221 // Create the block for the RHS expression.
1222 Succ = ConfluenceBlock;
John McCallc07a0c72011-02-17 10:25:35 +00001223 CFGBlock* RHSBlock = Visit(C->getFalseExpr(), alwaysAdd);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001224 if (badCFG)
Ted Kremenek51d40b02009-07-17 18:15:54 +00001225 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001226
Ted Kremenek51d40b02009-07-17 18:15:54 +00001227 // Create the block that will contain the condition.
1228 Block = createBlock(false);
Mike Stump11289f42009-09-09 15:08:12 +00001229
Mike Stump773582d2009-07-23 23:25:26 +00001230 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001231 const TryResult& KnownVal = tryEvaluateBool(C->getCond());
John McCallc07a0c72011-02-17 10:25:35 +00001232 if (LHSBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001233 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001234 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek51d40b02009-07-17 18:15:54 +00001235 Block->setTerminator(C);
John McCallc07a0c72011-02-17 10:25:35 +00001236 Expr *condExpr = C->getCond();
John McCall68cc3352011-02-19 03:13:26 +00001237
1238 CFGBlock *result = 0;
1239
1240 // Run the condition expression if it's not trivially expressed in
1241 // terms of the opaque value (or if there is no opaque value).
John McCallc07a0c72011-02-17 10:25:35 +00001242 if (condExpr != opaqueValue) result = addStmt(condExpr);
John McCall68cc3352011-02-19 03:13:26 +00001243
1244 // Before that, run the common subexpression if there was one.
1245 // At least one of this or the above will be run.
1246 if (opaqueValue) result = addStmt(BCO->getCommon());
1247
John McCallc07a0c72011-02-17 10:25:35 +00001248 return result;
Ted Kremenek51d40b02009-07-17 18:15:54 +00001249}
1250
Ted Kremenek93668002009-07-17 22:18:43 +00001251CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001252 if (DS->isSingleDecl())
1253 return VisitDeclSubExpr(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001254
Ted Kremenek93668002009-07-17 22:18:43 +00001255 CFGBlock *B = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001256
Ted Kremenek93668002009-07-17 22:18:43 +00001257 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1258 typedef llvm::SmallVector<Decl*,10> BufTy;
1259 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump11289f42009-09-09 15:08:12 +00001260
Ted Kremenek93668002009-07-17 22:18:43 +00001261 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1262 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1263 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1264 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump11289f42009-09-09 15:08:12 +00001265
Ted Kremenek93668002009-07-17 22:18:43 +00001266 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1267 // automatically freed with the CFG.
1268 DeclGroupRef DG(*I);
1269 Decl *D = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001270 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek93668002009-07-17 22:18:43 +00001271 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump11289f42009-09-09 15:08:12 +00001272
Ted Kremenek93668002009-07-17 22:18:43 +00001273 // Append the fake DeclStmt to block.
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001274 B = VisitDeclSubExpr(DSNew);
Ted Kremenek93668002009-07-17 22:18:43 +00001275 }
Mike Stump11289f42009-09-09 15:08:12 +00001276
1277 return B;
Ted Kremenek93668002009-07-17 22:18:43 +00001278}
Mike Stump11289f42009-09-09 15:08:12 +00001279
Ted Kremenek93668002009-07-17 22:18:43 +00001280/// VisitDeclSubExpr - Utility method to add block-level expressions for
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001281/// DeclStmts and initializers in them.
1282CFGBlock *CFGBuilder::VisitDeclSubExpr(DeclStmt* DS) {
1283 assert(DS->isSingleDecl() && "Can handle single declarations only.");
Ted Kremenekf6998822008-02-26 00:22:58 +00001284
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001285 VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001286
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001287 if (!VD) {
1288 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001289 appendStmt(Block, DS);
Ted Kremenek93668002009-07-17 22:18:43 +00001290 return Block;
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001293 bool IsReference = false;
1294 bool HasTemporaries = false;
1295
1296 // Destructors of temporaries in initialization expression should be called
1297 // after initialization finishes.
Ted Kremenek93668002009-07-17 22:18:43 +00001298 Expr *Init = VD->getInit();
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001299 if (Init) {
1300 IsReference = VD->getType()->isReferenceType();
John McCall5d413782010-12-06 08:20:24 +00001301 HasTemporaries = isa<ExprWithCleanups>(Init);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001302
1303 if (BuildOpts.AddImplicitDtors && HasTemporaries) {
1304 // Generate destructors for temporaries in initialization expression.
John McCall5d413782010-12-06 08:20:24 +00001305 VisitForTemporaryDtors(cast<ExprWithCleanups>(Init)->getSubExpr(),
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001306 IsReference);
1307 }
1308 }
1309
1310 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001311 appendStmt(Block, DS);
Mike Stump11289f42009-09-09 15:08:12 +00001312
Ted Kremenek93668002009-07-17 22:18:43 +00001313 if (Init) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001314 if (HasTemporaries)
1315 // For expression with temporaries go directly to subexpression to omit
1316 // generating destructors for the second time.
Ted Kremenek8219b822010-12-16 07:46:53 +00001317 Visit(cast<ExprWithCleanups>(Init)->getSubExpr());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00001318 else
Ted Kremenek8219b822010-12-16 07:46:53 +00001319 Visit(Init);
Ted Kremenek93668002009-07-17 22:18:43 +00001320 }
Mike Stump11289f42009-09-09 15:08:12 +00001321
Ted Kremenek93668002009-07-17 22:18:43 +00001322 // If the type of VD is a VLA, then we must process its size expressions.
John McCall424cec92011-01-19 06:33:43 +00001323 for (const VariableArrayType* VA = FindVA(VD->getType().getTypePtr());
1324 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
Ted Kremenek93668002009-07-17 22:18:43 +00001325 Block = addStmt(VA->getSizeExpr());
Mike Stump11289f42009-09-09 15:08:12 +00001326
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001327 // Remove variable from local scope.
1328 if (ScopePos && VD == *ScopePos)
1329 ++ScopePos;
1330
Ted Kremenek93668002009-07-17 22:18:43 +00001331 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001332}
1333
1334CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00001335 // We may see an if statement in the middle of a basic block, or it may be the
1336 // first statement we are processing. In either case, we create a new basic
1337 // block. First, we create the blocks for the then...else statements, and
1338 // then we create the block containing the if statement. If we were in the
Ted Kremenek0868eea2009-09-24 18:45:41 +00001339 // middle of a block, we stop processing that block. That block is then the
1340 // implicit successor for the "then" and "else" clauses.
Mike Stump31feda52009-07-17 01:31:16 +00001341
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001342 // Save local scope position because in case of condition variable ScopePos
1343 // won't be restored when traversing AST.
1344 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1345
1346 // Create local scope for possible condition variable.
1347 // Store scope position. Add implicit destructor.
1348 if (VarDecl* VD = I->getConditionVariable()) {
1349 LocalScope::const_iterator BeginScopePos = ScopePos;
1350 addLocalScopeForVarDecl(VD);
1351 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1352 }
1353
Mike Stump31feda52009-07-17 01:31:16 +00001354 // The block we were proccessing is now finished. Make it the successor
1355 // block.
1356 if (Block) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001357 Succ = Block;
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001358 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001359 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001360 }
Mike Stump31feda52009-07-17 01:31:16 +00001361
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001362 // Process the false branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001363 CFGBlock* ElseBlock = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001364
Ted Kremenek9aae5132007-08-23 21:42:29 +00001365 if (Stmt* Else = I->getElse()) {
1366 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001367
Ted Kremenek9aae5132007-08-23 21:42:29 +00001368 // NULL out Block so that the recursive call to Visit will
Mike Stump31feda52009-07-17 01:31:16 +00001369 // create a new basic block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001370 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001371
1372 // If branch is not a compound statement create implicit scope
1373 // and add destructors.
1374 if (!isa<CompoundStmt>(Else))
1375 addLocalScopeAndDtors(Else);
1376
Ted Kremenek93668002009-07-17 22:18:43 +00001377 ElseBlock = addStmt(Else);
Mike Stump31feda52009-07-17 01:31:16 +00001378
Ted Kremenekbbad8ce2007-08-30 18:13:31 +00001379 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1380 ElseBlock = sv.get();
Ted Kremenek55957a82009-05-02 00:13:27 +00001381 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001382 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001383 return 0;
1384 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001385 }
Mike Stump31feda52009-07-17 01:31:16 +00001386
Ted Kremenek0bcdc982009-07-17 18:04:55 +00001387 // Process the true branch.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001388 CFGBlock* ThenBlock;
1389 {
1390 Stmt* Then = I->getThen();
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001391 assert(Then);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001392 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump31feda52009-07-17 01:31:16 +00001393 Block = NULL;
Marcin Swiderskif883ade2010-10-01 00:52:17 +00001394
1395 // If branch is not a compound statement create implicit scope
1396 // and add destructors.
1397 if (!isa<CompoundStmt>(Then))
1398 addLocalScopeAndDtors(Then);
1399
Ted Kremenek93668002009-07-17 22:18:43 +00001400 ThenBlock = addStmt(Then);
Mike Stump31feda52009-07-17 01:31:16 +00001401
Ted Kremenek1b379512009-04-01 03:52:47 +00001402 if (!ThenBlock) {
1403 // We can reach here if the "then" body has all NullStmts.
1404 // Create an empty block so we can distinguish between true and false
1405 // branches in path-sensitive analyses.
1406 ThenBlock = createBlock(false);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001407 addSuccessor(ThenBlock, sv.get());
Mike Stump31feda52009-07-17 01:31:16 +00001408 } else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001409 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001410 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001411 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001412 }
1413
Mike Stump31feda52009-07-17 01:31:16 +00001414 // Now create a new block containing the if statement.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001415 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001416
Ted Kremenek9aae5132007-08-23 21:42:29 +00001417 // Set the terminator of the new block to the If statement.
1418 Block->setTerminator(I);
Mike Stump31feda52009-07-17 01:31:16 +00001419
Mike Stump773582d2009-07-23 23:25:26 +00001420 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001421 const TryResult &KnownVal = tryEvaluateBool(I->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001422
Ted Kremenek9aae5132007-08-23 21:42:29 +00001423 // Now add the successors.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001424 addSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1425 addSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00001426
1427 // Add the condition as the last statement in the new block. This may create
1428 // new blocks as the condition may contain control-flow. Any newly created
1429 // blocks will be pointed to be "Block".
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001430 Block = addStmt(I->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001431
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001432 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1433 // and the condition variable initialization to the CFG.
1434 if (VarDecl *VD = I->getConditionVariable()) {
1435 if (Expr *Init = VD->getInit()) {
1436 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001437 appendStmt(Block, I, AddStmtChoice::AlwaysAdd);
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001438 addStmt(Init);
1439 }
1440 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001441
Ted Kremeneka7bcbde2009-12-23 04:49:01 +00001442 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001443}
Mike Stump31feda52009-07-17 01:31:16 +00001444
1445
Ted Kremenek9aae5132007-08-23 21:42:29 +00001446CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001447 // If we were in the middle of a block we stop processing that block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001448 //
Mike Stump31feda52009-07-17 01:31:16 +00001449 // NOTE: If a "return" appears in the middle of a block, this means that the
1450 // code afterwards is DEAD (unreachable). We still keep a basic block
1451 // for that code; a simple "mark-and-sweep" from the entry block will be
1452 // able to report such dead blocks.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001453
1454 // Create the new block.
1455 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001456
Ted Kremenek9aae5132007-08-23 21:42:29 +00001457 // The Exit block is the only successor.
Marcin Swiderski667ffec2010-10-01 00:23:17 +00001458 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001459 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001460
1461 // Add the return statement to the block. This may create new blocks if R
1462 // contains control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001463 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001464}
1465
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001466CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt *L) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001467 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek93668002009-07-17 22:18:43 +00001468 addStmt(L->getSubStmt());
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001469 CFGBlock *LabelBlock = Block;
Mike Stump31feda52009-07-17 01:31:16 +00001470
Ted Kremenek93668002009-07-17 22:18:43 +00001471 if (!LabelBlock) // This can happen when the body is empty, i.e.
1472 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump31feda52009-07-17 01:31:16 +00001473
Chris Lattnerc8e630e2011-02-17 07:39:24 +00001474 assert(LabelMap.find(L->getDecl()) == LabelMap.end() &&
1475 "label already in map");
1476 LabelMap[L->getDecl()] = JumpTarget(LabelBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001477
1478 // Labels partition blocks, so this is the end of the basic block we were
1479 // processing (L is the block's label). Because this is label (and we have
1480 // already processed the substatement) there is no extra control-flow to worry
1481 // about.
Ted Kremenek71eca012007-08-29 23:20:49 +00001482 LabelBlock->setLabel(L);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001483 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001484 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001485
1486 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001487 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001488
Ted Kremenek9aae5132007-08-23 21:42:29 +00001489 // This block is now the implicit successor of other blocks.
1490 Succ = LabelBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001491
Ted Kremenek9aae5132007-08-23 21:42:29 +00001492 return LabelBlock;
1493}
1494
1495CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump31feda52009-07-17 01:31:16 +00001496 // Goto is a control-flow statement. Thus we stop processing the current
1497 // block and create a new one.
Ted Kremenek93668002009-07-17 22:18:43 +00001498
Ted Kremenek9aae5132007-08-23 21:42:29 +00001499 Block = createBlock(false);
1500 Block->setTerminator(G);
Mike Stump31feda52009-07-17 01:31:16 +00001501
1502 // If we already know the mapping to the label block add the successor now.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001503 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump31feda52009-07-17 01:31:16 +00001504
Ted Kremenek9aae5132007-08-23 21:42:29 +00001505 if (I == LabelMap.end())
1506 // We will need to backpatch this block later.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001507 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1508 else {
1509 JumpTarget JT = I->second;
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001510 addAutomaticObjDtors(ScopePos, JT.scopePosition, G);
1511 addSuccessor(Block, JT.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001512 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001513
Mike Stump31feda52009-07-17 01:31:16 +00001514 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001515}
1516
1517CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001518 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001519
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001520 // Save local scope position because in case of condition variable ScopePos
1521 // won't be restored when traversing AST.
1522 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1523
1524 // Create local scope for init statement and possible condition variable.
1525 // Add destructor for init statement and condition variable.
1526 // Store scope position for continue statement.
1527 if (Stmt* Init = F->getInit())
1528 addLocalScopeForStmt(Init);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001529 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1530
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001531 if (VarDecl* VD = F->getConditionVariable())
1532 addLocalScopeForVarDecl(VD);
1533 LocalScope::const_iterator ContinueScopePos = ScopePos;
1534
1535 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1536
Mike Stump014b3ea2009-07-21 01:12:51 +00001537 // "for" is a control-flow statement. Thus we stop processing the current
1538 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001539 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001540 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001541 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001542 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001543 } else
1544 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001545
Ted Kremenek304a9532010-05-21 20:30:15 +00001546 // Save the current value for the break targets.
1547 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001548 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001549 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek304a9532010-05-21 20:30:15 +00001550
Mike Stump31feda52009-07-17 01:31:16 +00001551 // Because of short-circuit evaluation, the condition of the loop can span
1552 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1553 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001554 CFGBlock* ExitConditionBlock = createBlock(false);
1555 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001556
Ted Kremenek81e14852007-08-27 19:46:09 +00001557 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001558 ExitConditionBlock->setTerminator(F);
1559
1560 // Now add the actual condition to the condition block. Because the condition
1561 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00001562 if (Stmt* C = F->getCond()) {
1563 Block = ExitConditionBlock;
1564 EntryConditionBlock = addStmt(C);
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001565 if (badCFG)
1566 return 0;
Ted Kremenek7b31a612010-09-15 07:01:20 +00001567 assert(Block == EntryConditionBlock ||
1568 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenekec92f942009-12-24 01:49:06 +00001569
1570 // If this block contains a condition variable, add both the condition
1571 // variable and initializer to the CFG.
1572 if (VarDecl *VD = F->getConditionVariable()) {
1573 if (Expr *Init = VD->getInit()) {
1574 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001575 appendStmt(Block, F, AddStmtChoice::AlwaysAdd);
Ted Kremenekec92f942009-12-24 01:49:06 +00001576 EntryConditionBlock = addStmt(Init);
1577 assert(Block == EntryConditionBlock);
1578 }
1579 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001580
Ted Kremenek55957a82009-05-02 00:13:27 +00001581 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001582 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001583 return 0;
1584 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001585 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00001586
Mike Stump31feda52009-07-17 01:31:16 +00001587 // The condition block is the implicit successor for the loop body as well as
1588 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001589 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001590
Mike Stump773582d2009-07-23 23:25:26 +00001591 // See if this is a known constant.
Ted Kremenek30754282009-07-24 04:47:11 +00001592 TryResult KnownVal(true);
Mike Stump11289f42009-09-09 15:08:12 +00001593
Mike Stump773582d2009-07-23 23:25:26 +00001594 if (F->getCond())
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001595 KnownVal = tryEvaluateBool(F->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001596
Ted Kremenek9aae5132007-08-23 21:42:29 +00001597 // Now create the loop body.
1598 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00001599 assert(F->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001600
Ted Kremenek304a9532010-05-21 20:30:15 +00001601 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001602 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1603 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001604
Ted Kremeneke9610502007-08-30 18:39:40 +00001605 // Create a new block to contain the (bottom) of the loop body.
1606 Block = NULL;
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001607
1608 // Loop body should end with destructor of Condition variable (if any).
1609 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump31feda52009-07-17 01:31:16 +00001610
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001611 if (Stmt* I = F->getInc()) {
Mike Stump31feda52009-07-17 01:31:16 +00001612 // Generate increment code in its own basic block. This is the target of
1613 // continue statements.
Ted Kremenek93668002009-07-17 22:18:43 +00001614 Succ = addStmt(I);
Mike Stump31feda52009-07-17 01:31:16 +00001615 } else {
1616 // No increment code. Create a special, empty, block that is used as the
1617 // target block for "looping back" to the start of the loop.
Ted Kremenek902393b2009-04-28 00:51:56 +00001618 assert(Succ == EntryConditionBlock);
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001619 Succ = Block ? Block : createBlock();
Ted Kremenekb0746ca2008-09-04 21:48:47 +00001620 }
Mike Stump31feda52009-07-17 01:31:16 +00001621
Ted Kremenek902393b2009-04-28 00:51:56 +00001622 // Finish up the increment (or empty) block if it hasn't been already.
1623 if (Block) {
1624 assert(Block == Succ);
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001625 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001626 return 0;
Ted Kremenek902393b2009-04-28 00:51:56 +00001627 Block = 0;
1628 }
Mike Stump31feda52009-07-17 01:31:16 +00001629
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001630 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001631
Ted Kremenek902393b2009-04-28 00:51:56 +00001632 // The starting block for the loop increment is the block that should
1633 // represent the 'loop target' for looping back to the start of the loop.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001634 ContinueJumpTarget.block->setLoopTarget(F);
Ted Kremenek902393b2009-04-28 00:51:56 +00001635
Marcin Swiderski6d5ee0c2010-10-01 01:38:14 +00001636 // If body is not a compound statement create implicit scope
1637 // and add destructors.
1638 if (!isa<CompoundStmt>(F->getBody()))
1639 addLocalScopeAndDtors(F->getBody());
1640
Mike Stump31feda52009-07-17 01:31:16 +00001641 // Now populate the body block, and in the process create new blocks as we
1642 // walk the body of the loop.
Ted Kremenek93668002009-07-17 22:18:43 +00001643 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremeneke9610502007-08-30 18:39:40 +00001644
1645 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001646 BodyBlock = ContinueJumpTarget.block;//can happen for "for (...;...;...);"
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001647 else if (badCFG)
Ted Kremenek30754282009-07-24 04:47:11 +00001648 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001649
Ted Kremenek30754282009-07-24 04:47:11 +00001650 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001651 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001652 }
Mike Stump31feda52009-07-17 01:31:16 +00001653
Ted Kremenek30754282009-07-24 04:47:11 +00001654 // Link up the condition block with the code that follows the loop. (the
1655 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001656 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001657
Ted Kremenek9aae5132007-08-23 21:42:29 +00001658 // If the loop contains initialization, create a new block for those
Mike Stump31feda52009-07-17 01:31:16 +00001659 // statements. This block can also contain statements that precede the loop.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001660 if (Stmt* I = F->getInit()) {
1661 Block = createBlock();
Ted Kremenek81e14852007-08-27 19:46:09 +00001662 return addStmt(I);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001663 }
Zhanyong Wan59f09c72010-11-22 19:32:14 +00001664
1665 // There is no loop initialization. We are thus basically a while loop.
1666 // NULL out Block to force lazy block construction.
1667 Block = NULL;
1668 Succ = EntryConditionBlock;
1669 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001670}
1671
Ted Kremenek5868ec62010-04-11 17:02:10 +00001672CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1673 if (asc.alwaysAdd()) {
1674 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001675 appendStmt(Block, M, asc);
Ted Kremenek5868ec62010-04-11 17:02:10 +00001676 }
Ted Kremenek8219b822010-12-16 07:46:53 +00001677 return Visit(M->getBase());
Ted Kremenek5868ec62010-04-11 17:02:10 +00001678}
1679
Ted Kremenek9d56e642008-11-11 17:10:00 +00001680CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1681 // Objective-C fast enumeration 'for' statements:
1682 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1683 //
1684 // for ( Type newVariable in collection_expression ) { statements }
1685 //
1686 // becomes:
1687 //
1688 // prologue:
1689 // 1. collection_expression
1690 // T. jump to loop_entry
1691 // loop_entry:
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001692 // 1. side-effects of element expression
Ted Kremenek9d56e642008-11-11 17:10:00 +00001693 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1694 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1695 // TB:
1696 // statements
1697 // T. jump to loop_entry
1698 // FB:
1699 // what comes after
1700 //
1701 // and
1702 //
1703 // Type existingItem;
1704 // for ( existingItem in expression ) { statements }
1705 //
1706 // becomes:
1707 //
Mike Stump31feda52009-07-17 01:31:16 +00001708 // the same with newVariable replaced with existingItem; the binding works
1709 // the same except that for one ObjCForCollectionStmt::getElement() returns
1710 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek9d56e642008-11-11 17:10:00 +00001711 //
Mike Stump31feda52009-07-17 01:31:16 +00001712
Ted Kremenek9d56e642008-11-11 17:10:00 +00001713 CFGBlock* LoopSuccessor = 0;
Mike Stump31feda52009-07-17 01:31:16 +00001714
Ted Kremenek9d56e642008-11-11 17:10:00 +00001715 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001716 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001717 return 0;
Ted Kremenek9d56e642008-11-11 17:10:00 +00001718 LoopSuccessor = Block;
1719 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001720 } else
1721 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001722
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001723 // Build the condition blocks.
1724 CFGBlock* ExitConditionBlock = createBlock(false);
1725 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001726
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001727 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00001728 ExitConditionBlock->setTerminator(S);
1729
1730 // The last statement in the block should be the ObjCForCollectionStmt, which
1731 // performs the actual binding to 'element' and determines if there are any
1732 // more items in the collection.
Ted Kremenek8219b822010-12-16 07:46:53 +00001733 appendStmt(ExitConditionBlock, S);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001734 Block = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001735
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001736 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump31feda52009-07-17 01:31:16 +00001737 // generate new blocks as necesary. We DON'T add the statement by default to
1738 // the CFG unless it contains control-flow.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001739 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump31feda52009-07-17 01:31:16 +00001740 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001741 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001742 return 0;
1743 Block = 0;
1744 }
Mike Stump31feda52009-07-17 01:31:16 +00001745
1746 // The condition block is the implicit successor for the loop body as well as
1747 // any code above the loop.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001748 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001749
Ted Kremenek9d56e642008-11-11 17:10:00 +00001750 // Now create the true branch.
Mike Stump31feda52009-07-17 01:31:16 +00001751 {
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001752 // Save the current values for Succ, continue and break targets.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001753 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1754 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1755 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00001756
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001757 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1758 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001759
Ted Kremenek93668002009-07-17 22:18:43 +00001760 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001761
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001762 if (!BodyBlock)
1763 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001764 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001765 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001766 return 0;
1767 }
Mike Stump31feda52009-07-17 01:31:16 +00001768
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001769 // This new body block is a successor to our "exit" condition block.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001770 addSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001771 }
Mike Stump31feda52009-07-17 01:31:16 +00001772
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001773 // Link up the condition block with the code that follows the loop.
1774 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001775 addSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001776
Ted Kremenek9d56e642008-11-11 17:10:00 +00001777 // Now create a prologue block to contain the collection expression.
Ted Kremenek5cf87ff2008-11-14 01:57:41 +00001778 Block = createBlock();
Ted Kremenek9d56e642008-11-11 17:10:00 +00001779 return addStmt(S->getCollection());
Mike Stump31feda52009-07-17 01:31:16 +00001780}
1781
Ted Kremenek49805452009-05-02 01:49:13 +00001782CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1783 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump31feda52009-07-17 01:31:16 +00001784
Ted Kremenek49805452009-05-02 01:49:13 +00001785 // Inline the body.
Ted Kremenek93668002009-07-17 22:18:43 +00001786 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump31feda52009-07-17 01:31:16 +00001787
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001788 // The sync body starts its own basic block. This makes it a little easier
1789 // for diagnostic clients.
1790 if (SyncBlock) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001791 if (badCFG)
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001792 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001793
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001794 Block = 0;
Ted Kremenekecc31c92010-05-13 16:38:08 +00001795 Succ = SyncBlock;
Ted Kremenekb3c657b2009-05-05 23:11:51 +00001796 }
Mike Stump31feda52009-07-17 01:31:16 +00001797
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001798 // Add the @synchronized to the CFG.
1799 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001800 appendStmt(Block, S, AddStmtChoice::AlwaysAdd);
Ted Kremeneked12f1b2010-09-10 03:05:33 +00001801
Ted Kremenek49805452009-05-02 01:49:13 +00001802 // Inline the sync expression.
Ted Kremenek93668002009-07-17 22:18:43 +00001803 return addStmt(S->getSynchExpr());
Ted Kremenek49805452009-05-02 01:49:13 +00001804}
Mike Stump31feda52009-07-17 01:31:16 +00001805
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001806CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek93668002009-07-17 22:18:43 +00001807 // FIXME
Ted Kremenek89be6522009-04-07 04:26:02 +00001808 return NYS();
Ted Kremenek89cc8ea2009-03-30 22:29:21 +00001809}
Ted Kremenek9d56e642008-11-11 17:10:00 +00001810
Ted Kremenek9aae5132007-08-23 21:42:29 +00001811CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001812 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001813
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001814 // Save local scope position because in case of condition variable ScopePos
1815 // won't be restored when traversing AST.
1816 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1817
1818 // Create local scope for possible condition variable.
1819 // Store scope position for continue statement.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001820 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001821 if (VarDecl* VD = W->getConditionVariable()) {
1822 addLocalScopeForVarDecl(VD);
1823 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1824 }
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001825
Mike Stump014b3ea2009-07-21 01:12:51 +00001826 // "while" is a control-flow statement. Thus we stop processing the current
1827 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001828 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001829 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001830 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001831 LoopSuccessor = Block;
Ted Kremenek828f6312011-02-21 22:11:26 +00001832 Block = 0;
Ted Kremenek93668002009-07-17 22:18:43 +00001833 } else
1834 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001835
1836 // Because of short-circuit evaluation, the condition of the loop can span
1837 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1838 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001839 CFGBlock* ExitConditionBlock = createBlock(false);
1840 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001841
Ted Kremenek81e14852007-08-27 19:46:09 +00001842 // Set the terminator for the "exit" condition block.
1843 ExitConditionBlock->setTerminator(W);
Mike Stump31feda52009-07-17 01:31:16 +00001844
1845 // Now add the actual condition to the condition block. Because the condition
1846 // itself may contain control-flow, new blocks may be created. Thus we update
1847 // "Succ" after adding the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001848 if (Stmt* C = W->getCond()) {
1849 Block = ExitConditionBlock;
1850 EntryConditionBlock = addStmt(C);
Zhongxing Xud95ccd52010-10-27 03:23:10 +00001851 // The condition might finish the current 'Block'.
1852 Block = EntryConditionBlock;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001853
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001854 // If this block contains a condition variable, add both the condition
1855 // variable and initializer to the CFG.
1856 if (VarDecl *VD = W->getConditionVariable()) {
1857 if (Expr *Init = VD->getInit()) {
1858 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00001859 appendStmt(Block, W, AddStmtChoice::AlwaysAdd);
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001860 EntryConditionBlock = addStmt(Init);
1861 assert(Block == EntryConditionBlock);
1862 }
1863 }
1864
Ted Kremenek55957a82009-05-02 00:13:27 +00001865 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001866 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001867 return 0;
1868 }
Ted Kremenek81e14852007-08-27 19:46:09 +00001869 }
Mike Stump31feda52009-07-17 01:31:16 +00001870
1871 // The condition block is the implicit successor for the loop body as well as
1872 // any code above the loop.
Ted Kremenek81e14852007-08-27 19:46:09 +00001873 Succ = EntryConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001874
Mike Stump773582d2009-07-23 23:25:26 +00001875 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001876 const TryResult& KnownVal = tryEvaluateBool(W->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00001877
Ted Kremenek9aae5132007-08-23 21:42:29 +00001878 // Process the loop body.
1879 {
Ted Kremenek49936f72009-04-28 03:09:44 +00001880 assert(W->getBody());
Ted Kremenek9aae5132007-08-23 21:42:29 +00001881
1882 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001883 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1884 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1885 save_break(BreakJumpTarget);
Ted Kremenek49936f72009-04-28 03:09:44 +00001886
Mike Stump31feda52009-07-17 01:31:16 +00001887 // Create an empty block to represent the transition block for looping back
1888 // to the head of the loop.
Ted Kremenek49936f72009-04-28 03:09:44 +00001889 Block = 0;
1890 assert(Succ == EntryConditionBlock);
1891 Succ = createBlock();
1892 Succ->setLoopTarget(W);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00001893 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001894
Ted Kremenek9aae5132007-08-23 21:42:29 +00001895 // All breaks should go to the code following the loop.
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001896 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00001897
Ted Kremenek9aae5132007-08-23 21:42:29 +00001898 // NULL out Block to force lazy instantiation of blocks for the body.
1899 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001900
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00001901 // Loop body should end with destructor of Condition variable (if any).
1902 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1903
1904 // If body is not a compound statement create implicit scope
1905 // and add destructors.
1906 if (!isa<CompoundStmt>(W->getBody()))
1907 addLocalScopeAndDtors(W->getBody());
1908
Ted Kremenek9aae5132007-08-23 21:42:29 +00001909 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00001910 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00001911
Ted Kremeneke9610502007-08-30 18:39:40 +00001912 if (!BodyBlock)
Ted Kremenekef81e9e2011-01-07 19:37:16 +00001913 BodyBlock = ContinueJumpTarget.block; // can happen for "while(...) ;"
Ted Kremenek55957a82009-05-02 00:13:27 +00001914 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001915 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001916 return 0;
1917 }
Mike Stump31feda52009-07-17 01:31:16 +00001918
Ted Kremenek30754282009-07-24 04:47:11 +00001919 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001920 addSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenek9aae5132007-08-23 21:42:29 +00001921 }
Mike Stump31feda52009-07-17 01:31:16 +00001922
Ted Kremenek30754282009-07-24 04:47:11 +00001923 // Link up the condition block with the code that follows the loop. (the
1924 // false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001925 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00001926
1927 // There can be no more statements in the condition block since we loop back
1928 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001929 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001930
Ted Kremenek1ce53c42009-12-24 01:34:10 +00001931 // Return the condition block, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00001932 Succ = EntryConditionBlock;
Ted Kremenek81e14852007-08-27 19:46:09 +00001933 return EntryConditionBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001934}
Mike Stump11289f42009-09-09 15:08:12 +00001935
1936
Ted Kremenek93668002009-07-17 22:18:43 +00001937CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1938 // FIXME: For now we pretend that @catch and the code it contains does not
1939 // exit.
1940 return Block;
1941}
Mike Stump31feda52009-07-17 01:31:16 +00001942
Ted Kremenek93041ba2008-12-09 20:20:09 +00001943CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1944 // FIXME: This isn't complete. We basically treat @throw like a return
1945 // statement.
Mike Stump31feda52009-07-17 01:31:16 +00001946
Ted Kremenek0868eea2009-09-24 18:45:41 +00001947 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001948 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00001949 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00001950
Ted Kremenek93041ba2008-12-09 20:20:09 +00001951 // Create the new block.
1952 Block = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00001953
Ted Kremenek93041ba2008-12-09 20:20:09 +00001954 // The Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001955 addSuccessor(Block, &cfg->getExit());
Mike Stump31feda52009-07-17 01:31:16 +00001956
1957 // Add the statement to the block. This may create new blocks if S contains
1958 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001959 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek93041ba2008-12-09 20:20:09 +00001960}
Ted Kremenek9aae5132007-08-23 21:42:29 +00001961
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001962CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek0868eea2009-09-24 18:45:41 +00001963 // If we were in the middle of a block we stop processing that block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001964 if (badCFG)
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001965 return 0;
1966
1967 // Create the new block.
1968 Block = createBlock(false);
1969
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001970 if (TryTerminatedBlock)
1971 // The current try statement is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001972 addSuccessor(Block, TryTerminatedBlock);
Ted Kremenekdc03bd02010-08-02 23:46:59 +00001973 else
Mike Stumpbbf5ba62010-01-19 02:20:09 +00001974 // otherwise the Exit block is the only successor.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00001975 addSuccessor(Block, &cfg->getExit());
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001976
1977 // Add the statement to the block. This may create new blocks if S contains
1978 // control-flow (short-circuit operations).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001979 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump8dd1b6b2009-07-22 22:56:04 +00001980}
1981
Ted Kremenek93668002009-07-17 22:18:43 +00001982CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenek9aae5132007-08-23 21:42:29 +00001983 CFGBlock* LoopSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00001984
Mike Stump8d50b6a2009-07-21 01:27:50 +00001985 // "do...while" is a control-flow statement. Thus we stop processing the
1986 // current block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00001987 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00001988 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00001989 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00001990 LoopSuccessor = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00001991 } else
1992 LoopSuccessor = Succ;
Mike Stump31feda52009-07-17 01:31:16 +00001993
1994 // Because of short-circuit evaluation, the condition of the loop can span
1995 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1996 // evaluate the condition.
Ted Kremenek81e14852007-08-27 19:46:09 +00001997 CFGBlock* ExitConditionBlock = createBlock(false);
1998 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump31feda52009-07-17 01:31:16 +00001999
Ted Kremenek81e14852007-08-27 19:46:09 +00002000 // Set the terminator for the "exit" condition block.
Mike Stump31feda52009-07-17 01:31:16 +00002001 ExitConditionBlock->setTerminator(D);
2002
2003 // Now add the actual condition to the condition block. Because the condition
2004 // itself may contain control-flow, new blocks may be created.
Ted Kremenek81e14852007-08-27 19:46:09 +00002005 if (Stmt* C = D->getCond()) {
2006 Block = ExitConditionBlock;
2007 EntryConditionBlock = addStmt(C);
Ted Kremenek55957a82009-05-02 00:13:27 +00002008 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002009 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002010 return 0;
2011 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002012 }
Mike Stump31feda52009-07-17 01:31:16 +00002013
Ted Kremeneka1523a32008-02-27 07:20:00 +00002014 // The condition block is the implicit successor for the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002015 Succ = EntryConditionBlock;
2016
Mike Stump773582d2009-07-23 23:25:26 +00002017 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002018 const TryResult &KnownVal = tryEvaluateBool(D->getCond());
Mike Stump773582d2009-07-23 23:25:26 +00002019
Ted Kremenek9aae5132007-08-23 21:42:29 +00002020 // Process the loop body.
Ted Kremenek81e14852007-08-27 19:46:09 +00002021 CFGBlock* BodyBlock = NULL;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002022 {
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002023 assert(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002024
Ted Kremenek9aae5132007-08-23 21:42:29 +00002025 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002026 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
2027 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
2028 save_break(BreakJumpTarget);
Mike Stump31feda52009-07-17 01:31:16 +00002029
Ted Kremenek9aae5132007-08-23 21:42:29 +00002030 // All continues within this loop should go to the condition block
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002031 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002032
Ted Kremenek9aae5132007-08-23 21:42:29 +00002033 // All breaks should go to the code following the loop.
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002034 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002035
Ted Kremenek9aae5132007-08-23 21:42:29 +00002036 // NULL out Block to force lazy instantiation of blocks for the body.
2037 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002038
Marcin Swiderski1f4e15c2010-10-01 01:14:17 +00002039 // If body is not a compound statement create implicit scope
2040 // and add destructors.
2041 if (!isa<CompoundStmt>(D->getBody()))
2042 addLocalScopeAndDtors(D->getBody());
2043
Ted Kremenek9aae5132007-08-23 21:42:29 +00002044 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek93668002009-07-17 22:18:43 +00002045 BodyBlock = addStmt(D->getBody());
Mike Stump31feda52009-07-17 01:31:16 +00002046
Ted Kremeneke9610502007-08-30 18:39:40 +00002047 if (!BodyBlock)
Ted Kremenek39321aa2008-02-27 00:28:17 +00002048 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek55957a82009-05-02 00:13:27 +00002049 else if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002050 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002051 return 0;
2052 }
Mike Stump31feda52009-07-17 01:31:16 +00002053
Ted Kremenek110974d2010-08-17 20:59:56 +00002054 if (!KnownVal.isFalse()) {
2055 // Add an intermediate block between the BodyBlock and the
2056 // ExitConditionBlock to represent the "loop back" transition. Create an
2057 // empty block to represent the transition block for looping back to the
2058 // head of the loop.
2059 // FIXME: Can we do this more efficiently without adding another block?
2060 Block = NULL;
2061 Succ = BodyBlock;
2062 CFGBlock *LoopBackBlock = createBlock();
2063 LoopBackBlock->setLoopTarget(D);
Mike Stump31feda52009-07-17 01:31:16 +00002064
Ted Kremenek110974d2010-08-17 20:59:56 +00002065 // Add the loop body entry as a successor to the condition.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002066 addSuccessor(ExitConditionBlock, LoopBackBlock);
Ted Kremenek110974d2010-08-17 20:59:56 +00002067 }
2068 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002069 addSuccessor(ExitConditionBlock, NULL);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002070 }
Mike Stump31feda52009-07-17 01:31:16 +00002071
Ted Kremenek30754282009-07-24 04:47:11 +00002072 // Link up the condition block with the code that follows the loop.
2073 // (the false branch).
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002074 addSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump31feda52009-07-17 01:31:16 +00002075
2076 // There can be no more statements in the body block(s) since we loop back to
2077 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002078 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002079
Ted Kremenek9aae5132007-08-23 21:42:29 +00002080 // Return the loop body, which is the dominating block for the loop.
Ted Kremeneka1523a32008-02-27 07:20:00 +00002081 Succ = BodyBlock;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002082 return BodyBlock;
2083}
2084
2085CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
2086 // "continue" is a control-flow statement. Thus we stop processing the
2087 // current block.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002088 if (badCFG)
2089 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002090
Ted Kremenek9aae5132007-08-23 21:42:29 +00002091 // Now create a new block that ends with the continue statement.
2092 Block = createBlock(false);
2093 Block->setTerminator(C);
Mike Stump31feda52009-07-17 01:31:16 +00002094
Ted Kremenek9aae5132007-08-23 21:42:29 +00002095 // If there is no target for the continue, then we are looking at an
Ted Kremenek882cf062009-04-07 18:53:24 +00002096 // incomplete AST. This means the CFG cannot be constructed.
Ted Kremenekef81e9e2011-01-07 19:37:16 +00002097 if (ContinueJumpTarget.block) {
2098 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.scopePosition, C);
2099 addSuccessor(Block, ContinueJumpTarget.block);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002100 } else
Ted Kremenek882cf062009-04-07 18:53:24 +00002101 badCFG = true;
Mike Stump31feda52009-07-17 01:31:16 +00002102
Ted Kremenek9aae5132007-08-23 21:42:29 +00002103 return Block;
2104}
Mike Stump11289f42009-09-09 15:08:12 +00002105
Ted Kremenek0747de62009-07-18 00:47:21 +00002106CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002107 AddStmtChoice asc) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002108
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002109 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002110 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002111 appendStmt(Block, E);
Ted Kremenek0747de62009-07-18 00:47:21 +00002112 }
Mike Stump11289f42009-09-09 15:08:12 +00002113
Ted Kremenek93668002009-07-17 22:18:43 +00002114 // VLA types have expressions that must be evaluated.
2115 if (E->isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002116 for (const VariableArrayType *VA =FindVA(E->getArgumentType().getTypePtr());
Ted Kremenek93668002009-07-17 22:18:43 +00002117 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
2118 addStmt(VA->getSizeExpr());
Ted Kremenek55957a82009-05-02 00:13:27 +00002119 }
Mike Stump11289f42009-09-09 15:08:12 +00002120
Mike Stump31feda52009-07-17 01:31:16 +00002121 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002122}
Mike Stump11289f42009-09-09 15:08:12 +00002123
Ted Kremenek93668002009-07-17 22:18:43 +00002124/// VisitStmtExpr - Utility method to handle (nested) statement
2125/// expressions (a GCC extension).
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00002126CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2127 if (asc.alwaysAdd()) {
Ted Kremenek0747de62009-07-18 00:47:21 +00002128 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002129 appendStmt(Block, SE);
Ted Kremenek0747de62009-07-18 00:47:21 +00002130 }
Ted Kremenek93668002009-07-17 22:18:43 +00002131 return VisitCompoundStmt(SE->getSubStmt());
2132}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002133
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002134CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump31feda52009-07-17 01:31:16 +00002135 // "switch" is a control-flow statement. Thus we stop processing the current
2136 // block.
Ted Kremenek9aae5132007-08-23 21:42:29 +00002137 CFGBlock* SwitchSuccessor = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002138
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002139 // Save local scope position because in case of condition variable ScopePos
2140 // won't be restored when traversing AST.
2141 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2142
2143 // Create local scope for possible condition variable.
2144 // Store scope position. Add implicit destructor.
2145 if (VarDecl* VD = Terminator->getConditionVariable()) {
2146 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2147 addLocalScopeForVarDecl(VD);
2148 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2149 }
2150
Ted Kremenek9aae5132007-08-23 21:42:29 +00002151 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002152 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002153 return 0;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002154 SwitchSuccessor = Block;
Mike Stump31feda52009-07-17 01:31:16 +00002155 } else SwitchSuccessor = Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002156
2157 // Save the current "switch" context.
2158 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenek654c78f2008-02-13 22:05:39 +00002159 save_default(DefaultCaseBlock);
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002160 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenek654c78f2008-02-13 22:05:39 +00002161
Mike Stump31feda52009-07-17 01:31:16 +00002162 // Set the "default" case to be the block after the switch statement. If the
2163 // switch statement contains a "default:", this value will be overwritten with
2164 // the block for that code.
Ted Kremenek654c78f2008-02-13 22:05:39 +00002165 DefaultCaseBlock = SwitchSuccessor;
Mike Stump31feda52009-07-17 01:31:16 +00002166
Ted Kremenek9aae5132007-08-23 21:42:29 +00002167 // Create a new block that will contain the switch statement.
2168 SwitchTerminatedBlock = createBlock(false);
Mike Stump31feda52009-07-17 01:31:16 +00002169
Ted Kremenek9aae5132007-08-23 21:42:29 +00002170 // Now process the switch body. The code after the switch is the implicit
2171 // successor.
2172 Succ = SwitchSuccessor;
Marcin Swiderski8b99b8a2010-09-25 11:05:21 +00002173 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump31feda52009-07-17 01:31:16 +00002174
2175 // When visiting the body, the case statements should automatically get linked
2176 // up to the switch. We also don't keep a pointer to the body, since all
2177 // control-flow from the switch goes to case/default statements.
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002178 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek81e14852007-08-27 19:46:09 +00002179 Block = NULL;
Marcin Swiderskie407a3b2010-10-01 01:24:41 +00002180
2181 // If body is not a compound statement create implicit scope
2182 // and add destructors.
2183 if (!isa<CompoundStmt>(Terminator->getBody()))
2184 addLocalScopeAndDtors(Terminator->getBody());
2185
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002186 addStmt(Terminator->getBody());
Ted Kremenek55957a82009-05-02 00:13:27 +00002187 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002188 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002189 return 0;
2190 }
Ted Kremenek81e14852007-08-27 19:46:09 +00002191
Mike Stump31feda52009-07-17 01:31:16 +00002192 // If we have no "default:" case, the default transition is to the code
2193 // following the switch body.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002194 addSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002195
Ted Kremenek81e14852007-08-27 19:46:09 +00002196 // Add the terminator and condition in the switch block.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002197 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002198 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenek9aae5132007-08-23 21:42:29 +00002199 Block = SwitchTerminatedBlock;
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002200 Block = addStmt(Terminator->getCond());
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002201
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002202 // Finally, if the SwitchStmt contains a condition variable, add both the
2203 // SwitchStmt and the condition variable initialization to the CFG.
2204 if (VarDecl *VD = Terminator->getConditionVariable()) {
2205 if (Expr *Init = VD->getInit()) {
2206 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002207 appendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002208 addStmt(Init);
2209 }
2210 }
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002211
Ted Kremenek8b5dc122009-12-24 00:39:26 +00002212 return Block;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002213}
2214
Ted Kremenek93668002009-07-17 22:18:43 +00002215CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump31feda52009-07-17 01:31:16 +00002216 // CaseStmts are essentially labels, so they are the first statement in a
2217 // block.
Ted Kremenek60fa6572010-08-04 23:54:30 +00002218 CFGBlock *TopBlock = 0, *LastBlock = 0;
2219
2220 if (Stmt *Sub = CS->getSubStmt()) {
2221 // For deeply nested chains of CaseStmts, instead of doing a recursion
2222 // (which can blow out the stack), manually unroll and create blocks
2223 // along the way.
2224 while (isa<CaseStmt>(Sub)) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002225 CFGBlock *currentBlock = createBlock(false);
2226 currentBlock->setLabel(CS);
Ted Kremenek55e91e82007-08-30 18:48:11 +00002227
Ted Kremenek60fa6572010-08-04 23:54:30 +00002228 if (TopBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002229 addSuccessor(LastBlock, currentBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002230 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002231 TopBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002232
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002233 addSuccessor(SwitchTerminatedBlock, currentBlock);
2234 LastBlock = currentBlock;
Ted Kremenek60fa6572010-08-04 23:54:30 +00002235
2236 CS = cast<CaseStmt>(Sub);
2237 Sub = CS->getSubStmt();
2238 }
2239
2240 addStmt(Sub);
2241 }
Mike Stump11289f42009-09-09 15:08:12 +00002242
Ted Kremenek55e91e82007-08-30 18:48:11 +00002243 CFGBlock* CaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002244 if (!CaseBlock)
2245 CaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002246
2247 // Cases statements partition blocks, so this is the top of the basic block we
2248 // were processing (the "case XXX:" is the label).
Ted Kremenek93668002009-07-17 22:18:43 +00002249 CaseBlock->setLabel(CS);
2250
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002251 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002252 return 0;
Mike Stump31feda52009-07-17 01:31:16 +00002253
2254 // Add this block to the list of successors for the block with the switch
2255 // statement.
Ted Kremenek93668002009-07-17 22:18:43 +00002256 assert(SwitchTerminatedBlock);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002257 addSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump31feda52009-07-17 01:31:16 +00002258
Ted Kremenek9aae5132007-08-23 21:42:29 +00002259 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2260 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002261
Ted Kremenek60fa6572010-08-04 23:54:30 +00002262 if (TopBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002263 addSuccessor(LastBlock, CaseBlock);
Ted Kremenek60fa6572010-08-04 23:54:30 +00002264 Succ = TopBlock;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002265 } else {
Ted Kremenek60fa6572010-08-04 23:54:30 +00002266 // This block is now the implicit successor of other blocks.
2267 Succ = CaseBlock;
2268 }
Mike Stump31feda52009-07-17 01:31:16 +00002269
Ted Kremenek60fa6572010-08-04 23:54:30 +00002270 return Succ;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002271}
Mike Stump31feda52009-07-17 01:31:16 +00002272
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002273CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek93668002009-07-17 22:18:43 +00002274 if (Terminator->getSubStmt())
2275 addStmt(Terminator->getSubStmt());
Mike Stump11289f42009-09-09 15:08:12 +00002276
Ted Kremenek654c78f2008-02-13 22:05:39 +00002277 DefaultCaseBlock = Block;
Ted Kremenek93668002009-07-17 22:18:43 +00002278
2279 if (!DefaultCaseBlock)
2280 DefaultCaseBlock = createBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002281
2282 // Default statements partition blocks, so this is the top of the basic block
2283 // we were processing (the "default:" is the label).
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002284 DefaultCaseBlock->setLabel(Terminator);
Mike Stump11289f42009-09-09 15:08:12 +00002285
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002286 if (badCFG)
Ted Kremenek55957a82009-05-02 00:13:27 +00002287 return 0;
Ted Kremenek654c78f2008-02-13 22:05:39 +00002288
Mike Stump31feda52009-07-17 01:31:16 +00002289 // Unlike case statements, we don't add the default block to the successors
2290 // for the switch statement immediately. This is done when we finish
2291 // processing the switch statement. This allows for the default case
2292 // (including a fall-through to the code after the switch statement) to always
2293 // be the last successor of a switch-terminated block.
2294
Ted Kremenek654c78f2008-02-13 22:05:39 +00002295 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2296 Block = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00002297
Ted Kremenek654c78f2008-02-13 22:05:39 +00002298 // This block is now the implicit successor of other blocks.
2299 Succ = DefaultCaseBlock;
Mike Stump31feda52009-07-17 01:31:16 +00002300
2301 return DefaultCaseBlock;
Ted Kremenek9682be12008-02-13 21:46:34 +00002302}
Ted Kremenek9aae5132007-08-23 21:42:29 +00002303
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002304CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2305 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2306 // current block.
2307 CFGBlock* TrySuccessor = NULL;
2308
2309 if (Block) {
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002310 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002311 return 0;
2312 TrySuccessor = Block;
2313 } else TrySuccessor = Succ;
2314
Mike Stump0bdba6c2010-01-20 01:15:34 +00002315 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002316
2317 // Create a new block that will contain the try statement.
Mike Stump845384a2010-01-20 01:30:58 +00002318 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002319 // Add the terminator in the try block.
Mike Stump845384a2010-01-20 01:30:58 +00002320 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002321
Mike Stump0bdba6c2010-01-20 01:15:34 +00002322 bool HasCatchAll = false;
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002323 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2324 // The code after the try is the implicit successor.
2325 Succ = TrySuccessor;
2326 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002327 if (CS->getExceptionDecl() == 0) {
2328 HasCatchAll = true;
2329 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002330 Block = NULL;
2331 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2332 if (CatchBlock == 0)
2333 return 0;
2334 // Add this block to the list of successors for the block with the try
2335 // statement.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002336 addSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002337 }
Mike Stump0bdba6c2010-01-20 01:15:34 +00002338 if (!HasCatchAll) {
2339 if (PrevTryTerminatedBlock)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002340 addSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stump0bdba6c2010-01-20 01:15:34 +00002341 else
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002342 addSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stump0bdba6c2010-01-20 01:15:34 +00002343 }
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002344
2345 // The code after the try is the implicit successor.
2346 Succ = TrySuccessor;
2347
Mike Stump845384a2010-01-20 01:30:58 +00002348 // Save the current "try" context.
2349 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2350 TryTerminatedBlock = NewTryTerminatedBlock;
2351
Ted Kremenek1362b8b2010-01-19 20:46:35 +00002352 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002353 Block = NULL;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002354 Block = addStmt(Terminator->getTryBlock());
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002355 return Block;
2356}
2357
2358CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2359 // CXXCatchStmt are treated like labels, so they are the first statement in a
2360 // block.
2361
Marcin Swiderski3546b1a2010-10-01 01:46:52 +00002362 // Save local scope position because in case of exception variable ScopePos
2363 // won't be restored when traversing AST.
2364 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2365
2366 // Create local scope for possible exception variable.
2367 // Store scope position. Add implicit destructor.
2368 if (VarDecl* VD = CS->getExceptionDecl()) {
2369 LocalScope::const_iterator BeginScopePos = ScopePos;
2370 addLocalScopeForVarDecl(VD);
2371 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2372 }
2373
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002374 if (CS->getHandlerBlock())
2375 addStmt(CS->getHandlerBlock());
2376
2377 CFGBlock* CatchBlock = Block;
2378 if (!CatchBlock)
2379 CatchBlock = createBlock();
2380
2381 CatchBlock->setLabel(CS);
2382
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002383 if (badCFG)
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002384 return 0;
2385
2386 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2387 Block = NULL;
2388
2389 return CatchBlock;
2390}
2391
John McCall5d413782010-12-06 08:20:24 +00002392CFGBlock *CFGBuilder::VisitExprWithCleanups(ExprWithCleanups *E,
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002393 AddStmtChoice asc) {
2394 if (BuildOpts.AddImplicitDtors) {
2395 // If adding implicit destructors visit the full expression for adding
2396 // destructors of temporaries.
2397 VisitForTemporaryDtors(E->getSubExpr());
2398
2399 // Full expression has to be added as CFGStmt so it will be sequenced
2400 // before destructors of it's temporaries.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002401 asc = asc.withAlwaysAdd(true);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002402 }
2403 return Visit(E->getSubExpr(), asc);
2404}
2405
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002406CFGBlock *CFGBuilder::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E,
2407 AddStmtChoice asc) {
2408 if (asc.alwaysAdd()) {
2409 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002410 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002411
2412 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002413 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002414 }
2415 return Visit(E->getSubExpr(), asc);
2416}
2417
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002418CFGBlock *CFGBuilder::VisitCXXConstructExpr(CXXConstructExpr *C,
2419 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002420 autoCreateBlock();
Zhongxing Xufb2f8162010-11-03 11:14:06 +00002421 if (!C->isElidable())
Ted Kremenek8219b822010-12-16 07:46:53 +00002422 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002423
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002424 return VisitChildren(C);
2425}
2426
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002427CFGBlock *CFGBuilder::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E,
2428 AddStmtChoice asc) {
2429 if (asc.alwaysAdd()) {
2430 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002431 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002432 // We do not want to propagate the AlwaysAdd property.
Zhanyong Wanb5d11c12010-11-24 03:28:53 +00002433 asc = asc.withAlwaysAdd(false);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002434 }
2435 return Visit(E->getSubExpr(), asc);
2436}
2437
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002438CFGBlock *CFGBuilder::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *C,
2439 AddStmtChoice asc) {
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002440 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002441 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00002442 return VisitChildren(C);
2443}
2444
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002445CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xu7e612172010-04-13 09:38:01 +00002446 AddStmtChoice asc) {
Zhongxing Xu7e612172010-04-13 09:38:01 +00002447 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002448 appendStmt(Block, C, asc.withAlwaysAdd(true));
Zhongxing Xu7e612172010-04-13 09:38:01 +00002449 return VisitChildren(C);
2450}
2451
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002452CFGBlock *CFGBuilder::VisitImplicitCastExpr(ImplicitCastExpr *E,
2453 AddStmtChoice asc) {
2454 if (asc.alwaysAdd()) {
2455 autoCreateBlock();
Ted Kremenek8219b822010-12-16 07:46:53 +00002456 appendStmt(Block, E, asc);
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002457 }
Ted Kremenek8219b822010-12-16 07:46:53 +00002458 return Visit(E->getSubExpr(), AddStmtChoice());
Zhongxing Xue1dbeb22010-11-01 13:04:58 +00002459}
2460
Ted Kremenekeda180e22007-08-28 19:26:49 +00002461CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump31feda52009-07-17 01:31:16 +00002462 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenekeda180e22007-08-28 19:26:49 +00002463 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump31feda52009-07-17 01:31:16 +00002464
Ted Kremenekeda180e22007-08-28 19:26:49 +00002465 if (!IBlock) {
2466 IBlock = createBlock(false);
2467 cfg->setIndirectGotoBlock(IBlock);
2468 }
Mike Stump31feda52009-07-17 01:31:16 +00002469
Ted Kremenekeda180e22007-08-28 19:26:49 +00002470 // IndirectGoto is a control-flow statement. Thus we stop processing the
2471 // current block and create a new one.
Zhongxing Xu33dfc072010-09-06 07:32:31 +00002472 if (badCFG)
Ted Kremenek93668002009-07-17 22:18:43 +00002473 return 0;
2474
Ted Kremenekeda180e22007-08-28 19:26:49 +00002475 Block = createBlock(false);
2476 Block->setTerminator(I);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002477 addSuccessor(Block, IBlock);
Ted Kremenekeda180e22007-08-28 19:26:49 +00002478 return addStmt(I->getTarget());
2479}
2480
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002481CFGBlock *CFGBuilder::VisitForTemporaryDtors(Stmt *E, bool BindToTemporary) {
2482tryAgain:
2483 if (!E) {
2484 badCFG = true;
2485 return NULL;
2486 }
2487 switch (E->getStmtClass()) {
2488 default:
2489 return VisitChildrenForTemporaryDtors(E);
2490
2491 case Stmt::BinaryOperatorClass:
2492 return VisitBinaryOperatorForTemporaryDtors(cast<BinaryOperator>(E));
2493
2494 case Stmt::CXXBindTemporaryExprClass:
2495 return VisitCXXBindTemporaryExprForTemporaryDtors(
2496 cast<CXXBindTemporaryExpr>(E), BindToTemporary);
2497
John McCallc07a0c72011-02-17 10:25:35 +00002498 case Stmt::BinaryConditionalOperatorClass:
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002499 case Stmt::ConditionalOperatorClass:
2500 return VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002501 cast<AbstractConditionalOperator>(E), BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002502
2503 case Stmt::ImplicitCastExprClass:
2504 // For implicit cast we want BindToTemporary to be passed further.
2505 E = cast<CastExpr>(E)->getSubExpr();
2506 goto tryAgain;
2507
2508 case Stmt::ParenExprClass:
2509 E = cast<ParenExpr>(E)->getSubExpr();
2510 goto tryAgain;
2511 }
2512}
2513
2514CFGBlock *CFGBuilder::VisitChildrenForTemporaryDtors(Stmt *E) {
2515 // When visiting children for destructors we want to visit them in reverse
2516 // order. Because there's no reverse iterator for children must to reverse
2517 // them in helper vector.
2518 typedef llvm::SmallVector<Stmt *, 4> ChildrenVect;
2519 ChildrenVect ChildrenRev;
John McCall8322c3a2011-02-13 04:07:26 +00002520 for (Stmt::child_range I = E->children(); I; ++I) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002521 if (*I) ChildrenRev.push_back(*I);
2522 }
2523
2524 CFGBlock *B = Block;
2525 for (ChildrenVect::reverse_iterator I = ChildrenRev.rbegin(),
2526 L = ChildrenRev.rend(); I != L; ++I) {
2527 if (CFGBlock *R = VisitForTemporaryDtors(*I))
2528 B = R;
2529 }
2530 return B;
2531}
2532
2533CFGBlock *CFGBuilder::VisitBinaryOperatorForTemporaryDtors(BinaryOperator *E) {
2534 if (E->isLogicalOp()) {
2535 // Destructors for temporaries in LHS expression should be called after
2536 // those for RHS expression. Even if this will unnecessarily create a block,
2537 // this block will be used at least by the full expression.
2538 autoCreateBlock();
2539 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getLHS());
2540 if (badCFG)
2541 return NULL;
2542
2543 Succ = ConfluenceBlock;
2544 Block = NULL;
2545 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2546
2547 if (RHSBlock) {
2548 if (badCFG)
2549 return NULL;
2550
2551 // If RHS expression did produce destructors we need to connect created
2552 // blocks to CFG in same manner as for binary operator itself.
2553 CFGBlock *LHSBlock = createBlock(false);
2554 LHSBlock->setTerminator(CFGTerminator(E, true));
2555
2556 // For binary operator LHS block is before RHS in list of predecessors
2557 // of ConfluenceBlock.
2558 std::reverse(ConfluenceBlock->pred_begin(),
2559 ConfluenceBlock->pred_end());
2560
2561 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002562 TryResult KnownVal = tryEvaluateBool(E->getLHS());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002563 if (KnownVal.isKnown() && (E->getOpcode() == BO_LOr))
2564 KnownVal.negate();
2565
2566 // Link LHSBlock with RHSBlock exactly the same way as for binary operator
2567 // itself.
2568 if (E->getOpcode() == BO_LOr) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002569 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
2570 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002571 } else {
2572 assert (E->getOpcode() == BO_LAnd);
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002573 addSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
2574 addSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002575 }
2576
2577 Block = LHSBlock;
2578 return LHSBlock;
2579 }
2580
2581 Block = ConfluenceBlock;
2582 return ConfluenceBlock;
2583 }
2584
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002585 if (E->isAssignmentOp()) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002586 // For assignment operator (=) LHS expression is visited
2587 // before RHS expression. For destructors visit them in reverse order.
2588 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2589 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2590 return LHSBlock ? LHSBlock : RHSBlock;
2591 }
2592
2593 // For any other binary operator RHS expression is visited before
2594 // LHS expression (order of children). For destructors visit them in reverse
2595 // order.
2596 CFGBlock *LHSBlock = VisitForTemporaryDtors(E->getLHS());
2597 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getRHS());
2598 return RHSBlock ? RHSBlock : LHSBlock;
2599}
2600
2601CFGBlock *CFGBuilder::VisitCXXBindTemporaryExprForTemporaryDtors(
2602 CXXBindTemporaryExpr *E, bool BindToTemporary) {
2603 // First add destructors for temporaries in subexpression.
2604 CFGBlock *B = VisitForTemporaryDtors(E->getSubExpr());
Zhongxing Xufee455f2010-11-14 15:23:50 +00002605 if (!BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002606 // If lifetime of temporary is not prolonged (by assigning to constant
2607 // reference) add destructor for it.
2608 autoCreateBlock();
2609 appendTemporaryDtor(Block, E);
2610 B = Block;
2611 }
2612 return B;
2613}
2614
2615CFGBlock *CFGBuilder::VisitConditionalOperatorForTemporaryDtors(
John McCallc07a0c72011-02-17 10:25:35 +00002616 AbstractConditionalOperator *E, bool BindToTemporary) {
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002617 // First add destructors for condition expression. Even if this will
2618 // unnecessarily create a block, this block will be used at least by the full
2619 // expression.
2620 autoCreateBlock();
2621 CFGBlock *ConfluenceBlock = VisitForTemporaryDtors(E->getCond());
2622 if (badCFG)
2623 return NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002624 if (BinaryConditionalOperator *BCO
2625 = dyn_cast<BinaryConditionalOperator>(E)) {
2626 ConfluenceBlock = VisitForTemporaryDtors(BCO->getCommon());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002627 if (badCFG)
2628 return NULL;
2629 }
2630
John McCallc07a0c72011-02-17 10:25:35 +00002631 // Try to add block with destructors for LHS expression.
2632 CFGBlock *LHSBlock = NULL;
2633 Succ = ConfluenceBlock;
2634 Block = NULL;
2635 LHSBlock = VisitForTemporaryDtors(E->getTrueExpr(), BindToTemporary);
2636 if (badCFG)
2637 return NULL;
2638
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002639 // Try to add block with destructors for RHS expression;
2640 Succ = ConfluenceBlock;
2641 Block = NULL;
John McCallc07a0c72011-02-17 10:25:35 +00002642 CFGBlock *RHSBlock = VisitForTemporaryDtors(E->getFalseExpr(),
2643 BindToTemporary);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002644 if (badCFG)
2645 return NULL;
2646
2647 if (!RHSBlock && !LHSBlock) {
2648 // If neither LHS nor RHS expression had temporaries to destroy don't create
2649 // more blocks.
2650 Block = ConfluenceBlock;
2651 return Block;
2652 }
2653
2654 Block = createBlock(false);
2655 Block->setTerminator(CFGTerminator(E, true));
2656
2657 // See if this is a known constant.
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002658 const TryResult &KnownVal = tryEvaluateBool(E->getCond());
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002659
2660 if (LHSBlock) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002661 addSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002662 } else if (KnownVal.isFalse()) {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002663 addSuccessor(Block, NULL);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002664 } else {
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002665 addSuccessor(Block, ConfluenceBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002666 std::reverse(ConfluenceBlock->pred_begin(), ConfluenceBlock->pred_end());
2667 }
2668
2669 if (!RHSBlock)
2670 RHSBlock = ConfluenceBlock;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002671 addSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00002672
2673 return Block;
2674}
2675
Ted Kremenek04cca642007-08-23 21:26:19 +00002676} // end anonymous namespace
Ted Kremenek889073f2007-08-23 16:51:22 +00002677
Mike Stump31feda52009-07-17 01:31:16 +00002678/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2679/// no successors or predecessors. If this is the first block created in the
2680/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek813dd672007-09-05 20:02:05 +00002681CFGBlock* CFG::createBlock() {
Ted Kremenek889073f2007-08-23 16:51:22 +00002682 bool first_block = begin() == end();
2683
2684 // Create the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002685 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2686 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2687 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek889073f2007-08-23 16:51:22 +00002688
2689 // If this is the first block, set it as the Entry and Exit.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002690 if (first_block)
2691 Entry = Exit = &back();
Ted Kremenek889073f2007-08-23 16:51:22 +00002692
2693 // Return the block.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002694 return &back();
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00002695}
2696
Ted Kremenek889073f2007-08-23 16:51:22 +00002697/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2698/// CFG is returned to the caller.
Mike Stump6bf1c082010-01-21 02:21:40 +00002699CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002700 BuildOptions BO) {
Ted Kremenek889073f2007-08-23 16:51:22 +00002701 CFGBuilder Builder;
Ted Kremeneke97b1eb2010-09-14 23:41:16 +00002702 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek889073f2007-08-23 16:51:22 +00002703}
2704
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002705//===----------------------------------------------------------------------===//
2706// CFG: Queries for BlkExprs.
2707//===----------------------------------------------------------------------===//
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002708
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002709namespace {
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002710 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002711}
2712
Ted Kremenekbff98442009-12-23 23:37:10 +00002713static void FindSubExprAssignments(Stmt *S,
2714 llvm::SmallPtrSet<Expr*,50>& Set) {
2715 if (!S)
Ted Kremenek95a123c2008-01-26 00:03:27 +00002716 return;
Mike Stump31feda52009-07-17 01:31:16 +00002717
John McCall8322c3a2011-02-13 04:07:26 +00002718 for (Stmt::child_range I = S->children(); I; ++I) {
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002719 Stmt *child = *I;
Ted Kremenekbff98442009-12-23 23:37:10 +00002720 if (!child)
2721 continue;
Ted Kremenekdc03bd02010-08-02 23:46:59 +00002722
Ted Kremenekbff98442009-12-23 23:37:10 +00002723 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002724 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump31feda52009-07-17 01:31:16 +00002725
Ted Kremenekbff98442009-12-23 23:37:10 +00002726 FindSubExprAssignments(child, Set);
Ted Kremenek95a123c2008-01-26 00:03:27 +00002727 }
2728}
2729
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002730static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2731 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump31feda52009-07-17 01:31:16 +00002732
2733 // Look for assignments that are used as subexpressions. These are the only
2734 // assignments that we want to *possibly* register as a block-level
2735 // expression. Basically, if an assignment occurs both in a subexpression and
2736 // at the block-level, it is a block-level expression.
Ted Kremenek95a123c2008-01-26 00:03:27 +00002737 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump31feda52009-07-17 01:31:16 +00002738
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002739 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002740 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002741 if (CFGStmt S = BI->getAs<CFGStmt>())
2742 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002743
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002744 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump31feda52009-07-17 01:31:16 +00002745
2746 // Iterate over the statements again on identify the Expr* and Stmt* at the
2747 // block-level that are block-level expressions.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002748
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002749 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2750 CFGStmt CS = BI->getAs<CFGStmt>();
2751 if (!CS.isValid())
2752 continue;
2753 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump31feda52009-07-17 01:31:16 +00002754
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002755 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002756 // Assignment expressions that are not nested within another
Mike Stump31feda52009-07-17 01:31:16 +00002757 // expression are really "statements" whose value is never used by
2758 // another expression.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002759 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek95a123c2008-01-26 00:03:27 +00002760 continue;
Mike Stump31feda52009-07-17 01:31:16 +00002761 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2762 // Special handling for statement expressions. The last statement in
2763 // the statement expression is also a block-level expr.
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002764 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002765 if (!C->body_empty()) {
Ted Kremenek95a123c2008-01-26 00:03:27 +00002766 unsigned x = M->size();
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002767 (*M)[C->body_back()] = x;
2768 }
2769 }
Ted Kremenek0cb1ba22008-01-25 23:22:27 +00002770
Ted Kremenek95a123c2008-01-26 00:03:27 +00002771 unsigned x = M->size();
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002772 (*M)[Exp] = x;
Ted Kremenek95a123c2008-01-26 00:03:27 +00002773 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002774 }
Mike Stump31feda52009-07-17 01:31:16 +00002775
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002776 // Look at terminators. The condition is a block-level expression.
Mike Stump31feda52009-07-17 01:31:16 +00002777
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002778 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump31feda52009-07-17 01:31:16 +00002779
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002780 if (S && M->find(S) == M->end()) {
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002781 unsigned x = M->size();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00002782 (*M)[S] = x;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002783 }
2784 }
Mike Stump31feda52009-07-17 01:31:16 +00002785
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002786 return M;
2787}
2788
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002789CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2790 assert(S != NULL);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002791 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump31feda52009-07-17 01:31:16 +00002792
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002793 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek85be7cf2008-01-17 20:48:37 +00002794 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek60983dc2010-01-19 20:52:05 +00002795 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002796}
2797
2798unsigned CFG::getNumBlkExprs() {
2799 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2800 return M->size();
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002801
2802 // We assume callers interested in the number of BlkExprs will want
2803 // the map constructed if it doesn't already exist.
2804 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2805 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002806}
2807
Ted Kremenek6065ef62008-04-28 18:00:46 +00002808//===----------------------------------------------------------------------===//
Ted Kremenekb0371852010-09-09 00:06:04 +00002809// Filtered walking of the CFG.
2810//===----------------------------------------------------------------------===//
2811
2812bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekf146cd12010-09-09 02:57:48 +00002813 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002814
2815 if (F.IgnoreDefaultsWithCoveredEnums) {
2816 // If the 'To' has no label or is labeled but the label isn't a
2817 // CaseStmt then filter this edge.
2818 if (const SwitchStmt *S =
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00002819 dyn_cast_or_null<SwitchStmt>(From->getTerminator().getStmt())) {
Ted Kremenekb0371852010-09-09 00:06:04 +00002820 if (S->isAllEnumCasesCovered()) {
Ted Kremenekf146cd12010-09-09 02:57:48 +00002821 const Stmt *L = To->getLabel();
2822 if (!L || !isa<CaseStmt>(L))
2823 return true;
Ted Kremenekb0371852010-09-09 00:06:04 +00002824 }
2825 }
2826 }
2827
2828 return false;
2829}
2830
2831//===----------------------------------------------------------------------===//
Ted Kremenek6065ef62008-04-28 18:00:46 +00002832// Cleanup: CFG dstor.
2833//===----------------------------------------------------------------------===//
2834
Ted Kremenekf2d4372b2007-10-01 19:33:33 +00002835CFG::~CFG() {
2836 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2837}
Mike Stump31feda52009-07-17 01:31:16 +00002838
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00002839//===----------------------------------------------------------------------===//
2840// CFG pretty printing
2841//===----------------------------------------------------------------------===//
2842
Ted Kremenek7e776b12007-08-22 18:22:34 +00002843namespace {
2844
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002845class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002846 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002847 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002848 StmtMapTy StmtMap;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002849 DeclMapTy DeclMap;
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002850 signed currentBlock;
2851 unsigned currentStmt;
Chris Lattnerc61089a2009-06-30 01:26:17 +00002852 const LangOptions &LangOpts;
Ted Kremenek9aae5132007-08-23 21:42:29 +00002853public:
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002854
Chris Lattnerc61089a2009-06-30 01:26:17 +00002855 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002856 : currentBlock(0), currentStmt(0), LangOpts(LO) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002857 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2858 unsigned j = 1;
Ted Kremenek289ae4f2009-10-12 20:55:07 +00002859 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002860 BI != BEnd; ++BI, ++j ) {
2861 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2862 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2863 StmtMap[SE] = P;
2864
2865 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2866 DeclMap[DS->getSingleDecl()] = P;
Zhanyong Wan59f09c72010-11-22 19:32:14 +00002867
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002868 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2869 if (VarDecl* VD = IS->getConditionVariable())
2870 DeclMap[VD] = P;
2871
2872 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2873 if (VarDecl* VD = FS->getConditionVariable())
2874 DeclMap[VD] = P;
2875
2876 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2877 if (VarDecl* VD = WS->getConditionVariable())
2878 DeclMap[VD] = P;
2879
2880 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2881 if (VarDecl* VD = SS->getConditionVariable())
2882 DeclMap[VD] = P;
2883
2884 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2885 if (VarDecl* VD = CS->getExceptionDecl())
2886 DeclMap[VD] = P;
2887 }
2888 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002889 }
Zhongxing Xu2cd7a782010-09-16 01:25:47 +00002890 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002891 }
Mike Stump31feda52009-07-17 01:31:16 +00002892
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002893 virtual ~StmtPrinterHelper() {}
Mike Stump31feda52009-07-17 01:31:16 +00002894
Chris Lattnerc61089a2009-06-30 01:26:17 +00002895 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002896 void setBlockID(signed i) { currentBlock = i; }
2897 void setStmtID(unsigned i) { currentStmt = i; }
Mike Stump31feda52009-07-17 01:31:16 +00002898
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002899 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2900 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002901
2902 if (I == StmtMap.end())
2903 return false;
Mike Stump31feda52009-07-17 01:31:16 +00002904
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002905 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2906 && I->second.second == currentStmt) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002907 return false;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002908 }
Mike Stump31feda52009-07-17 01:31:16 +00002909
Ted Kremenek60983dc2010-01-19 20:52:05 +00002910 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002911 return true;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002912 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002913
2914 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2915 DeclMapTy::iterator I = DeclMap.find(D);
2916
2917 if (I == DeclMap.end())
2918 return false;
2919
Ted Kremenek3a9a2a52010-12-17 04:44:39 +00002920 if (currentBlock >= 0 && I->second.first == (unsigned) currentBlock
2921 && I->second.second == currentStmt) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00002922 return false;
2923 }
2924
2925 OS << "[B" << I->second.first << "." << I->second.second << "]";
2926 return true;
2927 }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002928};
Chris Lattnerc61089a2009-06-30 01:26:17 +00002929} // end anonymous namespace
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002930
Chris Lattnerc61089a2009-06-30 01:26:17 +00002931
2932namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002933class CFGBlockTerminatorPrint
Ted Kremenek83ebcef2008-01-08 18:15:10 +00002934 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump31feda52009-07-17 01:31:16 +00002935
Ted Kremenek2d470fc2008-09-13 05:16:45 +00002936 llvm::raw_ostream& OS;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002937 StmtPrinterHelper* Helper;
Douglas Gregor7de59662009-05-29 20:38:28 +00002938 PrintingPolicy Policy;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00002939public:
Douglas Gregor7de59662009-05-29 20:38:28 +00002940 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnerc61089a2009-06-30 01:26:17 +00002941 const PrintingPolicy &Policy)
Douglas Gregor7de59662009-05-29 20:38:28 +00002942 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump31feda52009-07-17 01:31:16 +00002943
Ted Kremenek9aae5132007-08-23 21:42:29 +00002944 void VisitIfStmt(IfStmt* I) {
2945 OS << "if ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002946 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002947 }
Mike Stump31feda52009-07-17 01:31:16 +00002948
Ted Kremenek9aae5132007-08-23 21:42:29 +00002949 // Default case.
Mike Stump31feda52009-07-17 01:31:16 +00002950 void VisitStmt(Stmt* Terminator) {
2951 Terminator->printPretty(OS, Helper, Policy);
2952 }
2953
Ted Kremenek9aae5132007-08-23 21:42:29 +00002954 void VisitForStmt(ForStmt* F) {
2955 OS << "for (" ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002956 if (F->getInit())
2957 OS << "...";
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002958 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002959 if (Stmt* C = F->getCond())
2960 C->printPretty(OS, Helper, Policy);
Ted Kremenekfc7aafc2007-08-30 21:28:02 +00002961 OS << "; ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002962 if (F->getInc())
2963 OS << "...";
Ted Kremenek15647632008-01-30 23:02:42 +00002964 OS << ")";
Ted Kremenek9aae5132007-08-23 21:42:29 +00002965 }
Mike Stump31feda52009-07-17 01:31:16 +00002966
Ted Kremenek9aae5132007-08-23 21:42:29 +00002967 void VisitWhileStmt(WhileStmt* W) {
2968 OS << "while " ;
Ted Kremenek60983dc2010-01-19 20:52:05 +00002969 if (Stmt* C = W->getCond())
2970 C->printPretty(OS, Helper, Policy);
Ted Kremenek9aae5132007-08-23 21:42:29 +00002971 }
Mike Stump31feda52009-07-17 01:31:16 +00002972
Ted Kremenek9aae5132007-08-23 21:42:29 +00002973 void VisitDoStmt(DoStmt* D) {
2974 OS << "do ... while ";
Ted Kremenek60983dc2010-01-19 20:52:05 +00002975 if (Stmt* C = D->getCond())
2976 C->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002977 }
Mike Stump31feda52009-07-17 01:31:16 +00002978
Ted Kremenekc1f9a282008-04-16 21:10:48 +00002979 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9e248872007-08-27 21:27:44 +00002980 OS << "switch ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002981 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9e248872007-08-27 21:27:44 +00002982 }
Mike Stump31feda52009-07-17 01:31:16 +00002983
Mike Stumpbbf5ba62010-01-19 02:20:09 +00002984 void VisitCXXTryStmt(CXXTryStmt* CS) {
2985 OS << "try ...";
2986 }
2987
John McCallc07a0c72011-02-17 10:25:35 +00002988 void VisitAbstractConditionalOperator(AbstractConditionalOperator* C) {
Douglas Gregor7de59662009-05-29 20:38:28 +00002989 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00002990 OS << " ? ... : ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00002991 }
Mike Stump31feda52009-07-17 01:31:16 +00002992
Ted Kremenek391f94a2007-08-31 22:29:13 +00002993 void VisitChooseExpr(ChooseExpr* C) {
2994 OS << "__builtin_choose_expr( ";
Douglas Gregor7de59662009-05-29 20:38:28 +00002995 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek15647632008-01-30 23:02:42 +00002996 OS << " )";
Ted Kremenek391f94a2007-08-31 22:29:13 +00002997 }
Mike Stump31feda52009-07-17 01:31:16 +00002998
Ted Kremenekf8b50e92007-08-31 22:26:13 +00002999 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
3000 OS << "goto *";
Douglas Gregor7de59662009-05-29 20:38:28 +00003001 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003002 }
Mike Stump31feda52009-07-17 01:31:16 +00003003
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003004 void VisitBinaryOperator(BinaryOperator* B) {
3005 if (!B->isLogicalOp()) {
3006 VisitExpr(B);
3007 return;
3008 }
Mike Stump31feda52009-07-17 01:31:16 +00003009
Douglas Gregor7de59662009-05-29 20:38:28 +00003010 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003011
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003012 switch (B->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +00003013 case BO_LOr:
Ted Kremenek15647632008-01-30 23:02:42 +00003014 OS << " || ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003015 return;
John McCalle3027922010-08-25 11:45:40 +00003016 case BO_LAnd:
Ted Kremenek15647632008-01-30 23:02:42 +00003017 OS << " && ...";
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003018 return;
3019 default:
3020 assert(false && "Invalid logical operator.");
Mike Stump31feda52009-07-17 01:31:16 +00003021 }
Ted Kremenek7f7dd762007-08-31 21:49:40 +00003022 }
Mike Stump31feda52009-07-17 01:31:16 +00003023
Ted Kremenek12687ff2007-08-27 21:54:41 +00003024 void VisitExpr(Expr* E) {
Douglas Gregor7de59662009-05-29 20:38:28 +00003025 E->printPretty(OS, Helper, Policy);
Mike Stump31feda52009-07-17 01:31:16 +00003026 }
Ted Kremenek9aae5132007-08-23 21:42:29 +00003027};
Chris Lattnerc61089a2009-06-30 01:26:17 +00003028} // end anonymous namespace
3029
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003030static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump92244b02010-01-19 22:00:14 +00003031 const CFGElement &E) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003032 if (CFGStmt CS = E.getAs<CFGStmt>()) {
3033 Stmt *S = CS;
3034
3035 if (Helper) {
Mike Stump31feda52009-07-17 01:31:16 +00003036
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003037 // special printing for statement-expressions.
3038 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
3039 CompoundStmt* Sub = SE->getSubStmt();
3040
John McCall8322c3a2011-02-13 04:07:26 +00003041 if (Sub->children()) {
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003042 OS << "({ ... ; ";
3043 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
3044 OS << " })\n";
3045 return;
3046 }
3047 }
3048 // special printing for comma expressions.
3049 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
3050 if (B->getOpcode() == BO_Comma) {
3051 OS << "... , ";
3052 Helper->handledStmt(B->getRHS(),OS);
3053 OS << '\n';
3054 return;
3055 }
Ted Kremenekf8b50e92007-08-31 22:26:13 +00003056 }
3057 }
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003058 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump31feda52009-07-17 01:31:16 +00003059
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003060 if (isa<CXXOperatorCallExpr>(S)) {
Zhanyong Wan59f09c72010-11-22 19:32:14 +00003061 OS << " (OperatorCall)";
3062 } else if (isa<CXXBindTemporaryExpr>(S)) {
3063 OS << " (BindTemporary)";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003064 }
Mike Stump31feda52009-07-17 01:31:16 +00003065
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003066 // Expressions need a newline.
3067 if (isa<Expr>(S))
3068 OS << '\n';
Ted Kremenek0f5d8bc2010-08-31 18:47:37 +00003069
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003070 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
Alexis Hunt1d792652011-01-08 20:30:50 +00003071 CXXCtorInitializer* I = IE;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003072 if (I->isBaseInitializer())
3073 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
Francois Pichetd583da02010-12-04 09:14:42 +00003074 else OS << I->getAnyMember()->getName();
Mike Stump31feda52009-07-17 01:31:16 +00003075
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003076 OS << "(";
3077 if (Expr* IE = I->getInit())
3078 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
3079 OS << ")";
3080
3081 if (I->isBaseInitializer())
3082 OS << " (Base initializer)\n";
3083 else OS << " (Member initializer)\n";
3084
3085 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
3086 VarDecl* VD = DE.getVarDecl();
3087 Helper->handleDecl(VD, OS);
3088
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003089 const Type* T = VD->getType().getTypePtr();
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003090 if (const ReferenceType* RT = T->getAs<ReferenceType>())
3091 T = RT->getPointeeType().getTypePtr();
Marcin Swiderski52e4bc12010-10-25 07:00:40 +00003092 else if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3093 T = ET;
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003094
3095 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
3096 OS << " (Implicit destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003097
3098 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
3099 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
3100 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003101 OS << " (Base object destructor)\n";
Marcin Swiderski20b88732010-10-05 05:37:00 +00003102
3103 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
3104 FieldDecl *FD = ME.getFieldDecl();
Marcin Swiderski01769902010-10-25 07:05:54 +00003105
3106 const Type *T = FD->getType().getTypePtr();
3107 if (const Type *ET = T->getArrayElementTypeNoTypeQual())
3108 T = ET;
3109
Marcin Swiderski20b88732010-10-05 05:37:00 +00003110 OS << "this->" << FD->getName();
Marcin Swiderski01769902010-10-25 07:05:54 +00003111 OS << ".~" << T->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu614e17d2010-10-05 08:38:06 +00003112 OS << " (Member object destructor)\n";
Marcin Swiderski3ab17ad2010-11-03 06:19:35 +00003113
3114 } else if (CFGTemporaryDtor TE = E.getAs<CFGTemporaryDtor>()) {
3115 CXXBindTemporaryExpr *BT = TE.getBindTemporaryExpr();
3116 OS << "~" << BT->getType()->getAsCXXRecordDecl()->getName() << "()";
3117 OS << " (Temporary object destructor)\n";
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003118 }
Zhongxing Xu0b51d4d2010-11-01 06:46:05 +00003119}
Mike Stump31feda52009-07-17 01:31:16 +00003120
Chris Lattnerc61089a2009-06-30 01:26:17 +00003121static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
3122 const CFGBlock& B,
3123 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump31feda52009-07-17 01:31:16 +00003124
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003125 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump31feda52009-07-17 01:31:16 +00003126
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003127 // Print the header.
Mike Stump31feda52009-07-17 01:31:16 +00003128 OS << "\n [ B" << B.getBlockID();
3129
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003130 if (&B == &cfg->getEntry())
3131 OS << " (ENTRY) ]\n";
3132 else if (&B == &cfg->getExit())
3133 OS << " (EXIT) ]\n";
3134 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003135 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003136 else
3137 OS << " ]\n";
Mike Stump31feda52009-07-17 01:31:16 +00003138
Ted Kremenek71eca012007-08-29 23:20:49 +00003139 // Print the label of this block.
Mike Stump92244b02010-01-19 22:00:14 +00003140 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003141
3142 if (print_edges)
3143 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003144
Mike Stump92244b02010-01-19 22:00:14 +00003145 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003146 OS << L->getName();
Mike Stump92244b02010-01-19 22:00:14 +00003147 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek71eca012007-08-29 23:20:49 +00003148 OS << "case ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003149 C->getLHS()->printPretty(OS, Helper,
3150 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003151 if (C->getRHS()) {
3152 OS << " ... ";
Chris Lattnerc61089a2009-06-30 01:26:17 +00003153 C->getRHS()->printPretty(OS, Helper,
3154 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek71eca012007-08-29 23:20:49 +00003155 }
Mike Stump92244b02010-01-19 22:00:14 +00003156 } else if (isa<DefaultStmt>(Label))
Ted Kremenek71eca012007-08-29 23:20:49 +00003157 OS << "default";
Mike Stump92244b02010-01-19 22:00:14 +00003158 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003159 OS << "catch (";
Mike Stump0bdba6c2010-01-20 01:15:34 +00003160 if (CS->getExceptionDecl())
3161 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
3162 0);
3163 else
3164 OS << "...";
Mike Stumpbbf5ba62010-01-19 02:20:09 +00003165 OS << ")";
3166
3167 } else
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003168 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump31feda52009-07-17 01:31:16 +00003169
Ted Kremenek71eca012007-08-29 23:20:49 +00003170 OS << ":\n";
3171 }
Mike Stump31feda52009-07-17 01:31:16 +00003172
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003173 // Iterate through the statements in the block and print them.
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003174 unsigned j = 1;
Mike Stump31feda52009-07-17 01:31:16 +00003175
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003176 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
3177 I != E ; ++I, ++j ) {
Mike Stump31feda52009-07-17 01:31:16 +00003178
Ted Kremenek71eca012007-08-29 23:20:49 +00003179 // Print the statement # in the basic block and the statement itself.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003180 if (print_edges)
3181 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003182
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003183 OS << llvm::format("%3d", j) << ": ";
Mike Stump31feda52009-07-17 01:31:16 +00003184
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003185 if (Helper)
3186 Helper->setStmtID(j);
Mike Stump31feda52009-07-17 01:31:16 +00003187
Marcin Swiderskic0ca7312010-09-21 05:58:15 +00003188 print_elem(OS,Helper,*I);
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003189 }
Mike Stump31feda52009-07-17 01:31:16 +00003190
Ted Kremenek71eca012007-08-29 23:20:49 +00003191 // Print the terminator of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003192 if (B.getTerminator()) {
3193 if (print_edges)
3194 OS << " ";
Mike Stump31feda52009-07-17 01:31:16 +00003195
Ted Kremenek71eca012007-08-29 23:20:49 +00003196 OS << " T: ";
Mike Stump31feda52009-07-17 01:31:16 +00003197
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003198 if (Helper) Helper->setBlockID(-1);
Mike Stump31feda52009-07-17 01:31:16 +00003199
Chris Lattnerc61089a2009-06-30 01:26:17 +00003200 CFGBlockTerminatorPrint TPrinter(OS, Helper,
3201 PrintingPolicy(Helper->getLangOpts()));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003202 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003203 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003204 }
Mike Stump31feda52009-07-17 01:31:16 +00003205
Ted Kremenek71eca012007-08-29 23:20:49 +00003206 if (print_edges) {
3207 // Print the predecessors of this block.
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003208 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek71eca012007-08-29 23:20:49 +00003209 unsigned i = 0;
Ted Kremenek71eca012007-08-29 23:20:49 +00003210
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003211 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
3212 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003213
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003214 if (i == 8 || (i-8) == 0)
3215 OS << "\n ";
Mike Stump31feda52009-07-17 01:31:16 +00003216
Ted Kremenek71eca012007-08-29 23:20:49 +00003217 OS << " B" << (*I)->getBlockID();
3218 }
Mike Stump31feda52009-07-17 01:31:16 +00003219
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003220 OS << '\n';
Mike Stump31feda52009-07-17 01:31:16 +00003221
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003222 // Print the successors of this block.
3223 OS << " Successors (" << B.succ_size() << "):";
3224 i = 0;
3225
3226 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
3227 I != E; ++I, ++i) {
Mike Stump31feda52009-07-17 01:31:16 +00003228
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003229 if (i == 8 || (i-8) % 10 == 0)
3230 OS << "\n ";
3231
Mike Stump0d76d072009-07-20 23:24:15 +00003232 if (*I)
3233 OS << " B" << (*I)->getBlockID();
3234 else
3235 OS << " NULL";
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003236 }
Mike Stump31feda52009-07-17 01:31:16 +00003237
Ted Kremenek71eca012007-08-29 23:20:49 +00003238 OS << '\n';
Ted Kremenek4aa1e8b2007-08-21 21:42:03 +00003239 }
Mike Stump31feda52009-07-17 01:31:16 +00003240}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003241
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003242
3243/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003244void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003245
3246/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003247void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
3248 StmtPrinterHelper Helper(this, LO);
Mike Stump31feda52009-07-17 01:31:16 +00003249
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003250 // Print the entry block.
3251 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump31feda52009-07-17 01:31:16 +00003252
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003253 // Iterate through the CFGBlocks and print them one by one.
3254 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
3255 // Skip the entry block, because we already printed it.
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003256 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003257 continue;
Mike Stump31feda52009-07-17 01:31:16 +00003258
Ted Kremenek289ae4f2009-10-12 20:55:07 +00003259 print_block(OS, this, **I, &Helper, true);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003260 }
Mike Stump31feda52009-07-17 01:31:16 +00003261
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003262 // Print the exit block.
3263 print_block(OS, this, getExit(), &Helper, true);
Ted Kremeneke03879b2008-11-24 20:50:24 +00003264 OS.flush();
Mike Stump31feda52009-07-17 01:31:16 +00003265}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003266
3267/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003268void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
3269 print(llvm::errs(), cfg, LO);
3270}
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003271
3272/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
3273/// Generally this will only be called from CFG::print.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003274void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
3275 const LangOptions &LO) const {
3276 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003277 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek889073f2007-08-23 16:51:22 +00003278}
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003279
Ted Kremenek15647632008-01-30 23:02:42 +00003280/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnerc61089a2009-06-30 01:26:17 +00003281void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump31feda52009-07-17 01:31:16 +00003282 const LangOptions &LO) const {
Chris Lattnerc61089a2009-06-30 01:26:17 +00003283 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003284 TPrinter.Visit(const_cast<Stmt*>(getTerminator().getStmt()));
Ted Kremenek15647632008-01-30 23:02:42 +00003285}
3286
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003287Stmt* CFGBlock::getTerminatorCondition() {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003288 Stmt *Terminator = this->Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003289 if (!Terminator)
3290 return NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003291
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003292 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003293
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003294 switch (Terminator->getStmtClass()) {
3295 default:
3296 break;
Mike Stump31feda52009-07-17 01:31:16 +00003297
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003298 case Stmt::ForStmtClass:
3299 E = cast<ForStmt>(Terminator)->getCond();
3300 break;
Mike Stump31feda52009-07-17 01:31:16 +00003301
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003302 case Stmt::WhileStmtClass:
3303 E = cast<WhileStmt>(Terminator)->getCond();
3304 break;
Mike Stump31feda52009-07-17 01:31:16 +00003305
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003306 case Stmt::DoStmtClass:
3307 E = cast<DoStmt>(Terminator)->getCond();
3308 break;
Mike Stump31feda52009-07-17 01:31:16 +00003309
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003310 case Stmt::IfStmtClass:
3311 E = cast<IfStmt>(Terminator)->getCond();
3312 break;
Mike Stump31feda52009-07-17 01:31:16 +00003313
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003314 case Stmt::ChooseExprClass:
3315 E = cast<ChooseExpr>(Terminator)->getCond();
3316 break;
Mike Stump31feda52009-07-17 01:31:16 +00003317
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003318 case Stmt::IndirectGotoStmtClass:
3319 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
3320 break;
Mike Stump31feda52009-07-17 01:31:16 +00003321
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003322 case Stmt::SwitchStmtClass:
3323 E = cast<SwitchStmt>(Terminator)->getCond();
3324 break;
Mike Stump31feda52009-07-17 01:31:16 +00003325
John McCallc07a0c72011-02-17 10:25:35 +00003326 case Stmt::BinaryConditionalOperatorClass:
3327 E = cast<BinaryConditionalOperator>(Terminator)->getCond();
3328 break;
3329
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003330 case Stmt::ConditionalOperatorClass:
3331 E = cast<ConditionalOperator>(Terminator)->getCond();
3332 break;
Mike Stump31feda52009-07-17 01:31:16 +00003333
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003334 case Stmt::BinaryOperatorClass: // '&&' and '||'
3335 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003336 break;
Mike Stump31feda52009-07-17 01:31:16 +00003337
Ted Kremenek6d8b46e2008-11-12 21:11:49 +00003338 case Stmt::ObjCForCollectionStmtClass:
Mike Stump31feda52009-07-17 01:31:16 +00003339 return Terminator;
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003340 }
Mike Stump31feda52009-07-17 01:31:16 +00003341
Ted Kremenekc1f9a282008-04-16 21:10:48 +00003342 return E ? E->IgnoreParens() : NULL;
3343}
3344
Ted Kremenek92137a32008-05-16 16:06:00 +00003345bool CFGBlock::hasBinaryBranchTerminator() const {
Marcin Swiderskia7d84a72010-10-29 05:21:47 +00003346 const Stmt *Terminator = this->Terminator;
Ted Kremenek92137a32008-05-16 16:06:00 +00003347 if (!Terminator)
3348 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003349
Ted Kremenek92137a32008-05-16 16:06:00 +00003350 Expr* E = NULL;
Mike Stump31feda52009-07-17 01:31:16 +00003351
Ted Kremenek92137a32008-05-16 16:06:00 +00003352 switch (Terminator->getStmtClass()) {
3353 default:
3354 return false;
Mike Stump31feda52009-07-17 01:31:16 +00003355
3356 case Stmt::ForStmtClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003357 case Stmt::WhileStmtClass:
3358 case Stmt::DoStmtClass:
3359 case Stmt::IfStmtClass:
3360 case Stmt::ChooseExprClass:
John McCallc07a0c72011-02-17 10:25:35 +00003361 case Stmt::BinaryConditionalOperatorClass:
Ted Kremenek92137a32008-05-16 16:06:00 +00003362 case Stmt::ConditionalOperatorClass:
3363 case Stmt::BinaryOperatorClass:
Mike Stump31feda52009-07-17 01:31:16 +00003364 return true;
Ted Kremenek92137a32008-05-16 16:06:00 +00003365 }
Mike Stump31feda52009-07-17 01:31:16 +00003366
Ted Kremenek92137a32008-05-16 16:06:00 +00003367 return E ? E->IgnoreParens() : NULL;
3368}
3369
Ted Kremenek15647632008-01-30 23:02:42 +00003370
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003371//===----------------------------------------------------------------------===//
3372// CFG Graphviz Visualization
3373//===----------------------------------------------------------------------===//
3374
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003375
3376#ifndef NDEBUG
Mike Stump31feda52009-07-17 01:31:16 +00003377static StmtPrinterHelper* GraphHelper;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003378#endif
3379
Chris Lattnerc61089a2009-06-30 01:26:17 +00003380void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003381#ifndef NDEBUG
Chris Lattnerc61089a2009-06-30 01:26:17 +00003382 StmtPrinterHelper H(this, LO);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003383 GraphHelper = &H;
3384 llvm::ViewGraph(this,"CFG");
3385 GraphHelper = NULL;
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003386#endif
3387}
3388
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003389namespace llvm {
3390template<>
3391struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser9fc223a2009-11-30 14:16:05 +00003392
3393 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
3394
3395 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003396
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003397#ifndef NDEBUG
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003398 std::string OutSStr;
3399 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek04f3cee2007-08-31 21:30:12 +00003400 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremenek2d470fc2008-09-13 05:16:45 +00003401 std::string& OutStr = Out.str();
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003402
3403 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3404
3405 // Process string output to make it nicer...
3406 for (unsigned i = 0; i != OutStr.length(); ++i)
3407 if (OutStr[i] == '\n') { // Left justify
3408 OutStr[i] = '\\';
3409 OutStr.insert(OutStr.begin()+i+1, 'l');
3410 }
Mike Stump31feda52009-07-17 01:31:16 +00003411
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003412 return OutStr;
Hartmut Kaiser04bd2ef2007-09-16 00:28:28 +00003413#else
3414 return "";
3415#endif
Ted Kremenek4e5f99d2007-08-29 21:56:09 +00003416 }
3417};
3418} // end namespace llvm