blob: 6f7be9a252ce198622ca890ffd6712445c99c80e [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekfddd5182007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenekad5a8942010-08-02 23:46:59 +000038
Ted Kremenek852274d2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5ba290a2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramer792bea92010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000047
Benjamin Kramer792bea92010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek431ac2d2010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5ba290a2010-03-02 21:43:54 +000050
Ted Kremenek852274d2009-12-16 03:18:58 +000051private:
Benjamin Kramer792bea92010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek852274d2009-12-16 03:18:58 +000053};
Mike Stump6d9828c2009-07-17 01:31:16 +000054
Marcin Swiderskif1308c72010-09-25 11:05:21 +000055/// LocalScope - Node in tree of local scopes created for C++ implicit
56/// destructor calls generation. It contains list of automatic variables
57/// declared in the scope and link to position in previous scope this scope
58/// began in.
59///
60/// The process of creating local scopes is as follows:
61/// - Init CFGBuilder::ScopePos with invalid position (equivalent for null),
62/// - Before processing statements in scope (e.g. CompoundStmt) create
63/// LocalScope object using CFGBuilder::ScopePos as link to previous scope
64/// and set CFGBuilder::ScopePos to the end of new scope,
Marcin Swiderski35387a02010-09-30 22:42:32 +000065/// - On every occurrence of VarDecl increase CFGBuilder::ScopePos if it points
Marcin Swiderskif1308c72010-09-25 11:05:21 +000066/// at this VarDecl,
67/// - For every normal (without jump) end of scope add to CFGBlock destructors
68/// for objects in the current scope,
69/// - For every jump add to CFGBlock destructors for objects
70/// between CFGBuilder::ScopePos and local scope position saved for jump
71/// target. Thanks to C++ restrictions on goto jumps we can be sure that
72/// jump target position will be on the path to root from CFGBuilder::ScopePos
73/// (adding any variable that doesn't need constructor to be called to
74/// LocalScope can break this assumption),
75///
76class LocalScope {
77public:
78 typedef llvm::SmallVector<VarDecl*, 4> AutomaticVarsTy;
79
80 /// const_iterator - Iterates local scope backwards and jumps to previous
Marcin Swiderski35387a02010-09-30 22:42:32 +000081 /// scope on reaching the beginning of currently iterated scope.
Marcin Swiderskif1308c72010-09-25 11:05:21 +000082 class const_iterator {
83 const LocalScope* Scope;
84
85 /// VarIter is guaranteed to be greater then 0 for every valid iterator.
86 /// Invalid iterator (with null Scope) has VarIter equal to 0.
87 unsigned VarIter;
88
89 public:
90 /// Create invalid iterator. Dereferencing invalid iterator is not allowed.
91 /// Incrementing invalid iterator is allowed and will result in invalid
92 /// iterator.
93 const_iterator()
94 : Scope(NULL), VarIter(0) {}
95
96 /// Create valid iterator. In case when S.Prev is an invalid iterator and
97 /// I is equal to 0, this will create invalid iterator.
98 const_iterator(const LocalScope& S, unsigned I)
99 : Scope(&S), VarIter(I) {
100 // Iterator to "end" of scope is not allowed. Handle it by going up
101 // in scopes tree possibly up to invalid iterator in the root.
102 if (VarIter == 0 && Scope)
103 *this = Scope->Prev;
104 }
105
106 VarDecl* const* operator->() const {
107 assert (Scope && "Dereferencing invalid iterator is not allowed");
108 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
109 return &Scope->Vars[VarIter - 1];
110 }
111 VarDecl* operator*() const {
112 return *this->operator->();
113 }
114
115 const_iterator& operator++() {
116 if (!Scope)
117 return *this;
118
119 assert (VarIter != 0 && "Iterator has invalid value of VarIter member");
120 --VarIter;
121 if (VarIter == 0)
122 *this = Scope->Prev;
123 return *this;
124 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000125 const_iterator operator++(int) {
126 const_iterator P = *this;
127 ++*this;
128 return P;
129 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000130
131 bool operator==(const const_iterator& rhs) const {
132 return Scope == rhs.Scope && VarIter == rhs.VarIter;
133 }
134 bool operator!=(const const_iterator& rhs) const {
135 return !(*this == rhs);
136 }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000137
138 operator bool() const {
139 return *this != const_iterator();
140 }
141
142 int distance(const_iterator L);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000143 };
144
145 friend class const_iterator;
146
147private:
148 /// Automatic variables in order of declaration.
149 AutomaticVarsTy Vars;
150 /// Iterator to variable in previous scope that was declared just before
151 /// begin of this scope.
152 const_iterator Prev;
153
154public:
155 /// Constructs empty scope linked to previous scope in specified place.
156 LocalScope(const_iterator P)
157 : Vars()
158 , Prev(P) {}
159
160 /// Begin of scope in direction of CFG building (backwards).
161 const_iterator begin() const { return const_iterator(*this, Vars.size()); }
Marcin Swiderski35387a02010-09-30 22:42:32 +0000162
163 void addVar(VarDecl* VD) {
164 Vars.push_back(VD);
165 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000166};
167
Marcin Swiderski35387a02010-09-30 22:42:32 +0000168/// distance - Calculates distance from this to L. L must be reachable from this
169/// (with use of ++ operator). Cost of calculating the distance is linear w.r.t.
170/// number of scopes between this and L.
171int LocalScope::const_iterator::distance(LocalScope::const_iterator L) {
172 int D = 0;
173 const_iterator F = *this;
174 while (F.Scope != L.Scope) {
175 assert (F != const_iterator()
176 && "L iterator is not reachable from F iterator.");
177 D += F.VarIter;
178 F = F.Scope->Prev;
179 }
180 D += F.VarIter - L.VarIter;
181 return D;
182}
183
184/// BlockScopePosPair - Structure for specifying position in CFG during its
185/// build process. It consists of CFGBlock that specifies position in CFG graph
186/// and LocalScope::const_iterator that specifies position in LocalScope graph.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000187struct BlockScopePosPair {
188 BlockScopePosPair() {}
189 BlockScopePosPair(CFGBlock* B, LocalScope::const_iterator S)
190 : Block(B), ScopePos(S) {}
191
192 CFGBlock* Block;
193 LocalScope::const_iterator ScopePos;
194};
195
Ted Kremeneka34ea072008-08-04 22:51:42 +0000196/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +0000197/// The builder is stateful: an instance of the builder should be used to only
198/// construct a single CFG.
199///
200/// Example usage:
201///
202/// CFGBuilder builder;
203/// CFG* cfg = builder.BuildAST(stmt1);
204///
Mike Stump6d9828c2009-07-17 01:31:16 +0000205/// CFG construction is done via a recursive walk of an AST. We actually parse
206/// the AST in reverse order so that the successor of a basic block is
207/// constructed prior to its predecessor. This allows us to nicely capture
208/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +0000209///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000210class CFGBuilder {
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000211 typedef BlockScopePosPair JumpTarget;
212 typedef BlockScopePosPair JumpSource;
213
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000214 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000215 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000216
Ted Kremenekfddd5182007-08-21 21:42:03 +0000217 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000218 CFGBlock* Succ;
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000219 JumpTarget ContinueJumpTarget;
220 JumpTarget BreakJumpTarget;
Ted Kremenekb5c13b02007-08-23 18:43:24 +0000221 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +0000222 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +0000223 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000224
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000225 // Current position in local scope.
226 LocalScope::const_iterator ScopePos;
227
228 // LabelMap records the mapping from Label expressions to their jump targets.
229 typedef llvm::DenseMap<LabelStmt*, JumpTarget> LabelMapTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000230 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +0000231
232 // A list of blocks that end with a "goto" that must be backpatched to their
233 // resolved targets upon completion of CFG construction.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000234 typedef std::vector<JumpSource> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +0000235 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +0000236
Ted Kremenek19bb3562007-08-28 19:26:49 +0000237 // A list of labels whose address has been taken (for indirect gotos).
238 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
239 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +0000240
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000241 bool badCFG;
242 CFG::BuildOptions BuildOpts;
243
Mike Stump6d9828c2009-07-17 01:31:16 +0000244public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000245 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
246 Block(NULL), Succ(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +0000247 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
Zhongxing Xu49b4ef32010-09-16 03:28:18 +0000248 TryTerminatedBlock(NULL), badCFG(false) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000249
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000250 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000251 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000252 CFG::BuildOptions BO);
Mike Stump6d9828c2009-07-17 01:31:16 +0000253
Ted Kremenek4f880632009-07-17 22:18:43 +0000254private:
255 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000256 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
257 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
258 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000259 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000260 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
261 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
262 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000263 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000264 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000265 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000266 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000267 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000268 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000269 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000270 CFGBlock *VisitDeclStmt(DeclStmt *DS);
271 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000272 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
273 CFGBlock *VisitDoStmt(DoStmt *D);
274 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000275 CFGBlock *VisitGotoStmt(GotoStmt* G);
276 CFGBlock *VisitIfStmt(IfStmt *I);
277 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
278 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000279 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000280 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
281 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
282 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
283 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
284 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
285 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000286 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
287 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000288 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
289 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000290
Ted Kremenek852274d2009-12-16 03:18:58 +0000291 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
292 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000293 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000294
Ted Kremenek274f4332008-04-28 18:00:46 +0000295 // NYS == Not Yet Supported
296 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000297 badCFG = true;
298 return Block;
299 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000300
Ted Kremenek4f880632009-07-17 22:18:43 +0000301 void autoCreateBlock() { if (!Block) Block = createBlock(); }
302 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000303
Zhongxing Xudf119892010-06-03 06:43:23 +0000304 CFGBlock *addStmt(Stmt *S) {
305 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000306 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000307 CFGBlock *addInitializer(CXXBaseOrMemberInitializer *I);
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000308 void addAutomaticObjDtors(LocalScope::const_iterator B,
309 LocalScope::const_iterator E, Stmt* S);
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000310 void addImplicitDtorsForDestructor(const CXXDestructorDecl *DD);
Ted Kremenekad5a8942010-08-02 23:46:59 +0000311
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000312 // Local scopes creation.
313 LocalScope* createOrReuseLocalScope(LocalScope* Scope);
314
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000315 void addLocalScopeForStmt(Stmt* S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000316 LocalScope* addLocalScopeForDeclStmt(DeclStmt* DS, LocalScope* Scope = NULL);
317 LocalScope* addLocalScopeForVarDecl(VarDecl* VD, LocalScope* Scope = NULL);
318
319 void addLocalScopeAndDtors(Stmt* S);
320
321 // Interface to CFGBlock - adding CFGElements.
Ted Kremenek852274d2009-12-16 03:18:58 +0000322 void AppendStmt(CFGBlock *B, Stmt *S,
323 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
324 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000325 }
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000326 void appendInitializer(CFGBlock *B, CXXBaseOrMemberInitializer *I) {
327 B->appendInitializer(I, cfg->getBumpVectorContext());
328 }
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000329 void appendBaseDtor(CFGBlock *B, const CXXBaseSpecifier *BS) {
330 B->appendBaseDtor(BS, cfg->getBumpVectorContext());
331 }
332 void appendMemberDtor(CFGBlock *B, FieldDecl *FD) {
333 B->appendMemberDtor(FD, cfg->getBumpVectorContext());
334 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000335
Marcin Swiderski53de1342010-09-30 22:54:37 +0000336 void insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
337 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S);
338 void appendAutomaticObjDtors(CFGBlock* Blk, LocalScope::const_iterator B,
339 LocalScope::const_iterator E, Stmt* S);
340 void prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
341 LocalScope::const_iterator B, LocalScope::const_iterator E);
342
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000343 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
344 B->addSuccessor(S, cfg->getBumpVectorContext());
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000347 /// TryResult - a class representing a variant over the values
348 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
349 /// and is used by the CFGBuilder to decide if a branch condition
350 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000351 class TryResult {
352 int X;
353 public:
354 TryResult(bool b) : X(b ? 1 : 0) {}
355 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek941fde82009-07-24 04:47:11 +0000357 bool isTrue() const { return X == 1; }
358 bool isFalse() const { return X == 0; }
359 bool isKnown() const { return X >= 0; }
360 void negate() {
361 assert(isKnown());
362 X ^= 0x1;
363 }
364 };
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Mike Stump00998a02009-07-23 23:25:26 +0000366 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
367 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000368 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000369 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000370 return TryResult();
371
Mike Stump00998a02009-07-23 23:25:26 +0000372 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000373 if (!S->isTypeDependent() && !S->isValueDependent() &&
374 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000375 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000376
377 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000378 }
Ted Kremenekfddd5182007-08-21 21:42:03 +0000379};
Mike Stump6d9828c2009-07-17 01:31:16 +0000380
Douglas Gregor898574e2008-12-05 23:32:09 +0000381// FIXME: Add support for dependent-sized array types in C++?
382// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000383static VariableArrayType* FindVA(Type* t) {
384 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
385 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
386 if (vat->getSizeExpr())
387 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000388
Ted Kremenek610a09e2008-09-26 22:58:57 +0000389 t = vt->getElementType().getTypePtr();
390 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000391
Ted Kremenek610a09e2008-09-26 22:58:57 +0000392 return 0;
393}
Mike Stump6d9828c2009-07-17 01:31:16 +0000394
395/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
396/// arbitrary statement. Examples include a single expression or a function
397/// body (compound statement). The ownership of the returned CFG is
398/// transferred to the caller. If CFG construction fails, this method returns
399/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000400CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000401 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000402
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000403 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000404 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000405 if (!Statement)
406 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000407
Ted Kremenek6c52c782010-09-14 23:41:16 +0000408 BuildOpts = BO;
Mike Stump6d9828c2009-07-17 01:31:16 +0000409
410 // Create an empty block that will serve as the exit block for the CFG. Since
411 // this is the first block added to the CFG, it will be implicitly registered
412 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000413 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000414 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000415 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000416
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000417 if (BuildOpts.AddImplicitDtors)
418 if (const CXXDestructorDecl *DD = dyn_cast_or_null<CXXDestructorDecl>(D))
419 addImplicitDtorsForDestructor(DD);
420
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000421 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000422 CFGBlock *B = addStmt(Statement);
423
424 if (badCFG)
425 return NULL;
426
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000427 // For C++ constructor add initializers to CFG.
428 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
429 for (CXXConstructorDecl::init_const_reverse_iterator I = CD->init_rbegin(),
430 E = CD->init_rend(); I != E; ++I) {
431 B = addInitializer(*I);
432 if (badCFG)
433 return NULL;
434 }
435 }
436
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000437 if (B)
438 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000439
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000440 // Backpatch the gotos whose label -> block mappings we didn't know when we
441 // encountered them.
442 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
443 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000444
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000445 CFGBlock* B = I->Block;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000446 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
447 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000448
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000449 // If there is no target for the goto, then we are looking at an
450 // incomplete AST. Handle this by not registering a successor.
451 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000452
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000453 JumpTarget JT = LI->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +0000454 prependAutomaticObjDtorsWithTerminator(B, I->ScopePos, JT.ScopePos);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000455 AddSuccessor(B, JT.Block);
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000456 }
457
458 // Add successors to the Indirect Goto Dispatch block (if we have one).
459 if (CFGBlock* B = cfg->getIndirectGotoBlock())
460 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
461 E = AddressTakenLabels.end(); I != E; ++I ) {
462
463 // Lookup the target block.
464 LabelMapTy::iterator LI = LabelMap.find(*I);
465
466 // If there is no target block that contains label, then we are looking
467 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000468 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000469
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000470 AddSuccessor(B, LI->second.Block);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000471 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000472
Mike Stump6d9828c2009-07-17 01:31:16 +0000473 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000474 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000475
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000476 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000477}
Mike Stump6d9828c2009-07-17 01:31:16 +0000478
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000479/// createBlock - Used to lazily create blocks that are connected
480/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000481CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000482 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000483 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000484 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000485 return B;
486}
Mike Stump6d9828c2009-07-17 01:31:16 +0000487
Marcin Swiderski82bc3fd2010-10-04 03:38:22 +0000488/// addInitializer - Add C++ base or member initializer element to CFG.
489CFGBlock *CFGBuilder::addInitializer(CXXBaseOrMemberInitializer *I) {
490 if (!BuildOpts.AddInitializers)
491 return Block;
492
493 autoCreateBlock();
494 appendInitializer(Block, I);
495
496 if (Expr *Init = I->getInit()) {
497 AddStmtChoice::Kind K = AddStmtChoice::NotAlwaysAdd;
498 if (FieldDecl *FD = I->getMember())
499 if (FD->getType()->isReferenceType())
500 K = AddStmtChoice::AsLValueNotAlwaysAdd;
501
502 return Visit(Init, AddStmtChoice(K));
503 }
504
505 return Block;
506}
507
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000508/// addAutomaticObjDtors - Add to current block automatic objects destructors
509/// for objects in range of local scope positions. Use S as trigger statement
510/// for destructors.
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000511void CFGBuilder::addAutomaticObjDtors(LocalScope::const_iterator B,
512 LocalScope::const_iterator E, Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000513 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000514 return;
515
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000516 if (B == E)
Zhongxing Xu6a16a302010-10-01 03:22:39 +0000517 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000518
519 autoCreateBlock();
520 appendAutomaticObjDtors(Block, B, E, S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000521}
522
Marcin Swiderski7c625d82010-10-05 05:37:00 +0000523/// addImplicitDtorsForDestructor - Add implicit destructors generated for
524/// base and member objects in destructor.
525void CFGBuilder::addImplicitDtorsForDestructor(const CXXDestructorDecl *DD) {
526 assert (BuildOpts.AddImplicitDtors
527 && "Can be called only when dtors should be added");
528 const CXXRecordDecl *RD = DD->getParent();
529
530 // At the end destroy virtual base objects.
531 for (CXXRecordDecl::base_class_const_iterator VI = RD->vbases_begin(),
532 VE = RD->vbases_end(); VI != VE; ++VI) {
533 const CXXRecordDecl *CD = VI->getType()->getAsCXXRecordDecl();
534 if (!CD->hasTrivialDestructor()) {
535 autoCreateBlock();
536 appendBaseDtor(Block, VI);
537 }
538 }
539
540 // Before virtual bases destroy direct base objects.
541 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
542 BE = RD->bases_end(); BI != BE; ++BI) {
543 if (!BI->isVirtual()) {
544 const CXXRecordDecl *CD = BI->getType()->getAsCXXRecordDecl();
545 if (!CD->hasTrivialDestructor()) {
546 autoCreateBlock();
547 appendBaseDtor(Block, BI);
548 }
549 }
550 }
551
552 // First destroy member objects.
553 for (CXXRecordDecl::field_iterator FI = RD->field_begin(),
554 FE = RD->field_end(); FI != FE; ++FI) {
555 if (const CXXRecordDecl *CD = FI->getType()->getAsCXXRecordDecl())
556 if (!CD->hasTrivialDestructor()) {
557 autoCreateBlock();
558 appendMemberDtor(Block, *FI);
559 }
560 }
561}
562
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000563/// createOrReuseLocalScope - If Scope is NULL create new LocalScope. Either
564/// way return valid LocalScope object.
565LocalScope* CFGBuilder::createOrReuseLocalScope(LocalScope* Scope) {
566 if (!Scope) {
567 Scope = cfg->getAllocator().Allocate<LocalScope>();
568 new (Scope) LocalScope(ScopePos);
569 }
570 return Scope;
571}
572
573/// addLocalScopeForStmt - Add LocalScope to local scopes tree for statement
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000574/// that should create implicit scope (e.g. if/else substatements).
575void CFGBuilder::addLocalScopeForStmt(Stmt* S) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000576 if (!BuildOpts.AddImplicitDtors)
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000577 return;
578
579 LocalScope *Scope = 0;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000580
581 // For compound statement we will be creating explicit scope.
582 if (CompoundStmt* CS = dyn_cast<CompoundStmt>(S)) {
583 for (CompoundStmt::body_iterator BI = CS->body_begin(), BE = CS->body_end()
584 ; BI != BE; ++BI) {
585 Stmt* SI = *BI;
586 if (LabelStmt* LS = dyn_cast<LabelStmt>(SI))
587 SI = LS->getSubStmt();
588 if (DeclStmt* DS = dyn_cast<DeclStmt>(SI))
589 Scope = addLocalScopeForDeclStmt(DS, Scope);
590 }
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000591 return;
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000592 }
593
594 // For any other statement scope will be implicit and as such will be
595 // interesting only for DeclStmt.
596 if (LabelStmt* LS = dyn_cast<LabelStmt>(S))
597 S = LS->getSubStmt();
598 if (DeclStmt* DS = dyn_cast<DeclStmt>(S))
Zhongxing Xub6edff52010-10-01 03:09:09 +0000599 addLocalScopeForDeclStmt(DS);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000600}
601
602/// addLocalScopeForDeclStmt - Add LocalScope for declaration statement. Will
603/// reuse Scope if not NULL.
604LocalScope* CFGBuilder::addLocalScopeForDeclStmt(DeclStmt* DS,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000605 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000606 if (!BuildOpts.AddImplicitDtors)
607 return Scope;
608
609 for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end()
610 ; DI != DE; ++DI) {
611 if (VarDecl* VD = dyn_cast<VarDecl>(*DI))
612 Scope = addLocalScopeForVarDecl(VD, Scope);
613 }
614 return Scope;
615}
616
617/// addLocalScopeForVarDecl - Add LocalScope for variable declaration. It will
618/// create add scope for automatic objects and temporary objects bound to
619/// const reference. Will reuse Scope if not NULL.
620LocalScope* CFGBuilder::addLocalScopeForVarDecl(VarDecl* VD,
Zhongxing Xub6edff52010-10-01 03:09:09 +0000621 LocalScope* Scope) {
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000622 if (!BuildOpts.AddImplicitDtors)
623 return Scope;
624
625 // Check if variable is local.
626 switch (VD->getStorageClass()) {
627 case SC_None:
628 case SC_Auto:
629 case SC_Register:
630 break;
631 default: return Scope;
632 }
633
634 // Check for const references bound to temporary. Set type to pointee.
635 QualType QT = VD->getType();
636 if (const ReferenceType* RT = QT.getTypePtr()->getAs<ReferenceType>()) {
637 QT = RT->getPointeeType();
638 if (!QT.isConstQualified())
639 return Scope;
640 if (!VD->getInit() || !VD->getInit()->Classify(*Context).isRValue())
641 return Scope;
642 }
643
644 // Check if type is a C++ class with non-trivial destructor.
Zhongxing Xu4e493e02010-10-05 08:38:06 +0000645
646 if (const CXXRecordDecl* CD = QT->getAsCXXRecordDecl())
647 if (!CD->hasTrivialDestructor()) {
648 // Add the variable to scope
649 Scope = createOrReuseLocalScope(Scope);
650 Scope->addVar(VD);
651 ScopePos = Scope->begin();
652 }
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000653 return Scope;
654}
655
656/// addLocalScopeAndDtors - For given statement add local scope for it and
657/// add destructors that will cleanup the scope. Will reuse Scope if not NULL.
658void CFGBuilder::addLocalScopeAndDtors(Stmt* S) {
659 if (!BuildOpts.AddImplicitDtors)
660 return;
661
662 LocalScope::const_iterator scopeBeginPos = ScopePos;
Zhongxing Xu02acdfa2010-10-01 03:00:16 +0000663 addLocalScopeForStmt(S);
Marcin Swiderski239a7c42010-09-30 23:05:00 +0000664 addAutomaticObjDtors(ScopePos, scopeBeginPos, S);
665}
666
Marcin Swiderski53de1342010-09-30 22:54:37 +0000667/// insertAutomaticObjDtors - Insert destructor CFGElements for variables with
668/// automatic storage duration to CFGBlock's elements vector. Insertion will be
669/// performed in place specified with iterator.
670void CFGBuilder::insertAutomaticObjDtors(CFGBlock* Blk, CFGBlock::iterator I,
671 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
672 BumpVectorContext& C = cfg->getBumpVectorContext();
673 I = Blk->beginAutomaticObjDtorsInsert(I, B.distance(E), C);
674 while (B != E)
675 I = Blk->insertAutomaticObjDtor(I, *B++, S);
676}
677
678/// appendAutomaticObjDtors - Append destructor CFGElements for variables with
679/// automatic storage duration to CFGBlock's elements vector. Elements will be
680/// appended to physical end of the vector which happens to be logical
681/// beginning.
682void CFGBuilder::appendAutomaticObjDtors(CFGBlock* Blk,
683 LocalScope::const_iterator B, LocalScope::const_iterator E, Stmt* S) {
684 insertAutomaticObjDtors(Blk, Blk->begin(), B, E, S);
685}
686
687/// prependAutomaticObjDtorsWithTerminator - Prepend destructor CFGElements for
688/// variables with automatic storage duration to CFGBlock's elements vector.
689/// Elements will be prepended to physical beginning of the vector which
690/// happens to be logical end. Use blocks terminator as statement that specifies
691/// destructors call site.
692void CFGBuilder::prependAutomaticObjDtorsWithTerminator(CFGBlock* Blk,
693 LocalScope::const_iterator B, LocalScope::const_iterator E) {
694 insertAutomaticObjDtors(Blk, Blk->end(), B, E, Blk->getTerminator());
695}
696
Ted Kremenek4f880632009-07-17 22:18:43 +0000697/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000698/// blocks for ternary operators, &&, and ||. We also process "," and
699/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000700CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000701tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000702 if (!S) {
703 badCFG = true;
704 return 0;
705 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000706 switch (S->getStmtClass()) {
707 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000708 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000709
710 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000711 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek4f880632009-07-17 22:18:43 +0000713 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000714 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremenek4f880632009-07-17 22:18:43 +0000716 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000717 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000718
Ted Kremenek4f880632009-07-17 22:18:43 +0000719 case Stmt::BreakStmtClass:
720 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek4f880632009-07-17 22:18:43 +0000722 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000723 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000724 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek4f880632009-07-17 22:18:43 +0000726 case Stmt::CaseStmtClass:
727 return VisitCaseStmt(cast<CaseStmt>(S));
728
729 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000730 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek4f880632009-07-17 22:18:43 +0000732 case Stmt::CompoundStmtClass:
733 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek4f880632009-07-17 22:18:43 +0000735 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000736 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek4f880632009-07-17 22:18:43 +0000738 case Stmt::ContinueStmtClass:
739 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek021c8af2010-01-19 20:40:33 +0000741 case Stmt::CXXCatchStmtClass:
742 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
743
Ted Kremenek47e331e2010-08-28 00:19:02 +0000744 case Stmt::CXXExprWithTemporariesClass: {
745 // FIXME: Handle temporaries. For now, just visit the subexpression
746 // so we don't artificially create extra blocks.
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000747 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000748 }
749
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000750 case Stmt::CXXMemberCallExprClass:
751 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
752
Ted Kremenek021c8af2010-01-19 20:40:33 +0000753 case Stmt::CXXThrowExprClass:
754 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000755
Ted Kremenek021c8af2010-01-19 20:40:33 +0000756 case Stmt::CXXTryStmtClass:
757 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000758
Ted Kremenek4f880632009-07-17 22:18:43 +0000759 case Stmt::DeclStmtClass:
760 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek4f880632009-07-17 22:18:43 +0000762 case Stmt::DefaultStmtClass:
763 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek4f880632009-07-17 22:18:43 +0000765 case Stmt::DoStmtClass:
766 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek4f880632009-07-17 22:18:43 +0000768 case Stmt::ForStmtClass:
769 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Ted Kremenek4f880632009-07-17 22:18:43 +0000771 case Stmt::GotoStmtClass:
772 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Ted Kremenek4f880632009-07-17 22:18:43 +0000774 case Stmt::IfStmtClass:
775 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek4f880632009-07-17 22:18:43 +0000777 case Stmt::IndirectGotoStmtClass:
778 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenek4f880632009-07-17 22:18:43 +0000780 case Stmt::LabelStmtClass:
781 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek115c1b92010-04-11 17:02:10 +0000783 case Stmt::MemberExprClass:
784 return VisitMemberExpr(cast<MemberExpr>(S), asc);
785
Ted Kremenek4f880632009-07-17 22:18:43 +0000786 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000787 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
788
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 case Stmt::ObjCAtSynchronizedStmtClass:
790 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenek4f880632009-07-17 22:18:43 +0000792 case Stmt::ObjCAtThrowStmtClass:
793 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek4f880632009-07-17 22:18:43 +0000795 case Stmt::ObjCAtTryStmtClass:
796 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek4f880632009-07-17 22:18:43 +0000798 case Stmt::ObjCForCollectionStmtClass:
799 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Ted Kremenek4f880632009-07-17 22:18:43 +0000801 case Stmt::ParenExprClass:
802 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000803 goto tryAgain;
804
Ted Kremenek4f880632009-07-17 22:18:43 +0000805 case Stmt::NullStmtClass:
806 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek4f880632009-07-17 22:18:43 +0000808 case Stmt::ReturnStmtClass:
809 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek4f880632009-07-17 22:18:43 +0000811 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000812 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenek4f880632009-07-17 22:18:43 +0000814 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000815 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Ted Kremenek4f880632009-07-17 22:18:43 +0000817 case Stmt::SwitchStmtClass:
818 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Ted Kremenek4f880632009-07-17 22:18:43 +0000820 case Stmt::WhileStmtClass:
821 return VisitWhileStmt(cast<WhileStmt>(S));
822 }
823}
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek852274d2009-12-16 03:18:58 +0000825CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
826 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000827 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000828 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek4f880632009-07-17 22:18:43 +0000831 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000832}
Mike Stump6d9828c2009-07-17 01:31:16 +0000833
Ted Kremenek4f880632009-07-17 22:18:43 +0000834/// VisitChildren - Visit the children of a Stmt.
835CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
836 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000837 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000838 E = Terminator->child_end(); I != E; ++I) {
839 if (*I) B = Visit(*I);
840 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000841 return B;
842}
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Ted Kremenek852274d2009-12-16 03:18:58 +0000844CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
845 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000846 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000847
Ted Kremenek852274d2009-12-16 03:18:58 +0000848 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000849 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000850 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000851 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000852
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000853 return Block;
854}
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Ted Kremenek852274d2009-12-16 03:18:58 +0000856CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
857 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000858 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000859 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000860 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000862 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000863 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Ted Kremenek4f880632009-07-17 22:18:43 +0000865 // create the block evaluating the LHS
866 CFGBlock* LHSBlock = createBlock(false);
867 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Ted Kremenek4f880632009-07-17 22:18:43 +0000869 // create the block evaluating the RHS
870 Succ = ConfluenceBlock;
871 Block = NULL;
872 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000873
874 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000875 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000876 return 0;
877 }
878 else {
879 // Create an empty block for cases where the RHS doesn't require
880 // any explicit statements in the CFG.
881 RHSBlock = createBlock();
882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Mike Stump00998a02009-07-23 23:25:26 +0000884 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000885 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000886 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000887 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000888
Ted Kremenek4f880632009-07-17 22:18:43 +0000889 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000890 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000891 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
892 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000893 } else {
John McCall2de56d12010-08-25 11:45:40 +0000894 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000895 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
896 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000897 }
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Ted Kremenek4f880632009-07-17 22:18:43 +0000899 // Generate the blocks for evaluating the LHS.
900 Block = LHSBlock;
901 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000902 }
John McCall2de56d12010-08-25 11:45:40 +0000903 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000904 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000905 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000906 addStmt(B->getRHS());
907 return addStmt(B->getLHS());
908 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000909 else if (B->isAssignmentOp()) {
910 if (asc.alwaysAdd()) {
911 autoCreateBlock();
912 AppendStmt(Block, B, asc);
913 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000914
Marcin Swiderskie1667192010-10-24 08:21:40 +0000915 Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
916 return Visit(B->getRHS());
Zhongxing Xufc61d942010-06-03 06:23:18 +0000917 }
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Marcin Swiderskie1667192010-10-24 08:21:40 +0000919 if (asc.alwaysAdd()) {
920 autoCreateBlock();
921 AppendStmt(Block, B, asc);
922 }
923
924 Visit(B->getRHS());
925 return Visit(B->getLHS());
Ted Kremenek4f880632009-07-17 22:18:43 +0000926}
927
Ted Kremenek852274d2009-12-16 03:18:58 +0000928CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
929 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000930 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000931 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000932 }
933 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000934}
935
Ted Kremenek4f880632009-07-17 22:18:43 +0000936CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
937 // "break" is a control-flow statement. Thus we stop processing the current
938 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000939 if (badCFG)
940 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremenek4f880632009-07-17 22:18:43 +0000942 // Now create a new block that ends with the break statement.
943 Block = createBlock(false);
944 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Ted Kremenek4f880632009-07-17 22:18:43 +0000946 // If there is no target for the break, then we are looking at an incomplete
947 // AST. This means that the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000948 if (BreakJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +0000949 addAutomaticObjDtors(ScopePos, BreakJumpTarget.ScopePos, B);
Marcin Swiderskif1308c72010-09-25 11:05:21 +0000950 AddSuccessor(Block, BreakJumpTarget.Block);
951 } else
Ted Kremenek4f880632009-07-17 22:18:43 +0000952 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000953
954
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000955 return Block;
956}
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Mike Stump4c45aa12010-01-21 15:20:48 +0000958static bool CanThrow(Expr *E) {
959 QualType Ty = E->getType();
960 if (Ty->isFunctionPointerType())
961 Ty = Ty->getAs<PointerType>()->getPointeeType();
962 else if (Ty->isBlockPointerType())
963 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +0000964
Mike Stump4c45aa12010-01-21 15:20:48 +0000965 const FunctionType *FT = Ty->getAs<FunctionType>();
966 if (FT) {
967 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
968 if (Proto->hasEmptyExceptionSpec())
969 return false;
970 }
971 return true;
972}
973
Ted Kremenek852274d2009-12-16 03:18:58 +0000974CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000975 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000976 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000977 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000978 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000979 }
Mike Stump24556362009-07-25 21:26:53 +0000980
Mike Stump4c45aa12010-01-21 15:20:48 +0000981 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000982
983 // Languages without exceptions are assumed to not throw.
984 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000985 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +0000986 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000987 }
988
989 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000990 if (FD->hasAttr<NoReturnAttr>())
991 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000992 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000993 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000994 }
Mike Stump24556362009-07-25 21:26:53 +0000995
Mike Stump4c45aa12010-01-21 15:20:48 +0000996 if (!CanThrow(C->getCallee()))
997 AddEHEdge = false;
998
Zhongxing Xufc61d942010-06-03 06:23:18 +0000999 if (!NoReturn && !AddEHEdge) {
1000 if (asc.asLValue())
1001 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
1002 else
1003 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
1004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Mike Stump079bd722010-01-19 22:00:14 +00001006 if (Block) {
1007 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001008 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +00001009 return 0;
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Mike Stump079bd722010-01-19 22:00:14 +00001012 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +00001013 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +00001014
Mike Stump079bd722010-01-19 22:00:14 +00001015 if (NoReturn) {
1016 // Wire this to the exit block directly.
1017 AddSuccessor(Block, &cfg->getExit());
1018 }
Mike Stump4c45aa12010-01-21 15:20:48 +00001019 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +00001020 // Add exceptional edges.
1021 if (TryTerminatedBlock)
1022 AddSuccessor(Block, TryTerminatedBlock);
1023 else
1024 AddSuccessor(Block, &cfg->getExit());
1025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Mike Stump24556362009-07-25 21:26:53 +00001027 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +00001028}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001029
Ted Kremenek852274d2009-12-16 03:18:58 +00001030CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
1031 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001032 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001033 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001034 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001035 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Ted Kremenek115c1b92010-04-11 17:02:10 +00001037 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1038 : AddStmtChoice::AlwaysAdd;
1039
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001040 Succ = ConfluenceBlock;
1041 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +00001042 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001043 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001044 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001046 Succ = ConfluenceBlock;
1047 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +00001048 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001049 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001050 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001052 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +00001053 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001054 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001055 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
1056 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001057 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001058 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +00001059}
Mike Stump1eb44332009-09-09 15:08:12 +00001060
1061
1062CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001063 addLocalScopeAndDtors(C);
Mike Stump1eb44332009-09-09 15:08:12 +00001064 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001065
1066 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
1067 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +00001068 // If we hit a segment of code just containing ';' (NullStmts), we can
1069 // get a null block back. In such cases, just use the LastBlock
1070 if (CFGBlock *newBlock = addStmt(*I))
1071 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +00001073 if (badCFG)
1074 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +00001075 }
Mike Stump079bd722010-01-19 22:00:14 +00001076
Ted Kremenek4f880632009-07-17 22:18:43 +00001077 return LastBlock;
1078}
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremenek852274d2009-12-16 03:18:58 +00001080CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
1081 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001082 // Create the confluence block that will "merge" the results of the ternary
1083 // expression.
1084 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +00001085 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001086 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001087 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Ted Kremenek115c1b92010-04-11 17:02:10 +00001089 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1090 : AddStmtChoice::AlwaysAdd;
1091
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001092 // Create a block for the LHS expression if there is an LHS expression. A
1093 // GCC extension allows LHS to be NULL, causing the condition to be the
1094 // value that is returned instead.
1095 // e.g: x ?: y is shorthand for: x ? x : y;
1096 Succ = ConfluenceBlock;
1097 Block = NULL;
1098 CFGBlock* LHSBlock = NULL;
1099 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +00001100 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001101 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001102 return 0;
1103 Block = NULL;
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001106 // Create the block for the RHS expression.
1107 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +00001108 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001109 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001110 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001112 // Create the block that will contain the condition.
1113 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Mike Stump00998a02009-07-23 23:25:26 +00001115 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001116 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001117 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001118 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001119 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +00001120 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001121 // If we know the condition is false, add NULL as the successor for
1122 // the block containing the condition. In this case, the confluence
1123 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001124 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +00001125 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001126 } else {
1127 // If we have no LHS expression, add the ConfluenceBlock as a direct
1128 // successor for the block containing the condition. Moreover, we need to
1129 // reverse the order of the predecessors in the ConfluenceBlock because
1130 // the RHSBlock will have been added to the succcessors already, and we
1131 // want the first predecessor to the the block containing the expression
1132 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001133 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +00001134 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001135 std::reverse(ConfluenceBlock->pred_begin(),
1136 ConfluenceBlock->pred_end());
1137 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001140 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +00001141 Block->setTerminator(C);
1142 return addStmt(C->getCond());
1143}
1144
Ted Kremenek4f880632009-07-17 22:18:43 +00001145CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
1146 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001147
Ted Kremenek4f880632009-07-17 22:18:43 +00001148 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001149 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +00001150 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +00001151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Ted Kremenek4f880632009-07-17 22:18:43 +00001153 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremenek4f880632009-07-17 22:18:43 +00001155 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
1156 typedef llvm::SmallVector<Decl*,10> BufTy;
1157 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremenek4f880632009-07-17 22:18:43 +00001159 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
1160 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
1161 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
1162 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenek4f880632009-07-17 22:18:43 +00001164 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
1165 // automatically freed with the CFG.
1166 DeclGroupRef DG(*I);
1167 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001168 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +00001169 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenek4f880632009-07-17 22:18:43 +00001171 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001172 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +00001173 B = VisitDeclSubExpr(D);
1174 }
Mike Stump1eb44332009-09-09 15:08:12 +00001175
1176 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +00001177}
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Ted Kremenek4f880632009-07-17 22:18:43 +00001179/// VisitDeclSubExpr - Utility method to add block-level expressions for
1180/// initializers in Decls.
1181CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
1182 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +00001183
Ted Kremenek4f880632009-07-17 22:18:43 +00001184 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenek4f880632009-07-17 22:18:43 +00001186 if (!VD)
1187 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremenek4f880632009-07-17 22:18:43 +00001189 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremenek4f880632009-07-17 22:18:43 +00001191 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +00001192 AddStmtChoice::Kind k =
1193 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
1194 : AddStmtChoice::NotAlwaysAdd;
1195 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +00001196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenek4f880632009-07-17 22:18:43 +00001198 // If the type of VD is a VLA, then we must process its size expressions.
1199 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
1200 VA = FindVA(VA->getElementType().getTypePtr()))
1201 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001203 // Remove variable from local scope.
1204 if (ScopePos && VD == *ScopePos)
1205 ++ScopePos;
1206
Ted Kremenek4f880632009-07-17 22:18:43 +00001207 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001208}
1209
1210CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001211 // We may see an if statement in the middle of a basic block, or it may be the
1212 // first statement we are processing. In either case, we create a new basic
1213 // block. First, we create the blocks for the then...else statements, and
1214 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +00001215 // middle of a block, we stop processing that block. That block is then the
1216 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +00001217
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001218 // Save local scope position because in case of condition variable ScopePos
1219 // won't be restored when traversing AST.
1220 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1221
1222 // Create local scope for possible condition variable.
1223 // Store scope position. Add implicit destructor.
1224 if (VarDecl* VD = I->getConditionVariable()) {
1225 LocalScope::const_iterator BeginScopePos = ScopePos;
1226 addLocalScopeForVarDecl(VD);
1227 addAutomaticObjDtors(ScopePos, BeginScopePos, I);
1228 }
1229
Mike Stump6d9828c2009-07-17 01:31:16 +00001230 // The block we were proccessing is now finished. Make it the successor
1231 // block.
1232 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001233 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001234 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001235 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001236 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001237
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001238 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001239 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001240
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001241 if (Stmt* Else = I->getElse()) {
1242 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001243
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001244 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +00001245 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001246 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001247
1248 // If branch is not a compound statement create implicit scope
1249 // and add destructors.
1250 if (!isa<CompoundStmt>(Else))
1251 addLocalScopeAndDtors(Else);
1252
Ted Kremenek4f880632009-07-17 22:18:43 +00001253 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +00001254
Ted Kremenekb6f7b722007-08-30 18:13:31 +00001255 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
1256 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001257 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001258 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001259 return 0;
1260 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001261 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001262
Ted Kremenekb6f1d782009-07-17 18:04:55 +00001263 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001264 CFGBlock* ThenBlock;
1265 {
1266 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001267 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001268 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +00001269 Block = NULL;
Marcin Swiderski04e046c2010-10-01 00:52:17 +00001270
1271 // If branch is not a compound statement create implicit scope
1272 // and add destructors.
1273 if (!isa<CompoundStmt>(Then))
1274 addLocalScopeAndDtors(Then);
1275
Ted Kremenek4f880632009-07-17 22:18:43 +00001276 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +00001277
Ted Kremenekdbdf7942009-04-01 03:52:47 +00001278 if (!ThenBlock) {
1279 // We can reach here if the "then" body has all NullStmts.
1280 // Create an empty block so we can distinguish between true and false
1281 // branches in path-sensitive analyses.
1282 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001283 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +00001284 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001285 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001286 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001287 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001288 }
1289
Mike Stump6d9828c2009-07-17 01:31:16 +00001290 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001291 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001293 // Set the terminator of the new block to the If statement.
1294 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001295
Mike Stump00998a02009-07-23 23:25:26 +00001296 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001297 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001298
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001299 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001300 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
1301 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001302
1303 // Add the condition as the last statement in the new block. This may create
1304 // new blocks as the condition may contain control-flow. Any newly created
1305 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001306 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001307
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001308 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
1309 // and the condition variable initialization to the CFG.
1310 if (VarDecl *VD = I->getConditionVariable()) {
1311 if (Expr *Init = VD->getInit()) {
1312 autoCreateBlock();
1313 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
1314 addStmt(Init);
1315 }
1316 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001317
Ted Kremenek61dfbec2009-12-23 04:49:01 +00001318 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001319}
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
1321
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001322CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001323 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001324 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001325 // NOTE: If a "return" appears in the middle of a block, this means that the
1326 // code afterwards is DEAD (unreachable). We still keep a basic block
1327 // for that code; a simple "mark-and-sweep" from the entry block will be
1328 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001329
1330 // Create the new block.
1331 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001332
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001333 // The Exit block is the only successor.
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001334 addAutomaticObjDtors(ScopePos, LocalScope::const_iterator(), R);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001335 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001336
1337 // Add the return statement to the block. This may create new blocks if R
1338 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001339 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001340}
1341
1342CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
1343 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +00001344 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +00001345 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001346
Ted Kremenek4f880632009-07-17 22:18:43 +00001347 if (!LabelBlock) // This can happen when the body is empty, i.e.
1348 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +00001349
Ted Kremenek4f880632009-07-17 22:18:43 +00001350 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001351 LabelMap[ L ] = JumpTarget(LabelBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001352
1353 // Labels partition blocks, so this is the end of the basic block we were
1354 // processing (L is the block's label). Because this is label (and we have
1355 // already processed the substatement) there is no extra control-flow to worry
1356 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +00001357 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001358 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001359 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001360
1361 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001362 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001363
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001364 // This block is now the implicit successor of other blocks.
1365 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001366
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001367 return LabelBlock;
1368}
1369
1370CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001371 // Goto is a control-flow statement. Thus we stop processing the current
1372 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001373
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001374 Block = createBlock(false);
1375 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +00001376
1377 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001378 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +00001379
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001380 if (I == LabelMap.end())
1381 // We will need to backpatch this block later.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001382 BackpatchBlocks.push_back(JumpSource(Block, ScopePos));
1383 else {
1384 JumpTarget JT = I->second;
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001385 addAutomaticObjDtors(ScopePos, JT.ScopePos, G);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001386 AddSuccessor(Block, JT.Block);
1387 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001388
Mike Stump6d9828c2009-07-17 01:31:16 +00001389 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001390}
1391
1392CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001393 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001394
Marcin Swiderski47575f12010-10-01 01:38:14 +00001395 // Save local scope position because in case of condition variable ScopePos
1396 // won't be restored when traversing AST.
1397 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1398
1399 // Create local scope for init statement and possible condition variable.
1400 // Add destructor for init statement and condition variable.
1401 // Store scope position for continue statement.
1402 if (Stmt* Init = F->getInit())
1403 addLocalScopeForStmt(Init);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001404 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
1405
Marcin Swiderski47575f12010-10-01 01:38:14 +00001406 if (VarDecl* VD = F->getConditionVariable())
1407 addLocalScopeForVarDecl(VD);
1408 LocalScope::const_iterator ContinueScopePos = ScopePos;
1409
1410 addAutomaticObjDtors(ScopePos, save_scope_pos.get(), F);
1411
Mike Stumpfefb9f72009-07-21 01:12:51 +00001412 // "for" is a control-flow statement. Thus we stop processing the current
1413 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001414 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001415 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001416 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001417 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001418 } else
1419 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001420
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001421 // Save the current value for the break targets.
1422 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001423 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001424 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001425
Mike Stump6d9828c2009-07-17 01:31:16 +00001426 // Because of short-circuit evaluation, the condition of the loop can span
1427 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1428 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001429 CFGBlock* ExitConditionBlock = createBlock(false);
1430 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001432 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001433 ExitConditionBlock->setTerminator(F);
1434
1435 // Now add the actual condition to the condition block. Because the condition
1436 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001437 if (Stmt* C = F->getCond()) {
1438 Block = ExitConditionBlock;
1439 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +00001440 assert(Block == EntryConditionBlock ||
1441 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001442
1443 // If this block contains a condition variable, add both the condition
1444 // variable and initializer to the CFG.
1445 if (VarDecl *VD = F->getConditionVariable()) {
1446 if (Expr *Init = VD->getInit()) {
1447 autoCreateBlock();
1448 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1449 EntryConditionBlock = addStmt(Init);
1450 assert(Block == EntryConditionBlock);
1451 }
1452 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001453
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001454 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001455 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001456 return 0;
1457 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001458 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001459
Mike Stump6d9828c2009-07-17 01:31:16 +00001460 // The condition block is the implicit successor for the loop body as well as
1461 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001462 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001463
Mike Stump00998a02009-07-23 23:25:26 +00001464 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001465 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Mike Stump00998a02009-07-23 23:25:26 +00001467 if (F->getCond())
1468 KnownVal = TryEvaluateBool(F->getCond());
1469
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001470 // Now create the loop body.
1471 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001472 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001473
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001474 // Save the current values for Block, Succ, and continue targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001475 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1476 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001477
Ted Kremenekaf603f72007-08-30 18:39:40 +00001478 // Create a new block to contain the (bottom) of the loop body.
1479 Block = NULL;
Marcin Swiderski47575f12010-10-01 01:38:14 +00001480
1481 // Loop body should end with destructor of Condition variable (if any).
1482 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, F);
Mike Stump6d9828c2009-07-17 01:31:16 +00001483
Ted Kremeneke9334502008-09-04 21:48:47 +00001484 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001485 // Generate increment code in its own basic block. This is the target of
1486 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001487 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001488 } else {
1489 // No increment code. Create a special, empty, block that is used as the
1490 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001491 assert(Succ == EntryConditionBlock);
Marcin Swiderski47575f12010-10-01 01:38:14 +00001492 Succ = Block ? Block : createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001493 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001494
Ted Kremenek3575f842009-04-28 00:51:56 +00001495 // Finish up the increment (or empty) block if it hasn't been already.
1496 if (Block) {
1497 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001498 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001499 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001500 Block = 0;
1501 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001502
Marcin Swiderski47575f12010-10-01 01:38:14 +00001503 ContinueJumpTarget = JumpTarget(Succ, ContinueScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001504
Ted Kremenek3575f842009-04-28 00:51:56 +00001505 // The starting block for the loop increment is the block that should
1506 // represent the 'loop target' for looping back to the start of the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001507 ContinueJumpTarget.Block->setLoopTarget(F);
Ted Kremenek3575f842009-04-28 00:51:56 +00001508
Marcin Swiderski47575f12010-10-01 01:38:14 +00001509 // If body is not a compound statement create implicit scope
1510 // and add destructors.
1511 if (!isa<CompoundStmt>(F->getBody()))
1512 addLocalScopeAndDtors(F->getBody());
1513
Mike Stump6d9828c2009-07-17 01:31:16 +00001514 // Now populate the body block, and in the process create new blocks as we
1515 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001516 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001517
1518 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001519 BodyBlock = ContinueJumpTarget.Block;//can happen for "for (...;...;...);"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001520 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001521 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001522
Ted Kremenek941fde82009-07-24 04:47:11 +00001523 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001524 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001525 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001526
Ted Kremenek941fde82009-07-24 04:47:11 +00001527 // Link up the condition block with the code that follows the loop. (the
1528 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001529 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001530
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001531 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001532 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001533 if (Stmt* I = F->getInit()) {
1534 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001535 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001536 } else {
1537 // There is no loop initialization. We are thus basically a while loop.
1538 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001539 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001540 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001541 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001542 }
1543}
1544
Ted Kremenek115c1b92010-04-11 17:02:10 +00001545CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1546 if (asc.alwaysAdd()) {
1547 autoCreateBlock();
1548 AppendStmt(Block, M, asc);
1549 }
1550 return Visit(M->getBase(),
1551 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1552 : AddStmtChoice::AsLValueNotAlwaysAdd);
1553}
1554
Ted Kremenek514de5a2008-11-11 17:10:00 +00001555CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1556 // Objective-C fast enumeration 'for' statements:
1557 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1558 //
1559 // for ( Type newVariable in collection_expression ) { statements }
1560 //
1561 // becomes:
1562 //
1563 // prologue:
1564 // 1. collection_expression
1565 // T. jump to loop_entry
1566 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001567 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001568 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1569 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1570 // TB:
1571 // statements
1572 // T. jump to loop_entry
1573 // FB:
1574 // what comes after
1575 //
1576 // and
1577 //
1578 // Type existingItem;
1579 // for ( existingItem in expression ) { statements }
1580 //
1581 // becomes:
1582 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001583 // the same with newVariable replaced with existingItem; the binding works
1584 // the same except that for one ObjCForCollectionStmt::getElement() returns
1585 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001586 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001587
Ted Kremenek514de5a2008-11-11 17:10:00 +00001588 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Ted Kremenek514de5a2008-11-11 17:10:00 +00001590 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001591 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001592 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001593 LoopSuccessor = Block;
1594 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001595 } else
1596 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001597
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001598 // Build the condition blocks.
1599 CFGBlock* ExitConditionBlock = createBlock(false);
1600 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001601
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001602 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001603 ExitConditionBlock->setTerminator(S);
1604
1605 // The last statement in the block should be the ObjCForCollectionStmt, which
1606 // performs the actual binding to 'element' and determines if there are any
1607 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001608 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001609 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001610
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001611 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001612 // generate new blocks as necesary. We DON'T add the statement by default to
1613 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001614 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001615 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001616 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001617 return 0;
1618 Block = 0;
1619 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
1621 // The condition block is the implicit successor for the loop body as well as
1622 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001623 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001624
Ted Kremenek514de5a2008-11-11 17:10:00 +00001625 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001626 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001627 // Save the current values for Succ, continue and break targets.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001628 SaveAndRestore<CFGBlock*> save_Succ(Succ);
1629 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1630 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001631
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001632 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
1633 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001634
Ted Kremenek4f880632009-07-17 22:18:43 +00001635 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001636
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001637 if (!BodyBlock)
1638 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001639 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001640 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001641 return 0;
1642 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001643
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001644 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001645 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001646 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001647
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001648 // Link up the condition block with the code that follows the loop.
1649 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001650 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001651
Ted Kremenek514de5a2008-11-11 17:10:00 +00001652 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001653 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001654 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001655}
1656
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001657CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1658 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001659
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001660 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001661 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001662
Ted Kremenekda5348e2009-05-05 23:11:51 +00001663 // The sync body starts its own basic block. This makes it a little easier
1664 // for diagnostic clients.
1665 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001666 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001667 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001668
Ted Kremenekda5348e2009-05-05 23:11:51 +00001669 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001670 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001671 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001672
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001673 // Add the @synchronized to the CFG.
1674 autoCreateBlock();
1675 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1676
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001677 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001678 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001679}
Mike Stump6d9828c2009-07-17 01:31:16 +00001680
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001681CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001682 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001683 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001684}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001685
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001686CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001687 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001688
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001689 // Save local scope position because in case of condition variable ScopePos
1690 // won't be restored when traversing AST.
1691 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
1692
1693 // Create local scope for possible condition variable.
1694 // Store scope position for continue statement.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001695 LocalScope::const_iterator LoopBeginScopePos = ScopePos;
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001696 if (VarDecl* VD = W->getConditionVariable()) {
1697 addLocalScopeForVarDecl(VD);
1698 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1699 }
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001700
Mike Stumpfefb9f72009-07-21 01:12:51 +00001701 // "while" is a control-flow statement. Thus we stop processing the current
1702 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001703 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001704 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001705 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001706 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001707 } else
1708 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001709
1710 // Because of short-circuit evaluation, the condition of the loop can span
1711 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1712 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001713 CFGBlock* ExitConditionBlock = createBlock(false);
1714 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001715
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001716 // Set the terminator for the "exit" condition block.
1717 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001718
1719 // Now add the actual condition to the condition block. Because the condition
1720 // itself may contain control-flow, new blocks may be created. Thus we update
1721 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001722 if (Stmt* C = W->getCond()) {
1723 Block = ExitConditionBlock;
1724 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001725 assert(Block == EntryConditionBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001726
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001727 // If this block contains a condition variable, add both the condition
1728 // variable and initializer to the CFG.
1729 if (VarDecl *VD = W->getConditionVariable()) {
1730 if (Expr *Init = VD->getInit()) {
1731 autoCreateBlock();
1732 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1733 EntryConditionBlock = addStmt(Init);
1734 assert(Block == EntryConditionBlock);
1735 }
1736 }
1737
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001738 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001739 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001740 return 0;
1741 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001742 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001743
1744 // The condition block is the implicit successor for the loop body as well as
1745 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001746 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001747
Mike Stump00998a02009-07-23 23:25:26 +00001748 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001749 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001750
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001751 // Process the loop body.
1752 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001753 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001754
1755 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001756 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1757 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1758 save_break(BreakJumpTarget);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001759
Mike Stump6d9828c2009-07-17 01:31:16 +00001760 // Create an empty block to represent the transition block for looping back
1761 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001762 Block = 0;
1763 assert(Succ == EntryConditionBlock);
1764 Succ = createBlock();
1765 Succ->setLoopTarget(W);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001766 ContinueJumpTarget = JumpTarget(Succ, LoopBeginScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001767
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001768 // All breaks should go to the code following the loop.
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001769 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001770
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001771 // NULL out Block to force lazy instantiation of blocks for the body.
1772 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001773
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001774 // Loop body should end with destructor of Condition variable (if any).
1775 addAutomaticObjDtors(ScopePos, LoopBeginScopePos, W);
1776
1777 // If body is not a compound statement create implicit scope
1778 // and add destructors.
1779 if (!isa<CompoundStmt>(W->getBody()))
1780 addLocalScopeAndDtors(W->getBody());
1781
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001782 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001783 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001784
Ted Kremenekaf603f72007-08-30 18:39:40 +00001785 if (!BodyBlock)
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001786 BodyBlock = ContinueJumpTarget.Block; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001787 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001788 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001789 return 0;
1790 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001791
Ted Kremenek941fde82009-07-24 04:47:11 +00001792 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001793 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001794 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001795
Ted Kremenek941fde82009-07-24 04:47:11 +00001796 // Link up the condition block with the code that follows the loop. (the
1797 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001798 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001799
1800 // There can be no more statements in the condition block since we loop back
1801 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001802 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001803
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001804 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001805 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001806 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001807}
Mike Stump1eb44332009-09-09 15:08:12 +00001808
1809
Ted Kremenek4f880632009-07-17 22:18:43 +00001810CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1811 // FIXME: For now we pretend that @catch and the code it contains does not
1812 // exit.
1813 return Block;
1814}
Mike Stump6d9828c2009-07-17 01:31:16 +00001815
Ted Kremenek2fda5042008-12-09 20:20:09 +00001816CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1817 // FIXME: This isn't complete. We basically treat @throw like a return
1818 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001819
Ted Kremenek6c249722009-09-24 18:45:41 +00001820 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001821 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001822 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001823
Ted Kremenek2fda5042008-12-09 20:20:09 +00001824 // Create the new block.
1825 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001826
Ted Kremenek2fda5042008-12-09 20:20:09 +00001827 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001828 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001829
1830 // Add the statement to the block. This may create new blocks if S contains
1831 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001832 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001833}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001834
Mike Stump0979d802009-07-22 22:56:04 +00001835CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001836 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001837 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001838 return 0;
1839
1840 // Create the new block.
1841 Block = createBlock(false);
1842
Mike Stump5d1d2022010-01-19 02:20:09 +00001843 if (TryTerminatedBlock)
1844 // The current try statement is the only successor.
1845 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001846 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001847 // otherwise the Exit block is the only successor.
1848 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001849
1850 // Add the statement to the block. This may create new blocks if S contains
1851 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001852 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001853}
1854
Ted Kremenek4f880632009-07-17 22:18:43 +00001855CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001856 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
Mike Stump8f9893a2009-07-21 01:27:50 +00001858 // "do...while" is a control-flow statement. Thus we stop processing the
1859 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001860 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001861 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001862 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001863 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001864 } else
1865 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001866
1867 // Because of short-circuit evaluation, the condition of the loop can span
1868 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1869 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001870 CFGBlock* ExitConditionBlock = createBlock(false);
1871 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001872
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001873 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001874 ExitConditionBlock->setTerminator(D);
1875
1876 // Now add the actual condition to the condition block. Because the condition
1877 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001878 if (Stmt* C = D->getCond()) {
1879 Block = ExitConditionBlock;
1880 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001881 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001882 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001883 return 0;
1884 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001885 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001886
Ted Kremenek54827132008-02-27 07:20:00 +00001887 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001888 Succ = EntryConditionBlock;
1889
Mike Stump00998a02009-07-23 23:25:26 +00001890 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001891 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001892
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001893 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001894 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001895 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001896 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001897
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001898 // Save the current values for Block, Succ, and continue and break targets
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001899 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ);
1900 SaveAndRestore<JumpTarget> save_continue(ContinueJumpTarget),
1901 save_break(BreakJumpTarget);
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001903 // All continues within this loop should go to the condition block
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001904 ContinueJumpTarget = JumpTarget(EntryConditionBlock, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001905
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001906 // All breaks should go to the code following the loop.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001907 BreakJumpTarget = JumpTarget(LoopSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00001908
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001909 // NULL out Block to force lazy instantiation of blocks for the body.
1910 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001911
Marcin Swiderski05adedc2010-10-01 01:14:17 +00001912 // If body is not a compound statement create implicit scope
1913 // and add destructors.
1914 if (!isa<CompoundStmt>(D->getBody()))
1915 addLocalScopeAndDtors(D->getBody());
1916
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001917 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001918 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001919
Ted Kremenekaf603f72007-08-30 18:39:40 +00001920 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001921 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001922 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001923 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001924 return 0;
1925 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001926
Ted Kremenekd173dc72010-08-17 20:59:56 +00001927 if (!KnownVal.isFalse()) {
1928 // Add an intermediate block between the BodyBlock and the
1929 // ExitConditionBlock to represent the "loop back" transition. Create an
1930 // empty block to represent the transition block for looping back to the
1931 // head of the loop.
1932 // FIXME: Can we do this more efficiently without adding another block?
1933 Block = NULL;
1934 Succ = BodyBlock;
1935 CFGBlock *LoopBackBlock = createBlock();
1936 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001937
Ted Kremenekd173dc72010-08-17 20:59:56 +00001938 // Add the loop body entry as a successor to the condition.
1939 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1940 }
1941 else
1942 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001943 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001944
Ted Kremenek941fde82009-07-24 04:47:11 +00001945 // Link up the condition block with the code that follows the loop.
1946 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001947 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001948
1949 // There can be no more statements in the body block(s) since we loop back to
1950 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001951 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001952
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001953 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001954 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001955 return BodyBlock;
1956}
1957
1958CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1959 // "continue" is a control-flow statement. Thus we stop processing the
1960 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001961 if (badCFG)
1962 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001963
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001964 // Now create a new block that ends with the continue statement.
1965 Block = createBlock(false);
1966 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001967
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001968 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001969 // incomplete AST. This means the CFG cannot be constructed.
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001970 if (ContinueJumpTarget.Block) {
Marcin Swiderskifcb72ac2010-10-01 00:23:17 +00001971 addAutomaticObjDtors(ScopePos, ContinueJumpTarget.ScopePos, C);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00001972 AddSuccessor(Block, ContinueJumpTarget.Block);
1973 } else
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001974 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001975
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001976 return Block;
1977}
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001979CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001980 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001981
Ted Kremenek852274d2009-12-16 03:18:58 +00001982 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001983 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001984 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001985 }
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Ted Kremenek4f880632009-07-17 22:18:43 +00001987 // VLA types have expressions that must be evaluated.
1988 if (E->isArgumentType()) {
1989 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1990 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1991 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001992 }
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Mike Stump6d9828c2009-07-17 01:31:16 +00001994 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001995}
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Ted Kremenek4f880632009-07-17 22:18:43 +00001997/// VisitStmtExpr - Utility method to handle (nested) statement
1998/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001999CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
2000 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002001 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002002 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00002003 }
Ted Kremenek4f880632009-07-17 22:18:43 +00002004 return VisitCompoundStmt(SE->getSubStmt());
2005}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002006
Ted Kremenek411cdee2008-04-16 21:10:48 +00002007CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002008 // "switch" is a control-flow statement. Thus we stop processing the current
2009 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002010 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002011
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002012 // Save local scope position because in case of condition variable ScopePos
2013 // won't be restored when traversing AST.
2014 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2015
2016 // Create local scope for possible condition variable.
2017 // Store scope position. Add implicit destructor.
2018 if (VarDecl* VD = Terminator->getConditionVariable()) {
2019 LocalScope::const_iterator SwitchBeginScopePos = ScopePos;
2020 addLocalScopeForVarDecl(VD);
2021 addAutomaticObjDtors(ScopePos, SwitchBeginScopePos, Terminator);
2022 }
2023
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002024 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002025 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002026 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002027 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00002028 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002029
2030 // Save the current "switch" context.
2031 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002032 save_default(DefaultCaseBlock);
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002033 SaveAndRestore<JumpTarget> save_break(BreakJumpTarget);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002034
Mike Stump6d9828c2009-07-17 01:31:16 +00002035 // Set the "default" case to be the block after the switch statement. If the
2036 // switch statement contains a "default:", this value will be overwritten with
2037 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002038 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00002039
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002040 // Create a new block that will contain the switch statement.
2041 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00002042
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002043 // Now process the switch body. The code after the switch is the implicit
2044 // successor.
2045 Succ = SwitchSuccessor;
Marcin Swiderskif1308c72010-09-25 11:05:21 +00002046 BreakJumpTarget = JumpTarget(SwitchSuccessor, ScopePos);
Mike Stump6d9828c2009-07-17 01:31:16 +00002047
2048 // When visiting the body, the case statements should automatically get linked
2049 // up to the switch. We also don't keep a pointer to the body, since all
2050 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002051 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002052 Block = NULL;
Marcin Swiderski8ae60582010-10-01 01:24:41 +00002053
2054 // If body is not a compound statement create implicit scope
2055 // and add destructors.
2056 if (!isa<CompoundStmt>(Terminator->getBody()))
2057 addLocalScopeAndDtors(Terminator->getBody());
2058
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002059 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002060 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002061 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002062 return 0;
2063 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002064
Mike Stump6d9828c2009-07-17 01:31:16 +00002065 // If we have no "default:" case, the default transition is to the code
2066 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002067 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002068
Ted Kremenek49af7cb2007-08-27 19:46:09 +00002069 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002070 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002071 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002072 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002073 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00002074
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002075 // Finally, if the SwitchStmt contains a condition variable, add both the
2076 // SwitchStmt and the condition variable initialization to the CFG.
2077 if (VarDecl *VD = Terminator->getConditionVariable()) {
2078 if (Expr *Init = VD->getInit()) {
2079 autoCreateBlock();
2080 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
2081 addStmt(Init);
2082 }
2083 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00002084
Ted Kremenek6b501eb2009-12-24 00:39:26 +00002085 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002086}
2087
Ted Kremenek4f880632009-07-17 22:18:43 +00002088CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002089 // CaseStmts are essentially labels, so they are the first statement in a
2090 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002091 CFGBlock *TopBlock = 0, *LastBlock = 0;
2092
2093 if (Stmt *Sub = CS->getSubStmt()) {
2094 // For deeply nested chains of CaseStmts, instead of doing a recursion
2095 // (which can blow out the stack), manually unroll and create blocks
2096 // along the way.
2097 while (isa<CaseStmt>(Sub)) {
2098 CFGBlock *CurrentBlock = createBlock(false);
2099 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002100
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002101 if (TopBlock)
2102 AddSuccessor(LastBlock, CurrentBlock);
2103 else
2104 TopBlock = CurrentBlock;
2105
2106 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
2107 LastBlock = CurrentBlock;
2108
2109 CS = cast<CaseStmt>(Sub);
2110 Sub = CS->getSubStmt();
2111 }
2112
2113 addStmt(Sub);
2114 }
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Ted Kremenek29ccaa12007-08-30 18:48:11 +00002116 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002117 if (!CaseBlock)
2118 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002119
2120 // Cases statements partition blocks, so this is the top of the basic block we
2121 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00002122 CaseBlock->setLabel(CS);
2123
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002124 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002125 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00002126
2127 // Add this block to the list of successors for the block with the switch
2128 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00002129 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002130 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00002131
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002132 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2133 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002134
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002135 if (TopBlock) {
2136 AddSuccessor(LastBlock, CaseBlock);
2137 Succ = TopBlock;
2138 }
2139 else {
2140 // This block is now the implicit successor of other blocks.
2141 Succ = CaseBlock;
2142 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002143
Ted Kremenek0fc67e22010-08-04 23:54:30 +00002144 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002145}
Mike Stump6d9828c2009-07-17 01:31:16 +00002146
Ted Kremenek411cdee2008-04-16 21:10:48 +00002147CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00002148 if (Terminator->getSubStmt())
2149 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002151 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00002152
2153 if (!DefaultCaseBlock)
2154 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002155
2156 // Default statements partition blocks, so this is the top of the basic block
2157 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00002158 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00002159
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002160 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00002161 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002162
Mike Stump6d9828c2009-07-17 01:31:16 +00002163 // Unlike case statements, we don't add the default block to the successors
2164 // for the switch statement immediately. This is done when we finish
2165 // processing the switch statement. This allows for the default case
2166 // (including a fall-through to the code after the switch statement) to always
2167 // be the last successor of a switch-terminated block.
2168
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002169 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2170 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002171
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00002172 // This block is now the implicit successor of other blocks.
2173 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00002174
2175 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00002176}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002177
Mike Stump5d1d2022010-01-19 02:20:09 +00002178CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
2179 // "try"/"catch" is a control-flow statement. Thus we stop processing the
2180 // current block.
2181 CFGBlock* TrySuccessor = NULL;
2182
2183 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002184 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002185 return 0;
2186 TrySuccessor = Block;
2187 } else TrySuccessor = Succ;
2188
Mike Stumpa1f93632010-01-20 01:15:34 +00002189 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00002190
2191 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002192 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00002193 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00002194 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00002195
Mike Stumpa1f93632010-01-20 01:15:34 +00002196 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00002197 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
2198 // The code after the try is the implicit successor.
2199 Succ = TrySuccessor;
2200 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00002201 if (CS->getExceptionDecl() == 0) {
2202 HasCatchAll = true;
2203 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002204 Block = NULL;
2205 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
2206 if (CatchBlock == 0)
2207 return 0;
2208 // Add this block to the list of successors for the block with the try
2209 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00002210 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00002211 }
Mike Stumpa1f93632010-01-20 01:15:34 +00002212 if (!HasCatchAll) {
2213 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00002214 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00002215 else
Mike Stumpf00cca52010-01-20 01:30:58 +00002216 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00002217 }
Mike Stump5d1d2022010-01-19 02:20:09 +00002218
2219 // The code after the try is the implicit successor.
2220 Succ = TrySuccessor;
2221
Mike Stumpf00cca52010-01-20 01:30:58 +00002222 // Save the current "try" context.
2223 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
2224 TryTerminatedBlock = NewTryTerminatedBlock;
2225
Ted Kremenek6db0ad32010-01-19 20:46:35 +00002226 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00002227 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002228 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00002229 return Block;
2230}
2231
2232CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
2233 // CXXCatchStmt are treated like labels, so they are the first statement in a
2234 // block.
2235
Marcin Swiderski0e97bcb2010-10-01 01:46:52 +00002236 // Save local scope position because in case of exception variable ScopePos
2237 // won't be restored when traversing AST.
2238 SaveAndRestore<LocalScope::const_iterator> save_scope_pos(ScopePos);
2239
2240 // Create local scope for possible exception variable.
2241 // Store scope position. Add implicit destructor.
2242 if (VarDecl* VD = CS->getExceptionDecl()) {
2243 LocalScope::const_iterator BeginScopePos = ScopePos;
2244 addLocalScopeForVarDecl(VD);
2245 addAutomaticObjDtors(ScopePos, BeginScopePos, CS);
2246 }
2247
Mike Stump5d1d2022010-01-19 02:20:09 +00002248 if (CS->getHandlerBlock())
2249 addStmt(CS->getHandlerBlock());
2250
2251 CFGBlock* CatchBlock = Block;
2252 if (!CatchBlock)
2253 CatchBlock = createBlock();
2254
2255 CatchBlock->setLabel(CS);
2256
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002257 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00002258 return 0;
2259
2260 // We set Block to NULL to allow lazy creation of a new block (if necessary)
2261 Block = NULL;
2262
2263 return CatchBlock;
2264}
2265
Ted Kremenekad5a8942010-08-02 23:46:59 +00002266CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002267 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002268 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002269 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002270 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00002271 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00002272 return VisitChildren(C);
2273}
2274
Ted Kremenek19bb3562007-08-28 19:26:49 +00002275CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002276 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00002277 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00002278
Ted Kremenek19bb3562007-08-28 19:26:49 +00002279 if (!IBlock) {
2280 IBlock = createBlock(false);
2281 cfg->setIndirectGotoBlock(IBlock);
2282 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002283
Ted Kremenek19bb3562007-08-28 19:26:49 +00002284 // IndirectGoto is a control-flow statement. Thus we stop processing the
2285 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00002286 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00002287 return 0;
2288
Ted Kremenek19bb3562007-08-28 19:26:49 +00002289 Block = createBlock(false);
2290 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002291 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00002292 return addStmt(I->getTarget());
2293}
2294
Ted Kremenekbefef2f2007-08-23 21:26:19 +00002295} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00002296
Mike Stump6d9828c2009-07-17 01:31:16 +00002297/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
2298/// no successors or predecessors. If this is the first block created in the
2299/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00002300CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00002301 bool first_block = begin() == end();
2302
2303 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002304 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
2305 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
2306 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00002307
2308 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002309 if (first_block)
2310 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00002311
2312 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002313 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00002314}
2315
Ted Kremenek026473c2007-08-23 16:51:22 +00002316/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
2317/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00002318CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00002319 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00002320 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00002321 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00002322}
2323
Ted Kremenek63f58872007-10-01 19:33:33 +00002324//===----------------------------------------------------------------------===//
2325// CFG: Queries for BlkExprs.
2326//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00002327
Ted Kremenek63f58872007-10-01 19:33:33 +00002328namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00002329 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00002330}
2331
Ted Kremenek8a693662009-12-23 23:37:10 +00002332static void FindSubExprAssignments(Stmt *S,
2333 llvm::SmallPtrSet<Expr*,50>& Set) {
2334 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002335 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002336
Ted Kremenek8a693662009-12-23 23:37:10 +00002337 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00002338 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00002339 if (!child)
2340 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00002341
Ted Kremenek8a693662009-12-23 23:37:10 +00002342 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002343 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00002344
Ted Kremenek8a693662009-12-23 23:37:10 +00002345 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002346 }
2347}
2348
Ted Kremenek63f58872007-10-01 19:33:33 +00002349static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
2350 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00002351
2352 // Look for assignments that are used as subexpressions. These are the only
2353 // assignments that we want to *possibly* register as a block-level
2354 // expression. Basically, if an assignment occurs both in a subexpression and
2355 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002356 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00002357
Ted Kremenek63f58872007-10-01 19:33:33 +00002358 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002359 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002360 if (CFGStmt S = BI->getAs<CFGStmt>())
2361 FindSubExprAssignments(S, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00002362
Ted Kremenek411cdee2008-04-16 21:10:48 +00002363 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002364
2365 // Iterate over the statements again on identify the Expr* and Stmt* at the
2366 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002367
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002368 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI) {
2369 CFGStmt CS = BI->getAs<CFGStmt>();
2370 if (!CS.isValid())
2371 continue;
2372 if (Expr* Exp = dyn_cast<Expr>(CS.getStmt())) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002373
Ted Kremenek411cdee2008-04-16 21:10:48 +00002374 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002375 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00002376 // expression are really "statements" whose value is never used by
2377 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002378 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002379 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002380 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
2381 // Special handling for statement expressions. The last statement in
2382 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002383 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00002384 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002385 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00002386 (*M)[C->body_back()] = x;
2387 }
2388 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00002389
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002390 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00002391 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00002392 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002393 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002394
Ted Kremenek411cdee2008-04-16 21:10:48 +00002395 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00002396
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002397 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00002398
Ted Kremenek390e48b2008-11-12 21:11:49 +00002399 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00002400 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002401 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002402 }
2403 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002404
Ted Kremenek63f58872007-10-01 19:33:33 +00002405 return M;
2406}
2407
Ted Kremenek86946742008-01-17 20:48:37 +00002408CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
2409 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00002410 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00002411
Ted Kremenek63f58872007-10-01 19:33:33 +00002412 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00002413 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002414 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00002415}
2416
2417unsigned CFG::getNumBlkExprs() {
2418 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
2419 return M->size();
2420 else {
2421 // We assume callers interested in the number of BlkExprs will want
2422 // the map constructed if it doesn't already exist.
2423 BlkExprMap = (void*) PopulateBlkExprMap(*this);
2424 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
2425 }
2426}
2427
Ted Kremenek274f4332008-04-28 18:00:46 +00002428//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002429// Filtered walking of the CFG.
2430//===----------------------------------------------------------------------===//
2431
2432bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00002433 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002434
2435 if (F.IgnoreDefaultsWithCoveredEnums) {
2436 // If the 'To' has no label or is labeled but the label isn't a
2437 // CaseStmt then filter this edge.
2438 if (const SwitchStmt *S =
Ted Kremenekbe39a562010-09-09 02:57:48 +00002439 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002440 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00002441 const Stmt *L = To->getLabel();
2442 if (!L || !isa<CaseStmt>(L))
2443 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00002444 }
2445 }
2446 }
2447
2448 return false;
2449}
2450
2451//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00002452// Cleanup: CFG dstor.
2453//===----------------------------------------------------------------------===//
2454
Ted Kremenek63f58872007-10-01 19:33:33 +00002455CFG::~CFG() {
2456 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
2457}
Mike Stump6d9828c2009-07-17 01:31:16 +00002458
Ted Kremenek7dba8602007-08-29 21:56:09 +00002459//===----------------------------------------------------------------------===//
2460// CFG pretty printing
2461//===----------------------------------------------------------------------===//
2462
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00002463namespace {
2464
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002465class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002466 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002467 typedef llvm::DenseMap<Decl*,std::pair<unsigned,unsigned> > DeclMapTy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002468 StmtMapTy StmtMap;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002469 DeclMapTy DeclMap;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002470 signed CurrentBlock;
2471 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00002472 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002473public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002474
Chris Lattnere4f21422009-06-30 01:26:17 +00002475 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
2476 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002477 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
2478 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002479 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002480 BI != BEnd; ++BI, ++j ) {
2481 if (CFGStmt SE = BI->getAs<CFGStmt>()) {
2482 std::pair<unsigned, unsigned> P((*I)->getBlockID(), j);
2483 StmtMap[SE] = P;
2484
2485 if (DeclStmt* DS = dyn_cast<DeclStmt>(SE.getStmt())) {
2486 DeclMap[DS->getSingleDecl()] = P;
2487
2488 } else if (IfStmt* IS = dyn_cast<IfStmt>(SE.getStmt())) {
2489 if (VarDecl* VD = IS->getConditionVariable())
2490 DeclMap[VD] = P;
2491
2492 } else if (ForStmt* FS = dyn_cast<ForStmt>(SE.getStmt())) {
2493 if (VarDecl* VD = FS->getConditionVariable())
2494 DeclMap[VD] = P;
2495
2496 } else if (WhileStmt* WS = dyn_cast<WhileStmt>(SE.getStmt())) {
2497 if (VarDecl* VD = WS->getConditionVariable())
2498 DeclMap[VD] = P;
2499
2500 } else if (SwitchStmt* SS = dyn_cast<SwitchStmt>(SE.getStmt())) {
2501 if (VarDecl* VD = SS->getConditionVariable())
2502 DeclMap[VD] = P;
2503
2504 } else if (CXXCatchStmt* CS = dyn_cast<CXXCatchStmt>(SE.getStmt())) {
2505 if (VarDecl* VD = CS->getExceptionDecl())
2506 DeclMap[VD] = P;
2507 }
2508 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002509 }
Zhongxing Xub36cd3e2010-09-16 01:25:47 +00002510 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002511 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002512
Ted Kremenek42a509f2007-08-31 21:30:12 +00002513 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002514
Chris Lattnere4f21422009-06-30 01:26:17 +00002515 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002516 void setBlockID(signed i) { CurrentBlock = i; }
2517 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00002518
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002519 virtual bool handledStmt(Stmt* S, llvm::raw_ostream& OS) {
2520 StmtMapTy::iterator I = StmtMap.find(S);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002521
2522 if (I == StmtMap.end())
2523 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002524
2525 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002526 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002527 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002528 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002529
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002530 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002531 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002532 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002533
2534 bool handleDecl(Decl* D, llvm::raw_ostream& OS) {
2535 DeclMapTy::iterator I = DeclMap.find(D);
2536
2537 if (I == DeclMap.end())
2538 return false;
2539
2540 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
2541 && I->second.second == CurrentStmt) {
2542 return false;
2543 }
2544
2545 OS << "[B" << I->second.first << "." << I->second.second << "]";
2546 return true;
2547 }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002548};
Chris Lattnere4f21422009-06-30 01:26:17 +00002549} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002550
Chris Lattnere4f21422009-06-30 01:26:17 +00002551
2552namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002553class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002554 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002555
Ted Kremeneka95d3752008-09-13 05:16:45 +00002556 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002557 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002558 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002559public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002560 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002561 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002562 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002563
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002564 void VisitIfStmt(IfStmt* I) {
2565 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002566 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002567 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002568
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002569 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002570 void VisitStmt(Stmt* Terminator) {
2571 Terminator->printPretty(OS, Helper, Policy);
2572 }
2573
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002574 void VisitForStmt(ForStmt* F) {
2575 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002576 if (F->getInit())
2577 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002578 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002579 if (Stmt* C = F->getCond())
2580 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002581 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002582 if (F->getInc())
2583 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002584 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002585 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002586
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002587 void VisitWhileStmt(WhileStmt* W) {
2588 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002589 if (Stmt* C = W->getCond())
2590 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002591 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002592
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002593 void VisitDoStmt(DoStmt* D) {
2594 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002595 if (Stmt* C = D->getCond())
2596 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002597 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002598
Ted Kremenek411cdee2008-04-16 21:10:48 +00002599 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002600 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002601 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002602 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002603
Mike Stump5d1d2022010-01-19 02:20:09 +00002604 void VisitCXXTryStmt(CXXTryStmt* CS) {
2605 OS << "try ...";
2606 }
2607
Ted Kremenek805e9a82007-08-31 21:49:40 +00002608 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002609 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002610 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002611 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002612
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002613 void VisitChooseExpr(ChooseExpr* C) {
2614 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002615 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002616 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002617 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002618
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002619 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2620 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002621 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002622 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002623
Ted Kremenek805e9a82007-08-31 21:49:40 +00002624 void VisitBinaryOperator(BinaryOperator* B) {
2625 if (!B->isLogicalOp()) {
2626 VisitExpr(B);
2627 return;
2628 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002629
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002630 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002631
Ted Kremenek805e9a82007-08-31 21:49:40 +00002632 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002633 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002634 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002635 return;
John McCall2de56d12010-08-25 11:45:40 +00002636 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002637 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002638 return;
2639 default:
2640 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002641 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002642 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002643
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002644 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002645 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002646 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002647};
Chris Lattnere4f21422009-06-30 01:26:17 +00002648} // end anonymous namespace
2649
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002650static void print_elem(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002651 const CFGElement &E) {
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002652 if (CFGStmt CS = E.getAs<CFGStmt>()) {
2653 Stmt *S = CS;
2654
2655 if (Helper) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002656
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002657 // special printing for statement-expressions.
2658 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
2659 CompoundStmt* Sub = SE->getSubStmt();
2660
2661 if (Sub->child_begin() != Sub->child_end()) {
2662 OS << "({ ... ; ";
2663 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
2664 OS << " })\n";
2665 return;
2666 }
2667 }
2668 // special printing for comma expressions.
2669 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
2670 if (B->getOpcode() == BO_Comma) {
2671 OS << "... , ";
2672 Helper->handledStmt(B->getRHS(),OS);
2673 OS << '\n';
2674 return;
2675 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002676 }
2677 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002678 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002679
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002680 if (isa<CXXOperatorCallExpr>(S)) {
2681 OS << " (OperatorCall)";
Mike Stump6d9828c2009-07-17 01:31:16 +00002682 }
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002683 else if (isa<CXXBindTemporaryExpr>(S)) {
2684 OS << " (BindTemporary)";
2685 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002686
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002687 // Expressions need a newline.
2688 if (isa<Expr>(S))
2689 OS << '\n';
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002690
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002691 } else if (CFGInitializer IE = E.getAs<CFGInitializer>()) {
2692 CXXBaseOrMemberInitializer* I = IE;
2693 if (I->isBaseInitializer())
2694 OS << I->getBaseClass()->getAsCXXRecordDecl()->getName();
2695 else OS << I->getMember()->getName();
Mike Stump6d9828c2009-07-17 01:31:16 +00002696
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002697 OS << "(";
2698 if (Expr* IE = I->getInit())
2699 IE->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2700 OS << ")";
2701
2702 if (I->isBaseInitializer())
2703 OS << " (Base initializer)\n";
2704 else OS << " (Member initializer)\n";
2705
2706 } else if (CFGAutomaticObjDtor DE = E.getAs<CFGAutomaticObjDtor>()){
2707 VarDecl* VD = DE.getVarDecl();
2708 Helper->handleDecl(VD, OS);
2709
2710 Type* T = VD->getType().getTypePtr();
2711 if (const ReferenceType* RT = T->getAs<ReferenceType>())
2712 T = RT->getPointeeType().getTypePtr();
2713
2714 OS << ".~" << T->getAsCXXRecordDecl()->getName().str() << "()";
2715 OS << " (Implicit destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00002716
2717 } else if (CFGBaseDtor BE = E.getAs<CFGBaseDtor>()) {
2718 const CXXBaseSpecifier *BS = BE.getBaseSpecifier();
2719 OS << "~" << BS->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00002720 OS << " (Base object destructor)\n";
Marcin Swiderski7c625d82010-10-05 05:37:00 +00002721
2722 } else if (CFGMemberDtor ME = E.getAs<CFGMemberDtor>()) {
2723 FieldDecl *FD = ME.getFieldDecl();
2724 OS << "this->" << FD->getName();
2725 OS << ".~" << FD->getType()->getAsCXXRecordDecl()->getName() << "()";
Zhongxing Xu4e493e02010-10-05 08:38:06 +00002726 OS << " (Member object destructor)\n";
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002727 }
2728 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002729
Chris Lattnere4f21422009-06-30 01:26:17 +00002730static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2731 const CFGBlock& B,
2732 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002733
Ted Kremenek42a509f2007-08-31 21:30:12 +00002734 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002735
Ted Kremenek7dba8602007-08-29 21:56:09 +00002736 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002737 OS << "\n [ B" << B.getBlockID();
2738
Ted Kremenek42a509f2007-08-31 21:30:12 +00002739 if (&B == &cfg->getEntry())
2740 OS << " (ENTRY) ]\n";
2741 else if (&B == &cfg->getExit())
2742 OS << " (EXIT) ]\n";
2743 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002744 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002745 else
2746 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002747
Ted Kremenek9cffe732007-08-29 23:20:49 +00002748 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002749 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002750
2751 if (print_edges)
2752 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002753
Mike Stump079bd722010-01-19 22:00:14 +00002754 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002755 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002756 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002757 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002758 C->getLHS()->printPretty(OS, Helper,
2759 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002760 if (C->getRHS()) {
2761 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002762 C->getRHS()->printPretty(OS, Helper,
2763 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002764 }
Mike Stump079bd722010-01-19 22:00:14 +00002765 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002766 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002767 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002768 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002769 if (CS->getExceptionDecl())
2770 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2771 0);
2772 else
2773 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002774 OS << ")";
2775
2776 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002777 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002778
Ted Kremenek9cffe732007-08-29 23:20:49 +00002779 OS << ":\n";
2780 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002781
Ted Kremenekfddd5182007-08-21 21:42:03 +00002782 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002783 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002784
Ted Kremenek42a509f2007-08-31 21:30:12 +00002785 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2786 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002787
Ted Kremenek9cffe732007-08-29 23:20:49 +00002788 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002789 if (print_edges)
2790 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002791
Ted Kremeneka95d3752008-09-13 05:16:45 +00002792 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002793
Ted Kremenek42a509f2007-08-31 21:30:12 +00002794 if (Helper)
2795 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002796
Marcin Swiderski1cff1322010-09-21 05:58:15 +00002797 print_elem(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002798 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002799
Ted Kremenek9cffe732007-08-29 23:20:49 +00002800 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002801 if (B.getTerminator()) {
2802 if (print_edges)
2803 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002804
Ted Kremenek9cffe732007-08-29 23:20:49 +00002805 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002806
Ted Kremenek42a509f2007-08-31 21:30:12 +00002807 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002808
Chris Lattnere4f21422009-06-30 01:26:17 +00002809 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2810 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002811 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002812 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002813 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002814
Ted Kremenek9cffe732007-08-29 23:20:49 +00002815 if (print_edges) {
2816 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002817 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002818 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002819
Ted Kremenek42a509f2007-08-31 21:30:12 +00002820 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2821 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002822
Ted Kremenek42a509f2007-08-31 21:30:12 +00002823 if (i == 8 || (i-8) == 0)
2824 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002825
Ted Kremenek9cffe732007-08-29 23:20:49 +00002826 OS << " B" << (*I)->getBlockID();
2827 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002828
Ted Kremenek42a509f2007-08-31 21:30:12 +00002829 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002830
Ted Kremenek42a509f2007-08-31 21:30:12 +00002831 // Print the successors of this block.
2832 OS << " Successors (" << B.succ_size() << "):";
2833 i = 0;
2834
2835 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2836 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002837
Ted Kremenek42a509f2007-08-31 21:30:12 +00002838 if (i == 8 || (i-8) % 10 == 0)
2839 OS << "\n ";
2840
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002841 if (*I)
2842 OS << " B" << (*I)->getBlockID();
2843 else
2844 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002845 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002846
Ted Kremenek9cffe732007-08-29 23:20:49 +00002847 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002848 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002849}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002850
Ted Kremenek42a509f2007-08-31 21:30:12 +00002851
2852/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002853void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002854
2855/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002856void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2857 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002858
Ted Kremenek42a509f2007-08-31 21:30:12 +00002859 // Print the entry block.
2860 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002861
Ted Kremenek42a509f2007-08-31 21:30:12 +00002862 // Iterate through the CFGBlocks and print them one by one.
2863 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2864 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002865 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002866 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002867
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002868 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002869 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002870
Ted Kremenek42a509f2007-08-31 21:30:12 +00002871 // Print the exit block.
2872 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002873 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002874}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002875
2876/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002877void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2878 print(llvm::errs(), cfg, LO);
2879}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002880
2881/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2882/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002883void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2884 const LangOptions &LO) const {
2885 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002886 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002887}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002888
Ted Kremeneka2925852008-01-30 23:02:42 +00002889/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002890void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002891 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002892 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002893 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2894}
2895
Ted Kremenek390e48b2008-11-12 21:11:49 +00002896Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002897
Ted Kremenek411cdee2008-04-16 21:10:48 +00002898 if (!Terminator)
2899 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002900
Ted Kremenek411cdee2008-04-16 21:10:48 +00002901 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002902
Ted Kremenek411cdee2008-04-16 21:10:48 +00002903 switch (Terminator->getStmtClass()) {
2904 default:
2905 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002906
Ted Kremenek411cdee2008-04-16 21:10:48 +00002907 case Stmt::ForStmtClass:
2908 E = cast<ForStmt>(Terminator)->getCond();
2909 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002910
Ted Kremenek411cdee2008-04-16 21:10:48 +00002911 case Stmt::WhileStmtClass:
2912 E = cast<WhileStmt>(Terminator)->getCond();
2913 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002914
Ted Kremenek411cdee2008-04-16 21:10:48 +00002915 case Stmt::DoStmtClass:
2916 E = cast<DoStmt>(Terminator)->getCond();
2917 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002918
Ted Kremenek411cdee2008-04-16 21:10:48 +00002919 case Stmt::IfStmtClass:
2920 E = cast<IfStmt>(Terminator)->getCond();
2921 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002922
Ted Kremenek411cdee2008-04-16 21:10:48 +00002923 case Stmt::ChooseExprClass:
2924 E = cast<ChooseExpr>(Terminator)->getCond();
2925 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002926
Ted Kremenek411cdee2008-04-16 21:10:48 +00002927 case Stmt::IndirectGotoStmtClass:
2928 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2929 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002930
Ted Kremenek411cdee2008-04-16 21:10:48 +00002931 case Stmt::SwitchStmtClass:
2932 E = cast<SwitchStmt>(Terminator)->getCond();
2933 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002934
Ted Kremenek411cdee2008-04-16 21:10:48 +00002935 case Stmt::ConditionalOperatorClass:
2936 E = cast<ConditionalOperator>(Terminator)->getCond();
2937 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002938
Ted Kremenek411cdee2008-04-16 21:10:48 +00002939 case Stmt::BinaryOperatorClass: // '&&' and '||'
2940 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002941 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002942
Ted Kremenek390e48b2008-11-12 21:11:49 +00002943 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002944 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002945 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002946
Ted Kremenek411cdee2008-04-16 21:10:48 +00002947 return E ? E->IgnoreParens() : NULL;
2948}
2949
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002950bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002951
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002952 if (!Terminator)
2953 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002954
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002955 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002956
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002957 switch (Terminator->getStmtClass()) {
2958 default:
2959 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002960
2961 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002962 case Stmt::WhileStmtClass:
2963 case Stmt::DoStmtClass:
2964 case Stmt::IfStmtClass:
2965 case Stmt::ChooseExprClass:
2966 case Stmt::ConditionalOperatorClass:
2967 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002968 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002969 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002970
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002971 return E ? E->IgnoreParens() : NULL;
2972}
2973
Ted Kremeneka2925852008-01-30 23:02:42 +00002974
Ted Kremenek7dba8602007-08-29 21:56:09 +00002975//===----------------------------------------------------------------------===//
2976// CFG Graphviz Visualization
2977//===----------------------------------------------------------------------===//
2978
Ted Kremenek42a509f2007-08-31 21:30:12 +00002979
2980#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002981static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002982#endif
2983
Chris Lattnere4f21422009-06-30 01:26:17 +00002984void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002985#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002986 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002987 GraphHelper = &H;
2988 llvm::ViewGraph(this,"CFG");
2989 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002990#endif
2991}
2992
Ted Kremenek7dba8602007-08-29 21:56:09 +00002993namespace llvm {
2994template<>
2995struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002996
2997 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2998
2999 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00003000
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003001#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00003002 std::string OutSStr;
3003 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00003004 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00003005 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00003006
3007 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3008
3009 // Process string output to make it nicer...
3010 for (unsigned i = 0; i != OutStr.length(); ++i)
3011 if (OutStr[i] == '\n') { // Left justify
3012 OutStr[i] = '\\';
3013 OutStr.insert(OutStr.begin()+i+1, 'l');
3014 }
Mike Stump6d9828c2009-07-17 01:31:16 +00003015
Ted Kremenek7dba8602007-08-29 21:56:09 +00003016 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00003017#else
3018 return "";
3019#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00003020 }
3021};
3022} // end namespace llvm