blob: 08543aacba5cba5ff2a23c71f0dbfc202f322728 [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 Kremenek852274d2009-12-16 03:18:58 +000038
39class 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.
Mike Stump4c45aa12010-01-21 15:20:48 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, bool AddEHEdges,
103 bool AddScopes);
Mike Stump6d9828c2009-07-17 01:31:16 +0000104
Ted Kremenek4f880632009-07-17 22:18:43 +0000105private:
106 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000107 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
108 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
109 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000110 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000111 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
112 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
113 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000114 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000115 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000116 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000117 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000118 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000119 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000120 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000121 CFGBlock *VisitDeclStmt(DeclStmt *DS);
122 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000123 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
124 CFGBlock *VisitDoStmt(DoStmt *D);
125 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000126 CFGBlock *VisitGotoStmt(GotoStmt* G);
127 CFGBlock *VisitIfStmt(IfStmt *I);
128 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
129 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000130 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000131 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
132 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
133 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
134 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
135 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
136 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000137 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
138 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000139 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
140 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000141
Ted Kremenek852274d2009-12-16 03:18:58 +0000142 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
143 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000144 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000145
Ted Kremenek274f4332008-04-28 18:00:46 +0000146 // NYS == Not Yet Supported
147 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000148 badCFG = true;
149 return Block;
150 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000151
Mike Stump079bd722010-01-19 22:00:14 +0000152 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
153 if (!AddScopes)
154 return B;
155
156 if (B == 0)
157 B = createBlock();
158 B->StartScope(S, cfg->getBumpVectorContext());
159 return B;
160 }
161
162 void EndScope(Stmt *S) {
163 if (!AddScopes)
164 return;
165
166 if (Block == 0)
167 Block = createBlock();
168 Block->EndScope(S, cfg->getBumpVectorContext());
169 }
170
Ted Kremenek4f880632009-07-17 22:18:43 +0000171 void autoCreateBlock() { if (!Block) Block = createBlock(); }
172 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000173 bool FinishBlock(CFGBlock* B);
Zhongxing Xudf119892010-06-03 06:43:23 +0000174 CFGBlock *addStmt(Stmt *S) {
175 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000176 }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000177
Ted Kremenek852274d2009-12-16 03:18:58 +0000178 void AppendStmt(CFGBlock *B, Stmt *S,
179 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
180 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000181 }
182
183 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
184 B->addSuccessor(S, cfg->getBumpVectorContext());
185 }
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000187 /// TryResult - a class representing a variant over the values
188 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
189 /// and is used by the CFGBuilder to decide if a branch condition
190 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000191 class TryResult {
192 int X;
193 public:
194 TryResult(bool b) : X(b ? 1 : 0) {}
195 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek941fde82009-07-24 04:47:11 +0000197 bool isTrue() const { return X == 1; }
198 bool isFalse() const { return X == 0; }
199 bool isKnown() const { return X >= 0; }
200 void negate() {
201 assert(isKnown());
202 X ^= 0x1;
203 }
204 };
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Mike Stump00998a02009-07-23 23:25:26 +0000206 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
207 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000208 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000209 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000210 if (!S->isTypeDependent() && !S->isValueDependent() &&
211 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000212 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000213
214 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000215 }
216
Ted Kremenek4102af92008-03-13 03:04:22 +0000217 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000218
219 // True iff EH edges on CallExprs should be added to the CFG.
220 bool AddEHEdges;
221
222 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump079bd722010-01-19 22:00:14 +0000223 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000224};
Mike Stump6d9828c2009-07-17 01:31:16 +0000225
Douglas Gregor898574e2008-12-05 23:32:09 +0000226// FIXME: Add support for dependent-sized array types in C++?
227// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000228static VariableArrayType* FindVA(Type* t) {
229 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
230 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
231 if (vat->getSizeExpr())
232 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000233
Ted Kremenek610a09e2008-09-26 22:58:57 +0000234 t = vt->getElementType().getTypePtr();
235 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000236
Ted Kremenek610a09e2008-09-26 22:58:57 +0000237 return 0;
238}
Mike Stump6d9828c2009-07-17 01:31:16 +0000239
240/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
241/// arbitrary statement. Examples include a single expression or a function
242/// body (compound statement). The ownership of the returned CFG is
243/// transferred to the caller. If CFG construction fails, this method returns
244/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000245CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Mike Stump55f988e2010-01-21 17:21:23 +0000246 bool addehedges, bool AddScopes) {
247 AddEHEdges = addehedges;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000248 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000249 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000250 if (!Statement)
251 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000252
Mike Stump079bd722010-01-19 22:00:14 +0000253 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000254 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000255
256 // Create an empty block that will serve as the exit block for the CFG. Since
257 // this is the first block added to the CFG, it will be implicitly registered
258 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000259 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000260 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000261 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000262
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000263 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000264 CFGBlock* B = addStmt(Statement);
Mike Stumpb978a442010-01-21 02:21:40 +0000265
266 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
267 // FIXME: Add code for base initializers and member initializers.
268 (void)CD;
269 }
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000270 if (!B)
271 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000272
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000273 if (B) {
274 // Finalize the last constructed block. This usually involves reversing the
275 // order of the statements in the block.
276 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000277
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000278 // Backpatch the gotos whose label -> block mappings we didn't know when we
279 // encountered them.
280 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000281 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000282
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000283 CFGBlock* B = *I;
284 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
285 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000286
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000287 // If there is no target for the goto, then we are looking at an
288 // incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000289 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000290
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000291 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000292 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000293
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000294 // Add successors to the Indirect Goto Dispatch block (if we have one).
295 if (CFGBlock* B = cfg->getIndirectGotoBlock())
296 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
297 E = AddressTakenLabels.end(); I != E; ++I ) {
298
299 // Lookup the target block.
300 LabelMapTy::iterator LI = LabelMap.find(*I);
301
302 // If there is no target block that contains label, then we are looking
303 // at an incomplete AST. Handle this by not registering a successor.
304 if (LI == LabelMap.end()) continue;
305
306 AddSuccessor(B, LI->second);
307 }
308
309 Succ = B;
310 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000311
312 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000313 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000314
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000315 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000316}
Mike Stump6d9828c2009-07-17 01:31:16 +0000317
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000318/// createBlock - Used to lazily create blocks that are connected
319/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000320CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000321 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000322 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000323 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000324 return B;
325}
Mike Stump6d9828c2009-07-17 01:31:16 +0000326
Ted Kremenek6c249722009-09-24 18:45:41 +0000327/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000328bool CFGBuilder::FinishBlock(CFGBlock* B) {
329 if (badCFG)
330 return false;
331
Ted Kremenek4f880632009-07-17 22:18:43 +0000332 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000333 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000334}
335
Ted Kremenek4f880632009-07-17 22:18:43 +0000336/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000337/// blocks for ternary operators, &&, and ||. We also process "," and
338/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000339CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000340tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000341 if (!S) {
342 badCFG = true;
343 return 0;
344 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000345 switch (S->getStmtClass()) {
346 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000347 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000348
349 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000350 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek4f880632009-07-17 22:18:43 +0000352 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000353 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek4f880632009-07-17 22:18:43 +0000355 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000356 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000357
Ted Kremenek4f880632009-07-17 22:18:43 +0000358 case Stmt::BreakStmtClass:
359 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek4f880632009-07-17 22:18:43 +0000361 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000362 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek4f880632009-07-17 22:18:43 +0000364 case Stmt::CaseStmtClass:
365 return VisitCaseStmt(cast<CaseStmt>(S));
366
367 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000368 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek4f880632009-07-17 22:18:43 +0000370 case Stmt::CompoundStmtClass:
371 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek4f880632009-07-17 22:18:43 +0000373 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000374 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek4f880632009-07-17 22:18:43 +0000376 case Stmt::ContinueStmtClass:
377 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Ted Kremenek021c8af2010-01-19 20:40:33 +0000379 case Stmt::CXXCatchStmtClass:
380 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
381
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000382 case Stmt::CXXMemberCallExprClass:
383 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
384
Ted Kremenek021c8af2010-01-19 20:40:33 +0000385 case Stmt::CXXThrowExprClass:
386 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
387
388 case Stmt::CXXTryStmtClass:
389 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
390
Ted Kremenek4f880632009-07-17 22:18:43 +0000391 case Stmt::DeclStmtClass:
392 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek4f880632009-07-17 22:18:43 +0000394 case Stmt::DefaultStmtClass:
395 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek4f880632009-07-17 22:18:43 +0000397 case Stmt::DoStmtClass:
398 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek4f880632009-07-17 22:18:43 +0000400 case Stmt::ForStmtClass:
401 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek4f880632009-07-17 22:18:43 +0000403 case Stmt::GotoStmtClass:
404 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek4f880632009-07-17 22:18:43 +0000406 case Stmt::IfStmtClass:
407 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Ted Kremenek4f880632009-07-17 22:18:43 +0000409 case Stmt::IndirectGotoStmtClass:
410 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek4f880632009-07-17 22:18:43 +0000412 case Stmt::LabelStmtClass:
413 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek115c1b92010-04-11 17:02:10 +0000415 case Stmt::MemberExprClass:
416 return VisitMemberExpr(cast<MemberExpr>(S), asc);
417
Ted Kremenek4f880632009-07-17 22:18:43 +0000418 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000419 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
420
Ted Kremenek4f880632009-07-17 22:18:43 +0000421 case Stmt::ObjCAtSynchronizedStmtClass:
422 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 case Stmt::ObjCAtThrowStmtClass:
425 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek4f880632009-07-17 22:18:43 +0000427 case Stmt::ObjCAtTryStmtClass:
428 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek4f880632009-07-17 22:18:43 +0000430 case Stmt::ObjCForCollectionStmtClass:
431 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek4f880632009-07-17 22:18:43 +0000433 case Stmt::ParenExprClass:
434 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000435 goto tryAgain;
436
Ted Kremenek4f880632009-07-17 22:18:43 +0000437 case Stmt::NullStmtClass:
438 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek4f880632009-07-17 22:18:43 +0000440 case Stmt::ReturnStmtClass:
441 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Ted Kremenek4f880632009-07-17 22:18:43 +0000443 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000444 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek4f880632009-07-17 22:18:43 +0000446 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000447 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Ted Kremenek4f880632009-07-17 22:18:43 +0000449 case Stmt::SwitchStmtClass:
450 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek4f880632009-07-17 22:18:43 +0000452 case Stmt::WhileStmtClass:
453 return VisitWhileStmt(cast<WhileStmt>(S));
454 }
455}
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek852274d2009-12-16 03:18:58 +0000457CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
458 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000459 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000460 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000461 }
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek4f880632009-07-17 22:18:43 +0000463 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000464}
Mike Stump6d9828c2009-07-17 01:31:16 +0000465
Ted Kremenek4f880632009-07-17 22:18:43 +0000466/// VisitChildren - Visit the children of a Stmt.
467CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
468 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000469 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000470 E = Terminator->child_end(); I != E; ++I) {
471 if (*I) B = Visit(*I);
472 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000473 return B;
474}
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenek852274d2009-12-16 03:18:58 +0000476CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
477 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000478 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000479
Ted Kremenek852274d2009-12-16 03:18:58 +0000480 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000481 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000482 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000483 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000484
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000485 return Block;
486}
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Ted Kremenek852274d2009-12-16 03:18:58 +0000488CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
489 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000490 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000491 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000492 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek4f880632009-07-17 22:18:43 +0000494 if (!FinishBlock(ConfluenceBlock))
495 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek4f880632009-07-17 22:18:43 +0000497 // create the block evaluating the LHS
498 CFGBlock* LHSBlock = createBlock(false);
499 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek4f880632009-07-17 22:18:43 +0000501 // create the block evaluating the RHS
502 Succ = ConfluenceBlock;
503 Block = NULL;
504 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000505
506 if (RHSBlock) {
507 if (!FinishBlock(RHSBlock))
508 return 0;
509 }
510 else {
511 // Create an empty block for cases where the RHS doesn't require
512 // any explicit statements in the CFG.
513 RHSBlock = createBlock();
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Mike Stump00998a02009-07-23 23:25:26 +0000516 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000517 TryResult KnownVal = TryEvaluateBool(B->getLHS());
518 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
519 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000520
Ted Kremenek4f880632009-07-17 22:18:43 +0000521 // Now link the LHSBlock with RHSBlock.
522 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000523 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
524 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000525 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000526 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000527 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
528 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Ted Kremenek4f880632009-07-17 22:18:43 +0000531 // Generate the blocks for evaluating the LHS.
532 Block = LHSBlock;
533 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000534 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000535 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000536 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000537 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000538 addStmt(B->getRHS());
539 return addStmt(B->getLHS());
540 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000541 else if (B->isAssignmentOp()) {
542 if (asc.alwaysAdd()) {
543 autoCreateBlock();
544 AppendStmt(Block, B, asc);
545 }
546
547 Visit(B->getRHS());
548 return Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek852274d2009-12-16 03:18:58 +0000551 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000552}
553
Ted Kremenek852274d2009-12-16 03:18:58 +0000554CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
555 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000556 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000557 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000558 }
559 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000560}
561
Ted Kremenek4f880632009-07-17 22:18:43 +0000562CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
563 // "break" is a control-flow statement. Thus we stop processing the current
564 // block.
565 if (Block && !FinishBlock(Block))
566 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Ted Kremenek4f880632009-07-17 22:18:43 +0000568 // Now create a new block that ends with the break statement.
569 Block = createBlock(false);
570 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Ted Kremenek4f880632009-07-17 22:18:43 +0000572 // If there is no target for the break, then we are looking at an incomplete
573 // AST. This means that the CFG cannot be constructed.
574 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000575 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000576 else
577 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000580 return Block;
581}
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Mike Stump4c45aa12010-01-21 15:20:48 +0000583static bool CanThrow(Expr *E) {
584 QualType Ty = E->getType();
585 if (Ty->isFunctionPointerType())
586 Ty = Ty->getAs<PointerType>()->getPointeeType();
587 else if (Ty->isBlockPointerType())
588 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
589
590 const FunctionType *FT = Ty->getAs<FunctionType>();
591 if (FT) {
592 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
593 if (Proto->hasEmptyExceptionSpec())
594 return false;
595 }
596 return true;
597}
598
Ted Kremenek852274d2009-12-16 03:18:58 +0000599CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000600 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000601 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000602 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000603 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000604 }
Mike Stump24556362009-07-25 21:26:53 +0000605
Mike Stump4c45aa12010-01-21 15:20:48 +0000606 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000607
608 // Languages without exceptions are assumed to not throw.
609 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000610 if (AddEHEdges)
611 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000612 }
613
614 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000615 if (FD->hasAttr<NoReturnAttr>())
616 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000617 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000618 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000619 }
Mike Stump24556362009-07-25 21:26:53 +0000620
Mike Stump4c45aa12010-01-21 15:20:48 +0000621 if (!CanThrow(C->getCallee()))
622 AddEHEdge = false;
623
Zhongxing Xufc61d942010-06-03 06:23:18 +0000624 if (!NoReturn && !AddEHEdge) {
625 if (asc.asLValue())
626 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
627 else
628 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Mike Stump079bd722010-01-19 22:00:14 +0000631 if (Block) {
632 Succ = Block;
633 if (!FinishBlock(Block))
634 return 0;
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Mike Stump079bd722010-01-19 22:00:14 +0000637 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000638 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000639
Mike Stump079bd722010-01-19 22:00:14 +0000640 if (NoReturn) {
641 // Wire this to the exit block directly.
642 AddSuccessor(Block, &cfg->getExit());
643 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000644 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000645 // Add exceptional edges.
646 if (TryTerminatedBlock)
647 AddSuccessor(Block, TryTerminatedBlock);
648 else
649 AddSuccessor(Block, &cfg->getExit());
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Mike Stump24556362009-07-25 21:26:53 +0000652 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000653}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000654
Ted Kremenek852274d2009-12-16 03:18:58 +0000655CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
656 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000657 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000658 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000659 if (!FinishBlock(ConfluenceBlock))
660 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek115c1b92010-04-11 17:02:10 +0000662 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
663 : AddStmtChoice::AlwaysAdd;
664
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000665 Succ = ConfluenceBlock;
666 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000667 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000668 if (!FinishBlock(LHSBlock))
669 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000671 Succ = ConfluenceBlock;
672 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000673 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000674 if (!FinishBlock(RHSBlock))
675 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000677 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000678 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000679 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000680 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
681 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000682 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000683 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000684}
Mike Stump1eb44332009-09-09 15:08:12 +0000685
686
687CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000688 EndScope(C);
689
Mike Stump1eb44332009-09-09 15:08:12 +0000690 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000691
692 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
693 I != E; ++I ) {
694 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000696 if (badCFG)
697 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000698 }
Mike Stump079bd722010-01-19 22:00:14 +0000699
700 LastBlock = StartScope(C, LastBlock);
701
Ted Kremenek4f880632009-07-17 22:18:43 +0000702 return LastBlock;
703}
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremenek852274d2009-12-16 03:18:58 +0000705CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
706 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000707 // Create the confluence block that will "merge" the results of the ternary
708 // expression.
709 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000710 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000711 if (!FinishBlock(ConfluenceBlock))
712 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek115c1b92010-04-11 17:02:10 +0000714 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
715 : AddStmtChoice::AlwaysAdd;
716
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000717 // Create a block for the LHS expression if there is an LHS expression. A
718 // GCC extension allows LHS to be NULL, causing the condition to be the
719 // value that is returned instead.
720 // e.g: x ?: y is shorthand for: x ? x : y;
721 Succ = ConfluenceBlock;
722 Block = NULL;
723 CFGBlock* LHSBlock = NULL;
724 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +0000725 LHSBlock = Visit(C->getLHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000726 if (!FinishBlock(LHSBlock))
727 return 0;
728 Block = NULL;
729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000731 // Create the block for the RHS expression.
732 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +0000733 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000734 if (!FinishBlock(RHSBlock))
735 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000737 // Create the block that will contain the condition.
738 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Mike Stump00998a02009-07-23 23:25:26 +0000740 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000741 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000742 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000743 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000744 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000745 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000746 // If we know the condition is false, add NULL as the successor for
747 // the block containing the condition. In this case, the confluence
748 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000749 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000750 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000751 } else {
752 // If we have no LHS expression, add the ConfluenceBlock as a direct
753 // successor for the block containing the condition. Moreover, we need to
754 // reverse the order of the predecessors in the ConfluenceBlock because
755 // the RHSBlock will have been added to the succcessors already, and we
756 // want the first predecessor to the the block containing the expression
757 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000758 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000759 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000760 std::reverse(ConfluenceBlock->pred_begin(),
761 ConfluenceBlock->pred_end());
762 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000765 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000766 Block->setTerminator(C);
767 return addStmt(C->getCond());
768}
769
Ted Kremenek4f880632009-07-17 22:18:43 +0000770CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
771 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000772
Ted Kremenek4f880632009-07-17 22:18:43 +0000773 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000774 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000775 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek4f880632009-07-17 22:18:43 +0000778 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenek4f880632009-07-17 22:18:43 +0000780 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
781 typedef llvm::SmallVector<Decl*,10> BufTy;
782 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Ted Kremenek4f880632009-07-17 22:18:43 +0000784 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
785 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
786 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
787 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
790 // automatically freed with the CFG.
791 DeclGroupRef DG(*I);
792 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000793 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000794 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek4f880632009-07-17 22:18:43 +0000796 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000797 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000798 B = VisitDeclSubExpr(D);
799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
801 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000802}
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenek4f880632009-07-17 22:18:43 +0000804/// VisitDeclSubExpr - Utility method to add block-level expressions for
805/// initializers in Decls.
806CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
807 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000808
Ted Kremenek4f880632009-07-17 22:18:43 +0000809 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek4f880632009-07-17 22:18:43 +0000811 if (!VD)
812 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenek4f880632009-07-17 22:18:43 +0000814 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Ted Kremenek4f880632009-07-17 22:18:43 +0000816 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000817 AddStmtChoice::Kind k =
818 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
819 : AddStmtChoice::NotAlwaysAdd;
820 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000821 }
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek4f880632009-07-17 22:18:43 +0000823 // If the type of VD is a VLA, then we must process its size expressions.
824 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
825 VA = FindVA(VA->getElementType().getTypePtr()))
826 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenek4f880632009-07-17 22:18:43 +0000828 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000829}
830
831CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000832 // We may see an if statement in the middle of a basic block, or it may be the
833 // first statement we are processing. In either case, we create a new basic
834 // block. First, we create the blocks for the then...else statements, and
835 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000836 // middle of a block, we stop processing that block. That block is then the
837 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000838
839 // The block we were proccessing is now finished. Make it the successor
840 // block.
841 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000843 if (!FinishBlock(Block))
844 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000845 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000846
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000847 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000848 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000849
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000850 if (Stmt* Else = I->getElse()) {
851 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000852
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000853 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000854 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000855 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000856 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000857
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000858 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
859 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000860 else if (Block) {
861 if (!FinishBlock(ElseBlock))
862 return 0;
863 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000864 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000865
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000866 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000867 CFGBlock* ThenBlock;
868 {
869 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000870 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000871 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000872 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000873 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000874
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000875 if (!ThenBlock) {
876 // We can reach here if the "then" body has all NullStmts.
877 // Create an empty block so we can distinguish between true and false
878 // branches in path-sensitive analyses.
879 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000880 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000881 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000882 if (!FinishBlock(ThenBlock))
883 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000884 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000885 }
886
Mike Stump6d9828c2009-07-17 01:31:16 +0000887 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000888 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000889
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000890 // Set the terminator of the new block to the If statement.
891 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000892
Mike Stump00998a02009-07-23 23:25:26 +0000893 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000894 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000895
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000896 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000897 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
898 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000899
900 // Add the condition as the last statement in the new block. This may create
901 // new blocks as the condition may contain control-flow. Any newly created
902 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000903 Block = addStmt(I->getCond());
904
905 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
906 // and the condition variable initialization to the CFG.
907 if (VarDecl *VD = I->getConditionVariable()) {
908 if (Expr *Init = VD->getInit()) {
909 autoCreateBlock();
910 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
911 addStmt(Init);
912 }
913 }
914
915 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000916}
Mike Stump6d9828c2009-07-17 01:31:16 +0000917
918
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000919CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000920 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000921 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000922 // NOTE: If a "return" appears in the middle of a block, this means that the
923 // code afterwards is DEAD (unreachable). We still keep a basic block
924 // for that code; a simple "mark-and-sweep" from the entry block will be
925 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000926 if (Block)
927 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000928
929 // Create the new block.
930 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000931
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000932 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000933 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000934
935 // Add the return statement to the block. This may create new blocks if R
936 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000937 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000938}
939
940CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
941 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000942 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000943 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000944
Ted Kremenek4f880632009-07-17 22:18:43 +0000945 if (!LabelBlock) // This can happen when the body is empty, i.e.
946 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000947
Ted Kremenek4f880632009-07-17 22:18:43 +0000948 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000949 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000950
951 // Labels partition blocks, so this is the end of the basic block we were
952 // processing (L is the block's label). Because this is label (and we have
953 // already processed the substatement) there is no extra control-flow to worry
954 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000955 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000956 if (!FinishBlock(LabelBlock))
957 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000958
959 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000960 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000961
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000962 // This block is now the implicit successor of other blocks.
963 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000964
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000965 return LabelBlock;
966}
967
968CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000969 // Goto is a control-flow statement. Thus we stop processing the current
970 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000971 if (Block)
972 FinishBlock(Block);
973
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000974 Block = createBlock(false);
975 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000976
977 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000978 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000979
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000980 if (I == LabelMap.end())
981 // We will need to backpatch this block later.
982 BackpatchBlocks.push_back(Block);
983 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000984 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000985
Mike Stump6d9828c2009-07-17 01:31:16 +0000986 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000987}
988
989CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000990 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000991
Mike Stumpfefb9f72009-07-21 01:12:51 +0000992 // "for" is a control-flow statement. Thus we stop processing the current
993 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000994 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000995 if (!FinishBlock(Block))
996 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000997 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000998 } else
999 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001000
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001001 // Save the current value for the break targets.
1002 // All breaks should go to the code following the loop.
1003 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
1004 BreakTargetBlock = LoopSuccessor;
1005
Mike Stump6d9828c2009-07-17 01:31:16 +00001006 // Because of short-circuit evaluation, the condition of the loop can span
1007 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1008 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001009 CFGBlock* ExitConditionBlock = createBlock(false);
1010 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001011
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001012 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001013 ExitConditionBlock->setTerminator(F);
1014
1015 // Now add the actual condition to the condition block. Because the condition
1016 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001017 if (Stmt* C = F->getCond()) {
1018 Block = ExitConditionBlock;
1019 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001020 assert(Block == EntryConditionBlock);
1021
1022 // If this block contains a condition variable, add both the condition
1023 // variable and initializer to the CFG.
1024 if (VarDecl *VD = F->getConditionVariable()) {
1025 if (Expr *Init = VD->getInit()) {
1026 autoCreateBlock();
1027 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1028 EntryConditionBlock = addStmt(Init);
1029 assert(Block == EntryConditionBlock);
1030 }
1031 }
1032
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001033 if (Block) {
1034 if (!FinishBlock(EntryConditionBlock))
1035 return 0;
1036 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001037 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001038
Mike Stump6d9828c2009-07-17 01:31:16 +00001039 // The condition block is the implicit successor for the loop body as well as
1040 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001041 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001042
Mike Stump00998a02009-07-23 23:25:26 +00001043 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001044 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Mike Stump00998a02009-07-23 23:25:26 +00001046 if (F->getCond())
1047 KnownVal = TryEvaluateBool(F->getCond());
1048
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001049 // Now create the loop body.
1050 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001051 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001052
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001053 // Save the current values for Block, Succ, and continue targets.
1054 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1055 save_continue(ContinueTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001056
Ted Kremenekaf603f72007-08-30 18:39:40 +00001057 // Create a new block to contain the (bottom) of the loop body.
1058 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001059
Ted Kremeneke9334502008-09-04 21:48:47 +00001060 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001061 // Generate increment code in its own basic block. This is the target of
1062 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001063 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001064 } else {
1065 // No increment code. Create a special, empty, block that is used as the
1066 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001067 assert(Succ == EntryConditionBlock);
1068 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001069 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001070
Ted Kremenek3575f842009-04-28 00:51:56 +00001071 // Finish up the increment (or empty) block if it hasn't been already.
1072 if (Block) {
1073 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001074 if (!FinishBlock(Block))
1075 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001076 Block = 0;
1077 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001078
Ted Kremenek3575f842009-04-28 00:51:56 +00001079 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001080
Ted Kremenek3575f842009-04-28 00:51:56 +00001081 // The starting block for the loop increment is the block that should
1082 // represent the 'loop target' for looping back to the start of the loop.
1083 ContinueTargetBlock->setLoopTarget(F);
1084
Mike Stump6d9828c2009-07-17 01:31:16 +00001085 // Now populate the body block, and in the process create new blocks as we
1086 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001087 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001088
1089 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001090 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001091 else if (Block && !FinishBlock(BodyBlock))
1092 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001093
Ted Kremenek941fde82009-07-24 04:47:11 +00001094 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001095 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001096 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001097
Ted Kremenek941fde82009-07-24 04:47:11 +00001098 // Link up the condition block with the code that follows the loop. (the
1099 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001100 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001101
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001102 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001103 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001104 if (Stmt* I = F->getInit()) {
1105 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001106 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001107 } else {
1108 // There is no loop initialization. We are thus basically a while loop.
1109 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001110 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001111 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001112 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001113 }
1114}
1115
Ted Kremenek115c1b92010-04-11 17:02:10 +00001116CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1117 if (asc.alwaysAdd()) {
1118 autoCreateBlock();
1119 AppendStmt(Block, M, asc);
1120 }
1121 return Visit(M->getBase(),
1122 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1123 : AddStmtChoice::AsLValueNotAlwaysAdd);
1124}
1125
Ted Kremenek514de5a2008-11-11 17:10:00 +00001126CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1127 // Objective-C fast enumeration 'for' statements:
1128 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1129 //
1130 // for ( Type newVariable in collection_expression ) { statements }
1131 //
1132 // becomes:
1133 //
1134 // prologue:
1135 // 1. collection_expression
1136 // T. jump to loop_entry
1137 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001138 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001139 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1140 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1141 // TB:
1142 // statements
1143 // T. jump to loop_entry
1144 // FB:
1145 // what comes after
1146 //
1147 // and
1148 //
1149 // Type existingItem;
1150 // for ( existingItem in expression ) { statements }
1151 //
1152 // becomes:
1153 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001154 // the same with newVariable replaced with existingItem; the binding works
1155 // the same except that for one ObjCForCollectionStmt::getElement() returns
1156 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001157 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001158
Ted Kremenek514de5a2008-11-11 17:10:00 +00001159 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001160
Ted Kremenek514de5a2008-11-11 17:10:00 +00001161 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001162 if (!FinishBlock(Block))
1163 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001164 LoopSuccessor = Block;
1165 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001166 } else
1167 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001168
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001169 // Build the condition blocks.
1170 CFGBlock* ExitConditionBlock = createBlock(false);
1171 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001172
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001173 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001174 ExitConditionBlock->setTerminator(S);
1175
1176 // The last statement in the block should be the ObjCForCollectionStmt, which
1177 // performs the actual binding to 'element' and determines if there are any
1178 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001179 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001180 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001181
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001182 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001183 // generate new blocks as necesary. We DON'T add the statement by default to
1184 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001185 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001186 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001187 if (!FinishBlock(EntryConditionBlock))
1188 return 0;
1189 Block = 0;
1190 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001191
1192 // The condition block is the implicit successor for the loop body as well as
1193 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001194 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001195
Ted Kremenek514de5a2008-11-11 17:10:00 +00001196 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001197 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001198 // Save the current values for Succ, continue and break targets.
1199 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001200 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1201
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001202 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001203 ContinueTargetBlock = EntryConditionBlock;
1204
Ted Kremenek4f880632009-07-17 22:18:43 +00001205 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001206
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001207 if (!BodyBlock)
1208 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001209 else if (Block) {
1210 if (!FinishBlock(BodyBlock))
1211 return 0;
1212 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001213
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001214 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001215 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001216 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001217
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001218 // Link up the condition block with the code that follows the loop.
1219 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001220 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001221
Ted Kremenek514de5a2008-11-11 17:10:00 +00001222 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001223 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001224 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001225}
1226
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001227CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1228 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001229
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001230 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001231 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001232
Ted Kremenekda5348e2009-05-05 23:11:51 +00001233 // The sync body starts its own basic block. This makes it a little easier
1234 // for diagnostic clients.
1235 if (SyncBlock) {
1236 if (!FinishBlock(SyncBlock))
1237 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001238
Ted Kremenekda5348e2009-05-05 23:11:51 +00001239 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001240 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001241 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001242
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001243 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001244 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001245}
Mike Stump6d9828c2009-07-17 01:31:16 +00001246
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001247CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001248 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001249 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001250}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001251
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001252CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001253 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001254
Mike Stumpfefb9f72009-07-21 01:12:51 +00001255 // "while" is a control-flow statement. Thus we stop processing the current
1256 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001257 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001258 if (!FinishBlock(Block))
1259 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001260 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001261 } else
1262 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001263
1264 // Because of short-circuit evaluation, the condition of the loop can span
1265 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1266 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001267 CFGBlock* ExitConditionBlock = createBlock(false);
1268 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001269
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001270 // Set the terminator for the "exit" condition block.
1271 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001272
1273 // Now add the actual condition to the condition block. Because the condition
1274 // itself may contain control-flow, new blocks may be created. Thus we update
1275 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001276 if (Stmt* C = W->getCond()) {
1277 Block = ExitConditionBlock;
1278 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001279 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001280
1281 // If this block contains a condition variable, add both the condition
1282 // variable and initializer to the CFG.
1283 if (VarDecl *VD = W->getConditionVariable()) {
1284 if (Expr *Init = VD->getInit()) {
1285 autoCreateBlock();
1286 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1287 EntryConditionBlock = addStmt(Init);
1288 assert(Block == EntryConditionBlock);
1289 }
1290 }
1291
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001292 if (Block) {
1293 if (!FinishBlock(EntryConditionBlock))
1294 return 0;
1295 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001296 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001297
1298 // The condition block is the implicit successor for the loop body as well as
1299 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001300 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001301
Mike Stump00998a02009-07-23 23:25:26 +00001302 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001303 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001304
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001305 // Process the loop body.
1306 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001307 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001308
1309 // Save the current values for Block, Succ, and continue and break targets
1310 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1311 save_continue(ContinueTargetBlock),
1312 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001313
Mike Stump6d9828c2009-07-17 01:31:16 +00001314 // Create an empty block to represent the transition block for looping back
1315 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001316 Block = 0;
1317 assert(Succ == EntryConditionBlock);
1318 Succ = createBlock();
1319 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001320 ContinueTargetBlock = Succ;
1321
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001322 // All breaks should go to the code following the loop.
1323 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001325 // NULL out Block to force lazy instantiation of blocks for the body.
1326 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001327
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001328 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001329 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001330
Ted Kremenekaf603f72007-08-30 18:39:40 +00001331 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001332 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001333 else if (Block) {
1334 if (!FinishBlock(BodyBlock))
1335 return 0;
1336 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001337
Ted Kremenek941fde82009-07-24 04:47:11 +00001338 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001339 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001340 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001341
Ted Kremenek941fde82009-07-24 04:47:11 +00001342 // Link up the condition block with the code that follows the loop. (the
1343 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001344 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001345
1346 // There can be no more statements in the condition block since we loop back
1347 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001348 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001349
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001350 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001351 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001352 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001353}
Mike Stump1eb44332009-09-09 15:08:12 +00001354
1355
Ted Kremenek4f880632009-07-17 22:18:43 +00001356CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1357 // FIXME: For now we pretend that @catch and the code it contains does not
1358 // exit.
1359 return Block;
1360}
Mike Stump6d9828c2009-07-17 01:31:16 +00001361
Ted Kremenek2fda5042008-12-09 20:20:09 +00001362CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1363 // FIXME: This isn't complete. We basically treat @throw like a return
1364 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001365
Ted Kremenek6c249722009-09-24 18:45:41 +00001366 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001367 if (Block && !FinishBlock(Block))
1368 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001369
Ted Kremenek2fda5042008-12-09 20:20:09 +00001370 // Create the new block.
1371 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
Ted Kremenek2fda5042008-12-09 20:20:09 +00001373 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001374 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001375
1376 // Add the statement to the block. This may create new blocks if S contains
1377 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001378 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001379}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001380
Mike Stump0979d802009-07-22 22:56:04 +00001381CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001382 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001383 if (Block && !FinishBlock(Block))
1384 return 0;
1385
1386 // Create the new block.
1387 Block = createBlock(false);
1388
Mike Stump5d1d2022010-01-19 02:20:09 +00001389 if (TryTerminatedBlock)
1390 // The current try statement is the only successor.
1391 AddSuccessor(Block, TryTerminatedBlock);
1392 else
1393 // otherwise the Exit block is the only successor.
1394 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +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(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001399}
1400
Ted Kremenek4f880632009-07-17 22:18:43 +00001401CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001402 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001403
Mike Stump8f9893a2009-07-21 01:27:50 +00001404 // "do...while" is a control-flow statement. Thus we stop processing the
1405 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001406 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001407 if (!FinishBlock(Block))
1408 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001409 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001410 } else
1411 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001412
1413 // Because of short-circuit evaluation, the condition of the loop can span
1414 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1415 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001416 CFGBlock* ExitConditionBlock = createBlock(false);
1417 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001418
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001419 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001420 ExitConditionBlock->setTerminator(D);
1421
1422 // Now add the actual condition to the condition block. Because the condition
1423 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001424 if (Stmt* C = D->getCond()) {
1425 Block = ExitConditionBlock;
1426 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001427 if (Block) {
1428 if (!FinishBlock(EntryConditionBlock))
1429 return 0;
1430 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001431 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenek54827132008-02-27 07:20:00 +00001433 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001434 Succ = EntryConditionBlock;
1435
Mike Stump00998a02009-07-23 23:25:26 +00001436 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001437 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001438
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001439 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001440 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001441 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001442 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001443
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001444 // Save the current values for Block, Succ, and continue and break targets
1445 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001446 save_continue(ContinueTargetBlock),
1447 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001448
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001449 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001450 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001451
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001452 // All breaks should go to the code following the loop.
1453 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001454
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001455 // NULL out Block to force lazy instantiation of blocks for the body.
1456 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001457
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001458 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001459 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
Ted Kremenekaf603f72007-08-30 18:39:40 +00001461 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001462 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001463 else if (Block) {
1464 if (!FinishBlock(BodyBlock))
1465 return 0;
1466 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001467
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001468 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001469 // ExitConditionBlock to represent the "loop back" transition. Create an
1470 // empty block to represent the transition block for looping back to the
1471 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001472 // FIXME: Can we do this more efficiently without adding another block?
1473 Block = NULL;
1474 Succ = BodyBlock;
1475 CFGBlock *LoopBackBlock = createBlock();
1476 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001477
Ted Kremenek941fde82009-07-24 04:47:11 +00001478 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001479 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001480 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001481
Ted Kremenek941fde82009-07-24 04:47:11 +00001482 // Link up the condition block with the code that follows the loop.
1483 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001484 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001485
1486 // There can be no more statements in the body block(s) since we loop back to
1487 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001488 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001489
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001490 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001491 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001492 return BodyBlock;
1493}
1494
1495CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1496 // "continue" is a control-flow statement. Thus we stop processing the
1497 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001498 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001499 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001500
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001501 // Now create a new block that ends with the continue statement.
1502 Block = createBlock(false);
1503 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001504
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001505 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001506 // incomplete AST. This means the CFG cannot be constructed.
1507 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001508 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001509 else
1510 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001511
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001512 return Block;
1513}
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001515CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001516 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001517
Ted Kremenek852274d2009-12-16 03:18:58 +00001518 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001519 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001520 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001521 }
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Ted Kremenek4f880632009-07-17 22:18:43 +00001523 // VLA types have expressions that must be evaluated.
1524 if (E->isArgumentType()) {
1525 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1526 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1527 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Mike Stump6d9828c2009-07-17 01:31:16 +00001530 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001531}
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Ted Kremenek4f880632009-07-17 22:18:43 +00001533/// VisitStmtExpr - Utility method to handle (nested) statement
1534/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001535CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1536 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001537 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001538 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001539 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001540 return VisitCompoundStmt(SE->getSubStmt());
1541}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001542
Ted Kremenek411cdee2008-04-16 21:10:48 +00001543CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001544 // "switch" is a control-flow statement. Thus we stop processing the current
1545 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001546 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001547
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001548 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001549 if (!FinishBlock(Block))
1550 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001551 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001552 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001553
1554 // Save the current "switch" context.
1555 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001556 save_break(BreakTargetBlock),
1557 save_default(DefaultCaseBlock);
1558
Mike Stump6d9828c2009-07-17 01:31:16 +00001559 // Set the "default" case to be the block after the switch statement. If the
1560 // switch statement contains a "default:", this value will be overwritten with
1561 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001562 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001563
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001564 // Create a new block that will contain the switch statement.
1565 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001566
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001567 // Now process the switch body. The code after the switch is the implicit
1568 // successor.
1569 Succ = SwitchSuccessor;
1570 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001571
1572 // When visiting the body, the case statements should automatically get linked
1573 // up to the switch. We also don't keep a pointer to the body, since all
1574 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001575 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001576 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001577 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001578 if (Block) {
1579 if (!FinishBlock(BodyBlock))
1580 return 0;
1581 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001582
Mike Stump6d9828c2009-07-17 01:31:16 +00001583 // If we have no "default:" case, the default transition is to the code
1584 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001585 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001586
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001587 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001588 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001589 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001590 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001591 Block = addStmt(Terminator->getCond());
1592
1593 // Finally, if the SwitchStmt contains a condition variable, add both the
1594 // SwitchStmt and the condition variable initialization to the CFG.
1595 if (VarDecl *VD = Terminator->getConditionVariable()) {
1596 if (Expr *Init = VD->getInit()) {
1597 autoCreateBlock();
1598 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1599 addStmt(Init);
1600 }
1601 }
1602
1603 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001604}
1605
Ted Kremenek4f880632009-07-17 22:18:43 +00001606CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001607 // CaseStmts are essentially labels, so they are the first statement in a
1608 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001609
Ted Kremenek4f880632009-07-17 22:18:43 +00001610 if (CS->getSubStmt())
1611 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001613 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001614 if (!CaseBlock)
1615 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001616
1617 // Cases statements partition blocks, so this is the top of the basic block we
1618 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001619 CaseBlock->setLabel(CS);
1620
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001621 if (!FinishBlock(CaseBlock))
1622 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001623
1624 // Add this block to the list of successors for the block with the switch
1625 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001626 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001627 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001628
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001629 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1630 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001631
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001632 // This block is now the implicit successor of other blocks.
1633 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001634
Ted Kremenek2677ea82008-03-15 07:45:02 +00001635 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001636}
Mike Stump6d9828c2009-07-17 01:31:16 +00001637
Ted Kremenek411cdee2008-04-16 21:10:48 +00001638CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001639 if (Terminator->getSubStmt())
1640 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001641
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001642 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001643
1644 if (!DefaultCaseBlock)
1645 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001646
1647 // Default statements partition blocks, so this is the top of the basic block
1648 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001649 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001651 if (!FinishBlock(DefaultCaseBlock))
1652 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001653
Mike Stump6d9828c2009-07-17 01:31:16 +00001654 // Unlike case statements, we don't add the default block to the successors
1655 // for the switch statement immediately. This is done when we finish
1656 // processing the switch statement. This allows for the default case
1657 // (including a fall-through to the code after the switch statement) to always
1658 // be the last successor of a switch-terminated block.
1659
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001660 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1661 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001662
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001663 // This block is now the implicit successor of other blocks.
1664 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001665
1666 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001667}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001668
Mike Stump5d1d2022010-01-19 02:20:09 +00001669CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1670 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1671 // current block.
1672 CFGBlock* TrySuccessor = NULL;
1673
1674 if (Block) {
1675 if (!FinishBlock(Block))
1676 return 0;
1677 TrySuccessor = Block;
1678 } else TrySuccessor = Succ;
1679
Mike Stumpa1f93632010-01-20 01:15:34 +00001680 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001681
1682 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001683 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001684 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001685 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001686
Mike Stumpa1f93632010-01-20 01:15:34 +00001687 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001688 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1689 // The code after the try is the implicit successor.
1690 Succ = TrySuccessor;
1691 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001692 if (CS->getExceptionDecl() == 0) {
1693 HasCatchAll = true;
1694 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001695 Block = NULL;
1696 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1697 if (CatchBlock == 0)
1698 return 0;
1699 // Add this block to the list of successors for the block with the try
1700 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001701 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001702 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001703 if (!HasCatchAll) {
1704 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001705 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001706 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001707 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001708 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001709
1710 // The code after the try is the implicit successor.
1711 Succ = TrySuccessor;
1712
Mike Stumpf00cca52010-01-20 01:30:58 +00001713 // Save the current "try" context.
1714 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1715 TryTerminatedBlock = NewTryTerminatedBlock;
1716
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001717 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001718 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001719 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001720 return Block;
1721}
1722
1723CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1724 // CXXCatchStmt are treated like labels, so they are the first statement in a
1725 // block.
1726
1727 if (CS->getHandlerBlock())
1728 addStmt(CS->getHandlerBlock());
1729
1730 CFGBlock* CatchBlock = Block;
1731 if (!CatchBlock)
1732 CatchBlock = createBlock();
1733
1734 CatchBlock->setLabel(CS);
1735
1736 if (!FinishBlock(CatchBlock))
1737 return 0;
1738
1739 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1740 Block = NULL;
1741
1742 return CatchBlock;
1743}
1744
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001745CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
1746 AddStmtChoice asc) {
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001747 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1748 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001749 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001750 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001751 return VisitChildren(C);
1752}
1753
Ted Kremenek19bb3562007-08-28 19:26:49 +00001754CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001755 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001756 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001757
Ted Kremenek19bb3562007-08-28 19:26:49 +00001758 if (!IBlock) {
1759 IBlock = createBlock(false);
1760 cfg->setIndirectGotoBlock(IBlock);
1761 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001762
Ted Kremenek19bb3562007-08-28 19:26:49 +00001763 // IndirectGoto is a control-flow statement. Thus we stop processing the
1764 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001765 if (Block && !FinishBlock(Block))
1766 return 0;
1767
Ted Kremenek19bb3562007-08-28 19:26:49 +00001768 Block = createBlock(false);
1769 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001770 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001771 return addStmt(I->getTarget());
1772}
1773
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001774} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001775
Mike Stump6d9828c2009-07-17 01:31:16 +00001776/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1777/// no successors or predecessors. If this is the first block created in the
1778/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001779CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001780 bool first_block = begin() == end();
1781
1782 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001783 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1784 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1785 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001786
1787 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001788 if (first_block)
1789 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001790
1791 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001792 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001793}
1794
Ted Kremenek026473c2007-08-23 16:51:22 +00001795/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1796/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001797CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump4c45aa12010-01-21 15:20:48 +00001798 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001799 CFGBuilder Builder;
Mike Stump4c45aa12010-01-21 15:20:48 +00001800 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001801}
1802
Ted Kremenek63f58872007-10-01 19:33:33 +00001803//===----------------------------------------------------------------------===//
1804// CFG: Queries for BlkExprs.
1805//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001806
Ted Kremenek63f58872007-10-01 19:33:33 +00001807namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001808 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001809}
1810
Ted Kremenek8a693662009-12-23 23:37:10 +00001811static void FindSubExprAssignments(Stmt *S,
1812 llvm::SmallPtrSet<Expr*,50>& Set) {
1813 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001814 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001815
Ted Kremenek8a693662009-12-23 23:37:10 +00001816 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1817 Stmt *child = *I;
1818 if (!child)
1819 continue;
1820
1821 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001822 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001823
Ted Kremenek8a693662009-12-23 23:37:10 +00001824 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001825 }
1826}
1827
Ted Kremenek63f58872007-10-01 19:33:33 +00001828static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1829 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001830
1831 // Look for assignments that are used as subexpressions. These are the only
1832 // assignments that we want to *possibly* register as a block-level
1833 // expression. Basically, if an assignment occurs both in a subexpression and
1834 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001835 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001836
Ted Kremenek63f58872007-10-01 19:33:33 +00001837 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001838 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001839 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001840
Ted Kremenek411cdee2008-04-16 21:10:48 +00001841 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001842
1843 // Iterate over the statements again on identify the Expr* and Stmt* at the
1844 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001845
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001846 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001847 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001848
Ted Kremenek411cdee2008-04-16 21:10:48 +00001849 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001850 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001851 // expression are really "statements" whose value is never used by
1852 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001853 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001854 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001855 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1856 // Special handling for statement expressions. The last statement in
1857 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001858 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001859 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001860 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001861 (*M)[C->body_back()] = x;
1862 }
1863 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001864
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001865 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001866 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001867 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Ted Kremenek411cdee2008-04-16 21:10:48 +00001869 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001870
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001871 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001872
Ted Kremenek390e48b2008-11-12 21:11:49 +00001873 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001874 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001875 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001876 }
1877 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001878
Ted Kremenek63f58872007-10-01 19:33:33 +00001879 return M;
1880}
1881
Ted Kremenek86946742008-01-17 20:48:37 +00001882CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1883 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001884 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001885
Ted Kremenek63f58872007-10-01 19:33:33 +00001886 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001887 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001888 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001889}
1890
1891unsigned CFG::getNumBlkExprs() {
1892 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1893 return M->size();
1894 else {
1895 // We assume callers interested in the number of BlkExprs will want
1896 // the map constructed if it doesn't already exist.
1897 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1898 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1899 }
1900}
1901
Ted Kremenek274f4332008-04-28 18:00:46 +00001902//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001903// Cleanup: CFG dstor.
1904//===----------------------------------------------------------------------===//
1905
Ted Kremenek63f58872007-10-01 19:33:33 +00001906CFG::~CFG() {
1907 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1908}
Mike Stump6d9828c2009-07-17 01:31:16 +00001909
Ted Kremenek7dba8602007-08-29 21:56:09 +00001910//===----------------------------------------------------------------------===//
1911// CFG pretty printing
1912//===----------------------------------------------------------------------===//
1913
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001914namespace {
1915
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001916class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001917 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1918 StmtMapTy StmtMap;
1919 signed CurrentBlock;
1920 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001921 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001922public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001923
Chris Lattnere4f21422009-06-30 01:26:17 +00001924 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1925 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001926 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1927 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001928 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001929 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001930 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001931 }
1932 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001933
Ted Kremenek42a509f2007-08-31 21:30:12 +00001934 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001935
Chris Lattnere4f21422009-06-30 01:26:17 +00001936 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001937 void setBlockID(signed i) { CurrentBlock = i; }
1938 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001939
Ted Kremeneka95d3752008-09-13 05:16:45 +00001940 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001941
Ted Kremenek411cdee2008-04-16 21:10:48 +00001942 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001943
1944 if (I == StmtMap.end())
1945 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001946
1947 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001948 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001949 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001950 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001951
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001952 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001953 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001954 }
1955};
Chris Lattnere4f21422009-06-30 01:26:17 +00001956} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001957
Chris Lattnere4f21422009-06-30 01:26:17 +00001958
1959namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001960class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001961 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001962
Ted Kremeneka95d3752008-09-13 05:16:45 +00001963 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001964 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001965 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001966public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001967 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001968 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001969 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001970
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001971 void VisitIfStmt(IfStmt* I) {
1972 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001973 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001974 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001975
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001976 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001977 void VisitStmt(Stmt* Terminator) {
1978 Terminator->printPretty(OS, Helper, Policy);
1979 }
1980
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001981 void VisitForStmt(ForStmt* F) {
1982 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001983 if (F->getInit())
1984 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001985 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001986 if (Stmt* C = F->getCond())
1987 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001988 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001989 if (F->getInc())
1990 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001991 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001992 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001993
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001994 void VisitWhileStmt(WhileStmt* W) {
1995 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001996 if (Stmt* C = W->getCond())
1997 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001998 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001999
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002000 void VisitDoStmt(DoStmt* D) {
2001 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002002 if (Stmt* C = D->getCond())
2003 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002004 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002005
Ted Kremenek411cdee2008-04-16 21:10:48 +00002006 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002007 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002008 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002009 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002010
Mike Stump5d1d2022010-01-19 02:20:09 +00002011 void VisitCXXTryStmt(CXXTryStmt* CS) {
2012 OS << "try ...";
2013 }
2014
Ted Kremenek805e9a82007-08-31 21:49:40 +00002015 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002016 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002017 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002018 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002019
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002020 void VisitChooseExpr(ChooseExpr* C) {
2021 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002022 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002023 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002024 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002025
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002026 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2027 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002028 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002029 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002030
Ted Kremenek805e9a82007-08-31 21:49:40 +00002031 void VisitBinaryOperator(BinaryOperator* B) {
2032 if (!B->isLogicalOp()) {
2033 VisitExpr(B);
2034 return;
2035 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002036
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002037 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002038
Ted Kremenek805e9a82007-08-31 21:49:40 +00002039 switch (B->getOpcode()) {
2040 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002041 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002042 return;
2043 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002044 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002045 return;
2046 default:
2047 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002048 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002049 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002050
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002051 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002052 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002053 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002054};
Chris Lattnere4f21422009-06-30 01:26:17 +00002055} // end anonymous namespace
2056
Mike Stump6d9828c2009-07-17 01:31:16 +00002057
Chris Lattnere4f21422009-06-30 01:26:17 +00002058static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002059 const CFGElement &E) {
2060 Stmt *Terminator = E;
2061
2062 if (E.asStartScope()) {
2063 OS << "start scope\n";
2064 return;
2065 }
2066 if (E.asEndScope()) {
2067 OS << "end scope\n";
2068 return;
2069 }
2070
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002071 if (Helper) {
2072 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002073 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002074 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002075
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002076 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002077 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002078 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002079 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002080 return;
2081 }
2082 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002083
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002084 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002085 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002086 if (B->getOpcode() == BinaryOperator::Comma) {
2087 OS << "... , ";
2088 Helper->handledStmt(B->getRHS(),OS);
2089 OS << '\n';
2090 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002091 }
2092 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002093 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002094
Chris Lattnere4f21422009-06-30 01:26:17 +00002095 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002096
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002097 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002098 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002099}
Mike Stump6d9828c2009-07-17 01:31:16 +00002100
Chris Lattnere4f21422009-06-30 01:26:17 +00002101static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2102 const CFGBlock& B,
2103 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002104
Ted Kremenek42a509f2007-08-31 21:30:12 +00002105 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002106
Ted Kremenek7dba8602007-08-29 21:56:09 +00002107 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002108 OS << "\n [ B" << B.getBlockID();
2109
Ted Kremenek42a509f2007-08-31 21:30:12 +00002110 if (&B == &cfg->getEntry())
2111 OS << " (ENTRY) ]\n";
2112 else if (&B == &cfg->getExit())
2113 OS << " (EXIT) ]\n";
2114 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002115 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002116 else
2117 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002118
Ted Kremenek9cffe732007-08-29 23:20:49 +00002119 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002120 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002121
2122 if (print_edges)
2123 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002124
Mike Stump079bd722010-01-19 22:00:14 +00002125 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002126 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002127 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002128 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002129 C->getLHS()->printPretty(OS, Helper,
2130 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002131 if (C->getRHS()) {
2132 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002133 C->getRHS()->printPretty(OS, Helper,
2134 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002135 }
Mike Stump079bd722010-01-19 22:00:14 +00002136 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002137 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002138 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002139 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002140 if (CS->getExceptionDecl())
2141 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2142 0);
2143 else
2144 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002145 OS << ")";
2146
2147 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002148 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002149
Ted Kremenek9cffe732007-08-29 23:20:49 +00002150 OS << ":\n";
2151 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002152
Ted Kremenekfddd5182007-08-21 21:42:03 +00002153 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002154 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002155
Ted Kremenek42a509f2007-08-31 21:30:12 +00002156 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2157 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002158
Ted Kremenek9cffe732007-08-29 23:20:49 +00002159 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002160 if (print_edges)
2161 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002162
Ted Kremeneka95d3752008-09-13 05:16:45 +00002163 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002164
Ted Kremenek42a509f2007-08-31 21:30:12 +00002165 if (Helper)
2166 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002167
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002168 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002169 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002170
Ted Kremenek9cffe732007-08-29 23:20:49 +00002171 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002172 if (B.getTerminator()) {
2173 if (print_edges)
2174 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002175
Ted Kremenek9cffe732007-08-29 23:20:49 +00002176 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002177
Ted Kremenek42a509f2007-08-31 21:30:12 +00002178 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002179
Chris Lattnere4f21422009-06-30 01:26:17 +00002180 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2181 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002182 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002183 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002184 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002185
Ted Kremenek9cffe732007-08-29 23:20:49 +00002186 if (print_edges) {
2187 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002188 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002189 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002190
Ted Kremenek42a509f2007-08-31 21:30:12 +00002191 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2192 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002193
Ted Kremenek42a509f2007-08-31 21:30:12 +00002194 if (i == 8 || (i-8) == 0)
2195 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002196
Ted Kremenek9cffe732007-08-29 23:20:49 +00002197 OS << " B" << (*I)->getBlockID();
2198 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002199
Ted Kremenek42a509f2007-08-31 21:30:12 +00002200 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002201
Ted Kremenek42a509f2007-08-31 21:30:12 +00002202 // Print the successors of this block.
2203 OS << " Successors (" << B.succ_size() << "):";
2204 i = 0;
2205
2206 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2207 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002208
Ted Kremenek42a509f2007-08-31 21:30:12 +00002209 if (i == 8 || (i-8) % 10 == 0)
2210 OS << "\n ";
2211
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002212 if (*I)
2213 OS << " B" << (*I)->getBlockID();
2214 else
2215 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002216 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002217
Ted Kremenek9cffe732007-08-29 23:20:49 +00002218 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002219 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002220}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002221
Ted Kremenek42a509f2007-08-31 21:30:12 +00002222
2223/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002224void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002225
2226/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002227void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2228 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002229
Ted Kremenek42a509f2007-08-31 21:30:12 +00002230 // Print the entry block.
2231 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002232
Ted Kremenek42a509f2007-08-31 21:30:12 +00002233 // Iterate through the CFGBlocks and print them one by one.
2234 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2235 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002236 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002237 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002238
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002239 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002240 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002241
Ted Kremenek42a509f2007-08-31 21:30:12 +00002242 // Print the exit block.
2243 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002244 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002245}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002246
2247/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002248void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2249 print(llvm::errs(), cfg, LO);
2250}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002251
2252/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2253/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002254void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2255 const LangOptions &LO) const {
2256 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002257 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002258}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002259
Ted Kremeneka2925852008-01-30 23:02:42 +00002260/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002261void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002262 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002263 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002264 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2265}
2266
Ted Kremenek390e48b2008-11-12 21:11:49 +00002267Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek411cdee2008-04-16 21:10:48 +00002269 if (!Terminator)
2270 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002271
Ted Kremenek411cdee2008-04-16 21:10:48 +00002272 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002273
Ted Kremenek411cdee2008-04-16 21:10:48 +00002274 switch (Terminator->getStmtClass()) {
2275 default:
2276 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002277
Ted Kremenek411cdee2008-04-16 21:10:48 +00002278 case Stmt::ForStmtClass:
2279 E = cast<ForStmt>(Terminator)->getCond();
2280 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002281
Ted Kremenek411cdee2008-04-16 21:10:48 +00002282 case Stmt::WhileStmtClass:
2283 E = cast<WhileStmt>(Terminator)->getCond();
2284 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002285
Ted Kremenek411cdee2008-04-16 21:10:48 +00002286 case Stmt::DoStmtClass:
2287 E = cast<DoStmt>(Terminator)->getCond();
2288 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002289
Ted Kremenek411cdee2008-04-16 21:10:48 +00002290 case Stmt::IfStmtClass:
2291 E = cast<IfStmt>(Terminator)->getCond();
2292 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002293
Ted Kremenek411cdee2008-04-16 21:10:48 +00002294 case Stmt::ChooseExprClass:
2295 E = cast<ChooseExpr>(Terminator)->getCond();
2296 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002297
Ted Kremenek411cdee2008-04-16 21:10:48 +00002298 case Stmt::IndirectGotoStmtClass:
2299 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2300 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002301
Ted Kremenek411cdee2008-04-16 21:10:48 +00002302 case Stmt::SwitchStmtClass:
2303 E = cast<SwitchStmt>(Terminator)->getCond();
2304 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002305
Ted Kremenek411cdee2008-04-16 21:10:48 +00002306 case Stmt::ConditionalOperatorClass:
2307 E = cast<ConditionalOperator>(Terminator)->getCond();
2308 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002309
Ted Kremenek411cdee2008-04-16 21:10:48 +00002310 case Stmt::BinaryOperatorClass: // '&&' and '||'
2311 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002312 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002313
Ted Kremenek390e48b2008-11-12 21:11:49 +00002314 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002315 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002316 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002317
Ted Kremenek411cdee2008-04-16 21:10:48 +00002318 return E ? E->IgnoreParens() : NULL;
2319}
2320
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002321bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002322
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002323 if (!Terminator)
2324 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002325
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002326 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002327
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002328 switch (Terminator->getStmtClass()) {
2329 default:
2330 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002331
2332 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002333 case Stmt::WhileStmtClass:
2334 case Stmt::DoStmtClass:
2335 case Stmt::IfStmtClass:
2336 case Stmt::ChooseExprClass:
2337 case Stmt::ConditionalOperatorClass:
2338 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002339 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002340 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002341
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002342 return E ? E->IgnoreParens() : NULL;
2343}
2344
Ted Kremeneka2925852008-01-30 23:02:42 +00002345
Ted Kremenek7dba8602007-08-29 21:56:09 +00002346//===----------------------------------------------------------------------===//
2347// CFG Graphviz Visualization
2348//===----------------------------------------------------------------------===//
2349
Ted Kremenek42a509f2007-08-31 21:30:12 +00002350
2351#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002352static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002353#endif
2354
Chris Lattnere4f21422009-06-30 01:26:17 +00002355void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002356#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002357 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002358 GraphHelper = &H;
2359 llvm::ViewGraph(this,"CFG");
2360 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002361#endif
2362}
2363
Ted Kremenek7dba8602007-08-29 21:56:09 +00002364namespace llvm {
2365template<>
2366struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002367
2368 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2369
2370 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002371
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002372#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002373 std::string OutSStr;
2374 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002375 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002376 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002377
2378 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2379
2380 // Process string output to make it nicer...
2381 for (unsigned i = 0; i != OutStr.length(); ++i)
2382 if (OutStr[i] == '\n') { // Left justify
2383 OutStr[i] = '\\';
2384 OutStr.insert(OutStr.begin()+i+1, 'l');
2385 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002386
Ted Kremenek7dba8602007-08-29 21:56:09 +00002387 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002388#else
2389 return "";
2390#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002391 }
2392};
2393} // end namespace llvm