blob: e447657e9ef6d3f4b87de2af7348c91607a49d4b [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);
Ted Kremenek852274d2009-12-16 03:18:58 +0000174 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
175 return Visit(S, asc);
176 }
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:
341 switch (S->getStmtClass()) {
342 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000343 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000344
345 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000346 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek4f880632009-07-17 22:18:43 +0000348 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000349 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek4f880632009-07-17 22:18:43 +0000351 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000352 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000353
Ted Kremenek4f880632009-07-17 22:18:43 +0000354 case Stmt::BreakStmtClass:
355 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek4f880632009-07-17 22:18:43 +0000357 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000358 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek4f880632009-07-17 22:18:43 +0000360 case Stmt::CaseStmtClass:
361 return VisitCaseStmt(cast<CaseStmt>(S));
362
363 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000364 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek4f880632009-07-17 22:18:43 +0000366 case Stmt::CompoundStmtClass:
367 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek4f880632009-07-17 22:18:43 +0000369 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000370 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek4f880632009-07-17 22:18:43 +0000372 case Stmt::ContinueStmtClass:
373 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek021c8af2010-01-19 20:40:33 +0000375 case Stmt::CXXCatchStmtClass:
376 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
377
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000378 case Stmt::CXXMemberCallExprClass:
379 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
380
Ted Kremenek021c8af2010-01-19 20:40:33 +0000381 case Stmt::CXXThrowExprClass:
382 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
383
384 case Stmt::CXXTryStmtClass:
385 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
386
Ted Kremenek4f880632009-07-17 22:18:43 +0000387 case Stmt::DeclStmtClass:
388 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek4f880632009-07-17 22:18:43 +0000390 case Stmt::DefaultStmtClass:
391 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek4f880632009-07-17 22:18:43 +0000393 case Stmt::DoStmtClass:
394 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek4f880632009-07-17 22:18:43 +0000396 case Stmt::ForStmtClass:
397 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek4f880632009-07-17 22:18:43 +0000399 case Stmt::GotoStmtClass:
400 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek4f880632009-07-17 22:18:43 +0000402 case Stmt::IfStmtClass:
403 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek4f880632009-07-17 22:18:43 +0000405 case Stmt::IndirectGotoStmtClass:
406 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek4f880632009-07-17 22:18:43 +0000408 case Stmt::LabelStmtClass:
409 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek115c1b92010-04-11 17:02:10 +0000411 case Stmt::MemberExprClass:
412 return VisitMemberExpr(cast<MemberExpr>(S), asc);
413
Ted Kremenek4f880632009-07-17 22:18:43 +0000414 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000415 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
416
Ted Kremenek4f880632009-07-17 22:18:43 +0000417 case Stmt::ObjCAtSynchronizedStmtClass:
418 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Ted Kremenek4f880632009-07-17 22:18:43 +0000420 case Stmt::ObjCAtThrowStmtClass:
421 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Ted Kremenek4f880632009-07-17 22:18:43 +0000423 case Stmt::ObjCAtTryStmtClass:
424 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Ted Kremenek4f880632009-07-17 22:18:43 +0000426 case Stmt::ObjCForCollectionStmtClass:
427 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek4f880632009-07-17 22:18:43 +0000429 case Stmt::ParenExprClass:
430 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000431 goto tryAgain;
432
Ted Kremenek4f880632009-07-17 22:18:43 +0000433 case Stmt::NullStmtClass:
434 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek4f880632009-07-17 22:18:43 +0000436 case Stmt::ReturnStmtClass:
437 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek4f880632009-07-17 22:18:43 +0000439 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000440 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremenek4f880632009-07-17 22:18:43 +0000442 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000443 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek4f880632009-07-17 22:18:43 +0000445 case Stmt::SwitchStmtClass:
446 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek4f880632009-07-17 22:18:43 +0000448 case Stmt::WhileStmtClass:
449 return VisitWhileStmt(cast<WhileStmt>(S));
450 }
451}
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Ted Kremenek852274d2009-12-16 03:18:58 +0000453CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
454 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000455 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000456 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Ted Kremenek4f880632009-07-17 22:18:43 +0000459 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000460}
Mike Stump6d9828c2009-07-17 01:31:16 +0000461
Ted Kremenek4f880632009-07-17 22:18:43 +0000462/// VisitChildren - Visit the children of a Stmt.
463CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
464 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000465 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000466 E = Terminator->child_end(); I != E; ++I) {
467 if (*I) B = Visit(*I);
468 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000469 return B;
470}
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenek852274d2009-12-16 03:18:58 +0000472CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
473 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000474 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000475
Ted Kremenek852274d2009-12-16 03:18:58 +0000476 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000477 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000478 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000479 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000480
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000481 return Block;
482}
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek852274d2009-12-16 03:18:58 +0000484CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
485 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000486 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000487 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000488 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek4f880632009-07-17 22:18:43 +0000490 if (!FinishBlock(ConfluenceBlock))
491 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek4f880632009-07-17 22:18:43 +0000493 // create the block evaluating the LHS
494 CFGBlock* LHSBlock = createBlock(false);
495 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Ted Kremenek4f880632009-07-17 22:18:43 +0000497 // create the block evaluating the RHS
498 Succ = ConfluenceBlock;
499 Block = NULL;
500 CFGBlock* RHSBlock = addStmt(B->getRHS());
501 if (!FinishBlock(RHSBlock))
502 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Mike Stump00998a02009-07-23 23:25:26 +0000504 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000505 TryResult KnownVal = TryEvaluateBool(B->getLHS());
506 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
507 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000508
Ted Kremenek4f880632009-07-17 22:18:43 +0000509 // Now link the LHSBlock with RHSBlock.
510 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000511 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
512 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000513 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000514 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000515 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
516 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000517 }
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Ted Kremenek4f880632009-07-17 22:18:43 +0000519 // Generate the blocks for evaluating the LHS.
520 Block = LHSBlock;
521 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000522 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000523 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000524 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000525 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000526 addStmt(B->getRHS());
527 return addStmt(B->getLHS());
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek852274d2009-12-16 03:18:58 +0000530 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000531}
532
Ted Kremenek852274d2009-12-16 03:18:58 +0000533CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
534 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000535 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000536 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000537 }
538 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000539}
540
Ted Kremenek4f880632009-07-17 22:18:43 +0000541CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
542 // "break" is a control-flow statement. Thus we stop processing the current
543 // block.
544 if (Block && !FinishBlock(Block))
545 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek4f880632009-07-17 22:18:43 +0000547 // Now create a new block that ends with the break statement.
548 Block = createBlock(false);
549 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek4f880632009-07-17 22:18:43 +0000551 // If there is no target for the break, then we are looking at an incomplete
552 // AST. This means that the CFG cannot be constructed.
553 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000554 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000555 else
556 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
558
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000559 return Block;
560}
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Mike Stump4c45aa12010-01-21 15:20:48 +0000562static bool CanThrow(Expr *E) {
563 QualType Ty = E->getType();
564 if (Ty->isFunctionPointerType())
565 Ty = Ty->getAs<PointerType>()->getPointeeType();
566 else if (Ty->isBlockPointerType())
567 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
568
569 const FunctionType *FT = Ty->getAs<FunctionType>();
570 if (FT) {
571 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
572 if (Proto->hasEmptyExceptionSpec())
573 return false;
574 }
575 return true;
576}
577
Ted Kremenek852274d2009-12-16 03:18:58 +0000578CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000579 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000580 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000581 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000582 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000583 }
Mike Stump24556362009-07-25 21:26:53 +0000584
Mike Stump4c45aa12010-01-21 15:20:48 +0000585 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000586
587 // Languages without exceptions are assumed to not throw.
588 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000589 if (AddEHEdges)
590 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000591 }
592
593 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000594 if (FD->hasAttr<NoReturnAttr>())
595 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000596 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000597 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000598 }
Mike Stump24556362009-07-25 21:26:53 +0000599
Mike Stump4c45aa12010-01-21 15:20:48 +0000600 if (!CanThrow(C->getCallee()))
601 AddEHEdge = false;
602
603 if (!NoReturn && !AddEHEdge)
Zhongxing Xu91071662010-02-24 02:19:28 +0000604 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Mike Stump079bd722010-01-19 22:00:14 +0000606 if (Block) {
607 Succ = Block;
608 if (!FinishBlock(Block))
609 return 0;
610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Mike Stump079bd722010-01-19 22:00:14 +0000612 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000613 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000614
Mike Stump079bd722010-01-19 22:00:14 +0000615 if (NoReturn) {
616 // Wire this to the exit block directly.
617 AddSuccessor(Block, &cfg->getExit());
618 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000619 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000620 // Add exceptional edges.
621 if (TryTerminatedBlock)
622 AddSuccessor(Block, TryTerminatedBlock);
623 else
624 AddSuccessor(Block, &cfg->getExit());
625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Mike Stump24556362009-07-25 21:26:53 +0000627 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000628}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000629
Ted Kremenek852274d2009-12-16 03:18:58 +0000630CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
631 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000632 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000633 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000634 if (!FinishBlock(ConfluenceBlock))
635 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek115c1b92010-04-11 17:02:10 +0000637 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
638 : AddStmtChoice::AlwaysAdd;
639
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000640 Succ = ConfluenceBlock;
641 Block = NULL;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000642 CFGBlock* LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000643 if (!FinishBlock(LHSBlock))
644 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000646 Succ = ConfluenceBlock;
647 Block = NULL;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000648 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000649 if (!FinishBlock(RHSBlock))
650 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000652 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000653 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000654 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000655 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
656 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000657 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000658 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000659}
Mike Stump1eb44332009-09-09 15:08:12 +0000660
661
662CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000663 EndScope(C);
664
Mike Stump1eb44332009-09-09 15:08:12 +0000665 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000666
667 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
668 I != E; ++I ) {
669 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000671 if (badCFG)
672 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000673 }
Mike Stump079bd722010-01-19 22:00:14 +0000674
675 LastBlock = StartScope(C, LastBlock);
676
Ted Kremenek4f880632009-07-17 22:18:43 +0000677 return LastBlock;
678}
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremenek852274d2009-12-16 03:18:58 +0000680CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
681 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000682 // Create the confluence block that will "merge" the results of the ternary
683 // expression.
684 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000685 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000686 if (!FinishBlock(ConfluenceBlock))
687 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Ted Kremenek115c1b92010-04-11 17:02:10 +0000689 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
690 : AddStmtChoice::AlwaysAdd;
691
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000692 // Create a block for the LHS expression if there is an LHS expression. A
693 // GCC extension allows LHS to be NULL, causing the condition to be the
694 // value that is returned instead.
695 // e.g: x ?: y is shorthand for: x ? x : y;
696 Succ = ConfluenceBlock;
697 Block = NULL;
698 CFGBlock* LHSBlock = NULL;
699 if (C->getLHS()) {
Ted Kremenek115c1b92010-04-11 17:02:10 +0000700 LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000701 if (!FinishBlock(LHSBlock))
702 return 0;
703 Block = NULL;
704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000706 // Create the block for the RHS expression.
707 Succ = ConfluenceBlock;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000708 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000709 if (!FinishBlock(RHSBlock))
710 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000712 // Create the block that will contain the condition.
713 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Mike Stump00998a02009-07-23 23:25:26 +0000715 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000716 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000717 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000718 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000719 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000720 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000721 // If we know the condition is false, add NULL as the successor for
722 // the block containing the condition. In this case, the confluence
723 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000724 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000725 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000726 } else {
727 // If we have no LHS expression, add the ConfluenceBlock as a direct
728 // successor for the block containing the condition. Moreover, we need to
729 // reverse the order of the predecessors in the ConfluenceBlock because
730 // the RHSBlock will have been added to the succcessors already, and we
731 // want the first predecessor to the the block containing the expression
732 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000733 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000734 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000735 std::reverse(ConfluenceBlock->pred_begin(),
736 ConfluenceBlock->pred_end());
737 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000740 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000741 Block->setTerminator(C);
742 return addStmt(C->getCond());
743}
744
Ted Kremenek4f880632009-07-17 22:18:43 +0000745CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
746 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000747
Ted Kremenek4f880632009-07-17 22:18:43 +0000748 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000749 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000750 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000751 }
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenek4f880632009-07-17 22:18:43 +0000753 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek4f880632009-07-17 22:18:43 +0000755 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
756 typedef llvm::SmallVector<Decl*,10> BufTy;
757 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Ted Kremenek4f880632009-07-17 22:18:43 +0000759 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
760 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
761 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
762 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek4f880632009-07-17 22:18:43 +0000764 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
765 // automatically freed with the CFG.
766 DeclGroupRef DG(*I);
767 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000768 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000769 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Ted Kremenek4f880632009-07-17 22:18:43 +0000771 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000772 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000773 B = VisitDeclSubExpr(D);
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
776 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000777}
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek4f880632009-07-17 22:18:43 +0000779/// VisitDeclSubExpr - Utility method to add block-level expressions for
780/// initializers in Decls.
781CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
782 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000783
Ted Kremenek4f880632009-07-17 22:18:43 +0000784 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Ted Kremenek4f880632009-07-17 22:18:43 +0000786 if (!VD)
787 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek4f880632009-07-17 22:18:43 +0000791 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000792 AddStmtChoice::Kind k =
793 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
794 : AddStmtChoice::NotAlwaysAdd;
795 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek4f880632009-07-17 22:18:43 +0000798 // If the type of VD is a VLA, then we must process its size expressions.
799 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
800 VA = FindVA(VA->getElementType().getTypePtr()))
801 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek4f880632009-07-17 22:18:43 +0000803 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000804}
805
806CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000807 // We may see an if statement in the middle of a basic block, or it may be the
808 // first statement we are processing. In either case, we create a new basic
809 // block. First, we create the blocks for the then...else statements, and
810 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000811 // middle of a block, we stop processing that block. That block is then the
812 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000813
814 // The block we were proccessing is now finished. Make it the successor
815 // block.
816 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000817 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000818 if (!FinishBlock(Block))
819 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000820 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000821
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000822 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000823 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000824
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000825 if (Stmt* Else = I->getElse()) {
826 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000827
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000828 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000829 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000830 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000831 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000832
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000833 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
834 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000835 else if (Block) {
836 if (!FinishBlock(ElseBlock))
837 return 0;
838 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000839 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000840
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000841 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 CFGBlock* ThenBlock;
843 {
844 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000845 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000846 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000847 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000848 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000849
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000850 if (!ThenBlock) {
851 // We can reach here if the "then" body has all NullStmts.
852 // Create an empty block so we can distinguish between true and false
853 // branches in path-sensitive analyses.
854 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000855 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000856 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000857 if (!FinishBlock(ThenBlock))
858 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000859 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000860 }
861
Mike Stump6d9828c2009-07-17 01:31:16 +0000862 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000863 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000864
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000865 // Set the terminator of the new block to the If statement.
866 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000867
Mike Stump00998a02009-07-23 23:25:26 +0000868 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000869 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000870
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000871 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000872 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
873 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000874
875 // Add the condition as the last statement in the new block. This may create
876 // new blocks as the condition may contain control-flow. Any newly created
877 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000878 Block = addStmt(I->getCond());
879
880 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
881 // and the condition variable initialization to the CFG.
882 if (VarDecl *VD = I->getConditionVariable()) {
883 if (Expr *Init = VD->getInit()) {
884 autoCreateBlock();
885 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
886 addStmt(Init);
887 }
888 }
889
890 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000891}
Mike Stump6d9828c2009-07-17 01:31:16 +0000892
893
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000894CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000895 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000896 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000897 // NOTE: If a "return" appears in the middle of a block, this means that the
898 // code afterwards is DEAD (unreachable). We still keep a basic block
899 // for that code; a simple "mark-and-sweep" from the entry block will be
900 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000901 if (Block)
902 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000903
904 // Create the new block.
905 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000906
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000907 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000908 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000909
910 // Add the return statement to the block. This may create new blocks if R
911 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000912 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000913}
914
915CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
916 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000917 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000918 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000919
Ted Kremenek4f880632009-07-17 22:18:43 +0000920 if (!LabelBlock) // This can happen when the body is empty, i.e.
921 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000922
Ted Kremenek4f880632009-07-17 22:18:43 +0000923 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000924 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000925
926 // Labels partition blocks, so this is the end of the basic block we were
927 // processing (L is the block's label). Because this is label (and we have
928 // already processed the substatement) there is no extra control-flow to worry
929 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000930 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000931 if (!FinishBlock(LabelBlock))
932 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000933
934 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000935 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000936
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000937 // This block is now the implicit successor of other blocks.
938 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000939
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000940 return LabelBlock;
941}
942
943CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000944 // Goto is a control-flow statement. Thus we stop processing the current
945 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000946 if (Block)
947 FinishBlock(Block);
948
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000949 Block = createBlock(false);
950 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000951
952 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000953 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000954
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000955 if (I == LabelMap.end())
956 // We will need to backpatch this block later.
957 BackpatchBlocks.push_back(Block);
958 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000959 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000960
Mike Stump6d9828c2009-07-17 01:31:16 +0000961 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000962}
963
964CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000965 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000966
Mike Stumpfefb9f72009-07-21 01:12:51 +0000967 // "for" is a control-flow statement. Thus we stop processing the current
968 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000969 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000970 if (!FinishBlock(Block))
971 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000972 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000973 } else
974 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000975
976 // Because of short-circuit evaluation, the condition of the loop can span
977 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
978 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000979 CFGBlock* ExitConditionBlock = createBlock(false);
980 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000981
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000982 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000983 ExitConditionBlock->setTerminator(F);
984
985 // Now add the actual condition to the condition block. Because the condition
986 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000987 if (Stmt* C = F->getCond()) {
988 Block = ExitConditionBlock;
989 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000990 assert(Block == EntryConditionBlock);
991
992 // If this block contains a condition variable, add both the condition
993 // variable and initializer to the CFG.
994 if (VarDecl *VD = F->getConditionVariable()) {
995 if (Expr *Init = VD->getInit()) {
996 autoCreateBlock();
997 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
998 EntryConditionBlock = addStmt(Init);
999 assert(Block == EntryConditionBlock);
1000 }
1001 }
1002
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001003 if (Block) {
1004 if (!FinishBlock(EntryConditionBlock))
1005 return 0;
1006 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001007 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001008
Mike Stump6d9828c2009-07-17 01:31:16 +00001009 // The condition block is the implicit successor for the loop body as well as
1010 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001011 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001012
Mike Stump00998a02009-07-23 23:25:26 +00001013 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001014 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Mike Stump00998a02009-07-23 23:25:26 +00001016 if (F->getCond())
1017 KnownVal = TryEvaluateBool(F->getCond());
1018
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001019 // Now create the loop body.
1020 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001021 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001022
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001023 // Save the current values for Block, Succ, and continue and break targets
1024 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001025 save_continue(ContinueTargetBlock),
1026 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001027
Ted Kremenekaf603f72007-08-30 18:39:40 +00001028 // Create a new block to contain the (bottom) of the loop body.
1029 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001030
Ted Kremeneke9334502008-09-04 21:48:47 +00001031 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001032 // Generate increment code in its own basic block. This is the target of
1033 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001034 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001035 } else {
1036 // No increment code. Create a special, empty, block that is used as the
1037 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001038 assert(Succ == EntryConditionBlock);
1039 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001040 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001041
Ted Kremenek3575f842009-04-28 00:51:56 +00001042 // Finish up the increment (or empty) block if it hasn't been already.
1043 if (Block) {
1044 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001045 if (!FinishBlock(Block))
1046 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001047 Block = 0;
1048 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001049
Ted Kremenek3575f842009-04-28 00:51:56 +00001050 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001051
Ted Kremenek3575f842009-04-28 00:51:56 +00001052 // The starting block for the loop increment is the block that should
1053 // represent the 'loop target' for looping back to the start of the loop.
1054 ContinueTargetBlock->setLoopTarget(F);
1055
Ted Kremeneke9334502008-09-04 21:48:47 +00001056 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +00001057 BreakTargetBlock = LoopSuccessor;
1058
1059 // Now populate the body block, and in the process create new blocks as we
1060 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001061 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001062
1063 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001064 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001065 else if (Block && !FinishBlock(BodyBlock))
1066 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001067
Ted Kremenek941fde82009-07-24 04:47:11 +00001068 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001069 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001070 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001071
Ted Kremenek941fde82009-07-24 04:47:11 +00001072 // Link up the condition block with the code that follows the loop. (the
1073 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001074 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001075
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001076 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001077 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001078 if (Stmt* I = F->getInit()) {
1079 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001080 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001081 } else {
1082 // There is no loop initialization. We are thus basically a while loop.
1083 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001084 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001085 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001086 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001087 }
1088}
1089
Ted Kremenek115c1b92010-04-11 17:02:10 +00001090CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1091 if (asc.alwaysAdd()) {
1092 autoCreateBlock();
1093 AppendStmt(Block, M, asc);
1094 }
1095 return Visit(M->getBase(),
1096 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1097 : AddStmtChoice::AsLValueNotAlwaysAdd);
1098}
1099
Ted Kremenek514de5a2008-11-11 17:10:00 +00001100CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1101 // Objective-C fast enumeration 'for' statements:
1102 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1103 //
1104 // for ( Type newVariable in collection_expression ) { statements }
1105 //
1106 // becomes:
1107 //
1108 // prologue:
1109 // 1. collection_expression
1110 // T. jump to loop_entry
1111 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001112 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001113 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1114 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1115 // TB:
1116 // statements
1117 // T. jump to loop_entry
1118 // FB:
1119 // what comes after
1120 //
1121 // and
1122 //
1123 // Type existingItem;
1124 // for ( existingItem in expression ) { statements }
1125 //
1126 // becomes:
1127 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001128 // the same with newVariable replaced with existingItem; the binding works
1129 // the same except that for one ObjCForCollectionStmt::getElement() returns
1130 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001131 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001132
Ted Kremenek514de5a2008-11-11 17:10:00 +00001133 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001134
Ted Kremenek514de5a2008-11-11 17:10:00 +00001135 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001136 if (!FinishBlock(Block))
1137 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001138 LoopSuccessor = Block;
1139 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001140 } else
1141 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001142
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001143 // Build the condition blocks.
1144 CFGBlock* ExitConditionBlock = createBlock(false);
1145 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001146
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001147 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001148 ExitConditionBlock->setTerminator(S);
1149
1150 // The last statement in the block should be the ObjCForCollectionStmt, which
1151 // performs the actual binding to 'element' and determines if there are any
1152 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001153 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001154 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001155
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001156 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001157 // generate new blocks as necesary. We DON'T add the statement by default to
1158 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001159 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001160 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001161 if (!FinishBlock(EntryConditionBlock))
1162 return 0;
1163 Block = 0;
1164 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001165
1166 // The condition block is the implicit successor for the loop body as well as
1167 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001168 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001169
Ted Kremenek514de5a2008-11-11 17:10:00 +00001170 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001171 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001172 // Save the current values for Succ, continue and break targets.
1173 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001174 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1175
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001176 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001177 ContinueTargetBlock = EntryConditionBlock;
1178
Ted Kremenek4f880632009-07-17 22:18:43 +00001179 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001180
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001181 if (!BodyBlock)
1182 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001183 else if (Block) {
1184 if (!FinishBlock(BodyBlock))
1185 return 0;
1186 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001187
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001188 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001189 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001190 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001191
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001192 // Link up the condition block with the code that follows the loop.
1193 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001194 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001195
Ted Kremenek514de5a2008-11-11 17:10:00 +00001196 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001197 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001198 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001199}
1200
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001201CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1202 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001203
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001204 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001205 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001206
Ted Kremenekda5348e2009-05-05 23:11:51 +00001207 // The sync body starts its own basic block. This makes it a little easier
1208 // for diagnostic clients.
1209 if (SyncBlock) {
1210 if (!FinishBlock(SyncBlock))
1211 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001212
Ted Kremenekda5348e2009-05-05 23:11:51 +00001213 Block = 0;
1214 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001215
Ted Kremenekda5348e2009-05-05 23:11:51 +00001216 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001217
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001218 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001219 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001220}
Mike Stump6d9828c2009-07-17 01:31:16 +00001221
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001222CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001223 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001224 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001225}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001226
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001227CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001228 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001229
Mike Stumpfefb9f72009-07-21 01:12:51 +00001230 // "while" is a control-flow statement. Thus we stop processing the current
1231 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001232 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001233 if (!FinishBlock(Block))
1234 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001235 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001236 } else
1237 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001238
1239 // Because of short-circuit evaluation, the condition of the loop can span
1240 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1241 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001242 CFGBlock* ExitConditionBlock = createBlock(false);
1243 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001244
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001245 // Set the terminator for the "exit" condition block.
1246 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001247
1248 // Now add the actual condition to the condition block. Because the condition
1249 // itself may contain control-flow, new blocks may be created. Thus we update
1250 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001251 if (Stmt* C = W->getCond()) {
1252 Block = ExitConditionBlock;
1253 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001254 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001255
1256 // If this block contains a condition variable, add both the condition
1257 // variable and initializer to the CFG.
1258 if (VarDecl *VD = W->getConditionVariable()) {
1259 if (Expr *Init = VD->getInit()) {
1260 autoCreateBlock();
1261 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1262 EntryConditionBlock = addStmt(Init);
1263 assert(Block == EntryConditionBlock);
1264 }
1265 }
1266
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001267 if (Block) {
1268 if (!FinishBlock(EntryConditionBlock))
1269 return 0;
1270 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001271 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001272
1273 // The condition block is the implicit successor for the loop body as well as
1274 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001275 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001276
Mike Stump00998a02009-07-23 23:25:26 +00001277 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001278 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001279
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 // Process the loop body.
1281 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001282 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001283
1284 // Save the current values for Block, Succ, and continue and break targets
1285 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1286 save_continue(ContinueTargetBlock),
1287 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001288
Mike Stump6d9828c2009-07-17 01:31:16 +00001289 // Create an empty block to represent the transition block for looping back
1290 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001291 Block = 0;
1292 assert(Succ == EntryConditionBlock);
1293 Succ = createBlock();
1294 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001295 ContinueTargetBlock = Succ;
1296
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001297 // All breaks should go to the code following the loop.
1298 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001299
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001300 // NULL out Block to force lazy instantiation of blocks for the body.
1301 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001302
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001303 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001304 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001305
Ted Kremenekaf603f72007-08-30 18:39:40 +00001306 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001307 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001308 else if (Block) {
1309 if (!FinishBlock(BodyBlock))
1310 return 0;
1311 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001312
Ted Kremenek941fde82009-07-24 04:47:11 +00001313 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001314 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001315 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001316
Ted Kremenek941fde82009-07-24 04:47:11 +00001317 // Link up the condition block with the code that follows the loop. (the
1318 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001319 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
1321 // There can be no more statements in the condition block since we loop back
1322 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001323 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001325 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001326 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001327 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001328}
Mike Stump1eb44332009-09-09 15:08:12 +00001329
1330
Ted Kremenek4f880632009-07-17 22:18:43 +00001331CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1332 // FIXME: For now we pretend that @catch and the code it contains does not
1333 // exit.
1334 return Block;
1335}
Mike Stump6d9828c2009-07-17 01:31:16 +00001336
Ted Kremenek2fda5042008-12-09 20:20:09 +00001337CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1338 // FIXME: This isn't complete. We basically treat @throw like a return
1339 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001340
Ted Kremenek6c249722009-09-24 18:45:41 +00001341 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001342 if (Block && !FinishBlock(Block))
1343 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001344
Ted Kremenek2fda5042008-12-09 20:20:09 +00001345 // Create the new block.
1346 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001347
Ted Kremenek2fda5042008-12-09 20:20:09 +00001348 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001349 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001350
1351 // Add the statement to the block. This may create new blocks if S contains
1352 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001353 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001354}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001355
Mike Stump0979d802009-07-22 22:56:04 +00001356CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001357 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001358 if (Block && !FinishBlock(Block))
1359 return 0;
1360
1361 // Create the new block.
1362 Block = createBlock(false);
1363
Mike Stump5d1d2022010-01-19 02:20:09 +00001364 if (TryTerminatedBlock)
1365 // The current try statement is the only successor.
1366 AddSuccessor(Block, TryTerminatedBlock);
1367 else
1368 // otherwise the Exit block is the only successor.
1369 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001370
1371 // Add the statement to the block. This may create new blocks if S contains
1372 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001373 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001374}
1375
Ted Kremenek4f880632009-07-17 22:18:43 +00001376CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001377 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001378
Mike Stump8f9893a2009-07-21 01:27:50 +00001379 // "do...while" is a control-flow statement. Thus we stop processing the
1380 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001381 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001382 if (!FinishBlock(Block))
1383 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001384 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001385 } else
1386 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001387
1388 // Because of short-circuit evaluation, the condition of the loop can span
1389 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1390 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001391 CFGBlock* ExitConditionBlock = createBlock(false);
1392 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001393
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001394 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001395 ExitConditionBlock->setTerminator(D);
1396
1397 // Now add the actual condition to the condition block. Because the condition
1398 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001399 if (Stmt* C = D->getCond()) {
1400 Block = ExitConditionBlock;
1401 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001402 if (Block) {
1403 if (!FinishBlock(EntryConditionBlock))
1404 return 0;
1405 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001406 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001407
Ted Kremenek54827132008-02-27 07:20:00 +00001408 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001409 Succ = EntryConditionBlock;
1410
Mike Stump00998a02009-07-23 23:25:26 +00001411 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001412 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001413
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001414 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001415 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001416 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001417 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001418
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001419 // Save the current values for Block, Succ, and continue and break targets
1420 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001421 save_continue(ContinueTargetBlock),
1422 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001423
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001424 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001425 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001426
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001427 // All breaks should go to the code following the loop.
1428 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001429
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001430 // NULL out Block to force lazy instantiation of blocks for the body.
1431 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001434 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001435
Ted Kremenekaf603f72007-08-30 18:39:40 +00001436 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001437 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001438 else if (Block) {
1439 if (!FinishBlock(BodyBlock))
1440 return 0;
1441 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001442
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001443 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001444 // ExitConditionBlock to represent the "loop back" transition. Create an
1445 // empty block to represent the transition block for looping back to the
1446 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001447 // FIXME: Can we do this more efficiently without adding another block?
1448 Block = NULL;
1449 Succ = BodyBlock;
1450 CFGBlock *LoopBackBlock = createBlock();
1451 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
Ted Kremenek941fde82009-07-24 04:47:11 +00001453 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001454 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001455 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001456
Ted Kremenek941fde82009-07-24 04:47:11 +00001457 // Link up the condition block with the code that follows the loop.
1458 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001459 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
1461 // There can be no more statements in the body block(s) since we loop back to
1462 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001463 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001464
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001465 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001466 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001467 return BodyBlock;
1468}
1469
1470CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1471 // "continue" is a control-flow statement. Thus we stop processing the
1472 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001473 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001474 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001476 // Now create a new block that ends with the continue statement.
1477 Block = createBlock(false);
1478 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001479
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001480 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001481 // incomplete AST. This means the CFG cannot be constructed.
1482 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001483 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001484 else
1485 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001486
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001487 return Block;
1488}
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001490CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001491 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001492
Ted Kremenek852274d2009-12-16 03:18:58 +00001493 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001494 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001495 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001496 }
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek4f880632009-07-17 22:18:43 +00001498 // VLA types have expressions that must be evaluated.
1499 if (E->isArgumentType()) {
1500 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1501 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1502 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Mike Stump6d9828c2009-07-17 01:31:16 +00001505 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001506}
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Ted Kremenek4f880632009-07-17 22:18:43 +00001508/// VisitStmtExpr - Utility method to handle (nested) statement
1509/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001510CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1511 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001512 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001513 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001514 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001515 return VisitCompoundStmt(SE->getSubStmt());
1516}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001517
Ted Kremenek411cdee2008-04-16 21:10:48 +00001518CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001519 // "switch" is a control-flow statement. Thus we stop processing the current
1520 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001521 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001522
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001523 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001524 if (!FinishBlock(Block))
1525 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001526 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001527 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001528
1529 // Save the current "switch" context.
1530 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001531 save_break(BreakTargetBlock),
1532 save_default(DefaultCaseBlock);
1533
Mike Stump6d9828c2009-07-17 01:31:16 +00001534 // Set the "default" case to be the block after the switch statement. If the
1535 // switch statement contains a "default:", this value will be overwritten with
1536 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001537 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001538
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001539 // Create a new block that will contain the switch statement.
1540 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001541
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001542 // Now process the switch body. The code after the switch is the implicit
1543 // successor.
1544 Succ = SwitchSuccessor;
1545 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001546
1547 // When visiting the body, the case statements should automatically get linked
1548 // up to the switch. We also don't keep a pointer to the body, since all
1549 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001550 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001551 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001552 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001553 if (Block) {
1554 if (!FinishBlock(BodyBlock))
1555 return 0;
1556 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001557
Mike Stump6d9828c2009-07-17 01:31:16 +00001558 // If we have no "default:" case, the default transition is to the code
1559 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001560 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001561
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001562 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001563 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001564 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001565 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001566 Block = addStmt(Terminator->getCond());
1567
1568 // Finally, if the SwitchStmt contains a condition variable, add both the
1569 // SwitchStmt and the condition variable initialization to the CFG.
1570 if (VarDecl *VD = Terminator->getConditionVariable()) {
1571 if (Expr *Init = VD->getInit()) {
1572 autoCreateBlock();
1573 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1574 addStmt(Init);
1575 }
1576 }
1577
1578 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001579}
1580
Ted Kremenek4f880632009-07-17 22:18:43 +00001581CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001582 // CaseStmts are essentially labels, so they are the first statement in a
1583 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001584
Ted Kremenek4f880632009-07-17 22:18:43 +00001585 if (CS->getSubStmt())
1586 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001588 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001589 if (!CaseBlock)
1590 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001591
1592 // Cases statements partition blocks, so this is the top of the basic block we
1593 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001594 CaseBlock->setLabel(CS);
1595
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001596 if (!FinishBlock(CaseBlock))
1597 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001598
1599 // Add this block to the list of successors for the block with the switch
1600 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001601 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001602 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001603
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001604 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1605 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001606
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001607 // This block is now the implicit successor of other blocks.
1608 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001609
Ted Kremenek2677ea82008-03-15 07:45:02 +00001610 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001611}
Mike Stump6d9828c2009-07-17 01:31:16 +00001612
Ted Kremenek411cdee2008-04-16 21:10:48 +00001613CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001614 if (Terminator->getSubStmt())
1615 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001617 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001618
1619 if (!DefaultCaseBlock)
1620 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001621
1622 // Default statements partition blocks, so this is the top of the basic block
1623 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001624 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001626 if (!FinishBlock(DefaultCaseBlock))
1627 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001628
Mike Stump6d9828c2009-07-17 01:31:16 +00001629 // Unlike case statements, we don't add the default block to the successors
1630 // for the switch statement immediately. This is done when we finish
1631 // processing the switch statement. This allows for the default case
1632 // (including a fall-through to the code after the switch statement) to always
1633 // be the last successor of a switch-terminated block.
1634
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001635 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1636 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001637
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001638 // This block is now the implicit successor of other blocks.
1639 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001640
1641 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001642}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001643
Mike Stump5d1d2022010-01-19 02:20:09 +00001644CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1645 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1646 // current block.
1647 CFGBlock* TrySuccessor = NULL;
1648
1649 if (Block) {
1650 if (!FinishBlock(Block))
1651 return 0;
1652 TrySuccessor = Block;
1653 } else TrySuccessor = Succ;
1654
Mike Stumpa1f93632010-01-20 01:15:34 +00001655 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001656
1657 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001658 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001659 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001660 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001661
Mike Stumpa1f93632010-01-20 01:15:34 +00001662 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001663 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1664 // The code after the try is the implicit successor.
1665 Succ = TrySuccessor;
1666 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001667 if (CS->getExceptionDecl() == 0) {
1668 HasCatchAll = true;
1669 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001670 Block = NULL;
1671 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1672 if (CatchBlock == 0)
1673 return 0;
1674 // Add this block to the list of successors for the block with the try
1675 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001676 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001677 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001678 if (!HasCatchAll) {
1679 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001680 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001681 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001682 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001683 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001684
1685 // The code after the try is the implicit successor.
1686 Succ = TrySuccessor;
1687
Mike Stumpf00cca52010-01-20 01:30:58 +00001688 // Save the current "try" context.
1689 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1690 TryTerminatedBlock = NewTryTerminatedBlock;
1691
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001692 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001693 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001694 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001695 return Block;
1696}
1697
1698CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1699 // CXXCatchStmt are treated like labels, so they are the first statement in a
1700 // block.
1701
1702 if (CS->getHandlerBlock())
1703 addStmt(CS->getHandlerBlock());
1704
1705 CFGBlock* CatchBlock = Block;
1706 if (!CatchBlock)
1707 CatchBlock = createBlock();
1708
1709 CatchBlock->setLabel(CS);
1710
1711 if (!FinishBlock(CatchBlock))
1712 return 0;
1713
1714 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1715 Block = NULL;
1716
1717 return CatchBlock;
1718}
1719
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001720CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
1721 AddStmtChoice asc) {
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001722 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1723 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001724 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001725 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001726 return VisitChildren(C);
1727}
1728
Ted Kremenek19bb3562007-08-28 19:26:49 +00001729CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001730 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001731 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001732
Ted Kremenek19bb3562007-08-28 19:26:49 +00001733 if (!IBlock) {
1734 IBlock = createBlock(false);
1735 cfg->setIndirectGotoBlock(IBlock);
1736 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001737
Ted Kremenek19bb3562007-08-28 19:26:49 +00001738 // IndirectGoto is a control-flow statement. Thus we stop processing the
1739 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001740 if (Block && !FinishBlock(Block))
1741 return 0;
1742
Ted Kremenek19bb3562007-08-28 19:26:49 +00001743 Block = createBlock(false);
1744 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001745 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001746 return addStmt(I->getTarget());
1747}
1748
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001749} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001750
Mike Stump6d9828c2009-07-17 01:31:16 +00001751/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1752/// no successors or predecessors. If this is the first block created in the
1753/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001754CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001755 bool first_block = begin() == end();
1756
1757 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001758 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1759 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1760 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001761
1762 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001763 if (first_block)
1764 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001765
1766 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001767 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001768}
1769
Ted Kremenek026473c2007-08-23 16:51:22 +00001770/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1771/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001772CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump4c45aa12010-01-21 15:20:48 +00001773 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001774 CFGBuilder Builder;
Mike Stump4c45aa12010-01-21 15:20:48 +00001775 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001776}
1777
Ted Kremenek63f58872007-10-01 19:33:33 +00001778//===----------------------------------------------------------------------===//
1779// CFG: Queries for BlkExprs.
1780//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001781
Ted Kremenek63f58872007-10-01 19:33:33 +00001782namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001783 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001784}
1785
Ted Kremenek8a693662009-12-23 23:37:10 +00001786static void FindSubExprAssignments(Stmt *S,
1787 llvm::SmallPtrSet<Expr*,50>& Set) {
1788 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001789 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001790
Ted Kremenek8a693662009-12-23 23:37:10 +00001791 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1792 Stmt *child = *I;
1793 if (!child)
1794 continue;
1795
1796 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001797 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Ted Kremenek8a693662009-12-23 23:37:10 +00001799 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001800 }
1801}
1802
Ted Kremenek63f58872007-10-01 19:33:33 +00001803static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1804 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001805
1806 // Look for assignments that are used as subexpressions. These are the only
1807 // assignments that we want to *possibly* register as a block-level
1808 // expression. Basically, if an assignment occurs both in a subexpression and
1809 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001810 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001811
Ted Kremenek63f58872007-10-01 19:33:33 +00001812 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001813 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001814 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001815
Ted Kremenek411cdee2008-04-16 21:10:48 +00001816 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001817
1818 // Iterate over the statements again on identify the Expr* and Stmt* at the
1819 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001820
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001821 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001822 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001823
Ted Kremenek411cdee2008-04-16 21:10:48 +00001824 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001825 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001826 // expression are really "statements" whose value is never used by
1827 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001828 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001829 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001830 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1831 // Special handling for statement expressions. The last statement in
1832 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001833 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001834 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001835 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001836 (*M)[C->body_back()] = x;
1837 }
1838 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001839
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001840 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001841 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001842 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001843
Ted Kremenek411cdee2008-04-16 21:10:48 +00001844 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001845
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001846 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremenek390e48b2008-11-12 21:11:49 +00001848 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001849 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001850 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001851 }
1852 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001853
Ted Kremenek63f58872007-10-01 19:33:33 +00001854 return M;
1855}
1856
Ted Kremenek86946742008-01-17 20:48:37 +00001857CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1858 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001859 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001860
Ted Kremenek63f58872007-10-01 19:33:33 +00001861 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001862 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001863 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001864}
1865
1866unsigned CFG::getNumBlkExprs() {
1867 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1868 return M->size();
1869 else {
1870 // We assume callers interested in the number of BlkExprs will want
1871 // the map constructed if it doesn't already exist.
1872 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1873 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1874 }
1875}
1876
Ted Kremenek274f4332008-04-28 18:00:46 +00001877//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001878// Cleanup: CFG dstor.
1879//===----------------------------------------------------------------------===//
1880
Ted Kremenek63f58872007-10-01 19:33:33 +00001881CFG::~CFG() {
1882 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1883}
Mike Stump6d9828c2009-07-17 01:31:16 +00001884
Ted Kremenek7dba8602007-08-29 21:56:09 +00001885//===----------------------------------------------------------------------===//
1886// CFG pretty printing
1887//===----------------------------------------------------------------------===//
1888
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001889namespace {
1890
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001891class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001892 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1893 StmtMapTy StmtMap;
1894 signed CurrentBlock;
1895 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001896 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001897public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001898
Chris Lattnere4f21422009-06-30 01:26:17 +00001899 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1900 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001901 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1902 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001903 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001904 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001905 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001906 }
1907 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001908
Ted Kremenek42a509f2007-08-31 21:30:12 +00001909 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001910
Chris Lattnere4f21422009-06-30 01:26:17 +00001911 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001912 void setBlockID(signed i) { CurrentBlock = i; }
1913 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001914
Ted Kremeneka95d3752008-09-13 05:16:45 +00001915 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenek411cdee2008-04-16 21:10:48 +00001917 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001918
1919 if (I == StmtMap.end())
1920 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001921
1922 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001923 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001924 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001925 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001926
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001927 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001928 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001929 }
1930};
Chris Lattnere4f21422009-06-30 01:26:17 +00001931} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001932
Chris Lattnere4f21422009-06-30 01:26:17 +00001933
1934namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001935class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001936 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001937
Ted Kremeneka95d3752008-09-13 05:16:45 +00001938 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001939 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001940 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001941public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001942 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001943 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001944 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001945
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001946 void VisitIfStmt(IfStmt* I) {
1947 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001948 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001949 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001950
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001951 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001952 void VisitStmt(Stmt* Terminator) {
1953 Terminator->printPretty(OS, Helper, Policy);
1954 }
1955
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001956 void VisitForStmt(ForStmt* F) {
1957 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001958 if (F->getInit())
1959 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001960 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001961 if (Stmt* C = F->getCond())
1962 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001963 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001964 if (F->getInc())
1965 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001966 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001967 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001969 void VisitWhileStmt(WhileStmt* W) {
1970 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001971 if (Stmt* C = W->getCond())
1972 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001973 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001974
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001975 void VisitDoStmt(DoStmt* D) {
1976 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001977 if (Stmt* C = D->getCond())
1978 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001979 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001980
Ted Kremenek411cdee2008-04-16 21:10:48 +00001981 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001982 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001983 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001984 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001985
Mike Stump5d1d2022010-01-19 02:20:09 +00001986 void VisitCXXTryStmt(CXXTryStmt* CS) {
1987 OS << "try ...";
1988 }
1989
Ted Kremenek805e9a82007-08-31 21:49:40 +00001990 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001991 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001992 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001993 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001995 void VisitChooseExpr(ChooseExpr* C) {
1996 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001997 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001998 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001999 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002000
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002001 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2002 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002003 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002004 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002005
Ted Kremenek805e9a82007-08-31 21:49:40 +00002006 void VisitBinaryOperator(BinaryOperator* B) {
2007 if (!B->isLogicalOp()) {
2008 VisitExpr(B);
2009 return;
2010 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002011
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002012 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002013
Ted Kremenek805e9a82007-08-31 21:49:40 +00002014 switch (B->getOpcode()) {
2015 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002016 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002017 return;
2018 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002019 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002020 return;
2021 default:
2022 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002023 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002024 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002025
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002026 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002027 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002028 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002029};
Chris Lattnere4f21422009-06-30 01:26:17 +00002030} // end anonymous namespace
2031
Mike Stump6d9828c2009-07-17 01:31:16 +00002032
Chris Lattnere4f21422009-06-30 01:26:17 +00002033static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002034 const CFGElement &E) {
2035 Stmt *Terminator = E;
2036
2037 if (E.asStartScope()) {
2038 OS << "start scope\n";
2039 return;
2040 }
2041 if (E.asEndScope()) {
2042 OS << "end scope\n";
2043 return;
2044 }
2045
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002046 if (Helper) {
2047 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002048 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002049 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002050
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002051 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002052 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002053 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002054 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002055 return;
2056 }
2057 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002058
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002059 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002060 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002061 if (B->getOpcode() == BinaryOperator::Comma) {
2062 OS << "... , ";
2063 Helper->handledStmt(B->getRHS(),OS);
2064 OS << '\n';
2065 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002066 }
2067 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002068 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002069
Chris Lattnere4f21422009-06-30 01:26:17 +00002070 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002071
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002072 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002073 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002074}
Mike Stump6d9828c2009-07-17 01:31:16 +00002075
Chris Lattnere4f21422009-06-30 01:26:17 +00002076static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2077 const CFGBlock& B,
2078 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002079
Ted Kremenek42a509f2007-08-31 21:30:12 +00002080 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002081
Ted Kremenek7dba8602007-08-29 21:56:09 +00002082 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002083 OS << "\n [ B" << B.getBlockID();
2084
Ted Kremenek42a509f2007-08-31 21:30:12 +00002085 if (&B == &cfg->getEntry())
2086 OS << " (ENTRY) ]\n";
2087 else if (&B == &cfg->getExit())
2088 OS << " (EXIT) ]\n";
2089 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002090 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002091 else
2092 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002093
Ted Kremenek9cffe732007-08-29 23:20:49 +00002094 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002095 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002096
2097 if (print_edges)
2098 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002099
Mike Stump079bd722010-01-19 22:00:14 +00002100 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002101 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002102 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002103 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002104 C->getLHS()->printPretty(OS, Helper,
2105 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002106 if (C->getRHS()) {
2107 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002108 C->getRHS()->printPretty(OS, Helper,
2109 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002110 }
Mike Stump079bd722010-01-19 22:00:14 +00002111 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002112 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002113 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002114 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002115 if (CS->getExceptionDecl())
2116 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2117 0);
2118 else
2119 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002120 OS << ")";
2121
2122 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002123 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002124
Ted Kremenek9cffe732007-08-29 23:20:49 +00002125 OS << ":\n";
2126 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002127
Ted Kremenekfddd5182007-08-21 21:42:03 +00002128 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002129 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002130
Ted Kremenek42a509f2007-08-31 21:30:12 +00002131 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2132 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002133
Ted Kremenek9cffe732007-08-29 23:20:49 +00002134 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002135 if (print_edges)
2136 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002137
Ted Kremeneka95d3752008-09-13 05:16:45 +00002138 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002139
Ted Kremenek42a509f2007-08-31 21:30:12 +00002140 if (Helper)
2141 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002142
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002143 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002144 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002145
Ted Kremenek9cffe732007-08-29 23:20:49 +00002146 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002147 if (B.getTerminator()) {
2148 if (print_edges)
2149 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002150
Ted Kremenek9cffe732007-08-29 23:20:49 +00002151 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002152
Ted Kremenek42a509f2007-08-31 21:30:12 +00002153 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002154
Chris Lattnere4f21422009-06-30 01:26:17 +00002155 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2156 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002157 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002158 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002159 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002160
Ted Kremenek9cffe732007-08-29 23:20:49 +00002161 if (print_edges) {
2162 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002163 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002164 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002165
Ted Kremenek42a509f2007-08-31 21:30:12 +00002166 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2167 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002168
Ted Kremenek42a509f2007-08-31 21:30:12 +00002169 if (i == 8 || (i-8) == 0)
2170 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002171
Ted Kremenek9cffe732007-08-29 23:20:49 +00002172 OS << " B" << (*I)->getBlockID();
2173 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002174
Ted Kremenek42a509f2007-08-31 21:30:12 +00002175 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002176
Ted Kremenek42a509f2007-08-31 21:30:12 +00002177 // Print the successors of this block.
2178 OS << " Successors (" << B.succ_size() << "):";
2179 i = 0;
2180
2181 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2182 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002183
Ted Kremenek42a509f2007-08-31 21:30:12 +00002184 if (i == 8 || (i-8) % 10 == 0)
2185 OS << "\n ";
2186
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002187 if (*I)
2188 OS << " B" << (*I)->getBlockID();
2189 else
2190 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002191 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002192
Ted Kremenek9cffe732007-08-29 23:20:49 +00002193 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002194 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002195}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002196
Ted Kremenek42a509f2007-08-31 21:30:12 +00002197
2198/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002199void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002200
2201/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002202void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2203 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002204
Ted Kremenek42a509f2007-08-31 21:30:12 +00002205 // Print the entry block.
2206 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002207
Ted Kremenek42a509f2007-08-31 21:30:12 +00002208 // Iterate through the CFGBlocks and print them one by one.
2209 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2210 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002211 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002212 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002213
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002214 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002215 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002216
Ted Kremenek42a509f2007-08-31 21:30:12 +00002217 // Print the exit block.
2218 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002219 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002220}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002221
2222/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002223void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2224 print(llvm::errs(), cfg, LO);
2225}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002226
2227/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2228/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002229void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2230 const LangOptions &LO) const {
2231 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002232 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002233}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002234
Ted Kremeneka2925852008-01-30 23:02:42 +00002235/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002236void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002237 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002238 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002239 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2240}
2241
Ted Kremenek390e48b2008-11-12 21:11:49 +00002242Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002243
Ted Kremenek411cdee2008-04-16 21:10:48 +00002244 if (!Terminator)
2245 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002246
Ted Kremenek411cdee2008-04-16 21:10:48 +00002247 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002248
Ted Kremenek411cdee2008-04-16 21:10:48 +00002249 switch (Terminator->getStmtClass()) {
2250 default:
2251 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002252
Ted Kremenek411cdee2008-04-16 21:10:48 +00002253 case Stmt::ForStmtClass:
2254 E = cast<ForStmt>(Terminator)->getCond();
2255 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002256
Ted Kremenek411cdee2008-04-16 21:10:48 +00002257 case Stmt::WhileStmtClass:
2258 E = cast<WhileStmt>(Terminator)->getCond();
2259 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002260
Ted Kremenek411cdee2008-04-16 21:10:48 +00002261 case Stmt::DoStmtClass:
2262 E = cast<DoStmt>(Terminator)->getCond();
2263 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002264
Ted Kremenek411cdee2008-04-16 21:10:48 +00002265 case Stmt::IfStmtClass:
2266 E = cast<IfStmt>(Terminator)->getCond();
2267 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek411cdee2008-04-16 21:10:48 +00002269 case Stmt::ChooseExprClass:
2270 E = cast<ChooseExpr>(Terminator)->getCond();
2271 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002272
Ted Kremenek411cdee2008-04-16 21:10:48 +00002273 case Stmt::IndirectGotoStmtClass:
2274 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2275 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002276
Ted Kremenek411cdee2008-04-16 21:10:48 +00002277 case Stmt::SwitchStmtClass:
2278 E = cast<SwitchStmt>(Terminator)->getCond();
2279 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002280
Ted Kremenek411cdee2008-04-16 21:10:48 +00002281 case Stmt::ConditionalOperatorClass:
2282 E = cast<ConditionalOperator>(Terminator)->getCond();
2283 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002284
Ted Kremenek411cdee2008-04-16 21:10:48 +00002285 case Stmt::BinaryOperatorClass: // '&&' and '||'
2286 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002287 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002288
Ted Kremenek390e48b2008-11-12 21:11:49 +00002289 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002290 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002291 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002292
Ted Kremenek411cdee2008-04-16 21:10:48 +00002293 return E ? E->IgnoreParens() : NULL;
2294}
2295
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002296bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002297
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002298 if (!Terminator)
2299 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002300
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002301 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002302
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002303 switch (Terminator->getStmtClass()) {
2304 default:
2305 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002306
2307 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002308 case Stmt::WhileStmtClass:
2309 case Stmt::DoStmtClass:
2310 case Stmt::IfStmtClass:
2311 case Stmt::ChooseExprClass:
2312 case Stmt::ConditionalOperatorClass:
2313 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002314 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002315 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002316
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002317 return E ? E->IgnoreParens() : NULL;
2318}
2319
Ted Kremeneka2925852008-01-30 23:02:42 +00002320
Ted Kremenek7dba8602007-08-29 21:56:09 +00002321//===----------------------------------------------------------------------===//
2322// CFG Graphviz Visualization
2323//===----------------------------------------------------------------------===//
2324
Ted Kremenek42a509f2007-08-31 21:30:12 +00002325
2326#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002327static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002328#endif
2329
Chris Lattnere4f21422009-06-30 01:26:17 +00002330void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002331#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002332 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002333 GraphHelper = &H;
2334 llvm::ViewGraph(this,"CFG");
2335 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002336#endif
2337}
2338
Ted Kremenek7dba8602007-08-29 21:56:09 +00002339namespace llvm {
2340template<>
2341struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002342
2343 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2344
2345 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002346
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002347#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002348 std::string OutSStr;
2349 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002350 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002351 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002352
2353 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2354
2355 // Process string output to make it nicer...
2356 for (unsigned i = 0; i != OutStr.length(); ++i)
2357 if (OutStr[i] == '\n') { // Left justify
2358 OutStr[i] = '\\';
2359 OutStr.insert(OutStr.begin()+i+1, 'l');
2360 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002361
Ted Kremenek7dba8602007-08-29 21:56:09 +00002362 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002363#else
2364 return "";
2365#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002366 }
2367};
2368} // end namespace llvm