blob: 7c7504a1d82707448eea3990508f63f1b41398b7 [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());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000501
502 if (RHSBlock) {
503 if (!FinishBlock(RHSBlock))
504 return 0;
505 }
506 else {
507 // Create an empty block for cases where the RHS doesn't require
508 // any explicit statements in the CFG.
509 RHSBlock = createBlock();
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Mike Stump00998a02009-07-23 23:25:26 +0000512 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000513 TryResult KnownVal = TryEvaluateBool(B->getLHS());
514 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
515 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000516
Ted Kremenek4f880632009-07-17 22:18:43 +0000517 // Now link the LHSBlock with RHSBlock.
518 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000519 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
520 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000521 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000522 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000523 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
524 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek4f880632009-07-17 22:18:43 +0000527 // Generate the blocks for evaluating the LHS.
528 Block = LHSBlock;
529 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000530 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000531 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000532 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000533 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000534 addStmt(B->getRHS());
535 return addStmt(B->getLHS());
536 }
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Ted Kremenek852274d2009-12-16 03:18:58 +0000538 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000539}
540
Ted Kremenek852274d2009-12-16 03:18:58 +0000541CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
542 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000543 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000544 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000545 }
546 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000547}
548
Ted Kremenek4f880632009-07-17 22:18:43 +0000549CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
550 // "break" is a control-flow statement. Thus we stop processing the current
551 // block.
552 if (Block && !FinishBlock(Block))
553 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek4f880632009-07-17 22:18:43 +0000555 // Now create a new block that ends with the break statement.
556 Block = createBlock(false);
557 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek4f880632009-07-17 22:18:43 +0000559 // If there is no target for the break, then we are looking at an incomplete
560 // AST. This means that the CFG cannot be constructed.
561 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000562 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000563 else
564 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
566
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000567 return Block;
568}
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Mike Stump4c45aa12010-01-21 15:20:48 +0000570static bool CanThrow(Expr *E) {
571 QualType Ty = E->getType();
572 if (Ty->isFunctionPointerType())
573 Ty = Ty->getAs<PointerType>()->getPointeeType();
574 else if (Ty->isBlockPointerType())
575 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
576
577 const FunctionType *FT = Ty->getAs<FunctionType>();
578 if (FT) {
579 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
580 if (Proto->hasEmptyExceptionSpec())
581 return false;
582 }
583 return true;
584}
585
Ted Kremenek852274d2009-12-16 03:18:58 +0000586CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000587 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000588 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000589 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000590 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000591 }
Mike Stump24556362009-07-25 21:26:53 +0000592
Mike Stump4c45aa12010-01-21 15:20:48 +0000593 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000594
595 // Languages without exceptions are assumed to not throw.
596 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000597 if (AddEHEdges)
598 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000599 }
600
601 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000602 if (FD->hasAttr<NoReturnAttr>())
603 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000604 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000605 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000606 }
Mike Stump24556362009-07-25 21:26:53 +0000607
Mike Stump4c45aa12010-01-21 15:20:48 +0000608 if (!CanThrow(C->getCallee()))
609 AddEHEdge = false;
610
611 if (!NoReturn && !AddEHEdge)
Zhongxing Xu91071662010-02-24 02:19:28 +0000612 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Mike Stump079bd722010-01-19 22:00:14 +0000614 if (Block) {
615 Succ = Block;
616 if (!FinishBlock(Block))
617 return 0;
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Mike Stump079bd722010-01-19 22:00:14 +0000620 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000621 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000622
Mike Stump079bd722010-01-19 22:00:14 +0000623 if (NoReturn) {
624 // Wire this to the exit block directly.
625 AddSuccessor(Block, &cfg->getExit());
626 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000627 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000628 // Add exceptional edges.
629 if (TryTerminatedBlock)
630 AddSuccessor(Block, TryTerminatedBlock);
631 else
632 AddSuccessor(Block, &cfg->getExit());
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Mike Stump24556362009-07-25 21:26:53 +0000635 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000636}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000637
Ted Kremenek852274d2009-12-16 03:18:58 +0000638CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
639 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000640 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000641 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000642 if (!FinishBlock(ConfluenceBlock))
643 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Ted Kremenek115c1b92010-04-11 17:02:10 +0000645 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
646 : AddStmtChoice::AlwaysAdd;
647
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000648 Succ = ConfluenceBlock;
649 Block = NULL;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000650 CFGBlock* LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000651 if (!FinishBlock(LHSBlock))
652 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000654 Succ = ConfluenceBlock;
655 Block = NULL;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000656 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000657 if (!FinishBlock(RHSBlock))
658 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000660 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000661 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000662 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000663 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
664 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000665 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000666 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000667}
Mike Stump1eb44332009-09-09 15:08:12 +0000668
669
670CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000671 EndScope(C);
672
Mike Stump1eb44332009-09-09 15:08:12 +0000673 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000674
675 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
676 I != E; ++I ) {
677 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000679 if (badCFG)
680 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000681 }
Mike Stump079bd722010-01-19 22:00:14 +0000682
683 LastBlock = StartScope(C, LastBlock);
684
Ted Kremenek4f880632009-07-17 22:18:43 +0000685 return LastBlock;
686}
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenek852274d2009-12-16 03:18:58 +0000688CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
689 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000690 // Create the confluence block that will "merge" the results of the ternary
691 // expression.
692 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000693 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000694 if (!FinishBlock(ConfluenceBlock))
695 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek115c1b92010-04-11 17:02:10 +0000697 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
698 : AddStmtChoice::AlwaysAdd;
699
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000700 // Create a block for the LHS expression if there is an LHS expression. A
701 // GCC extension allows LHS to be NULL, causing the condition to be the
702 // value that is returned instead.
703 // e.g: x ?: y is shorthand for: x ? x : y;
704 Succ = ConfluenceBlock;
705 Block = NULL;
706 CFGBlock* LHSBlock = NULL;
707 if (C->getLHS()) {
Ted Kremenek115c1b92010-04-11 17:02:10 +0000708 LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000709 if (!FinishBlock(LHSBlock))
710 return 0;
711 Block = NULL;
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000714 // Create the block for the RHS expression.
715 Succ = ConfluenceBlock;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000716 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000717 if (!FinishBlock(RHSBlock))
718 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000720 // Create the block that will contain the condition.
721 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Mike Stump00998a02009-07-23 23:25:26 +0000723 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000724 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000725 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000726 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000727 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000728 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000729 // If we know the condition is false, add NULL as the successor for
730 // the block containing the condition. In this case, the confluence
731 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000732 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000733 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000734 } else {
735 // If we have no LHS expression, add the ConfluenceBlock as a direct
736 // successor for the block containing the condition. Moreover, we need to
737 // reverse the order of the predecessors in the ConfluenceBlock because
738 // the RHSBlock will have been added to the succcessors already, and we
739 // want the first predecessor to the the block containing the expression
740 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000741 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000742 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000743 std::reverse(ConfluenceBlock->pred_begin(),
744 ConfluenceBlock->pred_end());
745 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000748 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000749 Block->setTerminator(C);
750 return addStmt(C->getCond());
751}
752
Ted Kremenek4f880632009-07-17 22:18:43 +0000753CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
754 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000755
Ted Kremenek4f880632009-07-17 22:18:43 +0000756 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000757 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000758 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000759 }
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek4f880632009-07-17 22:18:43 +0000761 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek4f880632009-07-17 22:18:43 +0000763 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
764 typedef llvm::SmallVector<Decl*,10> BufTy;
765 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek4f880632009-07-17 22:18:43 +0000767 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
768 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
769 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
770 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek4f880632009-07-17 22:18:43 +0000772 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
773 // automatically freed with the CFG.
774 DeclGroupRef DG(*I);
775 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000776 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000777 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek4f880632009-07-17 22:18:43 +0000779 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000780 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000781 B = VisitDeclSubExpr(D);
782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
784 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000785}
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek4f880632009-07-17 22:18:43 +0000787/// VisitDeclSubExpr - Utility method to add block-level expressions for
788/// initializers in Decls.
789CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
790 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000791
Ted Kremenek4f880632009-07-17 22:18:43 +0000792 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek4f880632009-07-17 22:18:43 +0000794 if (!VD)
795 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek4f880632009-07-17 22:18:43 +0000797 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek4f880632009-07-17 22:18:43 +0000799 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000800 AddStmtChoice::Kind k =
801 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
802 : AddStmtChoice::NotAlwaysAdd;
803 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek4f880632009-07-17 22:18:43 +0000806 // If the type of VD is a VLA, then we must process its size expressions.
807 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
808 VA = FindVA(VA->getElementType().getTypePtr()))
809 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenek4f880632009-07-17 22:18:43 +0000811 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000812}
813
814CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000815 // We may see an if statement in the middle of a basic block, or it may be the
816 // first statement we are processing. In either case, we create a new basic
817 // block. First, we create the blocks for the then...else statements, and
818 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000819 // middle of a block, we stop processing that block. That block is then the
820 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000821
822 // The block we were proccessing is now finished. Make it the successor
823 // block.
824 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000825 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000826 if (!FinishBlock(Block))
827 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000828 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000829
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000830 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000831 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000832
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000833 if (Stmt* Else = I->getElse()) {
834 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000835
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000836 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000837 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000838 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000839 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000840
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000841 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
842 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000843 else if (Block) {
844 if (!FinishBlock(ElseBlock))
845 return 0;
846 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000847 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000848
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000849 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000850 CFGBlock* ThenBlock;
851 {
852 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000853 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000854 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000855 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000856 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000857
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000858 if (!ThenBlock) {
859 // We can reach here if the "then" body has all NullStmts.
860 // Create an empty block so we can distinguish between true and false
861 // branches in path-sensitive analyses.
862 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000863 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000864 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000865 if (!FinishBlock(ThenBlock))
866 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000867 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000868 }
869
Mike Stump6d9828c2009-07-17 01:31:16 +0000870 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000871 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000872
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000873 // Set the terminator of the new block to the If statement.
874 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000875
Mike Stump00998a02009-07-23 23:25:26 +0000876 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000877 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000878
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000879 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000880 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
881 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000882
883 // Add the condition as the last statement in the new block. This may create
884 // new blocks as the condition may contain control-flow. Any newly created
885 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000886 Block = addStmt(I->getCond());
887
888 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
889 // and the condition variable initialization to the CFG.
890 if (VarDecl *VD = I->getConditionVariable()) {
891 if (Expr *Init = VD->getInit()) {
892 autoCreateBlock();
893 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
894 addStmt(Init);
895 }
896 }
897
898 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000899}
Mike Stump6d9828c2009-07-17 01:31:16 +0000900
901
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000902CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000903 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000904 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000905 // NOTE: If a "return" appears in the middle of a block, this means that the
906 // code afterwards is DEAD (unreachable). We still keep a basic block
907 // for that code; a simple "mark-and-sweep" from the entry block will be
908 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000909 if (Block)
910 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000911
912 // Create the new block.
913 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000914
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000915 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000916 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000917
918 // Add the return statement to the block. This may create new blocks if R
919 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000920 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000921}
922
923CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
924 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000925 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000926 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000927
Ted Kremenek4f880632009-07-17 22:18:43 +0000928 if (!LabelBlock) // This can happen when the body is empty, i.e.
929 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000930
Ted Kremenek4f880632009-07-17 22:18:43 +0000931 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000932 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000933
934 // Labels partition blocks, so this is the end of the basic block we were
935 // processing (L is the block's label). Because this is label (and we have
936 // already processed the substatement) there is no extra control-flow to worry
937 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000938 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000939 if (!FinishBlock(LabelBlock))
940 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000941
942 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000943 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000944
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000945 // This block is now the implicit successor of other blocks.
946 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000947
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000948 return LabelBlock;
949}
950
951CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000952 // Goto is a control-flow statement. Thus we stop processing the current
953 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000954 if (Block)
955 FinishBlock(Block);
956
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000957 Block = createBlock(false);
958 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000959
960 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000961 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000962
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000963 if (I == LabelMap.end())
964 // We will need to backpatch this block later.
965 BackpatchBlocks.push_back(Block);
966 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000967 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000968
Mike Stump6d9828c2009-07-17 01:31:16 +0000969 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000970}
971
972CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000973 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000974
Mike Stumpfefb9f72009-07-21 01:12:51 +0000975 // "for" is a control-flow statement. Thus we stop processing the current
976 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000977 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000978 if (!FinishBlock(Block))
979 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000980 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000981 } else
982 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000983
984 // Because of short-circuit evaluation, the condition of the loop can span
985 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
986 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000987 CFGBlock* ExitConditionBlock = createBlock(false);
988 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000989
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000990 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000991 ExitConditionBlock->setTerminator(F);
992
993 // Now add the actual condition to the condition block. Because the condition
994 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000995 if (Stmt* C = F->getCond()) {
996 Block = ExitConditionBlock;
997 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000998 assert(Block == EntryConditionBlock);
999
1000 // If this block contains a condition variable, add both the condition
1001 // variable and initializer to the CFG.
1002 if (VarDecl *VD = F->getConditionVariable()) {
1003 if (Expr *Init = VD->getInit()) {
1004 autoCreateBlock();
1005 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1006 EntryConditionBlock = addStmt(Init);
1007 assert(Block == EntryConditionBlock);
1008 }
1009 }
1010
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001011 if (Block) {
1012 if (!FinishBlock(EntryConditionBlock))
1013 return 0;
1014 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001015 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001016
Mike Stump6d9828c2009-07-17 01:31:16 +00001017 // The condition block is the implicit successor for the loop body as well as
1018 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001019 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001020
Mike Stump00998a02009-07-23 23:25:26 +00001021 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001022 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Mike Stump00998a02009-07-23 23:25:26 +00001024 if (F->getCond())
1025 KnownVal = TryEvaluateBool(F->getCond());
1026
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001027 // Now create the loop body.
1028 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001029 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001030
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001031 // Save the current values for Block, Succ, and continue and break targets
1032 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001033 save_continue(ContinueTargetBlock),
1034 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001035
Ted Kremenekaf603f72007-08-30 18:39:40 +00001036 // Create a new block to contain the (bottom) of the loop body.
1037 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001038
Ted Kremeneke9334502008-09-04 21:48:47 +00001039 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001040 // Generate increment code in its own basic block. This is the target of
1041 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001042 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001043 } else {
1044 // No increment code. Create a special, empty, block that is used as the
1045 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001046 assert(Succ == EntryConditionBlock);
1047 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001048 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001049
Ted Kremenek3575f842009-04-28 00:51:56 +00001050 // Finish up the increment (or empty) block if it hasn't been already.
1051 if (Block) {
1052 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001053 if (!FinishBlock(Block))
1054 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001055 Block = 0;
1056 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001057
Ted Kremenek3575f842009-04-28 00:51:56 +00001058 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001059
Ted Kremenek3575f842009-04-28 00:51:56 +00001060 // The starting block for the loop increment is the block that should
1061 // represent the 'loop target' for looping back to the start of the loop.
1062 ContinueTargetBlock->setLoopTarget(F);
1063
Ted Kremeneke9334502008-09-04 21:48:47 +00001064 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +00001065 BreakTargetBlock = LoopSuccessor;
1066
1067 // Now populate the body block, and in the process create new blocks as we
1068 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001069 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001070
1071 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001072 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001073 else if (Block && !FinishBlock(BodyBlock))
1074 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001075
Ted Kremenek941fde82009-07-24 04:47:11 +00001076 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001077 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001078 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001079
Ted Kremenek941fde82009-07-24 04:47:11 +00001080 // Link up the condition block with the code that follows the loop. (the
1081 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001082 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001083
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001084 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001085 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001086 if (Stmt* I = F->getInit()) {
1087 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001088 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001089 } else {
1090 // There is no loop initialization. We are thus basically a while loop.
1091 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001092 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001093 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001094 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001095 }
1096}
1097
Ted Kremenek115c1b92010-04-11 17:02:10 +00001098CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1099 if (asc.alwaysAdd()) {
1100 autoCreateBlock();
1101 AppendStmt(Block, M, asc);
1102 }
1103 return Visit(M->getBase(),
1104 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1105 : AddStmtChoice::AsLValueNotAlwaysAdd);
1106}
1107
Ted Kremenek514de5a2008-11-11 17:10:00 +00001108CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1109 // Objective-C fast enumeration 'for' statements:
1110 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1111 //
1112 // for ( Type newVariable in collection_expression ) { statements }
1113 //
1114 // becomes:
1115 //
1116 // prologue:
1117 // 1. collection_expression
1118 // T. jump to loop_entry
1119 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001120 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001121 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1122 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1123 // TB:
1124 // statements
1125 // T. jump to loop_entry
1126 // FB:
1127 // what comes after
1128 //
1129 // and
1130 //
1131 // Type existingItem;
1132 // for ( existingItem in expression ) { statements }
1133 //
1134 // becomes:
1135 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001136 // the same with newVariable replaced with existingItem; the binding works
1137 // the same except that for one ObjCForCollectionStmt::getElement() returns
1138 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001139 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001140
Ted Kremenek514de5a2008-11-11 17:10:00 +00001141 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001142
Ted Kremenek514de5a2008-11-11 17:10:00 +00001143 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001144 if (!FinishBlock(Block))
1145 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001146 LoopSuccessor = Block;
1147 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001148 } else
1149 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001150
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001151 // Build the condition blocks.
1152 CFGBlock* ExitConditionBlock = createBlock(false);
1153 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001154
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001155 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001156 ExitConditionBlock->setTerminator(S);
1157
1158 // The last statement in the block should be the ObjCForCollectionStmt, which
1159 // performs the actual binding to 'element' and determines if there are any
1160 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001161 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001162 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001163
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001164 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001165 // generate new blocks as necesary. We DON'T add the statement by default to
1166 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001167 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001168 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001169 if (!FinishBlock(EntryConditionBlock))
1170 return 0;
1171 Block = 0;
1172 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001173
1174 // The condition block is the implicit successor for the loop body as well as
1175 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001176 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001177
Ted Kremenek514de5a2008-11-11 17:10:00 +00001178 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001179 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001180 // Save the current values for Succ, continue and break targets.
1181 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001182 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1183
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001184 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001185 ContinueTargetBlock = EntryConditionBlock;
1186
Ted Kremenek4f880632009-07-17 22:18:43 +00001187 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001188
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001189 if (!BodyBlock)
1190 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001191 else if (Block) {
1192 if (!FinishBlock(BodyBlock))
1193 return 0;
1194 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001195
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001196 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001197 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001198 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001199
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001200 // Link up the condition block with the code that follows the loop.
1201 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001202 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001203
Ted Kremenek514de5a2008-11-11 17:10:00 +00001204 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001205 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001206 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001207}
1208
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001209CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1210 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001211
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001212 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001213 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001214
Ted Kremenekda5348e2009-05-05 23:11:51 +00001215 // The sync body starts its own basic block. This makes it a little easier
1216 // for diagnostic clients.
1217 if (SyncBlock) {
1218 if (!FinishBlock(SyncBlock))
1219 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001220
Ted Kremenekda5348e2009-05-05 23:11:51 +00001221 Block = 0;
1222 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001223
Ted Kremenekda5348e2009-05-05 23:11:51 +00001224 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001225
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001226 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001227 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001228}
Mike Stump6d9828c2009-07-17 01:31:16 +00001229
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001230CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001231 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001232 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001233}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001234
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001235CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001236 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001237
Mike Stumpfefb9f72009-07-21 01:12:51 +00001238 // "while" is a control-flow statement. Thus we stop processing the current
1239 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001240 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001241 if (!FinishBlock(Block))
1242 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001243 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001244 } else
1245 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001246
1247 // Because of short-circuit evaluation, the condition of the loop can span
1248 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1249 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001250 CFGBlock* ExitConditionBlock = createBlock(false);
1251 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001253 // Set the terminator for the "exit" condition block.
1254 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001255
1256 // Now add the actual condition to the condition block. Because the condition
1257 // itself may contain control-flow, new blocks may be created. Thus we update
1258 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001259 if (Stmt* C = W->getCond()) {
1260 Block = ExitConditionBlock;
1261 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001262 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001263
1264 // If this block contains a condition variable, add both the condition
1265 // variable and initializer to the CFG.
1266 if (VarDecl *VD = W->getConditionVariable()) {
1267 if (Expr *Init = VD->getInit()) {
1268 autoCreateBlock();
1269 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1270 EntryConditionBlock = addStmt(Init);
1271 assert(Block == EntryConditionBlock);
1272 }
1273 }
1274
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001275 if (Block) {
1276 if (!FinishBlock(EntryConditionBlock))
1277 return 0;
1278 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001279 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001280
1281 // The condition block is the implicit successor for the loop body as well as
1282 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001283 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001284
Mike Stump00998a02009-07-23 23:25:26 +00001285 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001286 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001287
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001288 // Process the loop body.
1289 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001290 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001291
1292 // Save the current values for Block, Succ, and continue and break targets
1293 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1294 save_continue(ContinueTargetBlock),
1295 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001296
Mike Stump6d9828c2009-07-17 01:31:16 +00001297 // Create an empty block to represent the transition block for looping back
1298 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001299 Block = 0;
1300 assert(Succ == EntryConditionBlock);
1301 Succ = createBlock();
1302 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001303 ContinueTargetBlock = Succ;
1304
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001305 // All breaks should go to the code following the loop.
1306 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001307
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001308 // NULL out Block to force lazy instantiation of blocks for the body.
1309 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001310
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001311 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001312 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001313
Ted Kremenekaf603f72007-08-30 18:39:40 +00001314 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001315 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001316 else if (Block) {
1317 if (!FinishBlock(BodyBlock))
1318 return 0;
1319 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
Ted Kremenek941fde82009-07-24 04:47:11 +00001321 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001322 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001323 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
Ted Kremenek941fde82009-07-24 04:47:11 +00001325 // Link up the condition block with the code that follows the loop. (the
1326 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001327 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001328
1329 // There can be no more statements in the condition block since we loop back
1330 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001331 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001332
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001333 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001334 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001335 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001336}
Mike Stump1eb44332009-09-09 15:08:12 +00001337
1338
Ted Kremenek4f880632009-07-17 22:18:43 +00001339CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1340 // FIXME: For now we pretend that @catch and the code it contains does not
1341 // exit.
1342 return Block;
1343}
Mike Stump6d9828c2009-07-17 01:31:16 +00001344
Ted Kremenek2fda5042008-12-09 20:20:09 +00001345CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1346 // FIXME: This isn't complete. We basically treat @throw like a return
1347 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001348
Ted Kremenek6c249722009-09-24 18:45:41 +00001349 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001350 if (Block && !FinishBlock(Block))
1351 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001352
Ted Kremenek2fda5042008-12-09 20:20:09 +00001353 // Create the new block.
1354 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001355
Ted Kremenek2fda5042008-12-09 20:20:09 +00001356 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001357 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001358
1359 // Add the statement to the block. This may create new blocks if S contains
1360 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001361 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001362}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001363
Mike Stump0979d802009-07-22 22:56:04 +00001364CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001365 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001366 if (Block && !FinishBlock(Block))
1367 return 0;
1368
1369 // Create the new block.
1370 Block = createBlock(false);
1371
Mike Stump5d1d2022010-01-19 02:20:09 +00001372 if (TryTerminatedBlock)
1373 // The current try statement is the only successor.
1374 AddSuccessor(Block, TryTerminatedBlock);
1375 else
1376 // otherwise the Exit block is the only successor.
1377 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001378
1379 // Add the statement to the block. This may create new blocks if S contains
1380 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001381 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001382}
1383
Ted Kremenek4f880632009-07-17 22:18:43 +00001384CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001385 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001386
Mike Stump8f9893a2009-07-21 01:27:50 +00001387 // "do...while" is a control-flow statement. Thus we stop processing the
1388 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001389 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001390 if (!FinishBlock(Block))
1391 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001392 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001393 } else
1394 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001395
1396 // Because of short-circuit evaluation, the condition of the loop can span
1397 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1398 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001399 CFGBlock* ExitConditionBlock = createBlock(false);
1400 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001401
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001402 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001403 ExitConditionBlock->setTerminator(D);
1404
1405 // Now add the actual condition to the condition block. Because the condition
1406 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001407 if (Stmt* C = D->getCond()) {
1408 Block = ExitConditionBlock;
1409 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001410 if (Block) {
1411 if (!FinishBlock(EntryConditionBlock))
1412 return 0;
1413 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001414 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001415
Ted Kremenek54827132008-02-27 07:20:00 +00001416 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001417 Succ = EntryConditionBlock;
1418
Mike Stump00998a02009-07-23 23:25:26 +00001419 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001420 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001421
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001422 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001423 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001424 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001425 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001426
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001427 // Save the current values for Block, Succ, and continue and break targets
1428 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001429 save_continue(ContinueTargetBlock),
1430 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001432 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001433 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001434
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001435 // All breaks should go to the code following the loop.
1436 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001437
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001438 // NULL out Block to force lazy instantiation of blocks for the body.
1439 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001440
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001441 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001442 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001443
Ted Kremenekaf603f72007-08-30 18:39:40 +00001444 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001445 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001446 else if (Block) {
1447 if (!FinishBlock(BodyBlock))
1448 return 0;
1449 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001450
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001451 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001452 // ExitConditionBlock to represent the "loop back" transition. Create an
1453 // empty block to represent the transition block for looping back to the
1454 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001455 // FIXME: Can we do this more efficiently without adding another block?
1456 Block = NULL;
1457 Succ = BodyBlock;
1458 CFGBlock *LoopBackBlock = createBlock();
1459 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
Ted Kremenek941fde82009-07-24 04:47:11 +00001461 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001462 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001463 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001464
Ted Kremenek941fde82009-07-24 04:47:11 +00001465 // Link up the condition block with the code that follows the loop.
1466 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001467 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001468
1469 // There can be no more statements in the body block(s) since we loop back to
1470 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001471 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001472
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001473 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001474 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001475 return BodyBlock;
1476}
1477
1478CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1479 // "continue" is a control-flow statement. Thus we stop processing the
1480 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001481 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001482 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001483
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001484 // Now create a new block that ends with the continue statement.
1485 Block = createBlock(false);
1486 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001487
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001488 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001489 // incomplete AST. This means the CFG cannot be constructed.
1490 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001491 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001492 else
1493 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001494
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001495 return Block;
1496}
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001498CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001499 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001500
Ted Kremenek852274d2009-12-16 03:18:58 +00001501 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001502 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001503 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek4f880632009-07-17 22:18:43 +00001506 // VLA types have expressions that must be evaluated.
1507 if (E->isArgumentType()) {
1508 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1509 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1510 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001511 }
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Mike Stump6d9828c2009-07-17 01:31:16 +00001513 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001514}
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Ted Kremenek4f880632009-07-17 22:18:43 +00001516/// VisitStmtExpr - Utility method to handle (nested) statement
1517/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001518CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1519 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001520 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001521 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001522 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001523 return VisitCompoundStmt(SE->getSubStmt());
1524}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001525
Ted Kremenek411cdee2008-04-16 21:10:48 +00001526CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001527 // "switch" is a control-flow statement. Thus we stop processing the current
1528 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001529 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001530
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001531 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001532 if (!FinishBlock(Block))
1533 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001534 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001535 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001536
1537 // Save the current "switch" context.
1538 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001539 save_break(BreakTargetBlock),
1540 save_default(DefaultCaseBlock);
1541
Mike Stump6d9828c2009-07-17 01:31:16 +00001542 // Set the "default" case to be the block after the switch statement. If the
1543 // switch statement contains a "default:", this value will be overwritten with
1544 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001545 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001546
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001547 // Create a new block that will contain the switch statement.
1548 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001549
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001550 // Now process the switch body. The code after the switch is the implicit
1551 // successor.
1552 Succ = SwitchSuccessor;
1553 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001554
1555 // When visiting the body, the case statements should automatically get linked
1556 // up to the switch. We also don't keep a pointer to the body, since all
1557 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001558 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001559 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001560 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001561 if (Block) {
1562 if (!FinishBlock(BodyBlock))
1563 return 0;
1564 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001565
Mike Stump6d9828c2009-07-17 01:31:16 +00001566 // If we have no "default:" case, the default transition is to the code
1567 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001568 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001569
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001570 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001571 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001572 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001573 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001574 Block = addStmt(Terminator->getCond());
1575
1576 // Finally, if the SwitchStmt contains a condition variable, add both the
1577 // SwitchStmt and the condition variable initialization to the CFG.
1578 if (VarDecl *VD = Terminator->getConditionVariable()) {
1579 if (Expr *Init = VD->getInit()) {
1580 autoCreateBlock();
1581 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1582 addStmt(Init);
1583 }
1584 }
1585
1586 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001587}
1588
Ted Kremenek4f880632009-07-17 22:18:43 +00001589CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001590 // CaseStmts are essentially labels, so they are the first statement in a
1591 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001592
Ted Kremenek4f880632009-07-17 22:18:43 +00001593 if (CS->getSubStmt())
1594 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001596 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001597 if (!CaseBlock)
1598 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001599
1600 // Cases statements partition blocks, so this is the top of the basic block we
1601 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001602 CaseBlock->setLabel(CS);
1603
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001604 if (!FinishBlock(CaseBlock))
1605 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001606
1607 // Add this block to the list of successors for the block with the switch
1608 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001609 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001610 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001611
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001612 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1613 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001614
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001615 // This block is now the implicit successor of other blocks.
1616 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
Ted Kremenek2677ea82008-03-15 07:45:02 +00001618 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001619}
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
Ted Kremenek411cdee2008-04-16 21:10:48 +00001621CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001622 if (Terminator->getSubStmt())
1623 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001625 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001626
1627 if (!DefaultCaseBlock)
1628 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001629
1630 // Default statements partition blocks, so this is the top of the basic block
1631 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001632 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001634 if (!FinishBlock(DefaultCaseBlock))
1635 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001636
Mike Stump6d9828c2009-07-17 01:31:16 +00001637 // Unlike case statements, we don't add the default block to the successors
1638 // for the switch statement immediately. This is done when we finish
1639 // processing the switch statement. This allows for the default case
1640 // (including a fall-through to the code after the switch statement) to always
1641 // be the last successor of a switch-terminated block.
1642
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001643 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1644 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001645
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001646 // This block is now the implicit successor of other blocks.
1647 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001648
1649 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001650}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001651
Mike Stump5d1d2022010-01-19 02:20:09 +00001652CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1653 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1654 // current block.
1655 CFGBlock* TrySuccessor = NULL;
1656
1657 if (Block) {
1658 if (!FinishBlock(Block))
1659 return 0;
1660 TrySuccessor = Block;
1661 } else TrySuccessor = Succ;
1662
Mike Stumpa1f93632010-01-20 01:15:34 +00001663 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001664
1665 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001666 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001667 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001668 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001669
Mike Stumpa1f93632010-01-20 01:15:34 +00001670 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001671 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1672 // The code after the try is the implicit successor.
1673 Succ = TrySuccessor;
1674 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001675 if (CS->getExceptionDecl() == 0) {
1676 HasCatchAll = true;
1677 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001678 Block = NULL;
1679 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1680 if (CatchBlock == 0)
1681 return 0;
1682 // Add this block to the list of successors for the block with the try
1683 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001684 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001685 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001686 if (!HasCatchAll) {
1687 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001688 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001689 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001690 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001691 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001692
1693 // The code after the try is the implicit successor.
1694 Succ = TrySuccessor;
1695
Mike Stumpf00cca52010-01-20 01:30:58 +00001696 // Save the current "try" context.
1697 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1698 TryTerminatedBlock = NewTryTerminatedBlock;
1699
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001700 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001701 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001702 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001703 return Block;
1704}
1705
1706CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1707 // CXXCatchStmt are treated like labels, so they are the first statement in a
1708 // block.
1709
1710 if (CS->getHandlerBlock())
1711 addStmt(CS->getHandlerBlock());
1712
1713 CFGBlock* CatchBlock = Block;
1714 if (!CatchBlock)
1715 CatchBlock = createBlock();
1716
1717 CatchBlock->setLabel(CS);
1718
1719 if (!FinishBlock(CatchBlock))
1720 return 0;
1721
1722 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1723 Block = NULL;
1724
1725 return CatchBlock;
1726}
1727
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001728CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
1729 AddStmtChoice asc) {
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001730 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
1731 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001732 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001733 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001734 return VisitChildren(C);
1735}
1736
Ted Kremenek19bb3562007-08-28 19:26:49 +00001737CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001738 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001739 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001740
Ted Kremenek19bb3562007-08-28 19:26:49 +00001741 if (!IBlock) {
1742 IBlock = createBlock(false);
1743 cfg->setIndirectGotoBlock(IBlock);
1744 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001745
Ted Kremenek19bb3562007-08-28 19:26:49 +00001746 // IndirectGoto is a control-flow statement. Thus we stop processing the
1747 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001748 if (Block && !FinishBlock(Block))
1749 return 0;
1750
Ted Kremenek19bb3562007-08-28 19:26:49 +00001751 Block = createBlock(false);
1752 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001753 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001754 return addStmt(I->getTarget());
1755}
1756
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001757} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001758
Mike Stump6d9828c2009-07-17 01:31:16 +00001759/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1760/// no successors or predecessors. If this is the first block created in the
1761/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001762CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001763 bool first_block = begin() == end();
1764
1765 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001766 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1767 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1768 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001769
1770 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001771 if (first_block)
1772 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001773
1774 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001775 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001776}
1777
Ted Kremenek026473c2007-08-23 16:51:22 +00001778/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1779/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001780CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump4c45aa12010-01-21 15:20:48 +00001781 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001782 CFGBuilder Builder;
Mike Stump4c45aa12010-01-21 15:20:48 +00001783 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001784}
1785
Ted Kremenek63f58872007-10-01 19:33:33 +00001786//===----------------------------------------------------------------------===//
1787// CFG: Queries for BlkExprs.
1788//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001789
Ted Kremenek63f58872007-10-01 19:33:33 +00001790namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001791 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001792}
1793
Ted Kremenek8a693662009-12-23 23:37:10 +00001794static void FindSubExprAssignments(Stmt *S,
1795 llvm::SmallPtrSet<Expr*,50>& Set) {
1796 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001797 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Ted Kremenek8a693662009-12-23 23:37:10 +00001799 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1800 Stmt *child = *I;
1801 if (!child)
1802 continue;
1803
1804 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001805 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001806
Ted Kremenek8a693662009-12-23 23:37:10 +00001807 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001808 }
1809}
1810
Ted Kremenek63f58872007-10-01 19:33:33 +00001811static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1812 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001813
1814 // Look for assignments that are used as subexpressions. These are the only
1815 // assignments that we want to *possibly* register as a block-level
1816 // expression. Basically, if an assignment occurs both in a subexpression and
1817 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001818 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001819
Ted Kremenek63f58872007-10-01 19:33:33 +00001820 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001821 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001822 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001823
Ted Kremenek411cdee2008-04-16 21:10:48 +00001824 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001825
1826 // Iterate over the statements again on identify the Expr* and Stmt* at the
1827 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001828
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001829 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001830 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001831
Ted Kremenek411cdee2008-04-16 21:10:48 +00001832 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001833 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001834 // expression are really "statements" whose value is never used by
1835 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001836 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001837 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001838 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1839 // Special handling for statement expressions. The last statement in
1840 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001841 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001842 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001843 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001844 (*M)[C->body_back()] = x;
1845 }
1846 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001847
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001848 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001849 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001850 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001851
Ted Kremenek411cdee2008-04-16 21:10:48 +00001852 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001853
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001854 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
Ted Kremenek390e48b2008-11-12 21:11:49 +00001856 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001857 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001858 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001859 }
1860 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001861
Ted Kremenek63f58872007-10-01 19:33:33 +00001862 return M;
1863}
1864
Ted Kremenek86946742008-01-17 20:48:37 +00001865CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1866 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001867 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Ted Kremenek63f58872007-10-01 19:33:33 +00001869 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001870 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001871 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001872}
1873
1874unsigned CFG::getNumBlkExprs() {
1875 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1876 return M->size();
1877 else {
1878 // We assume callers interested in the number of BlkExprs will want
1879 // the map constructed if it doesn't already exist.
1880 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1881 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1882 }
1883}
1884
Ted Kremenek274f4332008-04-28 18:00:46 +00001885//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001886// Cleanup: CFG dstor.
1887//===----------------------------------------------------------------------===//
1888
Ted Kremenek63f58872007-10-01 19:33:33 +00001889CFG::~CFG() {
1890 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1891}
Mike Stump6d9828c2009-07-17 01:31:16 +00001892
Ted Kremenek7dba8602007-08-29 21:56:09 +00001893//===----------------------------------------------------------------------===//
1894// CFG pretty printing
1895//===----------------------------------------------------------------------===//
1896
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001897namespace {
1898
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001899class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001900 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1901 StmtMapTy StmtMap;
1902 signed CurrentBlock;
1903 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001904 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001905public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001906
Chris Lattnere4f21422009-06-30 01:26:17 +00001907 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1908 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001909 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1910 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001911 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001912 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001913 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001914 }
1915 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenek42a509f2007-08-31 21:30:12 +00001917 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001918
Chris Lattnere4f21422009-06-30 01:26:17 +00001919 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001920 void setBlockID(signed i) { CurrentBlock = i; }
1921 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001922
Ted Kremeneka95d3752008-09-13 05:16:45 +00001923 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001924
Ted Kremenek411cdee2008-04-16 21:10:48 +00001925 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001926
1927 if (I == StmtMap.end())
1928 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001929
1930 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001931 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001932 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001933 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001934
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001935 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001936 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001937 }
1938};
Chris Lattnere4f21422009-06-30 01:26:17 +00001939} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001940
Chris Lattnere4f21422009-06-30 01:26:17 +00001941
1942namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001943class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001944 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001945
Ted Kremeneka95d3752008-09-13 05:16:45 +00001946 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001947 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001948 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001949public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001950 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001951 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001952 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001953
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001954 void VisitIfStmt(IfStmt* I) {
1955 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001956 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001957 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001958
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001959 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001960 void VisitStmt(Stmt* Terminator) {
1961 Terminator->printPretty(OS, Helper, Policy);
1962 }
1963
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001964 void VisitForStmt(ForStmt* F) {
1965 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001966 if (F->getInit())
1967 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001968 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001969 if (Stmt* C = F->getCond())
1970 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001971 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001972 if (F->getInc())
1973 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001974 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001975 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001976
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001977 void VisitWhileStmt(WhileStmt* W) {
1978 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001979 if (Stmt* C = W->getCond())
1980 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001981 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001983 void VisitDoStmt(DoStmt* D) {
1984 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001985 if (Stmt* C = D->getCond())
1986 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001987 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001988
Ted Kremenek411cdee2008-04-16 21:10:48 +00001989 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001990 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001991 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001992 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001993
Mike Stump5d1d2022010-01-19 02:20:09 +00001994 void VisitCXXTryStmt(CXXTryStmt* CS) {
1995 OS << "try ...";
1996 }
1997
Ted Kremenek805e9a82007-08-31 21:49:40 +00001998 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001999 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002000 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002001 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002002
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002003 void VisitChooseExpr(ChooseExpr* C) {
2004 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002005 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002006 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002007 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002008
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002009 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2010 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002011 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002012 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002013
Ted Kremenek805e9a82007-08-31 21:49:40 +00002014 void VisitBinaryOperator(BinaryOperator* B) {
2015 if (!B->isLogicalOp()) {
2016 VisitExpr(B);
2017 return;
2018 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002019
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002020 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002021
Ted Kremenek805e9a82007-08-31 21:49:40 +00002022 switch (B->getOpcode()) {
2023 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002024 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002025 return;
2026 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002027 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002028 return;
2029 default:
2030 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002031 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002032 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002033
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002034 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002035 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002036 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002037};
Chris Lattnere4f21422009-06-30 01:26:17 +00002038} // end anonymous namespace
2039
Mike Stump6d9828c2009-07-17 01:31:16 +00002040
Chris Lattnere4f21422009-06-30 01:26:17 +00002041static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002042 const CFGElement &E) {
2043 Stmt *Terminator = E;
2044
2045 if (E.asStartScope()) {
2046 OS << "start scope\n";
2047 return;
2048 }
2049 if (E.asEndScope()) {
2050 OS << "end scope\n";
2051 return;
2052 }
2053
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002054 if (Helper) {
2055 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002056 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002057 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002058
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002059 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002060 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002061 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002062 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002063 return;
2064 }
2065 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002066
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002067 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002068 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002069 if (B->getOpcode() == BinaryOperator::Comma) {
2070 OS << "... , ";
2071 Helper->handledStmt(B->getRHS(),OS);
2072 OS << '\n';
2073 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002074 }
2075 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002076 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002077
Chris Lattnere4f21422009-06-30 01:26:17 +00002078 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002079
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002080 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002081 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002082}
Mike Stump6d9828c2009-07-17 01:31:16 +00002083
Chris Lattnere4f21422009-06-30 01:26:17 +00002084static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2085 const CFGBlock& B,
2086 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002087
Ted Kremenek42a509f2007-08-31 21:30:12 +00002088 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002089
Ted Kremenek7dba8602007-08-29 21:56:09 +00002090 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002091 OS << "\n [ B" << B.getBlockID();
2092
Ted Kremenek42a509f2007-08-31 21:30:12 +00002093 if (&B == &cfg->getEntry())
2094 OS << " (ENTRY) ]\n";
2095 else if (&B == &cfg->getExit())
2096 OS << " (EXIT) ]\n";
2097 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002098 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002099 else
2100 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002101
Ted Kremenek9cffe732007-08-29 23:20:49 +00002102 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002103 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002104
2105 if (print_edges)
2106 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002107
Mike Stump079bd722010-01-19 22:00:14 +00002108 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002109 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002110 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002111 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002112 C->getLHS()->printPretty(OS, Helper,
2113 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002114 if (C->getRHS()) {
2115 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002116 C->getRHS()->printPretty(OS, Helper,
2117 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002118 }
Mike Stump079bd722010-01-19 22:00:14 +00002119 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002120 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002121 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002122 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002123 if (CS->getExceptionDecl())
2124 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2125 0);
2126 else
2127 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002128 OS << ")";
2129
2130 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002131 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002132
Ted Kremenek9cffe732007-08-29 23:20:49 +00002133 OS << ":\n";
2134 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002135
Ted Kremenekfddd5182007-08-21 21:42:03 +00002136 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002137 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002138
Ted Kremenek42a509f2007-08-31 21:30:12 +00002139 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2140 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002141
Ted Kremenek9cffe732007-08-29 23:20:49 +00002142 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002143 if (print_edges)
2144 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002145
Ted Kremeneka95d3752008-09-13 05:16:45 +00002146 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002147
Ted Kremenek42a509f2007-08-31 21:30:12 +00002148 if (Helper)
2149 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002150
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002151 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002152 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002153
Ted Kremenek9cffe732007-08-29 23:20:49 +00002154 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002155 if (B.getTerminator()) {
2156 if (print_edges)
2157 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002158
Ted Kremenek9cffe732007-08-29 23:20:49 +00002159 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002160
Ted Kremenek42a509f2007-08-31 21:30:12 +00002161 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002162
Chris Lattnere4f21422009-06-30 01:26:17 +00002163 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2164 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002165 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002166 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002167 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002168
Ted Kremenek9cffe732007-08-29 23:20:49 +00002169 if (print_edges) {
2170 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002171 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002172 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002173
Ted Kremenek42a509f2007-08-31 21:30:12 +00002174 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2175 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002176
Ted Kremenek42a509f2007-08-31 21:30:12 +00002177 if (i == 8 || (i-8) == 0)
2178 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002179
Ted Kremenek9cffe732007-08-29 23:20:49 +00002180 OS << " B" << (*I)->getBlockID();
2181 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002182
Ted Kremenek42a509f2007-08-31 21:30:12 +00002183 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002184
Ted Kremenek42a509f2007-08-31 21:30:12 +00002185 // Print the successors of this block.
2186 OS << " Successors (" << B.succ_size() << "):";
2187 i = 0;
2188
2189 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2190 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002191
Ted Kremenek42a509f2007-08-31 21:30:12 +00002192 if (i == 8 || (i-8) % 10 == 0)
2193 OS << "\n ";
2194
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002195 if (*I)
2196 OS << " B" << (*I)->getBlockID();
2197 else
2198 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002199 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002200
Ted Kremenek9cffe732007-08-29 23:20:49 +00002201 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002202 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002203}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002204
Ted Kremenek42a509f2007-08-31 21:30:12 +00002205
2206/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002207void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002208
2209/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002210void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2211 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002212
Ted Kremenek42a509f2007-08-31 21:30:12 +00002213 // Print the entry block.
2214 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002215
Ted Kremenek42a509f2007-08-31 21:30:12 +00002216 // Iterate through the CFGBlocks and print them one by one.
2217 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2218 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002219 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002220 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002221
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002222 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002223 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002224
Ted Kremenek42a509f2007-08-31 21:30:12 +00002225 // Print the exit block.
2226 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002227 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002228}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002229
2230/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002231void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2232 print(llvm::errs(), cfg, LO);
2233}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002234
2235/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2236/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002237void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2238 const LangOptions &LO) const {
2239 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002240 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002241}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002242
Ted Kremeneka2925852008-01-30 23:02:42 +00002243/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002244void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002245 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002246 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002247 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2248}
2249
Ted Kremenek390e48b2008-11-12 21:11:49 +00002250Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002251
Ted Kremenek411cdee2008-04-16 21:10:48 +00002252 if (!Terminator)
2253 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002254
Ted Kremenek411cdee2008-04-16 21:10:48 +00002255 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002256
Ted Kremenek411cdee2008-04-16 21:10:48 +00002257 switch (Terminator->getStmtClass()) {
2258 default:
2259 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002260
Ted Kremenek411cdee2008-04-16 21:10:48 +00002261 case Stmt::ForStmtClass:
2262 E = cast<ForStmt>(Terminator)->getCond();
2263 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002264
Ted Kremenek411cdee2008-04-16 21:10:48 +00002265 case Stmt::WhileStmtClass:
2266 E = cast<WhileStmt>(Terminator)->getCond();
2267 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek411cdee2008-04-16 21:10:48 +00002269 case Stmt::DoStmtClass:
2270 E = cast<DoStmt>(Terminator)->getCond();
2271 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002272
Ted Kremenek411cdee2008-04-16 21:10:48 +00002273 case Stmt::IfStmtClass:
2274 E = cast<IfStmt>(Terminator)->getCond();
2275 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002276
Ted Kremenek411cdee2008-04-16 21:10:48 +00002277 case Stmt::ChooseExprClass:
2278 E = cast<ChooseExpr>(Terminator)->getCond();
2279 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002280
Ted Kremenek411cdee2008-04-16 21:10:48 +00002281 case Stmt::IndirectGotoStmtClass:
2282 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2283 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002284
Ted Kremenek411cdee2008-04-16 21:10:48 +00002285 case Stmt::SwitchStmtClass:
2286 E = cast<SwitchStmt>(Terminator)->getCond();
2287 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002288
Ted Kremenek411cdee2008-04-16 21:10:48 +00002289 case Stmt::ConditionalOperatorClass:
2290 E = cast<ConditionalOperator>(Terminator)->getCond();
2291 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002292
Ted Kremenek411cdee2008-04-16 21:10:48 +00002293 case Stmt::BinaryOperatorClass: // '&&' and '||'
2294 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002295 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002296
Ted Kremenek390e48b2008-11-12 21:11:49 +00002297 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002298 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002299 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002300
Ted Kremenek411cdee2008-04-16 21:10:48 +00002301 return E ? E->IgnoreParens() : NULL;
2302}
2303
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002304bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002305
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002306 if (!Terminator)
2307 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002308
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002309 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002310
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002311 switch (Terminator->getStmtClass()) {
2312 default:
2313 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002314
2315 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002316 case Stmt::WhileStmtClass:
2317 case Stmt::DoStmtClass:
2318 case Stmt::IfStmtClass:
2319 case Stmt::ChooseExprClass:
2320 case Stmt::ConditionalOperatorClass:
2321 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002322 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002323 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002324
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002325 return E ? E->IgnoreParens() : NULL;
2326}
2327
Ted Kremeneka2925852008-01-30 23:02:42 +00002328
Ted Kremenek7dba8602007-08-29 21:56:09 +00002329//===----------------------------------------------------------------------===//
2330// CFG Graphviz Visualization
2331//===----------------------------------------------------------------------===//
2332
Ted Kremenek42a509f2007-08-31 21:30:12 +00002333
2334#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002335static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002336#endif
2337
Chris Lattnere4f21422009-06-30 01:26:17 +00002338void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002339#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002340 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002341 GraphHelper = &H;
2342 llvm::ViewGraph(this,"CFG");
2343 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002344#endif
2345}
2346
Ted Kremenek7dba8602007-08-29 21:56:09 +00002347namespace llvm {
2348template<>
2349struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002350
2351 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2352
2353 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002354
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002355#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002356 std::string OutSStr;
2357 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002358 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002359 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002360
2361 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2362
2363 // Process string output to make it nicer...
2364 for (unsigned i = 0; i != OutStr.length(); ++i)
2365 if (OutStr[i] == '\n') { // Left justify
2366 OutStr[i] = '\\';
2367 OutStr.insert(OutStr.begin()+i+1, 'l');
2368 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002369
Ted Kremenek7dba8602007-08-29 21:56:09 +00002370 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002371#else
2372 return "";
2373#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002374 }
2375};
2376} // end namespace llvm