blob: 78979a4feeb4a655b082d957903fe3632dfd51e8 [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
Ted Kremeneka34ea072008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000056/// The builder is stateful: an instance of the builder should be used to only
57/// construct a single CFG.
58///
59/// Example usage:
60///
61/// CFGBuilder builder;
62/// CFG* cfg = builder.BuildAST(stmt1);
63///
Mike Stump6d9828c2009-07-17 01:31:16 +000064/// CFG construction is done via a recursive walk of an AST. We actually parse
65/// the AST in reverse order so that the successor of a basic block is
66/// constructed prior to its predecessor. This allows us to nicely capture
67/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000072
Ted Kremenekfddd5182007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000080
Ted Kremenek19bb3562007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000084
85 // A list of blocks that end with a "goto" that must be backpatched to their
86 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000089
Ted Kremenek19bb3562007-08-28 19:26:49 +000090 // A list of labels whose address has been taken (for indirect gotos).
91 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
92 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000093
94public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000095 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
96 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000097 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +000098 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
99 TryTerminatedBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000100
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000101 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
103 bool pruneTriviallyFalseEdges, bool AddEHEdges,
Mike Stump4c45aa12010-01-21 15:20:48 +0000104 bool AddScopes);
Mike Stump6d9828c2009-07-17 01:31:16 +0000105
Ted Kremenek4f880632009-07-17 22:18:43 +0000106private:
107 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000108 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
109 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
110 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000111 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000112 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
113 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
114 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000115 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000116 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000117 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000118 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000119 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000120 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000121 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000122 CFGBlock *VisitDeclStmt(DeclStmt *DS);
123 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000124 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
125 CFGBlock *VisitDoStmt(DoStmt *D);
126 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000127 CFGBlock *VisitGotoStmt(GotoStmt* G);
128 CFGBlock *VisitIfStmt(IfStmt *I);
129 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
130 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000131 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000132 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
133 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
134 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
135 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
136 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
137 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000138 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
139 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000140 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
141 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000142
Ted Kremenek852274d2009-12-16 03:18:58 +0000143 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
144 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000145 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000146
Ted Kremenek274f4332008-04-28 18:00:46 +0000147 // NYS == Not Yet Supported
148 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000149 badCFG = true;
150 return Block;
151 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000152
Mike Stump079bd722010-01-19 22:00:14 +0000153 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
154 if (!AddScopes)
155 return B;
156
157 if (B == 0)
158 B = createBlock();
159 B->StartScope(S, cfg->getBumpVectorContext());
160 return B;
161 }
162
163 void EndScope(Stmt *S) {
164 if (!AddScopes)
165 return;
166
167 if (Block == 0)
168 Block = createBlock();
169 Block->EndScope(S, cfg->getBumpVectorContext());
170 }
171
Ted Kremenek4f880632009-07-17 22:18:43 +0000172 void autoCreateBlock() { if (!Block) Block = createBlock(); }
173 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000174 bool FinishBlock(CFGBlock* B);
Zhongxing Xudf119892010-06-03 06:43:23 +0000175 CFGBlock *addStmt(Stmt *S) {
176 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000177 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000178
Ted Kremenek852274d2009-12-16 03:18:58 +0000179 void AppendStmt(CFGBlock *B, Stmt *S,
180 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
181 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000182 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000183
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000184 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
185 B->addSuccessor(S, cfg->getBumpVectorContext());
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000188 /// TryResult - a class representing a variant over the values
189 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
190 /// and is used by the CFGBuilder to decide if a branch condition
191 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000192 class TryResult {
193 int X;
194 public:
195 TryResult(bool b) : X(b ? 1 : 0) {}
196 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Ted Kremenek941fde82009-07-24 04:47:11 +0000198 bool isTrue() const { return X == 1; }
199 bool isFalse() const { return X == 0; }
200 bool isKnown() const { return X >= 0; }
201 void negate() {
202 assert(isKnown());
203 X ^= 0x1;
204 }
205 };
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Mike Stump00998a02009-07-23 23:25:26 +0000207 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
208 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000209 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000210 if (!PruneTriviallyFalseEdges)
211 return TryResult();
212
Mike Stump00998a02009-07-23 23:25:26 +0000213 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000214 if (!S->isTypeDependent() && !S->isValueDependent() &&
215 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000216 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000217
218 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000219 }
220
Ted Kremenek4102af92008-03-13 03:04:22 +0000221 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000222
Ted Kremenekad5a8942010-08-02 23:46:59 +0000223 // True iff trivially false edges should be pruned from the CFG.
224 bool PruneTriviallyFalseEdges;
225
Mike Stump4c45aa12010-01-21 15:20:48 +0000226 // True iff EH edges on CallExprs should be added to the CFG.
227 bool AddEHEdges;
228
229 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump079bd722010-01-19 22:00:14 +0000230 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000231};
Mike Stump6d9828c2009-07-17 01:31:16 +0000232
Douglas Gregor898574e2008-12-05 23:32:09 +0000233// FIXME: Add support for dependent-sized array types in C++?
234// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000235static VariableArrayType* FindVA(Type* t) {
236 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
237 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
238 if (vat->getSizeExpr())
239 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000240
Ted Kremenek610a09e2008-09-26 22:58:57 +0000241 t = vt->getElementType().getTypePtr();
242 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000243
Ted Kremenek610a09e2008-09-26 22:58:57 +0000244 return 0;
245}
Mike Stump6d9828c2009-07-17 01:31:16 +0000246
247/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
248/// arbitrary statement. Examples include a single expression or a function
249/// body (compound statement). The ownership of the returned CFG is
250/// transferred to the caller. If CFG construction fails, this method returns
251/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000252CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenekad5a8942010-08-02 23:46:59 +0000253 bool pruneTriviallyFalseEdges,
Mike Stump55f988e2010-01-21 17:21:23 +0000254 bool addehedges, bool AddScopes) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000255
Mike Stump55f988e2010-01-21 17:21:23 +0000256 AddEHEdges = addehedges;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000257 PruneTriviallyFalseEdges = pruneTriviallyFalseEdges;
258
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000259 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000260 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000261 if (!Statement)
262 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000263
Mike Stump079bd722010-01-19 22:00:14 +0000264 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000265 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000266
267 // Create an empty block that will serve as the exit block for the CFG. Since
268 // this is the first block added to the CFG, it will be implicitly registered
269 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000270 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000271 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000272 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000273
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000274 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000275 CFGBlock* B = addStmt(Statement);
Mike Stumpb978a442010-01-21 02:21:40 +0000276
277 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
278 // FIXME: Add code for base initializers and member initializers.
279 (void)CD;
280 }
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000281 if (!B)
282 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000283
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000284 if (B) {
285 // Finalize the last constructed block. This usually involves reversing the
286 // order of the statements in the block.
287 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000288
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000289 // Backpatch the gotos whose label -> block mappings we didn't know when we
290 // encountered them.
291 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000292 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000293
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000294 CFGBlock* B = *I;
295 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
296 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000297
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000298 // If there is no target for the goto, then we are looking at an
299 // incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000300 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000301
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000302 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000303 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000304
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000305 // Add successors to the Indirect Goto Dispatch block (if we have one).
306 if (CFGBlock* B = cfg->getIndirectGotoBlock())
307 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
308 E = AddressTakenLabels.end(); I != E; ++I ) {
309
310 // Lookup the target block.
311 LabelMapTy::iterator LI = LabelMap.find(*I);
312
313 // If there is no target block that contains label, then we are looking
314 // at an incomplete AST. Handle this by not registering a successor.
315 if (LI == LabelMap.end()) continue;
316
317 AddSuccessor(B, LI->second);
318 }
319
320 Succ = B;
321 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000322
323 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000324 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000325
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000326 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000327}
Mike Stump6d9828c2009-07-17 01:31:16 +0000328
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000329/// createBlock - Used to lazily create blocks that are connected
330/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000331CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000332 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000333 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000334 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000335 return B;
336}
Mike Stump6d9828c2009-07-17 01:31:16 +0000337
Ted Kremenek6c249722009-09-24 18:45:41 +0000338/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000339bool CFGBuilder::FinishBlock(CFGBlock* B) {
340 if (badCFG)
341 return false;
342
Ted Kremenek4f880632009-07-17 22:18:43 +0000343 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000344 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000345}
346
Ted Kremenek4f880632009-07-17 22:18:43 +0000347/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000348/// blocks for ternary operators, &&, and ||. We also process "," and
349/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000350CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000351tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000352 if (!S) {
353 badCFG = true;
354 return 0;
355 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 switch (S->getStmtClass()) {
357 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000358 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000359
360 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000361 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek4f880632009-07-17 22:18:43 +0000363 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000364 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek4f880632009-07-17 22:18:43 +0000366 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000367 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000368
Ted Kremenek4f880632009-07-17 22:18:43 +0000369 case Stmt::BreakStmtClass:
370 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek4f880632009-07-17 22:18:43 +0000372 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000373 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek4f880632009-07-17 22:18:43 +0000375 case Stmt::CaseStmtClass:
376 return VisitCaseStmt(cast<CaseStmt>(S));
377
378 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000379 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381 case Stmt::CompoundStmtClass:
382 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek4f880632009-07-17 22:18:43 +0000384 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000385 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek4f880632009-07-17 22:18:43 +0000387 case Stmt::ContinueStmtClass:
388 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek021c8af2010-01-19 20:40:33 +0000390 case Stmt::CXXCatchStmtClass:
391 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
392
Ted Kremenek47e331e2010-08-28 00:19:02 +0000393 case Stmt::CXXExprWithTemporariesClass: {
394 // FIXME: Handle temporaries. For now, just visit the subexpression
395 // so we don't artificially create extra blocks.
396 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr());
397 }
398
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000399 case Stmt::CXXMemberCallExprClass:
400 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
401
Ted Kremenek021c8af2010-01-19 20:40:33 +0000402 case Stmt::CXXThrowExprClass:
403 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000404
Ted Kremenek021c8af2010-01-19 20:40:33 +0000405 case Stmt::CXXTryStmtClass:
406 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000407
Ted Kremenek4f880632009-07-17 22:18:43 +0000408 case Stmt::DeclStmtClass:
409 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek4f880632009-07-17 22:18:43 +0000411 case Stmt::DefaultStmtClass:
412 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremenek4f880632009-07-17 22:18:43 +0000414 case Stmt::DoStmtClass:
415 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek4f880632009-07-17 22:18:43 +0000417 case Stmt::ForStmtClass:
418 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek4f880632009-07-17 22:18:43 +0000420 case Stmt::GotoStmtClass:
421 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek4f880632009-07-17 22:18:43 +0000423 case Stmt::IfStmtClass:
424 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek4f880632009-07-17 22:18:43 +0000426 case Stmt::IndirectGotoStmtClass:
427 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek4f880632009-07-17 22:18:43 +0000429 case Stmt::LabelStmtClass:
430 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek115c1b92010-04-11 17:02:10 +0000432 case Stmt::MemberExprClass:
433 return VisitMemberExpr(cast<MemberExpr>(S), asc);
434
Ted Kremenek4f880632009-07-17 22:18:43 +0000435 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000436 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
437
Ted Kremenek4f880632009-07-17 22:18:43 +0000438 case Stmt::ObjCAtSynchronizedStmtClass:
439 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek4f880632009-07-17 22:18:43 +0000441 case Stmt::ObjCAtThrowStmtClass:
442 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek4f880632009-07-17 22:18:43 +0000444 case Stmt::ObjCAtTryStmtClass:
445 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek4f880632009-07-17 22:18:43 +0000447 case Stmt::ObjCForCollectionStmtClass:
448 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Ted Kremenek4f880632009-07-17 22:18:43 +0000450 case Stmt::ParenExprClass:
451 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000452 goto tryAgain;
453
Ted Kremenek4f880632009-07-17 22:18:43 +0000454 case Stmt::NullStmtClass:
455 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek4f880632009-07-17 22:18:43 +0000457 case Stmt::ReturnStmtClass:
458 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek4f880632009-07-17 22:18:43 +0000460 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000461 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek4f880632009-07-17 22:18:43 +0000463 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000464 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Ted Kremenek4f880632009-07-17 22:18:43 +0000466 case Stmt::SwitchStmtClass:
467 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek4f880632009-07-17 22:18:43 +0000469 case Stmt::WhileStmtClass:
470 return VisitWhileStmt(cast<WhileStmt>(S));
471 }
472}
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Ted Kremenek852274d2009-12-16 03:18:58 +0000474CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
475 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000476 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000477 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000478 }
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek4f880632009-07-17 22:18:43 +0000480 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000481}
Mike Stump6d9828c2009-07-17 01:31:16 +0000482
Ted Kremenek4f880632009-07-17 22:18:43 +0000483/// VisitChildren - Visit the children of a Stmt.
484CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
485 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000486 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000487 E = Terminator->child_end(); I != E; ++I) {
488 if (*I) B = Visit(*I);
489 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000490 return B;
491}
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek852274d2009-12-16 03:18:58 +0000493CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
494 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000495 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000496
Ted Kremenek852274d2009-12-16 03:18:58 +0000497 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000498 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000499 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000500 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000501
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000502 return Block;
503}
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek852274d2009-12-16 03:18:58 +0000505CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
506 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000507 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000508 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000509 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Ted Kremenek4f880632009-07-17 22:18:43 +0000511 if (!FinishBlock(ConfluenceBlock))
512 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Ted Kremenek4f880632009-07-17 22:18:43 +0000514 // create the block evaluating the LHS
515 CFGBlock* LHSBlock = createBlock(false);
516 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Ted Kremenek4f880632009-07-17 22:18:43 +0000518 // create the block evaluating the RHS
519 Succ = ConfluenceBlock;
520 Block = NULL;
521 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000522
523 if (RHSBlock) {
524 if (!FinishBlock(RHSBlock))
525 return 0;
526 }
527 else {
528 // Create an empty block for cases where the RHS doesn't require
529 // any explicit statements in the CFG.
530 RHSBlock = createBlock();
531 }
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Mike Stump00998a02009-07-23 23:25:26 +0000533 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000534 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000535 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000536 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000537
Ted Kremenek4f880632009-07-17 22:18:43 +0000538 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000539 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000540 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
541 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000542 } else {
John McCall2de56d12010-08-25 11:45:40 +0000543 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000544 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
545 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Ted Kremenek4f880632009-07-17 22:18:43 +0000548 // Generate the blocks for evaluating the LHS.
549 Block = LHSBlock;
550 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000551 }
John McCall2de56d12010-08-25 11:45:40 +0000552 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000553 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000554 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000555 addStmt(B->getRHS());
556 return addStmt(B->getLHS());
557 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000558 else if (B->isAssignmentOp()) {
559 if (asc.alwaysAdd()) {
560 autoCreateBlock();
561 AppendStmt(Block, B, asc);
562 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000563
Zhongxing Xufc61d942010-06-03 06:23:18 +0000564 Visit(B->getRHS());
565 return Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek852274d2009-12-16 03:18:58 +0000568 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000569}
570
Ted Kremenek852274d2009-12-16 03:18:58 +0000571CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
572 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000573 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000574 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000575 }
576 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000577}
578
Ted Kremenek4f880632009-07-17 22:18:43 +0000579CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
580 // "break" is a control-flow statement. Thus we stop processing the current
581 // block.
582 if (Block && !FinishBlock(Block))
583 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Ted Kremenek4f880632009-07-17 22:18:43 +0000585 // Now create a new block that ends with the break statement.
586 Block = createBlock(false);
587 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Ted Kremenek4f880632009-07-17 22:18:43 +0000589 // If there is no target for the break, then we are looking at an incomplete
590 // AST. This means that the CFG cannot be constructed.
591 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000592 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000593 else
594 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000595
596
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000597 return Block;
598}
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Mike Stump4c45aa12010-01-21 15:20:48 +0000600static bool CanThrow(Expr *E) {
601 QualType Ty = E->getType();
602 if (Ty->isFunctionPointerType())
603 Ty = Ty->getAs<PointerType>()->getPointeeType();
604 else if (Ty->isBlockPointerType())
605 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +0000606
Mike Stump4c45aa12010-01-21 15:20:48 +0000607 const FunctionType *FT = Ty->getAs<FunctionType>();
608 if (FT) {
609 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
610 if (Proto->hasEmptyExceptionSpec())
611 return false;
612 }
613 return true;
614}
615
Ted Kremenek852274d2009-12-16 03:18:58 +0000616CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000617 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000618 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000619 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000620 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000621 }
Mike Stump24556362009-07-25 21:26:53 +0000622
Mike Stump4c45aa12010-01-21 15:20:48 +0000623 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000624
625 // Languages without exceptions are assumed to not throw.
626 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000627 if (AddEHEdges)
628 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000629 }
630
631 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000632 if (FD->hasAttr<NoReturnAttr>())
633 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000634 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000635 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000636 }
Mike Stump24556362009-07-25 21:26:53 +0000637
Mike Stump4c45aa12010-01-21 15:20:48 +0000638 if (!CanThrow(C->getCallee()))
639 AddEHEdge = false;
640
Zhongxing Xufc61d942010-06-03 06:23:18 +0000641 if (!NoReturn && !AddEHEdge) {
642 if (asc.asLValue())
643 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
644 else
645 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Mike Stump079bd722010-01-19 22:00:14 +0000648 if (Block) {
649 Succ = Block;
650 if (!FinishBlock(Block))
651 return 0;
652 }
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Mike Stump079bd722010-01-19 22:00:14 +0000654 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000655 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000656
Mike Stump079bd722010-01-19 22:00:14 +0000657 if (NoReturn) {
658 // Wire this to the exit block directly.
659 AddSuccessor(Block, &cfg->getExit());
660 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000661 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000662 // Add exceptional edges.
663 if (TryTerminatedBlock)
664 AddSuccessor(Block, TryTerminatedBlock);
665 else
666 AddSuccessor(Block, &cfg->getExit());
667 }
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Mike Stump24556362009-07-25 21:26:53 +0000669 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000670}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000671
Ted Kremenek852274d2009-12-16 03:18:58 +0000672CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
673 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000674 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000675 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000676 if (!FinishBlock(ConfluenceBlock))
677 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek115c1b92010-04-11 17:02:10 +0000679 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
680 : AddStmtChoice::AlwaysAdd;
681
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000682 Succ = ConfluenceBlock;
683 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000684 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000685 if (!FinishBlock(LHSBlock))
686 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000688 Succ = ConfluenceBlock;
689 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000690 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000691 if (!FinishBlock(RHSBlock))
692 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000694 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000695 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000696 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000697 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
698 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000699 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000700 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000701}
Mike Stump1eb44332009-09-09 15:08:12 +0000702
703
704CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000705 EndScope(C);
706
Mike Stump1eb44332009-09-09 15:08:12 +0000707 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000708
709 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
710 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +0000711 // If we hit a segment of code just containing ';' (NullStmts), we can
712 // get a null block back. In such cases, just use the LastBlock
713 if (CFGBlock *newBlock = addStmt(*I))
714 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000716 if (badCFG)
717 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000718 }
Mike Stump079bd722010-01-19 22:00:14 +0000719
720 LastBlock = StartScope(C, LastBlock);
721
Ted Kremenek4f880632009-07-17 22:18:43 +0000722 return LastBlock;
723}
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek852274d2009-12-16 03:18:58 +0000725CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
726 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000727 // Create the confluence block that will "merge" the results of the ternary
728 // expression.
729 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000730 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000731 if (!FinishBlock(ConfluenceBlock))
732 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek115c1b92010-04-11 17:02:10 +0000734 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
735 : AddStmtChoice::AlwaysAdd;
736
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000737 // Create a block for the LHS expression if there is an LHS expression. A
738 // GCC extension allows LHS to be NULL, causing the condition to be the
739 // value that is returned instead.
740 // e.g: x ?: y is shorthand for: x ? x : y;
741 Succ = ConfluenceBlock;
742 Block = NULL;
743 CFGBlock* LHSBlock = NULL;
744 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +0000745 LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000746 if (!FinishBlock(LHSBlock))
747 return 0;
748 Block = NULL;
749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000751 // Create the block for the RHS expression.
752 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +0000753 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000754 if (!FinishBlock(RHSBlock))
755 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000757 // Create the block that will contain the condition.
758 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Mike Stump00998a02009-07-23 23:25:26 +0000760 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000761 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000762 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000763 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000764 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000765 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000766 // If we know the condition is false, add NULL as the successor for
767 // the block containing the condition. In this case, the confluence
768 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000769 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000770 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000771 } else {
772 // If we have no LHS expression, add the ConfluenceBlock as a direct
773 // successor for the block containing the condition. Moreover, we need to
774 // reverse the order of the predecessors in the ConfluenceBlock because
775 // the RHSBlock will have been added to the succcessors already, and we
776 // want the first predecessor to the the block containing the expression
777 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000778 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000779 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000780 std::reverse(ConfluenceBlock->pred_begin(),
781 ConfluenceBlock->pred_end());
782 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000785 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000786 Block->setTerminator(C);
787 return addStmt(C->getCond());
788}
789
Ted Kremenek4f880632009-07-17 22:18:43 +0000790CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
791 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000792
Ted Kremenek4f880632009-07-17 22:18:43 +0000793 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000794 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000795 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek4f880632009-07-17 22:18:43 +0000798 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek4f880632009-07-17 22:18:43 +0000800 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
801 typedef llvm::SmallVector<Decl*,10> BufTy;
802 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek4f880632009-07-17 22:18:43 +0000804 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
805 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
806 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
807 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek4f880632009-07-17 22:18:43 +0000809 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
810 // automatically freed with the CFG.
811 DeclGroupRef DG(*I);
812 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000813 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000814 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek4f880632009-07-17 22:18:43 +0000816 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000817 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000818 B = VisitDeclSubExpr(D);
819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
821 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000822}
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenek4f880632009-07-17 22:18:43 +0000824/// VisitDeclSubExpr - Utility method to add block-level expressions for
825/// initializers in Decls.
826CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
827 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000828
Ted Kremenek4f880632009-07-17 22:18:43 +0000829 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek4f880632009-07-17 22:18:43 +0000831 if (!VD)
832 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Ted Kremenek4f880632009-07-17 22:18:43 +0000834 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Ted Kremenek4f880632009-07-17 22:18:43 +0000836 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000837 AddStmtChoice::Kind k =
838 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
839 : AddStmtChoice::NotAlwaysAdd;
840 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek4f880632009-07-17 22:18:43 +0000843 // If the type of VD is a VLA, then we must process its size expressions.
844 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
845 VA = FindVA(VA->getElementType().getTypePtr()))
846 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek4f880632009-07-17 22:18:43 +0000848 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000849}
850
851CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000852 // We may see an if statement in the middle of a basic block, or it may be the
853 // first statement we are processing. In either case, we create a new basic
854 // block. First, we create the blocks for the then...else statements, and
855 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000856 // middle of a block, we stop processing that block. That block is then the
857 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000858
859 // The block we were proccessing is now finished. Make it the successor
860 // block.
861 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000862 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000863 if (!FinishBlock(Block))
864 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000865 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000866
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000867 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000868 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000869
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000870 if (Stmt* Else = I->getElse()) {
871 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000872
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000873 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000874 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000875 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000876 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000877
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000878 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
879 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000880 else if (Block) {
881 if (!FinishBlock(ElseBlock))
882 return 0;
883 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000884 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000885
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000886 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000887 CFGBlock* ThenBlock;
888 {
889 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000890 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000891 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000892 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000893 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000894
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000895 if (!ThenBlock) {
896 // We can reach here if the "then" body has all NullStmts.
897 // Create an empty block so we can distinguish between true and false
898 // branches in path-sensitive analyses.
899 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000900 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000901 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000902 if (!FinishBlock(ThenBlock))
903 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000904 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000905 }
906
Mike Stump6d9828c2009-07-17 01:31:16 +0000907 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000908 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000909
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000910 // Set the terminator of the new block to the If statement.
911 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000912
Mike Stump00998a02009-07-23 23:25:26 +0000913 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000914 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000915
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000916 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000917 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
918 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000919
920 // Add the condition as the last statement in the new block. This may create
921 // new blocks as the condition may contain control-flow. Any newly created
922 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000923 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +0000924
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000925 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
926 // and the condition variable initialization to the CFG.
927 if (VarDecl *VD = I->getConditionVariable()) {
928 if (Expr *Init = VD->getInit()) {
929 autoCreateBlock();
930 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
931 addStmt(Init);
932 }
933 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000934
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000935 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000936}
Mike Stump6d9828c2009-07-17 01:31:16 +0000937
938
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000939CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000940 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000941 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000942 // NOTE: If a "return" appears in the middle of a block, this means that the
943 // code afterwards is DEAD (unreachable). We still keep a basic block
944 // for that code; a simple "mark-and-sweep" from the entry block will be
945 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000946 if (Block)
947 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000948
949 // Create the new block.
950 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000951
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000952 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000953 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000954
955 // Add the return statement to the block. This may create new blocks if R
956 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000957 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000958}
959
960CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
961 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000962 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000963 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000964
Ted Kremenek4f880632009-07-17 22:18:43 +0000965 if (!LabelBlock) // This can happen when the body is empty, i.e.
966 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000967
Ted Kremenek4f880632009-07-17 22:18:43 +0000968 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000969 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000970
971 // Labels partition blocks, so this is the end of the basic block we were
972 // processing (L is the block's label). Because this is label (and we have
973 // already processed the substatement) there is no extra control-flow to worry
974 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000975 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000976 if (!FinishBlock(LabelBlock))
977 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000978
979 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000980 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000981
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000982 // This block is now the implicit successor of other blocks.
983 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000984
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000985 return LabelBlock;
986}
987
988CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000989 // Goto is a control-flow statement. Thus we stop processing the current
990 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000991 if (Block)
992 FinishBlock(Block);
993
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000994 Block = createBlock(false);
995 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000996
997 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000998 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000999
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001000 if (I == LabelMap.end())
1001 // We will need to backpatch this block later.
1002 BackpatchBlocks.push_back(Block);
1003 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001004 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001005
Mike Stump6d9828c2009-07-17 01:31:16 +00001006 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001007}
1008
1009CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001010 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001011
Mike Stumpfefb9f72009-07-21 01:12:51 +00001012 // "for" is a control-flow statement. Thus we stop processing the current
1013 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001014 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001015 if (!FinishBlock(Block))
1016 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001017 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001018 } else
1019 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001020
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001021 // Save the current value for the break targets.
1022 // All breaks should go to the code following the loop.
1023 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
1024 BreakTargetBlock = LoopSuccessor;
1025
Mike Stump6d9828c2009-07-17 01:31:16 +00001026 // Because of short-circuit evaluation, the condition of the loop can span
1027 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1028 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001029 CFGBlock* ExitConditionBlock = createBlock(false);
1030 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001031
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001032 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001033 ExitConditionBlock->setTerminator(F);
1034
1035 // Now add the actual condition to the condition block. Because the condition
1036 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001037 if (Stmt* C = F->getCond()) {
1038 Block = ExitConditionBlock;
1039 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001040 assert(Block == EntryConditionBlock);
1041
1042 // If this block contains a condition variable, add both the condition
1043 // variable and initializer to the CFG.
1044 if (VarDecl *VD = F->getConditionVariable()) {
1045 if (Expr *Init = VD->getInit()) {
1046 autoCreateBlock();
1047 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1048 EntryConditionBlock = addStmt(Init);
1049 assert(Block == EntryConditionBlock);
1050 }
1051 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001052
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001053 if (Block) {
1054 if (!FinishBlock(EntryConditionBlock))
1055 return 0;
1056 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001057 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001058
Mike Stump6d9828c2009-07-17 01:31:16 +00001059 // The condition block is the implicit successor for the loop body as well as
1060 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001061 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001062
Mike Stump00998a02009-07-23 23:25:26 +00001063 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001064 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Mike Stump00998a02009-07-23 23:25:26 +00001066 if (F->getCond())
1067 KnownVal = TryEvaluateBool(F->getCond());
1068
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001069 // Now create the loop body.
1070 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001071 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001072
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001073 // Save the current values for Block, Succ, and continue targets.
1074 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1075 save_continue(ContinueTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001076
Ted Kremenekaf603f72007-08-30 18:39:40 +00001077 // Create a new block to contain the (bottom) of the loop body.
1078 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001079
Ted Kremeneke9334502008-09-04 21:48:47 +00001080 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001081 // Generate increment code in its own basic block. This is the target of
1082 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001083 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001084 } else {
1085 // No increment code. Create a special, empty, block that is used as the
1086 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001087 assert(Succ == EntryConditionBlock);
1088 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001089 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001090
Ted Kremenek3575f842009-04-28 00:51:56 +00001091 // Finish up the increment (or empty) block if it hasn't been already.
1092 if (Block) {
1093 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001094 if (!FinishBlock(Block))
1095 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001096 Block = 0;
1097 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001098
Ted Kremenek3575f842009-04-28 00:51:56 +00001099 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001100
Ted Kremenek3575f842009-04-28 00:51:56 +00001101 // The starting block for the loop increment is the block that should
1102 // represent the 'loop target' for looping back to the start of the loop.
1103 ContinueTargetBlock->setLoopTarget(F);
1104
Mike Stump6d9828c2009-07-17 01:31:16 +00001105 // Now populate the body block, and in the process create new blocks as we
1106 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001107 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001108
1109 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001110 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001111 else if (Block && !FinishBlock(BodyBlock))
1112 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001113
Ted Kremenek941fde82009-07-24 04:47:11 +00001114 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001115 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001116 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001117
Ted Kremenek941fde82009-07-24 04:47:11 +00001118 // Link up the condition block with the code that follows the loop. (the
1119 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001120 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001121
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001122 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001123 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001124 if (Stmt* I = F->getInit()) {
1125 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001126 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001127 } else {
1128 // There is no loop initialization. We are thus basically a while loop.
1129 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001130 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001131 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001132 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001133 }
1134}
1135
Ted Kremenek115c1b92010-04-11 17:02:10 +00001136CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1137 if (asc.alwaysAdd()) {
1138 autoCreateBlock();
1139 AppendStmt(Block, M, asc);
1140 }
1141 return Visit(M->getBase(),
1142 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1143 : AddStmtChoice::AsLValueNotAlwaysAdd);
1144}
1145
Ted Kremenek514de5a2008-11-11 17:10:00 +00001146CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1147 // Objective-C fast enumeration 'for' statements:
1148 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1149 //
1150 // for ( Type newVariable in collection_expression ) { statements }
1151 //
1152 // becomes:
1153 //
1154 // prologue:
1155 // 1. collection_expression
1156 // T. jump to loop_entry
1157 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001158 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001159 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1160 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1161 // TB:
1162 // statements
1163 // T. jump to loop_entry
1164 // FB:
1165 // what comes after
1166 //
1167 // and
1168 //
1169 // Type existingItem;
1170 // for ( existingItem in expression ) { statements }
1171 //
1172 // becomes:
1173 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001174 // the same with newVariable replaced with existingItem; the binding works
1175 // the same except that for one ObjCForCollectionStmt::getElement() returns
1176 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001177 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001178
Ted Kremenek514de5a2008-11-11 17:10:00 +00001179 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001180
Ted Kremenek514de5a2008-11-11 17:10:00 +00001181 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001182 if (!FinishBlock(Block))
1183 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001184 LoopSuccessor = Block;
1185 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001186 } else
1187 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001188
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001189 // Build the condition blocks.
1190 CFGBlock* ExitConditionBlock = createBlock(false);
1191 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001192
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001193 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001194 ExitConditionBlock->setTerminator(S);
1195
1196 // The last statement in the block should be the ObjCForCollectionStmt, which
1197 // performs the actual binding to 'element' and determines if there are any
1198 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001199 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001200 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001201
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001202 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001203 // generate new blocks as necesary. We DON'T add the statement by default to
1204 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001205 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001206 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001207 if (!FinishBlock(EntryConditionBlock))
1208 return 0;
1209 Block = 0;
1210 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001211
1212 // The condition block is the implicit successor for the loop body as well as
1213 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001214 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001215
Ted Kremenek514de5a2008-11-11 17:10:00 +00001216 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001217 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001218 // Save the current values for Succ, continue and break targets.
1219 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001220 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1221
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001222 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001223 ContinueTargetBlock = EntryConditionBlock;
1224
Ted Kremenek4f880632009-07-17 22:18:43 +00001225 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001226
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001227 if (!BodyBlock)
1228 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001229 else if (Block) {
1230 if (!FinishBlock(BodyBlock))
1231 return 0;
1232 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001233
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001234 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001235 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001236 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001237
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001238 // Link up the condition block with the code that follows the loop.
1239 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001240 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001241
Ted Kremenek514de5a2008-11-11 17:10:00 +00001242 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001243 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001244 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001245}
1246
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001247CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1248 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001249
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001250 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001251 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
Ted Kremenekda5348e2009-05-05 23:11:51 +00001253 // The sync body starts its own basic block. This makes it a little easier
1254 // for diagnostic clients.
1255 if (SyncBlock) {
1256 if (!FinishBlock(SyncBlock))
1257 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001258
Ted Kremenekda5348e2009-05-05 23:11:51 +00001259 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001260 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001261 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001262
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001263 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001264 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001265}
Mike Stump6d9828c2009-07-17 01:31:16 +00001266
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001267CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001268 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001269 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001270}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001271
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001272CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001273 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001274
Mike Stumpfefb9f72009-07-21 01:12:51 +00001275 // "while" is a control-flow statement. Thus we stop processing the current
1276 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001277 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001278 if (!FinishBlock(Block))
1279 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001281 } else
1282 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001283
1284 // Because of short-circuit evaluation, the condition of the loop can span
1285 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1286 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001287 CFGBlock* ExitConditionBlock = createBlock(false);
1288 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001289
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001290 // Set the terminator for the "exit" condition block.
1291 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
1293 // Now add the actual condition to the condition block. Because the condition
1294 // itself may contain control-flow, new blocks may be created. Thus we update
1295 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001296 if (Stmt* C = W->getCond()) {
1297 Block = ExitConditionBlock;
1298 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001299 assert(Block == EntryConditionBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001300
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001301 // If this block contains a condition variable, add both the condition
1302 // variable and initializer to the CFG.
1303 if (VarDecl *VD = W->getConditionVariable()) {
1304 if (Expr *Init = VD->getInit()) {
1305 autoCreateBlock();
1306 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1307 EntryConditionBlock = addStmt(Init);
1308 assert(Block == EntryConditionBlock);
1309 }
1310 }
1311
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001312 if (Block) {
1313 if (!FinishBlock(EntryConditionBlock))
1314 return 0;
1315 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001316 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001317
1318 // The condition block is the implicit successor for the loop body as well as
1319 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001320 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001321
Mike Stump00998a02009-07-23 23:25:26 +00001322 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001323 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001324
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001325 // Process the loop body.
1326 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001327 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001328
1329 // Save the current values for Block, Succ, and continue and break targets
1330 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1331 save_continue(ContinueTargetBlock),
1332 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001333
Mike Stump6d9828c2009-07-17 01:31:16 +00001334 // Create an empty block to represent the transition block for looping back
1335 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001336 Block = 0;
1337 assert(Succ == EntryConditionBlock);
1338 Succ = createBlock();
1339 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001340 ContinueTargetBlock = Succ;
1341
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001342 // All breaks should go to the code following the loop.
1343 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001344
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001345 // NULL out Block to force lazy instantiation of blocks for the body.
1346 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001347
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001348 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001349 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001350
Ted Kremenekaf603f72007-08-30 18:39:40 +00001351 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001352 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001353 else if (Block) {
1354 if (!FinishBlock(BodyBlock))
1355 return 0;
1356 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001357
Ted Kremenek941fde82009-07-24 04:47:11 +00001358 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001359 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001360 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001361
Ted Kremenek941fde82009-07-24 04:47:11 +00001362 // Link up the condition block with the code that follows the loop. (the
1363 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001364 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001365
1366 // There can be no more statements in the condition block since we loop back
1367 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001368 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001369
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001370 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001371 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001372 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373}
Mike Stump1eb44332009-09-09 15:08:12 +00001374
1375
Ted Kremenek4f880632009-07-17 22:18:43 +00001376CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1377 // FIXME: For now we pretend that @catch and the code it contains does not
1378 // exit.
1379 return Block;
1380}
Mike Stump6d9828c2009-07-17 01:31:16 +00001381
Ted Kremenek2fda5042008-12-09 20:20:09 +00001382CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1383 // FIXME: This isn't complete. We basically treat @throw like a return
1384 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001385
Ted Kremenek6c249722009-09-24 18:45:41 +00001386 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001387 if (Block && !FinishBlock(Block))
1388 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001389
Ted Kremenek2fda5042008-12-09 20:20:09 +00001390 // Create the new block.
1391 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001392
Ted Kremenek2fda5042008-12-09 20:20:09 +00001393 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001394 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001395
1396 // Add the statement to the block. This may create new blocks if S contains
1397 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001398 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001399}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001400
Mike Stump0979d802009-07-22 22:56:04 +00001401CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001402 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001403 if (Block && !FinishBlock(Block))
1404 return 0;
1405
1406 // Create the new block.
1407 Block = createBlock(false);
1408
Mike Stump5d1d2022010-01-19 02:20:09 +00001409 if (TryTerminatedBlock)
1410 // The current try statement is the only successor.
1411 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001412 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001413 // otherwise the Exit block is the only successor.
1414 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001415
1416 // Add the statement to the block. This may create new blocks if S contains
1417 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001418 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001419}
1420
Ted Kremenek4f880632009-07-17 22:18:43 +00001421CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001422 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001423
Mike Stump8f9893a2009-07-21 01:27:50 +00001424 // "do...while" is a control-flow statement. Thus we stop processing the
1425 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001426 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001427 if (!FinishBlock(Block))
1428 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001429 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001430 } else
1431 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
1433 // Because of short-circuit evaluation, the condition of the loop can span
1434 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1435 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001436 CFGBlock* ExitConditionBlock = createBlock(false);
1437 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001438
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001439 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001440 ExitConditionBlock->setTerminator(D);
1441
1442 // Now add the actual condition to the condition block. Because the condition
1443 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001444 if (Stmt* C = D->getCond()) {
1445 Block = ExitConditionBlock;
1446 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001447 if (Block) {
1448 if (!FinishBlock(EntryConditionBlock))
1449 return 0;
1450 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001451 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
Ted Kremenek54827132008-02-27 07:20:00 +00001453 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001454 Succ = EntryConditionBlock;
1455
Mike Stump00998a02009-07-23 23:25:26 +00001456 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001457 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001458
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001459 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001460 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001461 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001462 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001463
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001464 // Save the current values for Block, Succ, and continue and break targets
1465 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001466 save_continue(ContinueTargetBlock),
1467 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001468
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001469 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001470 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001471
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472 // All breaks should go to the code following the loop.
1473 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001474
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001475 // NULL out Block to force lazy instantiation of blocks for the body.
1476 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001477
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001478 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001479 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001480
Ted Kremenekaf603f72007-08-30 18:39:40 +00001481 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001482 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001483 else if (Block) {
1484 if (!FinishBlock(BodyBlock))
1485 return 0;
1486 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001487
Ted Kremenekd173dc72010-08-17 20:59:56 +00001488 if (!KnownVal.isFalse()) {
1489 // Add an intermediate block between the BodyBlock and the
1490 // ExitConditionBlock to represent the "loop back" transition. Create an
1491 // empty block to represent the transition block for looping back to the
1492 // head of the loop.
1493 // FIXME: Can we do this more efficiently without adding another block?
1494 Block = NULL;
1495 Succ = BodyBlock;
1496 CFGBlock *LoopBackBlock = createBlock();
1497 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001498
Ted Kremenekd173dc72010-08-17 20:59:56 +00001499 // Add the loop body entry as a successor to the condition.
1500 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1501 }
1502 else
1503 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001504 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001505
Ted Kremenek941fde82009-07-24 04:47:11 +00001506 // Link up the condition block with the code that follows the loop.
1507 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001508 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001509
1510 // There can be no more statements in the body block(s) since we loop back to
1511 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001512 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001513
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001514 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001515 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001516 return BodyBlock;
1517}
1518
1519CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1520 // "continue" is a control-flow statement. Thus we stop processing the
1521 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001522 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001523 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001524
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001525 // Now create a new block that ends with the continue statement.
1526 Block = createBlock(false);
1527 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001528
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001529 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001530 // incomplete AST. This means the CFG cannot be constructed.
1531 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001532 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001533 else
1534 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001535
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001536 return Block;
1537}
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001539CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001540 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001541
Ted Kremenek852274d2009-12-16 03:18:58 +00001542 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001543 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001544 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001545 }
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Ted Kremenek4f880632009-07-17 22:18:43 +00001547 // VLA types have expressions that must be evaluated.
1548 if (E->isArgumentType()) {
1549 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1550 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1551 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Mike Stump6d9828c2009-07-17 01:31:16 +00001554 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001555}
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Ted Kremenek4f880632009-07-17 22:18:43 +00001557/// VisitStmtExpr - Utility method to handle (nested) statement
1558/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001559CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1560 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001561 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001562 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001563 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001564 return VisitCompoundStmt(SE->getSubStmt());
1565}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001566
Ted Kremenek411cdee2008-04-16 21:10:48 +00001567CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001568 // "switch" is a control-flow statement. Thus we stop processing the current
1569 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001570 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001571
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001572 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001573 if (!FinishBlock(Block))
1574 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001575 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001576 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001577
1578 // Save the current "switch" context.
1579 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001580 save_break(BreakTargetBlock),
1581 save_default(DefaultCaseBlock);
1582
Mike Stump6d9828c2009-07-17 01:31:16 +00001583 // Set the "default" case to be the block after the switch statement. If the
1584 // switch statement contains a "default:", this value will be overwritten with
1585 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001586 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001587
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001588 // Create a new block that will contain the switch statement.
1589 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001590
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001591 // Now process the switch body. The code after the switch is the implicit
1592 // successor.
1593 Succ = SwitchSuccessor;
1594 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001595
1596 // When visiting the body, the case statements should automatically get linked
1597 // up to the switch. We also don't keep a pointer to the body, since all
1598 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001599 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001600 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001601 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001602 if (Block) {
1603 if (!FinishBlock(BodyBlock))
1604 return 0;
1605 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001606
Mike Stump6d9828c2009-07-17 01:31:16 +00001607 // If we have no "default:" case, the default transition is to the code
1608 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001609 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001610
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001611 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001612 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001613 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001614 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001615 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001616
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001617 // Finally, if the SwitchStmt contains a condition variable, add both the
1618 // SwitchStmt and the condition variable initialization to the CFG.
1619 if (VarDecl *VD = Terminator->getConditionVariable()) {
1620 if (Expr *Init = VD->getInit()) {
1621 autoCreateBlock();
1622 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1623 addStmt(Init);
1624 }
1625 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001626
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001627 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001628}
1629
Ted Kremenek4f880632009-07-17 22:18:43 +00001630CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001631 // CaseStmts are essentially labels, so they are the first statement in a
1632 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001633 CFGBlock *TopBlock = 0, *LastBlock = 0;
1634
1635 if (Stmt *Sub = CS->getSubStmt()) {
1636 // For deeply nested chains of CaseStmts, instead of doing a recursion
1637 // (which can blow out the stack), manually unroll and create blocks
1638 // along the way.
1639 while (isa<CaseStmt>(Sub)) {
1640 CFGBlock *CurrentBlock = createBlock(false);
1641 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001642
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001643 if (TopBlock)
1644 AddSuccessor(LastBlock, CurrentBlock);
1645 else
1646 TopBlock = CurrentBlock;
1647
1648 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1649 LastBlock = CurrentBlock;
1650
1651 CS = cast<CaseStmt>(Sub);
1652 Sub = CS->getSubStmt();
1653 }
1654
1655 addStmt(Sub);
1656 }
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001658 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001659 if (!CaseBlock)
1660 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001661
1662 // Cases statements partition blocks, so this is the top of the basic block we
1663 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001664 CaseBlock->setLabel(CS);
1665
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001666 if (!FinishBlock(CaseBlock))
1667 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001668
1669 // Add this block to the list of successors for the block with the switch
1670 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001671 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001672 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001673
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001674 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1675 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001676
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001677 if (TopBlock) {
1678 AddSuccessor(LastBlock, CaseBlock);
1679 Succ = TopBlock;
1680 }
1681 else {
1682 // This block is now the implicit successor of other blocks.
1683 Succ = CaseBlock;
1684 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001685
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001686 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001687}
Mike Stump6d9828c2009-07-17 01:31:16 +00001688
Ted Kremenek411cdee2008-04-16 21:10:48 +00001689CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001690 if (Terminator->getSubStmt())
1691 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001693 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001694
1695 if (!DefaultCaseBlock)
1696 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001697
1698 // Default statements partition blocks, so this is the top of the basic block
1699 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001700 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001702 if (!FinishBlock(DefaultCaseBlock))
1703 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001704
Mike Stump6d9828c2009-07-17 01:31:16 +00001705 // Unlike case statements, we don't add the default block to the successors
1706 // for the switch statement immediately. This is done when we finish
1707 // processing the switch statement. This allows for the default case
1708 // (including a fall-through to the code after the switch statement) to always
1709 // be the last successor of a switch-terminated block.
1710
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001711 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1712 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001713
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001714 // This block is now the implicit successor of other blocks.
1715 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001716
1717 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001718}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001719
Mike Stump5d1d2022010-01-19 02:20:09 +00001720CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1721 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1722 // current block.
1723 CFGBlock* TrySuccessor = NULL;
1724
1725 if (Block) {
1726 if (!FinishBlock(Block))
1727 return 0;
1728 TrySuccessor = Block;
1729 } else TrySuccessor = Succ;
1730
Mike Stumpa1f93632010-01-20 01:15:34 +00001731 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001732
1733 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001734 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001735 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001736 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001737
Mike Stumpa1f93632010-01-20 01:15:34 +00001738 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001739 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1740 // The code after the try is the implicit successor.
1741 Succ = TrySuccessor;
1742 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001743 if (CS->getExceptionDecl() == 0) {
1744 HasCatchAll = true;
1745 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001746 Block = NULL;
1747 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1748 if (CatchBlock == 0)
1749 return 0;
1750 // Add this block to the list of successors for the block with the try
1751 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001752 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001753 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001754 if (!HasCatchAll) {
1755 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001756 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001757 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001758 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001759 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001760
1761 // The code after the try is the implicit successor.
1762 Succ = TrySuccessor;
1763
Mike Stumpf00cca52010-01-20 01:30:58 +00001764 // Save the current "try" context.
1765 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1766 TryTerminatedBlock = NewTryTerminatedBlock;
1767
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001768 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001769 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001770 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001771 return Block;
1772}
1773
1774CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1775 // CXXCatchStmt are treated like labels, so they are the first statement in a
1776 // block.
1777
1778 if (CS->getHandlerBlock())
1779 addStmt(CS->getHandlerBlock());
1780
1781 CFGBlock* CatchBlock = Block;
1782 if (!CatchBlock)
1783 CatchBlock = createBlock();
1784
1785 CatchBlock->setLabel(CS);
1786
1787 if (!FinishBlock(CatchBlock))
1788 return 0;
1789
1790 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1791 Block = NULL;
1792
1793 return CatchBlock;
1794}
1795
Ted Kremenekad5a8942010-08-02 23:46:59 +00001796CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001797 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001798 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001799 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001800 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001801 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001802 return VisitChildren(C);
1803}
1804
Ted Kremenek19bb3562007-08-28 19:26:49 +00001805CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001806 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001807 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001808
Ted Kremenek19bb3562007-08-28 19:26:49 +00001809 if (!IBlock) {
1810 IBlock = createBlock(false);
1811 cfg->setIndirectGotoBlock(IBlock);
1812 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001813
Ted Kremenek19bb3562007-08-28 19:26:49 +00001814 // IndirectGoto is a control-flow statement. Thus we stop processing the
1815 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001816 if (Block && !FinishBlock(Block))
1817 return 0;
1818
Ted Kremenek19bb3562007-08-28 19:26:49 +00001819 Block = createBlock(false);
1820 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001821 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001822 return addStmt(I->getTarget());
1823}
1824
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001825} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001826
Mike Stump6d9828c2009-07-17 01:31:16 +00001827/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1828/// no successors or predecessors. If this is the first block created in the
1829/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001830CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001831 bool first_block = begin() == end();
1832
1833 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001834 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1835 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1836 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001837
1838 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001839 if (first_block)
1840 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001841
1842 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001843 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001844}
1845
Ted Kremenek026473c2007-08-23 16:51:22 +00001846/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1847/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001848CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekad5a8942010-08-02 23:46:59 +00001849 bool PruneTriviallyFalse,
Mike Stump4c45aa12010-01-21 15:20:48 +00001850 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001851 CFGBuilder Builder;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001852 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
1853 AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001854}
1855
Ted Kremenek63f58872007-10-01 19:33:33 +00001856//===----------------------------------------------------------------------===//
1857// CFG: Queries for BlkExprs.
1858//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001859
Ted Kremenek63f58872007-10-01 19:33:33 +00001860namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001861 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001862}
1863
Ted Kremenek8a693662009-12-23 23:37:10 +00001864static void FindSubExprAssignments(Stmt *S,
1865 llvm::SmallPtrSet<Expr*,50>& Set) {
1866 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001867 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Ted Kremenek8a693662009-12-23 23:37:10 +00001869 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001870 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00001871 if (!child)
1872 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001873
Ted Kremenek8a693662009-12-23 23:37:10 +00001874 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001875 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Ted Kremenek8a693662009-12-23 23:37:10 +00001877 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001878 }
1879}
1880
Ted Kremenek63f58872007-10-01 19:33:33 +00001881static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1882 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001883
1884 // Look for assignments that are used as subexpressions. These are the only
1885 // assignments that we want to *possibly* register as a block-level
1886 // expression. Basically, if an assignment occurs both in a subexpression and
1887 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001888 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001889
Ted Kremenek63f58872007-10-01 19:33:33 +00001890 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001891 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001892 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001893
Ted Kremenek411cdee2008-04-16 21:10:48 +00001894 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001895
1896 // Iterate over the statements again on identify the Expr* and Stmt* at the
1897 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001898
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001899 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001900 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001901
Ted Kremenek411cdee2008-04-16 21:10:48 +00001902 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001903 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001904 // expression are really "statements" whose value is never used by
1905 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001906 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001907 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001908 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1909 // Special handling for statement expressions. The last statement in
1910 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001911 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001912 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001913 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001914 (*M)[C->body_back()] = x;
1915 }
1916 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001917
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001918 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001919 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001920 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001921
Ted Kremenek411cdee2008-04-16 21:10:48 +00001922 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001923
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001924 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001925
Ted Kremenek390e48b2008-11-12 21:11:49 +00001926 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001927 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001928 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001929 }
1930 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001931
Ted Kremenek63f58872007-10-01 19:33:33 +00001932 return M;
1933}
1934
Ted Kremenek86946742008-01-17 20:48:37 +00001935CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1936 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001937 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001938
Ted Kremenek63f58872007-10-01 19:33:33 +00001939 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001940 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001941 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001942}
1943
1944unsigned CFG::getNumBlkExprs() {
1945 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1946 return M->size();
1947 else {
1948 // We assume callers interested in the number of BlkExprs will want
1949 // the map constructed if it doesn't already exist.
1950 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1951 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1952 }
1953}
1954
Ted Kremenek274f4332008-04-28 18:00:46 +00001955//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001956// Cleanup: CFG dstor.
1957//===----------------------------------------------------------------------===//
1958
Ted Kremenek63f58872007-10-01 19:33:33 +00001959CFG::~CFG() {
1960 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1961}
Mike Stump6d9828c2009-07-17 01:31:16 +00001962
Ted Kremenek7dba8602007-08-29 21:56:09 +00001963//===----------------------------------------------------------------------===//
1964// CFG pretty printing
1965//===----------------------------------------------------------------------===//
1966
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001967namespace {
1968
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001969class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001970 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1971 StmtMapTy StmtMap;
1972 signed CurrentBlock;
1973 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001974 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001975public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001976
Chris Lattnere4f21422009-06-30 01:26:17 +00001977 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1978 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001979 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1980 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001981 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001982 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001983 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001984 }
1985 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001986
Ted Kremenek42a509f2007-08-31 21:30:12 +00001987 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001988
Chris Lattnere4f21422009-06-30 01:26:17 +00001989 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001990 void setBlockID(signed i) { CurrentBlock = i; }
1991 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremeneka95d3752008-09-13 05:16:45 +00001993 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenek411cdee2008-04-16 21:10:48 +00001995 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001996
1997 if (I == StmtMap.end())
1998 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001999
2000 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002001 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002002 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002003 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002004
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002005 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002006 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002007 }
2008};
Chris Lattnere4f21422009-06-30 01:26:17 +00002009} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00002010
Chris Lattnere4f21422009-06-30 01:26:17 +00002011
2012namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002013class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002014 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002015
Ted Kremeneka95d3752008-09-13 05:16:45 +00002016 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002017 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002018 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002019public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002020 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002021 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002022 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002023
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002024 void VisitIfStmt(IfStmt* I) {
2025 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002026 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002027 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002028
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002029 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002030 void VisitStmt(Stmt* Terminator) {
2031 Terminator->printPretty(OS, Helper, Policy);
2032 }
2033
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002034 void VisitForStmt(ForStmt* F) {
2035 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002036 if (F->getInit())
2037 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002038 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002039 if (Stmt* C = F->getCond())
2040 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002041 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002042 if (F->getInc())
2043 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002044 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002045 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002046
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002047 void VisitWhileStmt(WhileStmt* W) {
2048 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002049 if (Stmt* C = W->getCond())
2050 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002051 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002052
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002053 void VisitDoStmt(DoStmt* D) {
2054 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002055 if (Stmt* C = D->getCond())
2056 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002057 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002058
Ted Kremenek411cdee2008-04-16 21:10:48 +00002059 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002060 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002061 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002062 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002063
Mike Stump5d1d2022010-01-19 02:20:09 +00002064 void VisitCXXTryStmt(CXXTryStmt* CS) {
2065 OS << "try ...";
2066 }
2067
Ted Kremenek805e9a82007-08-31 21:49:40 +00002068 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002069 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002070 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002071 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002072
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002073 void VisitChooseExpr(ChooseExpr* C) {
2074 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002075 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002076 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002077 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002078
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002079 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2080 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002081 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002082 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002083
Ted Kremenek805e9a82007-08-31 21:49:40 +00002084 void VisitBinaryOperator(BinaryOperator* B) {
2085 if (!B->isLogicalOp()) {
2086 VisitExpr(B);
2087 return;
2088 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002089
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002090 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002091
Ted Kremenek805e9a82007-08-31 21:49:40 +00002092 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002093 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002094 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002095 return;
John McCall2de56d12010-08-25 11:45:40 +00002096 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002097 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002098 return;
2099 default:
2100 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002101 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002102 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002103
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002104 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002105 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002106 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002107};
Chris Lattnere4f21422009-06-30 01:26:17 +00002108} // end anonymous namespace
2109
Mike Stump6d9828c2009-07-17 01:31:16 +00002110
Chris Lattnere4f21422009-06-30 01:26:17 +00002111static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002112 const CFGElement &E) {
2113 Stmt *Terminator = E;
2114
2115 if (E.asStartScope()) {
2116 OS << "start scope\n";
2117 return;
2118 }
2119 if (E.asEndScope()) {
2120 OS << "end scope\n";
2121 return;
2122 }
2123
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002124 if (Helper) {
2125 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002126 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002127 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002128
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002129 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002130 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002131 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002132 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002133 return;
2134 }
2135 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002136
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002137 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002138 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
John McCall2de56d12010-08-25 11:45:40 +00002139 if (B->getOpcode() == BO_Comma) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002140 OS << "... , ";
2141 Helper->handledStmt(B->getRHS(),OS);
2142 OS << '\n';
2143 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002144 }
2145 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002146 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002147
Chris Lattnere4f21422009-06-30 01:26:17 +00002148 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002149
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002150 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002151 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002152}
Mike Stump6d9828c2009-07-17 01:31:16 +00002153
Chris Lattnere4f21422009-06-30 01:26:17 +00002154static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2155 const CFGBlock& B,
2156 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002157
Ted Kremenek42a509f2007-08-31 21:30:12 +00002158 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002159
Ted Kremenek7dba8602007-08-29 21:56:09 +00002160 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002161 OS << "\n [ B" << B.getBlockID();
2162
Ted Kremenek42a509f2007-08-31 21:30:12 +00002163 if (&B == &cfg->getEntry())
2164 OS << " (ENTRY) ]\n";
2165 else if (&B == &cfg->getExit())
2166 OS << " (EXIT) ]\n";
2167 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002168 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002169 else
2170 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002171
Ted Kremenek9cffe732007-08-29 23:20:49 +00002172 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002173 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002174
2175 if (print_edges)
2176 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002177
Mike Stump079bd722010-01-19 22:00:14 +00002178 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002179 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002180 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002181 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002182 C->getLHS()->printPretty(OS, Helper,
2183 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002184 if (C->getRHS()) {
2185 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002186 C->getRHS()->printPretty(OS, Helper,
2187 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002188 }
Mike Stump079bd722010-01-19 22:00:14 +00002189 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002190 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002191 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002192 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002193 if (CS->getExceptionDecl())
2194 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2195 0);
2196 else
2197 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002198 OS << ")";
2199
2200 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002201 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002202
Ted Kremenek9cffe732007-08-29 23:20:49 +00002203 OS << ":\n";
2204 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002205
Ted Kremenekfddd5182007-08-21 21:42:03 +00002206 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002207 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002208
Ted Kremenek42a509f2007-08-31 21:30:12 +00002209 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2210 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002211
Ted Kremenek9cffe732007-08-29 23:20:49 +00002212 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002213 if (print_edges)
2214 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002215
Ted Kremeneka95d3752008-09-13 05:16:45 +00002216 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002217
Ted Kremenek42a509f2007-08-31 21:30:12 +00002218 if (Helper)
2219 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002220
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002221 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002222 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002223
Ted Kremenek9cffe732007-08-29 23:20:49 +00002224 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002225 if (B.getTerminator()) {
2226 if (print_edges)
2227 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002228
Ted Kremenek9cffe732007-08-29 23:20:49 +00002229 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002230
Ted Kremenek42a509f2007-08-31 21:30:12 +00002231 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002232
Chris Lattnere4f21422009-06-30 01:26:17 +00002233 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2234 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002235 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002236 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002237 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002238
Ted Kremenek9cffe732007-08-29 23:20:49 +00002239 if (print_edges) {
2240 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002241 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002242 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002243
Ted Kremenek42a509f2007-08-31 21:30:12 +00002244 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2245 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002246
Ted Kremenek42a509f2007-08-31 21:30:12 +00002247 if (i == 8 || (i-8) == 0)
2248 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002249
Ted Kremenek9cffe732007-08-29 23:20:49 +00002250 OS << " B" << (*I)->getBlockID();
2251 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002252
Ted Kremenek42a509f2007-08-31 21:30:12 +00002253 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002254
Ted Kremenek42a509f2007-08-31 21:30:12 +00002255 // Print the successors of this block.
2256 OS << " Successors (" << B.succ_size() << "):";
2257 i = 0;
2258
2259 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2260 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002261
Ted Kremenek42a509f2007-08-31 21:30:12 +00002262 if (i == 8 || (i-8) % 10 == 0)
2263 OS << "\n ";
2264
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002265 if (*I)
2266 OS << " B" << (*I)->getBlockID();
2267 else
2268 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002269 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002270
Ted Kremenek9cffe732007-08-29 23:20:49 +00002271 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002272 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002273}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002274
Ted Kremenek42a509f2007-08-31 21:30:12 +00002275
2276/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002277void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002278
2279/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002280void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2281 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002282
Ted Kremenek42a509f2007-08-31 21:30:12 +00002283 // Print the entry block.
2284 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002285
Ted Kremenek42a509f2007-08-31 21:30:12 +00002286 // Iterate through the CFGBlocks and print them one by one.
2287 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2288 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002289 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002290 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002291
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002292 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002293 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002294
Ted Kremenek42a509f2007-08-31 21:30:12 +00002295 // Print the exit block.
2296 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002297 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002298}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002299
2300/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002301void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2302 print(llvm::errs(), cfg, LO);
2303}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002304
2305/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2306/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002307void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2308 const LangOptions &LO) const {
2309 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002310 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002311}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002312
Ted Kremeneka2925852008-01-30 23:02:42 +00002313/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002314void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002315 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002316 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002317 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2318}
2319
Ted Kremenek390e48b2008-11-12 21:11:49 +00002320Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002321
Ted Kremenek411cdee2008-04-16 21:10:48 +00002322 if (!Terminator)
2323 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002324
Ted Kremenek411cdee2008-04-16 21:10:48 +00002325 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002326
Ted Kremenek411cdee2008-04-16 21:10:48 +00002327 switch (Terminator->getStmtClass()) {
2328 default:
2329 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002330
Ted Kremenek411cdee2008-04-16 21:10:48 +00002331 case Stmt::ForStmtClass:
2332 E = cast<ForStmt>(Terminator)->getCond();
2333 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002334
Ted Kremenek411cdee2008-04-16 21:10:48 +00002335 case Stmt::WhileStmtClass:
2336 E = cast<WhileStmt>(Terminator)->getCond();
2337 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002338
Ted Kremenek411cdee2008-04-16 21:10:48 +00002339 case Stmt::DoStmtClass:
2340 E = cast<DoStmt>(Terminator)->getCond();
2341 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002342
Ted Kremenek411cdee2008-04-16 21:10:48 +00002343 case Stmt::IfStmtClass:
2344 E = cast<IfStmt>(Terminator)->getCond();
2345 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002346
Ted Kremenek411cdee2008-04-16 21:10:48 +00002347 case Stmt::ChooseExprClass:
2348 E = cast<ChooseExpr>(Terminator)->getCond();
2349 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002350
Ted Kremenek411cdee2008-04-16 21:10:48 +00002351 case Stmt::IndirectGotoStmtClass:
2352 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2353 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002354
Ted Kremenek411cdee2008-04-16 21:10:48 +00002355 case Stmt::SwitchStmtClass:
2356 E = cast<SwitchStmt>(Terminator)->getCond();
2357 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002358
Ted Kremenek411cdee2008-04-16 21:10:48 +00002359 case Stmt::ConditionalOperatorClass:
2360 E = cast<ConditionalOperator>(Terminator)->getCond();
2361 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002362
Ted Kremenek411cdee2008-04-16 21:10:48 +00002363 case Stmt::BinaryOperatorClass: // '&&' and '||'
2364 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002365 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002366
Ted Kremenek390e48b2008-11-12 21:11:49 +00002367 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002368 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002369 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002370
Ted Kremenek411cdee2008-04-16 21:10:48 +00002371 return E ? E->IgnoreParens() : NULL;
2372}
2373
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002374bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002375
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002376 if (!Terminator)
2377 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002378
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002379 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002380
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002381 switch (Terminator->getStmtClass()) {
2382 default:
2383 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002384
2385 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002386 case Stmt::WhileStmtClass:
2387 case Stmt::DoStmtClass:
2388 case Stmt::IfStmtClass:
2389 case Stmt::ChooseExprClass:
2390 case Stmt::ConditionalOperatorClass:
2391 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002392 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002393 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002394
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002395 return E ? E->IgnoreParens() : NULL;
2396}
2397
Ted Kremeneka2925852008-01-30 23:02:42 +00002398
Ted Kremenek7dba8602007-08-29 21:56:09 +00002399//===----------------------------------------------------------------------===//
2400// CFG Graphviz Visualization
2401//===----------------------------------------------------------------------===//
2402
Ted Kremenek42a509f2007-08-31 21:30:12 +00002403
2404#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002405static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002406#endif
2407
Chris Lattnere4f21422009-06-30 01:26:17 +00002408void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002409#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002410 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002411 GraphHelper = &H;
2412 llvm::ViewGraph(this,"CFG");
2413 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002414#endif
2415}
2416
Ted Kremenek7dba8602007-08-29 21:56:09 +00002417namespace llvm {
2418template<>
2419struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002420
2421 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2422
2423 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002424
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002425#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002426 std::string OutSStr;
2427 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002428 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002429 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002430
2431 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2432
2433 // Process string output to make it nicer...
2434 for (unsigned i = 0; i != OutStr.length(); ++i)
2435 if (OutStr[i] == '\n') { // Left justify
2436 OutStr[i] = '\\';
2437 OutStr.insert(OutStr.begin()+i+1, 'l');
2438 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002439
Ted Kremenek7dba8602007-08-29 21:56:09 +00002440 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002441#else
2442 return "";
2443#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002444 }
2445};
2446} // end namespace llvm