blob: c17a2749b508371781b8b17bced171f57aca3def [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);
Ted Kremenek852274d2009-12-16 03:18:58 +0000114 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000115 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000116 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000117 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000118 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000119 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000120 CFGBlock *VisitDeclStmt(DeclStmt *DS);
121 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000122 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
123 CFGBlock *VisitDoStmt(DoStmt *D);
124 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000125 CFGBlock *VisitGotoStmt(GotoStmt* G);
126 CFGBlock *VisitIfStmt(IfStmt *I);
127 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
128 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000129 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000130 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
131 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
132 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
133 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
134 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
135 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000136 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
137 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000138 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
139 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000140
Ted Kremenek852274d2009-12-16 03:18:58 +0000141 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
142 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000143 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000144
Ted Kremenek274f4332008-04-28 18:00:46 +0000145 // NYS == Not Yet Supported
146 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000147 badCFG = true;
148 return Block;
149 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000150
Mike Stump079bd722010-01-19 22:00:14 +0000151 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
152 if (!AddScopes)
153 return B;
154
155 if (B == 0)
156 B = createBlock();
157 B->StartScope(S, cfg->getBumpVectorContext());
158 return B;
159 }
160
161 void EndScope(Stmt *S) {
162 if (!AddScopes)
163 return;
164
165 if (Block == 0)
166 Block = createBlock();
167 Block->EndScope(S, cfg->getBumpVectorContext());
168 }
169
Ted Kremenek4f880632009-07-17 22:18:43 +0000170 void autoCreateBlock() { if (!Block) Block = createBlock(); }
171 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000172 bool FinishBlock(CFGBlock* B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000173 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
174 return Visit(S, asc);
175 }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000176
Ted Kremenek852274d2009-12-16 03:18:58 +0000177 void AppendStmt(CFGBlock *B, Stmt *S,
178 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
179 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000180 }
181
182 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
183 B->addSuccessor(S, cfg->getBumpVectorContext());
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000186 /// TryResult - a class representing a variant over the values
187 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
188 /// and is used by the CFGBuilder to decide if a branch condition
189 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000190 class TryResult {
191 int X;
192 public:
193 TryResult(bool b) : X(b ? 1 : 0) {}
194 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek941fde82009-07-24 04:47:11 +0000196 bool isTrue() const { return X == 1; }
197 bool isFalse() const { return X == 0; }
198 bool isKnown() const { return X >= 0; }
199 void negate() {
200 assert(isKnown());
201 X ^= 0x1;
202 }
203 };
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Mike Stump00998a02009-07-23 23:25:26 +0000205 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
206 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000207 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000208 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000209 if (!S->isTypeDependent() && !S->isValueDependent() &&
210 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000211 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000212
213 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000214 }
215
Ted Kremenek4102af92008-03-13 03:04:22 +0000216 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000217
218 // True iff EH edges on CallExprs should be added to the CFG.
219 bool AddEHEdges;
220
221 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump079bd722010-01-19 22:00:14 +0000222 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000223};
Mike Stump6d9828c2009-07-17 01:31:16 +0000224
Douglas Gregor898574e2008-12-05 23:32:09 +0000225// FIXME: Add support for dependent-sized array types in C++?
226// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000227static VariableArrayType* FindVA(Type* t) {
228 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
229 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
230 if (vat->getSizeExpr())
231 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000232
Ted Kremenek610a09e2008-09-26 22:58:57 +0000233 t = vt->getElementType().getTypePtr();
234 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000235
Ted Kremenek610a09e2008-09-26 22:58:57 +0000236 return 0;
237}
Mike Stump6d9828c2009-07-17 01:31:16 +0000238
239/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
240/// arbitrary statement. Examples include a single expression or a function
241/// body (compound statement). The ownership of the returned CFG is
242/// transferred to the caller. If CFG construction fails, this method returns
243/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000244CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Mike Stump55f988e2010-01-21 17:21:23 +0000245 bool addehedges, bool AddScopes) {
246 AddEHEdges = addehedges;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000247 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000248 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000249 if (!Statement)
250 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000251
Mike Stump079bd722010-01-19 22:00:14 +0000252 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000253 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000254
255 // Create an empty block that will serve as the exit block for the CFG. Since
256 // this is the first block added to the CFG, it will be implicitly registered
257 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000258 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000259 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000260 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000261
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000262 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000263 CFGBlock* B = addStmt(Statement);
Mike Stumpb978a442010-01-21 02:21:40 +0000264
265 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
266 // FIXME: Add code for base initializers and member initializers.
267 (void)CD;
268 }
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000269 if (!B)
270 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000271
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000272 if (B) {
273 // Finalize the last constructed block. This usually involves reversing the
274 // order of the statements in the block.
275 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000276
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000277 // Backpatch the gotos whose label -> block mappings we didn't know when we
278 // encountered them.
279 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000280 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000281
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000282 CFGBlock* B = *I;
283 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
284 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000285
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000286 // If there is no target for the goto, then we are looking at an
287 // incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000288 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000289
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000290 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000291 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000292
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000293 // Add successors to the Indirect Goto Dispatch block (if we have one).
294 if (CFGBlock* B = cfg->getIndirectGotoBlock())
295 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
296 E = AddressTakenLabels.end(); I != E; ++I ) {
297
298 // Lookup the target block.
299 LabelMapTy::iterator LI = LabelMap.find(*I);
300
301 // If there is no target block that contains label, then we are looking
302 // at an incomplete AST. Handle this by not registering a successor.
303 if (LI == LabelMap.end()) continue;
304
305 AddSuccessor(B, LI->second);
306 }
307
308 Succ = B;
309 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000310
311 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000312 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000313
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000314 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000315}
Mike Stump6d9828c2009-07-17 01:31:16 +0000316
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000317/// createBlock - Used to lazily create blocks that are connected
318/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000319CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000320 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000321 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000322 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000323 return B;
324}
Mike Stump6d9828c2009-07-17 01:31:16 +0000325
Ted Kremenek6c249722009-09-24 18:45:41 +0000326/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000327bool CFGBuilder::FinishBlock(CFGBlock* B) {
328 if (badCFG)
329 return false;
330
Ted Kremenek4f880632009-07-17 22:18:43 +0000331 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000332 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000333}
334
Ted Kremenek4f880632009-07-17 22:18:43 +0000335/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000336/// blocks for ternary operators, &&, and ||. We also process "," and
337/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000338CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000339tryAgain:
340 switch (S->getStmtClass()) {
341 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000342 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000343
344 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000345 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek4f880632009-07-17 22:18:43 +0000347 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000348 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek4f880632009-07-17 22:18:43 +0000350 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000351 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000352
Ted Kremenek4f880632009-07-17 22:18:43 +0000353 case Stmt::BreakStmtClass:
354 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000357 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek4f880632009-07-17 22:18:43 +0000359 case Stmt::CaseStmtClass:
360 return VisitCaseStmt(cast<CaseStmt>(S));
361
362 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000363 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek4f880632009-07-17 22:18:43 +0000365 case Stmt::CompoundStmtClass:
366 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek4f880632009-07-17 22:18:43 +0000368 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000369 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenek4f880632009-07-17 22:18:43 +0000371 case Stmt::ContinueStmtClass:
372 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek021c8af2010-01-19 20:40:33 +0000374 case Stmt::CXXCatchStmtClass:
375 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
376
377 case Stmt::CXXThrowExprClass:
378 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
379
380 case Stmt::CXXTryStmtClass:
381 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
382
Ted Kremenek4f880632009-07-17 22:18:43 +0000383 case Stmt::DeclStmtClass:
384 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek4f880632009-07-17 22:18:43 +0000386 case Stmt::DefaultStmtClass:
387 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek4f880632009-07-17 22:18:43 +0000389 case Stmt::DoStmtClass:
390 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek4f880632009-07-17 22:18:43 +0000392 case Stmt::ForStmtClass:
393 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek4f880632009-07-17 22:18:43 +0000395 case Stmt::GotoStmtClass:
396 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek4f880632009-07-17 22:18:43 +0000398 case Stmt::IfStmtClass:
399 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek4f880632009-07-17 22:18:43 +0000401 case Stmt::IndirectGotoStmtClass:
402 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremenek4f880632009-07-17 22:18:43 +0000404 case Stmt::LabelStmtClass:
405 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenek115c1b92010-04-11 17:02:10 +0000407 case Stmt::MemberExprClass:
408 return VisitMemberExpr(cast<MemberExpr>(S), asc);
409
Ted Kremenek4f880632009-07-17 22:18:43 +0000410 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000411 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
412
Ted Kremenek4f880632009-07-17 22:18:43 +0000413 case Stmt::ObjCAtSynchronizedStmtClass:
414 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek4f880632009-07-17 22:18:43 +0000416 case Stmt::ObjCAtThrowStmtClass:
417 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek4f880632009-07-17 22:18:43 +0000419 case Stmt::ObjCAtTryStmtClass:
420 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek4f880632009-07-17 22:18:43 +0000422 case Stmt::ObjCForCollectionStmtClass:
423 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Ted Kremenek4f880632009-07-17 22:18:43 +0000425 case Stmt::ParenExprClass:
426 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000427 goto tryAgain;
428
Ted Kremenek4f880632009-07-17 22:18:43 +0000429 case Stmt::NullStmtClass:
430 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek4f880632009-07-17 22:18:43 +0000432 case Stmt::ReturnStmtClass:
433 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek4f880632009-07-17 22:18:43 +0000435 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000436 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek4f880632009-07-17 22:18:43 +0000438 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000439 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek4f880632009-07-17 22:18:43 +0000441 case Stmt::SwitchStmtClass:
442 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek4f880632009-07-17 22:18:43 +0000444 case Stmt::WhileStmtClass:
445 return VisitWhileStmt(cast<WhileStmt>(S));
446 }
447}
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Ted Kremenek852274d2009-12-16 03:18:58 +0000449CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
450 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000451 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000452 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Ted Kremenek4f880632009-07-17 22:18:43 +0000455 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000456}
Mike Stump6d9828c2009-07-17 01:31:16 +0000457
Ted Kremenek4f880632009-07-17 22:18:43 +0000458/// VisitChildren - Visit the children of a Stmt.
459CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
460 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000461 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000462 E = Terminator->child_end(); I != E; ++I) {
463 if (*I) B = Visit(*I);
464 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000465 return B;
466}
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremenek852274d2009-12-16 03:18:58 +0000468CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
469 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000470 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000471
Ted Kremenek852274d2009-12-16 03:18:58 +0000472 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000473 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000474 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000475 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000476
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000477 return Block;
478}
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Ted Kremenek852274d2009-12-16 03:18:58 +0000480CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
481 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000482 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000483 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000484 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek4f880632009-07-17 22:18:43 +0000486 if (!FinishBlock(ConfluenceBlock))
487 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek4f880632009-07-17 22:18:43 +0000489 // create the block evaluating the LHS
490 CFGBlock* LHSBlock = createBlock(false);
491 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek4f880632009-07-17 22:18:43 +0000493 // create the block evaluating the RHS
494 Succ = ConfluenceBlock;
495 Block = NULL;
496 CFGBlock* RHSBlock = addStmt(B->getRHS());
497 if (!FinishBlock(RHSBlock))
498 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Mike Stump00998a02009-07-23 23:25:26 +0000500 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000501 TryResult KnownVal = TryEvaluateBool(B->getLHS());
502 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
503 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000504
Ted Kremenek4f880632009-07-17 22:18:43 +0000505 // Now link the LHSBlock with RHSBlock.
506 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000507 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
508 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000509 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000510 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000511 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
512 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Ted Kremenek4f880632009-07-17 22:18:43 +0000515 // Generate the blocks for evaluating the LHS.
516 Block = LHSBlock;
517 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000518 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000519 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000520 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000521 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000522 addStmt(B->getRHS());
523 return addStmt(B->getLHS());
524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Ted Kremenek852274d2009-12-16 03:18:58 +0000526 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000527}
528
Ted Kremenek852274d2009-12-16 03:18:58 +0000529CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
530 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000531 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000532 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000533 }
534 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000535}
536
Ted Kremenek4f880632009-07-17 22:18:43 +0000537CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
538 // "break" is a control-flow statement. Thus we stop processing the current
539 // block.
540 if (Block && !FinishBlock(Block))
541 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek4f880632009-07-17 22:18:43 +0000543 // Now create a new block that ends with the break statement.
544 Block = createBlock(false);
545 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek4f880632009-07-17 22:18:43 +0000547 // If there is no target for the break, then we are looking at an incomplete
548 // AST. This means that the CFG cannot be constructed.
549 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000550 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000551 else
552 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
554
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000555 return Block;
556}
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Mike Stump4c45aa12010-01-21 15:20:48 +0000558static bool CanThrow(Expr *E) {
559 QualType Ty = E->getType();
560 if (Ty->isFunctionPointerType())
561 Ty = Ty->getAs<PointerType>()->getPointeeType();
562 else if (Ty->isBlockPointerType())
563 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
564
565 const FunctionType *FT = Ty->getAs<FunctionType>();
566 if (FT) {
567 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
568 if (Proto->hasEmptyExceptionSpec())
569 return false;
570 }
571 return true;
572}
573
Ted Kremenek852274d2009-12-16 03:18:58 +0000574CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000575 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000576 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000577 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000578 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000579 }
Mike Stump24556362009-07-25 21:26:53 +0000580
Mike Stump4c45aa12010-01-21 15:20:48 +0000581 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000582
583 // Languages without exceptions are assumed to not throw.
584 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000585 if (AddEHEdges)
586 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000587 }
588
589 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000590 if (FD->hasAttr<NoReturnAttr>())
591 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000592 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000593 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000594 }
Mike Stump24556362009-07-25 21:26:53 +0000595
Mike Stump4c45aa12010-01-21 15:20:48 +0000596 if (!CanThrow(C->getCallee()))
597 AddEHEdge = false;
598
599 if (!NoReturn && !AddEHEdge)
Zhongxing Xu91071662010-02-24 02:19:28 +0000600 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Mike Stump079bd722010-01-19 22:00:14 +0000602 if (Block) {
603 Succ = Block;
604 if (!FinishBlock(Block))
605 return 0;
606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Mike Stump079bd722010-01-19 22:00:14 +0000608 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000609 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000610
Mike Stump079bd722010-01-19 22:00:14 +0000611 if (NoReturn) {
612 // Wire this to the exit block directly.
613 AddSuccessor(Block, &cfg->getExit());
614 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000615 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000616 // Add exceptional edges.
617 if (TryTerminatedBlock)
618 AddSuccessor(Block, TryTerminatedBlock);
619 else
620 AddSuccessor(Block, &cfg->getExit());
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Mike Stump24556362009-07-25 21:26:53 +0000623 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000624}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000625
Ted Kremenek852274d2009-12-16 03:18:58 +0000626CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
627 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000628 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000629 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000630 if (!FinishBlock(ConfluenceBlock))
631 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek115c1b92010-04-11 17:02:10 +0000633 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
634 : AddStmtChoice::AlwaysAdd;
635
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000636 Succ = ConfluenceBlock;
637 Block = NULL;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000638 CFGBlock* LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000639 if (!FinishBlock(LHSBlock))
640 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000642 Succ = ConfluenceBlock;
643 Block = NULL;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000644 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000645 if (!FinishBlock(RHSBlock))
646 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000648 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000649 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000650 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000651 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
652 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000653 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000654 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000655}
Mike Stump1eb44332009-09-09 15:08:12 +0000656
657
658CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000659 EndScope(C);
660
Mike Stump1eb44332009-09-09 15:08:12 +0000661 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000662
663 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
664 I != E; ++I ) {
665 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000667 if (badCFG)
668 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000669 }
Mike Stump079bd722010-01-19 22:00:14 +0000670
671 LastBlock = StartScope(C, LastBlock);
672
Ted Kremenek4f880632009-07-17 22:18:43 +0000673 return LastBlock;
674}
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek852274d2009-12-16 03:18:58 +0000676CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
677 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000678 // Create the confluence block that will "merge" the results of the ternary
679 // expression.
680 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000681 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000682 if (!FinishBlock(ConfluenceBlock))
683 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremenek115c1b92010-04-11 17:02:10 +0000685 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
686 : AddStmtChoice::AlwaysAdd;
687
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000688 // Create a block for the LHS expression if there is an LHS expression. A
689 // GCC extension allows LHS to be NULL, causing the condition to be the
690 // value that is returned instead.
691 // e.g: x ?: y is shorthand for: x ? x : y;
692 Succ = ConfluenceBlock;
693 Block = NULL;
694 CFGBlock* LHSBlock = NULL;
695 if (C->getLHS()) {
Ted Kremenek115c1b92010-04-11 17:02:10 +0000696 LHSBlock = addStmt(C->getLHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000697 if (!FinishBlock(LHSBlock))
698 return 0;
699 Block = NULL;
700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000702 // Create the block for the RHS expression.
703 Succ = ConfluenceBlock;
Ted Kremenek115c1b92010-04-11 17:02:10 +0000704 CFGBlock* RHSBlock = addStmt(C->getRHS(), asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000705 if (!FinishBlock(RHSBlock))
706 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000708 // Create the block that will contain the condition.
709 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Mike Stump00998a02009-07-23 23:25:26 +0000711 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000712 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000713 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000714 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000715 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000716 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000717 // If we know the condition is false, add NULL as the successor for
718 // the block containing the condition. In this case, the confluence
719 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000720 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000721 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000722 } else {
723 // If we have no LHS expression, add the ConfluenceBlock as a direct
724 // successor for the block containing the condition. Moreover, we need to
725 // reverse the order of the predecessors in the ConfluenceBlock because
726 // the RHSBlock will have been added to the succcessors already, and we
727 // want the first predecessor to the the block containing the expression
728 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000729 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000730 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000731 std::reverse(ConfluenceBlock->pred_begin(),
732 ConfluenceBlock->pred_end());
733 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000736 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000737 Block->setTerminator(C);
738 return addStmt(C->getCond());
739}
740
Ted Kremenek4f880632009-07-17 22:18:43 +0000741CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
742 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000743
Ted Kremenek4f880632009-07-17 22:18:43 +0000744 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000745 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000746 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenek4f880632009-07-17 22:18:43 +0000749 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremenek4f880632009-07-17 22:18:43 +0000751 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
752 typedef llvm::SmallVector<Decl*,10> BufTy;
753 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek4f880632009-07-17 22:18:43 +0000755 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
756 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
757 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
758 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek4f880632009-07-17 22:18:43 +0000760 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
761 // automatically freed with the CFG.
762 DeclGroupRef DG(*I);
763 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000764 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000765 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek4f880632009-07-17 22:18:43 +0000767 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000768 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000769 B = VisitDeclSubExpr(D);
770 }
Mike Stump1eb44332009-09-09 15:08:12 +0000771
772 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000773}
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek4f880632009-07-17 22:18:43 +0000775/// VisitDeclSubExpr - Utility method to add block-level expressions for
776/// initializers in Decls.
777CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
778 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000779
Ted Kremenek4f880632009-07-17 22:18:43 +0000780 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Ted Kremenek4f880632009-07-17 22:18:43 +0000782 if (!VD)
783 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek4f880632009-07-17 22:18:43 +0000785 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek4f880632009-07-17 22:18:43 +0000787 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000788 AddStmtChoice::Kind k =
789 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
790 : AddStmtChoice::NotAlwaysAdd;
791 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000792 }
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek4f880632009-07-17 22:18:43 +0000794 // If the type of VD is a VLA, then we must process its size expressions.
795 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
796 VA = FindVA(VA->getElementType().getTypePtr()))
797 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek4f880632009-07-17 22:18:43 +0000799 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000800}
801
802CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000803 // We may see an if statement in the middle of a basic block, or it may be the
804 // first statement we are processing. In either case, we create a new basic
805 // block. First, we create the blocks for the then...else statements, and
806 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000807 // middle of a block, we stop processing that block. That block is then the
808 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000809
810 // The block we were proccessing is now finished. Make it the successor
811 // block.
812 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000813 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000814 if (!FinishBlock(Block))
815 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000816 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000817
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000818 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000819 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000820
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000821 if (Stmt* Else = I->getElse()) {
822 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000823
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000824 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000825 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000827 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000828
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000829 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
830 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000831 else if (Block) {
832 if (!FinishBlock(ElseBlock))
833 return 0;
834 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000835 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000836
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000837 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000838 CFGBlock* ThenBlock;
839 {
840 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000841 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000843 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000844 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000845
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000846 if (!ThenBlock) {
847 // We can reach here if the "then" body has all NullStmts.
848 // Create an empty block so we can distinguish between true and false
849 // branches in path-sensitive analyses.
850 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000851 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000852 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000853 if (!FinishBlock(ThenBlock))
854 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000855 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000856 }
857
Mike Stump6d9828c2009-07-17 01:31:16 +0000858 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000859 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000860
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000861 // Set the terminator of the new block to the If statement.
862 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000863
Mike Stump00998a02009-07-23 23:25:26 +0000864 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000865 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000866
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000867 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000868 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
869 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000870
871 // Add the condition as the last statement in the new block. This may create
872 // new blocks as the condition may contain control-flow. Any newly created
873 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000874 Block = addStmt(I->getCond());
875
876 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
877 // and the condition variable initialization to the CFG.
878 if (VarDecl *VD = I->getConditionVariable()) {
879 if (Expr *Init = VD->getInit()) {
880 autoCreateBlock();
881 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
882 addStmt(Init);
883 }
884 }
885
886 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000887}
Mike Stump6d9828c2009-07-17 01:31:16 +0000888
889
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000890CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000891 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000892 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000893 // NOTE: If a "return" appears in the middle of a block, this means that the
894 // code afterwards is DEAD (unreachable). We still keep a basic block
895 // for that code; a simple "mark-and-sweep" from the entry block will be
896 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000897 if (Block)
898 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000899
900 // Create the new block.
901 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000902
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000903 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000904 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000905
906 // Add the return statement to the block. This may create new blocks if R
907 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000908 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000909}
910
911CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
912 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000913 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000914 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000915
Ted Kremenek4f880632009-07-17 22:18:43 +0000916 if (!LabelBlock) // This can happen when the body is empty, i.e.
917 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000918
Ted Kremenek4f880632009-07-17 22:18:43 +0000919 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000920 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000921
922 // Labels partition blocks, so this is the end of the basic block we were
923 // processing (L is the block's label). Because this is label (and we have
924 // already processed the substatement) there is no extra control-flow to worry
925 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000926 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000927 if (!FinishBlock(LabelBlock))
928 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000929
930 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000931 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000932
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000933 // This block is now the implicit successor of other blocks.
934 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000935
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000936 return LabelBlock;
937}
938
939CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000940 // Goto is a control-flow statement. Thus we stop processing the current
941 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000942 if (Block)
943 FinishBlock(Block);
944
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000945 Block = createBlock(false);
946 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000947
948 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000949 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000950
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000951 if (I == LabelMap.end())
952 // We will need to backpatch this block later.
953 BackpatchBlocks.push_back(Block);
954 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000955 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000956
Mike Stump6d9828c2009-07-17 01:31:16 +0000957 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000958}
959
960CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000961 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000962
Mike Stumpfefb9f72009-07-21 01:12:51 +0000963 // "for" is a control-flow statement. Thus we stop processing the current
964 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000965 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000966 if (!FinishBlock(Block))
967 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000968 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000969 } else
970 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000971
972 // Because of short-circuit evaluation, the condition of the loop can span
973 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
974 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000975 CFGBlock* ExitConditionBlock = createBlock(false);
976 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000977
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000978 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000979 ExitConditionBlock->setTerminator(F);
980
981 // Now add the actual condition to the condition block. Because the condition
982 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000983 if (Stmt* C = F->getCond()) {
984 Block = ExitConditionBlock;
985 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000986 assert(Block == EntryConditionBlock);
987
988 // If this block contains a condition variable, add both the condition
989 // variable and initializer to the CFG.
990 if (VarDecl *VD = F->getConditionVariable()) {
991 if (Expr *Init = VD->getInit()) {
992 autoCreateBlock();
993 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
994 EntryConditionBlock = addStmt(Init);
995 assert(Block == EntryConditionBlock);
996 }
997 }
998
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000999 if (Block) {
1000 if (!FinishBlock(EntryConditionBlock))
1001 return 0;
1002 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001003 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001004
Mike Stump6d9828c2009-07-17 01:31:16 +00001005 // The condition block is the implicit successor for the loop body as well as
1006 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001007 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001008
Mike Stump00998a02009-07-23 23:25:26 +00001009 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001010 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Mike Stump00998a02009-07-23 23:25:26 +00001012 if (F->getCond())
1013 KnownVal = TryEvaluateBool(F->getCond());
1014
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001015 // Now create the loop body.
1016 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001017 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001018
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001019 // Save the current values for Block, Succ, and continue and break targets
1020 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001021 save_continue(ContinueTargetBlock),
1022 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001023
Ted Kremenekaf603f72007-08-30 18:39:40 +00001024 // Create a new block to contain the (bottom) of the loop body.
1025 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001026
Ted Kremeneke9334502008-09-04 21:48:47 +00001027 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001028 // Generate increment code in its own basic block. This is the target of
1029 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001030 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001031 } else {
1032 // No increment code. Create a special, empty, block that is used as the
1033 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001034 assert(Succ == EntryConditionBlock);
1035 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001036 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001037
Ted Kremenek3575f842009-04-28 00:51:56 +00001038 // Finish up the increment (or empty) block if it hasn't been already.
1039 if (Block) {
1040 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001041 if (!FinishBlock(Block))
1042 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001043 Block = 0;
1044 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001045
Ted Kremenek3575f842009-04-28 00:51:56 +00001046 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001047
Ted Kremenek3575f842009-04-28 00:51:56 +00001048 // The starting block for the loop increment is the block that should
1049 // represent the 'loop target' for looping back to the start of the loop.
1050 ContinueTargetBlock->setLoopTarget(F);
1051
Ted Kremeneke9334502008-09-04 21:48:47 +00001052 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +00001053 BreakTargetBlock = LoopSuccessor;
1054
1055 // Now populate the body block, and in the process create new blocks as we
1056 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001057 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001058
1059 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001060 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001061 else if (Block && !FinishBlock(BodyBlock))
1062 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001063
Ted Kremenek941fde82009-07-24 04:47:11 +00001064 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001065 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001066 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001067
Ted Kremenek941fde82009-07-24 04:47:11 +00001068 // Link up the condition block with the code that follows the loop. (the
1069 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001070 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001071
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001072 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001073 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001074 if (Stmt* I = F->getInit()) {
1075 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001076 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001077 } else {
1078 // There is no loop initialization. We are thus basically a while loop.
1079 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001080 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001081 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001082 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001083 }
1084}
1085
Ted Kremenek115c1b92010-04-11 17:02:10 +00001086CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1087 if (asc.alwaysAdd()) {
1088 autoCreateBlock();
1089 AppendStmt(Block, M, asc);
1090 }
1091 return Visit(M->getBase(),
1092 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1093 : AddStmtChoice::AsLValueNotAlwaysAdd);
1094}
1095
Ted Kremenek514de5a2008-11-11 17:10:00 +00001096CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1097 // Objective-C fast enumeration 'for' statements:
1098 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1099 //
1100 // for ( Type newVariable in collection_expression ) { statements }
1101 //
1102 // becomes:
1103 //
1104 // prologue:
1105 // 1. collection_expression
1106 // T. jump to loop_entry
1107 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001108 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001109 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1110 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1111 // TB:
1112 // statements
1113 // T. jump to loop_entry
1114 // FB:
1115 // what comes after
1116 //
1117 // and
1118 //
1119 // Type existingItem;
1120 // for ( existingItem in expression ) { statements }
1121 //
1122 // becomes:
1123 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001124 // the same with newVariable replaced with existingItem; the binding works
1125 // the same except that for one ObjCForCollectionStmt::getElement() returns
1126 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001127 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001128
Ted Kremenek514de5a2008-11-11 17:10:00 +00001129 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001130
Ted Kremenek514de5a2008-11-11 17:10:00 +00001131 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001132 if (!FinishBlock(Block))
1133 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001134 LoopSuccessor = Block;
1135 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001136 } else
1137 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001138
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001139 // Build the condition blocks.
1140 CFGBlock* ExitConditionBlock = createBlock(false);
1141 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001142
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001143 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001144 ExitConditionBlock->setTerminator(S);
1145
1146 // The last statement in the block should be the ObjCForCollectionStmt, which
1147 // performs the actual binding to 'element' and determines if there are any
1148 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001149 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001150 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001151
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001152 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001153 // generate new blocks as necesary. We DON'T add the statement by default to
1154 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001155 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001156 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001157 if (!FinishBlock(EntryConditionBlock))
1158 return 0;
1159 Block = 0;
1160 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001161
1162 // The condition block is the implicit successor for the loop body as well as
1163 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001164 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001165
Ted Kremenek514de5a2008-11-11 17:10:00 +00001166 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001167 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001168 // Save the current values for Succ, continue and break targets.
1169 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001170 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1171
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001172 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001173 ContinueTargetBlock = EntryConditionBlock;
1174
Ted Kremenek4f880632009-07-17 22:18:43 +00001175 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001176
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001177 if (!BodyBlock)
1178 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001179 else if (Block) {
1180 if (!FinishBlock(BodyBlock))
1181 return 0;
1182 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001183
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001184 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001185 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001186 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001187
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001188 // Link up the condition block with the code that follows the loop.
1189 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001190 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001191
Ted Kremenek514de5a2008-11-11 17:10:00 +00001192 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001193 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001194 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001195}
1196
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001197CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1198 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001199
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001200 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001201 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001202
Ted Kremenekda5348e2009-05-05 23:11:51 +00001203 // The sync body starts its own basic block. This makes it a little easier
1204 // for diagnostic clients.
1205 if (SyncBlock) {
1206 if (!FinishBlock(SyncBlock))
1207 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001208
Ted Kremenekda5348e2009-05-05 23:11:51 +00001209 Block = 0;
1210 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001211
Ted Kremenekda5348e2009-05-05 23:11:51 +00001212 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001213
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001214 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001215 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001216}
Mike Stump6d9828c2009-07-17 01:31:16 +00001217
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001218CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001219 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001220 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001221}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001222
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001223CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001224 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001225
Mike Stumpfefb9f72009-07-21 01:12:51 +00001226 // "while" is a control-flow statement. Thus we stop processing the current
1227 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001228 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001229 if (!FinishBlock(Block))
1230 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001231 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001232 } else
1233 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001234
1235 // Because of short-circuit evaluation, the condition of the loop can span
1236 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1237 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001238 CFGBlock* ExitConditionBlock = createBlock(false);
1239 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001240
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001241 // Set the terminator for the "exit" condition block.
1242 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001243
1244 // Now add the actual condition to the condition block. Because the condition
1245 // itself may contain control-flow, new blocks may be created. Thus we update
1246 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001247 if (Stmt* C = W->getCond()) {
1248 Block = ExitConditionBlock;
1249 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001250 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001251
1252 // If this block contains a condition variable, add both the condition
1253 // variable and initializer to the CFG.
1254 if (VarDecl *VD = W->getConditionVariable()) {
1255 if (Expr *Init = VD->getInit()) {
1256 autoCreateBlock();
1257 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1258 EntryConditionBlock = addStmt(Init);
1259 assert(Block == EntryConditionBlock);
1260 }
1261 }
1262
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001263 if (Block) {
1264 if (!FinishBlock(EntryConditionBlock))
1265 return 0;
1266 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001267 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001268
1269 // The condition block is the implicit successor for the loop body as well as
1270 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001271 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001272
Mike Stump00998a02009-07-23 23:25:26 +00001273 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001274 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001275
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001276 // Process the loop body.
1277 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001278 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001279
1280 // Save the current values for Block, Succ, and continue and break targets
1281 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1282 save_continue(ContinueTargetBlock),
1283 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001284
Mike Stump6d9828c2009-07-17 01:31:16 +00001285 // Create an empty block to represent the transition block for looping back
1286 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001287 Block = 0;
1288 assert(Succ == EntryConditionBlock);
1289 Succ = createBlock();
1290 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001291 ContinueTargetBlock = Succ;
1292
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001293 // All breaks should go to the code following the loop.
1294 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001295
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001296 // NULL out Block to force lazy instantiation of blocks for the body.
1297 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001298
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001299 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001300 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001301
Ted Kremenekaf603f72007-08-30 18:39:40 +00001302 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001303 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001304 else if (Block) {
1305 if (!FinishBlock(BodyBlock))
1306 return 0;
1307 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001308
Ted Kremenek941fde82009-07-24 04:47:11 +00001309 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001310 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001311 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001312
Ted Kremenek941fde82009-07-24 04:47:11 +00001313 // Link up the condition block with the code that follows the loop. (the
1314 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001315 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001316
1317 // There can be no more statements in the condition block since we loop back
1318 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001319 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001321 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001322 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001323 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001324}
Mike Stump1eb44332009-09-09 15:08:12 +00001325
1326
Ted Kremenek4f880632009-07-17 22:18:43 +00001327CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1328 // FIXME: For now we pretend that @catch and the code it contains does not
1329 // exit.
1330 return Block;
1331}
Mike Stump6d9828c2009-07-17 01:31:16 +00001332
Ted Kremenek2fda5042008-12-09 20:20:09 +00001333CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1334 // FIXME: This isn't complete. We basically treat @throw like a return
1335 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001336
Ted Kremenek6c249722009-09-24 18:45:41 +00001337 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001338 if (Block && !FinishBlock(Block))
1339 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001340
Ted Kremenek2fda5042008-12-09 20:20:09 +00001341 // Create the new block.
1342 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001343
Ted Kremenek2fda5042008-12-09 20:20:09 +00001344 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001345 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001346
1347 // Add the statement to the block. This may create new blocks if S contains
1348 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001349 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001350}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001351
Mike Stump0979d802009-07-22 22:56:04 +00001352CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001353 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001354 if (Block && !FinishBlock(Block))
1355 return 0;
1356
1357 // Create the new block.
1358 Block = createBlock(false);
1359
Mike Stump5d1d2022010-01-19 02:20:09 +00001360 if (TryTerminatedBlock)
1361 // The current try statement is the only successor.
1362 AddSuccessor(Block, TryTerminatedBlock);
1363 else
1364 // otherwise the Exit block is the only successor.
1365 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001366
1367 // Add the statement to the block. This may create new blocks if S contains
1368 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001369 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001370}
1371
Ted Kremenek4f880632009-07-17 22:18:43 +00001372CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001374
Mike Stump8f9893a2009-07-21 01:27:50 +00001375 // "do...while" is a control-flow statement. Thus we stop processing the
1376 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001377 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001378 if (!FinishBlock(Block))
1379 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001380 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001381 } else
1382 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001383
1384 // Because of short-circuit evaluation, the condition of the loop can span
1385 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1386 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001387 CFGBlock* ExitConditionBlock = createBlock(false);
1388 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001389
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001390 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001391 ExitConditionBlock->setTerminator(D);
1392
1393 // Now add the actual condition to the condition block. Because the condition
1394 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001395 if (Stmt* C = D->getCond()) {
1396 Block = ExitConditionBlock;
1397 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001398 if (Block) {
1399 if (!FinishBlock(EntryConditionBlock))
1400 return 0;
1401 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001402 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001403
Ted Kremenek54827132008-02-27 07:20:00 +00001404 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001405 Succ = EntryConditionBlock;
1406
Mike Stump00998a02009-07-23 23:25:26 +00001407 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001408 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001409
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001410 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001411 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001412 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001413 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001414
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001415 // Save the current values for Block, Succ, and continue and break targets
1416 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001417 save_continue(ContinueTargetBlock),
1418 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001419
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001420 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001421 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001422
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001423 // All breaks should go to the code following the loop.
1424 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001425
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001426 // NULL out Block to force lazy instantiation of blocks for the body.
1427 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001428
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001429 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001430 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
Ted Kremenekaf603f72007-08-30 18:39:40 +00001432 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001433 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001434 else if (Block) {
1435 if (!FinishBlock(BodyBlock))
1436 return 0;
1437 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001438
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001439 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001440 // ExitConditionBlock to represent the "loop back" transition. Create an
1441 // empty block to represent the transition block for looping back to the
1442 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001443 // FIXME: Can we do this more efficiently without adding another block?
1444 Block = NULL;
1445 Succ = BodyBlock;
1446 CFGBlock *LoopBackBlock = createBlock();
1447 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001448
Ted Kremenek941fde82009-07-24 04:47:11 +00001449 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001450 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001451 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
Ted Kremenek941fde82009-07-24 04:47:11 +00001453 // Link up the condition block with the code that follows the loop.
1454 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001455 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001456
1457 // There can be no more statements in the body block(s) since we loop back to
1458 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001459 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001460
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001461 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001462 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001463 return BodyBlock;
1464}
1465
1466CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1467 // "continue" is a control-flow statement. Thus we stop processing the
1468 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001469 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001470 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001471
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472 // Now create a new block that ends with the continue statement.
1473 Block = createBlock(false);
1474 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001475
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001476 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001477 // incomplete AST. This means the CFG cannot be constructed.
1478 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001479 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001480 else
1481 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001482
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001483 return Block;
1484}
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001486CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001487 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001488
Ted Kremenek852274d2009-12-16 03:18:58 +00001489 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001490 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001491 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001492 }
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek4f880632009-07-17 22:18:43 +00001494 // VLA types have expressions that must be evaluated.
1495 if (E->isArgumentType()) {
1496 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1497 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1498 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001499 }
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Mike Stump6d9828c2009-07-17 01:31:16 +00001501 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001502}
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Ted Kremenek4f880632009-07-17 22:18:43 +00001504/// VisitStmtExpr - Utility method to handle (nested) statement
1505/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001506CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1507 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001508 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001509 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001510 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001511 return VisitCompoundStmt(SE->getSubStmt());
1512}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001513
Ted Kremenek411cdee2008-04-16 21:10:48 +00001514CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001515 // "switch" is a control-flow statement. Thus we stop processing the current
1516 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001517 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001518
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001519 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001520 if (!FinishBlock(Block))
1521 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001522 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001523 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001524
1525 // Save the current "switch" context.
1526 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001527 save_break(BreakTargetBlock),
1528 save_default(DefaultCaseBlock);
1529
Mike Stump6d9828c2009-07-17 01:31:16 +00001530 // Set the "default" case to be the block after the switch statement. If the
1531 // switch statement contains a "default:", this value will be overwritten with
1532 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001533 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001534
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001535 // Create a new block that will contain the switch statement.
1536 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001537
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001538 // Now process the switch body. The code after the switch is the implicit
1539 // successor.
1540 Succ = SwitchSuccessor;
1541 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001542
1543 // When visiting the body, the case statements should automatically get linked
1544 // up to the switch. We also don't keep a pointer to the body, since all
1545 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001546 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001547 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001548 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001549 if (Block) {
1550 if (!FinishBlock(BodyBlock))
1551 return 0;
1552 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001553
Mike Stump6d9828c2009-07-17 01:31:16 +00001554 // If we have no "default:" case, the default transition is to the code
1555 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001556 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001557
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001558 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001559 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001560 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001561 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001562 Block = addStmt(Terminator->getCond());
1563
1564 // Finally, if the SwitchStmt contains a condition variable, add both the
1565 // SwitchStmt and the condition variable initialization to the CFG.
1566 if (VarDecl *VD = Terminator->getConditionVariable()) {
1567 if (Expr *Init = VD->getInit()) {
1568 autoCreateBlock();
1569 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1570 addStmt(Init);
1571 }
1572 }
1573
1574 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001575}
1576
Ted Kremenek4f880632009-07-17 22:18:43 +00001577CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001578 // CaseStmts are essentially labels, so they are the first statement in a
1579 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001580
Ted Kremenek4f880632009-07-17 22:18:43 +00001581 if (CS->getSubStmt())
1582 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001584 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001585 if (!CaseBlock)
1586 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001587
1588 // Cases statements partition blocks, so this is the top of the basic block we
1589 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001590 CaseBlock->setLabel(CS);
1591
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001592 if (!FinishBlock(CaseBlock))
1593 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001594
1595 // Add this block to the list of successors for the block with the switch
1596 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001597 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001598 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001599
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001600 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1601 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001602
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001603 // This block is now the implicit successor of other blocks.
1604 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001605
Ted Kremenek2677ea82008-03-15 07:45:02 +00001606 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001607}
Mike Stump6d9828c2009-07-17 01:31:16 +00001608
Ted Kremenek411cdee2008-04-16 21:10:48 +00001609CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001610 if (Terminator->getSubStmt())
1611 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001613 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001614
1615 if (!DefaultCaseBlock)
1616 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
1618 // Default statements partition blocks, so this is the top of the basic block
1619 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001620 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001622 if (!FinishBlock(DefaultCaseBlock))
1623 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001624
Mike Stump6d9828c2009-07-17 01:31:16 +00001625 // Unlike case statements, we don't add the default block to the successors
1626 // for the switch statement immediately. This is done when we finish
1627 // processing the switch statement. This allows for the default case
1628 // (including a fall-through to the code after the switch statement) to always
1629 // be the last successor of a switch-terminated block.
1630
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001631 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1632 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001633
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001634 // This block is now the implicit successor of other blocks.
1635 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001636
1637 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001638}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001639
Mike Stump5d1d2022010-01-19 02:20:09 +00001640CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1641 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1642 // current block.
1643 CFGBlock* TrySuccessor = NULL;
1644
1645 if (Block) {
1646 if (!FinishBlock(Block))
1647 return 0;
1648 TrySuccessor = Block;
1649 } else TrySuccessor = Succ;
1650
Mike Stumpa1f93632010-01-20 01:15:34 +00001651 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001652
1653 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001654 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001655 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001656 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001657
Mike Stumpa1f93632010-01-20 01:15:34 +00001658 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001659 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1660 // The code after the try is the implicit successor.
1661 Succ = TrySuccessor;
1662 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001663 if (CS->getExceptionDecl() == 0) {
1664 HasCatchAll = true;
1665 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001666 Block = NULL;
1667 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1668 if (CatchBlock == 0)
1669 return 0;
1670 // Add this block to the list of successors for the block with the try
1671 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001672 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001673 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001674 if (!HasCatchAll) {
1675 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001676 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001677 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001678 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001679 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001680
1681 // The code after the try is the implicit successor.
1682 Succ = TrySuccessor;
1683
Mike Stumpf00cca52010-01-20 01:30:58 +00001684 // Save the current "try" context.
1685 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1686 TryTerminatedBlock = NewTryTerminatedBlock;
1687
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001688 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001689 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001690 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001691 return Block;
1692}
1693
1694CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1695 // CXXCatchStmt are treated like labels, so they are the first statement in a
1696 // block.
1697
1698 if (CS->getHandlerBlock())
1699 addStmt(CS->getHandlerBlock());
1700
1701 CFGBlock* CatchBlock = Block;
1702 if (!CatchBlock)
1703 CatchBlock = createBlock();
1704
1705 CatchBlock->setLabel(CS);
1706
1707 if (!FinishBlock(CatchBlock))
1708 return 0;
1709
1710 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1711 Block = NULL;
1712
1713 return CatchBlock;
1714}
1715
Ted Kremenek19bb3562007-08-28 19:26:49 +00001716CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001717 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001718 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001719
Ted Kremenek19bb3562007-08-28 19:26:49 +00001720 if (!IBlock) {
1721 IBlock = createBlock(false);
1722 cfg->setIndirectGotoBlock(IBlock);
1723 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001724
Ted Kremenek19bb3562007-08-28 19:26:49 +00001725 // IndirectGoto is a control-flow statement. Thus we stop processing the
1726 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001727 if (Block && !FinishBlock(Block))
1728 return 0;
1729
Ted Kremenek19bb3562007-08-28 19:26:49 +00001730 Block = createBlock(false);
1731 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001732 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001733 return addStmt(I->getTarget());
1734}
1735
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001736} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001737
Mike Stump6d9828c2009-07-17 01:31:16 +00001738/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1739/// no successors or predecessors. If this is the first block created in the
1740/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001741CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001742 bool first_block = begin() == end();
1743
1744 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001745 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1746 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1747 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001748
1749 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001750 if (first_block)
1751 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001752
1753 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001754 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001755}
1756
Ted Kremenek026473c2007-08-23 16:51:22 +00001757/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1758/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001759CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump4c45aa12010-01-21 15:20:48 +00001760 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001761 CFGBuilder Builder;
Mike Stump4c45aa12010-01-21 15:20:48 +00001762 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001763}
1764
Ted Kremenek63f58872007-10-01 19:33:33 +00001765//===----------------------------------------------------------------------===//
1766// CFG: Queries for BlkExprs.
1767//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001768
Ted Kremenek63f58872007-10-01 19:33:33 +00001769namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001770 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001771}
1772
Ted Kremenek8a693662009-12-23 23:37:10 +00001773static void FindSubExprAssignments(Stmt *S,
1774 llvm::SmallPtrSet<Expr*,50>& Set) {
1775 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001776 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001777
Ted Kremenek8a693662009-12-23 23:37:10 +00001778 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1779 Stmt *child = *I;
1780 if (!child)
1781 continue;
1782
1783 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001784 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001785
Ted Kremenek8a693662009-12-23 23:37:10 +00001786 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001787 }
1788}
1789
Ted Kremenek63f58872007-10-01 19:33:33 +00001790static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1791 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001792
1793 // Look for assignments that are used as subexpressions. These are the only
1794 // assignments that we want to *possibly* register as a block-level
1795 // expression. Basically, if an assignment occurs both in a subexpression and
1796 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001797 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Ted Kremenek63f58872007-10-01 19:33:33 +00001799 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001800 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001801 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001802
Ted Kremenek411cdee2008-04-16 21:10:48 +00001803 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001804
1805 // Iterate over the statements again on identify the Expr* and Stmt* at the
1806 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001807
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001808 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001809 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001810
Ted Kremenek411cdee2008-04-16 21:10:48 +00001811 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001812 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001813 // expression are really "statements" whose value is never used by
1814 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001815 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001816 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001817 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1818 // Special handling for statement expressions. The last statement in
1819 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001820 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001821 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001822 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001823 (*M)[C->body_back()] = x;
1824 }
1825 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001826
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001827 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001828 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001829 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001830
Ted Kremenek411cdee2008-04-16 21:10:48 +00001831 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001832
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001833 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001834
Ted Kremenek390e48b2008-11-12 21:11:49 +00001835 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001836 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001837 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001838 }
1839 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001840
Ted Kremenek63f58872007-10-01 19:33:33 +00001841 return M;
1842}
1843
Ted Kremenek86946742008-01-17 20:48:37 +00001844CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1845 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001846 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremenek63f58872007-10-01 19:33:33 +00001848 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001849 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001850 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001851}
1852
1853unsigned CFG::getNumBlkExprs() {
1854 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1855 return M->size();
1856 else {
1857 // We assume callers interested in the number of BlkExprs will want
1858 // the map constructed if it doesn't already exist.
1859 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1860 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1861 }
1862}
1863
Ted Kremenek274f4332008-04-28 18:00:46 +00001864//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001865// Cleanup: CFG dstor.
1866//===----------------------------------------------------------------------===//
1867
Ted Kremenek63f58872007-10-01 19:33:33 +00001868CFG::~CFG() {
1869 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1870}
Mike Stump6d9828c2009-07-17 01:31:16 +00001871
Ted Kremenek7dba8602007-08-29 21:56:09 +00001872//===----------------------------------------------------------------------===//
1873// CFG pretty printing
1874//===----------------------------------------------------------------------===//
1875
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001876namespace {
1877
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001878class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001879 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1880 StmtMapTy StmtMap;
1881 signed CurrentBlock;
1882 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001883 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001884public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001885
Chris Lattnere4f21422009-06-30 01:26:17 +00001886 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1887 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001888 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1889 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001890 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001891 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001892 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001893 }
1894 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001895
Ted Kremenek42a509f2007-08-31 21:30:12 +00001896 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001897
Chris Lattnere4f21422009-06-30 01:26:17 +00001898 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001899 void setBlockID(signed i) { CurrentBlock = i; }
1900 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001901
Ted Kremeneka95d3752008-09-13 05:16:45 +00001902 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001903
Ted Kremenek411cdee2008-04-16 21:10:48 +00001904 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001905
1906 if (I == StmtMap.end())
1907 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001908
1909 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001910 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001911 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001912 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001913
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001914 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001915 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001916 }
1917};
Chris Lattnere4f21422009-06-30 01:26:17 +00001918} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001919
Chris Lattnere4f21422009-06-30 01:26:17 +00001920
1921namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001922class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001923 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001924
Ted Kremeneka95d3752008-09-13 05:16:45 +00001925 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001926 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001927 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001928public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001929 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001930 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001931 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001932
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001933 void VisitIfStmt(IfStmt* I) {
1934 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001935 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001936 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001937
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001938 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001939 void VisitStmt(Stmt* Terminator) {
1940 Terminator->printPretty(OS, Helper, Policy);
1941 }
1942
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001943 void VisitForStmt(ForStmt* F) {
1944 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001945 if (F->getInit())
1946 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001947 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001948 if (Stmt* C = F->getCond())
1949 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001950 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001951 if (F->getInc())
1952 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001953 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001954 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001955
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001956 void VisitWhileStmt(WhileStmt* W) {
1957 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001958 if (Stmt* C = W->getCond())
1959 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001960 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001961
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001962 void VisitDoStmt(DoStmt* D) {
1963 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001964 if (Stmt* C = D->getCond())
1965 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001966 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001967
Ted Kremenek411cdee2008-04-16 21:10:48 +00001968 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001969 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001970 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001971 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Mike Stump5d1d2022010-01-19 02:20:09 +00001973 void VisitCXXTryStmt(CXXTryStmt* CS) {
1974 OS << "try ...";
1975 }
1976
Ted Kremenek805e9a82007-08-31 21:49:40 +00001977 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001978 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001979 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001980 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001981
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001982 void VisitChooseExpr(ChooseExpr* C) {
1983 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001984 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001985 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001986 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001987
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001988 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1989 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001990 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001991 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremenek805e9a82007-08-31 21:49:40 +00001993 void VisitBinaryOperator(BinaryOperator* B) {
1994 if (!B->isLogicalOp()) {
1995 VisitExpr(B);
1996 return;
1997 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001998
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001999 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002000
Ted Kremenek805e9a82007-08-31 21:49:40 +00002001 switch (B->getOpcode()) {
2002 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002003 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002004 return;
2005 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002006 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002007 return;
2008 default:
2009 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002010 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002011 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002012
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002013 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002014 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002015 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002016};
Chris Lattnere4f21422009-06-30 01:26:17 +00002017} // end anonymous namespace
2018
Mike Stump6d9828c2009-07-17 01:31:16 +00002019
Chris Lattnere4f21422009-06-30 01:26:17 +00002020static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002021 const CFGElement &E) {
2022 Stmt *Terminator = E;
2023
2024 if (E.asStartScope()) {
2025 OS << "start scope\n";
2026 return;
2027 }
2028 if (E.asEndScope()) {
2029 OS << "end scope\n";
2030 return;
2031 }
2032
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002033 if (Helper) {
2034 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002035 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002036 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002037
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002038 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002039 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002040 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002041 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002042 return;
2043 }
2044 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002045
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002046 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002047 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002048 if (B->getOpcode() == BinaryOperator::Comma) {
2049 OS << "... , ";
2050 Helper->handledStmt(B->getRHS(),OS);
2051 OS << '\n';
2052 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002053 }
2054 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002055 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002056
Chris Lattnere4f21422009-06-30 01:26:17 +00002057 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002058
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002059 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002060 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002061}
Mike Stump6d9828c2009-07-17 01:31:16 +00002062
Chris Lattnere4f21422009-06-30 01:26:17 +00002063static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2064 const CFGBlock& B,
2065 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002066
Ted Kremenek42a509f2007-08-31 21:30:12 +00002067 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002068
Ted Kremenek7dba8602007-08-29 21:56:09 +00002069 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002070 OS << "\n [ B" << B.getBlockID();
2071
Ted Kremenek42a509f2007-08-31 21:30:12 +00002072 if (&B == &cfg->getEntry())
2073 OS << " (ENTRY) ]\n";
2074 else if (&B == &cfg->getExit())
2075 OS << " (EXIT) ]\n";
2076 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002077 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002078 else
2079 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002080
Ted Kremenek9cffe732007-08-29 23:20:49 +00002081 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002082 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002083
2084 if (print_edges)
2085 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002086
Mike Stump079bd722010-01-19 22:00:14 +00002087 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002088 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002089 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002090 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002091 C->getLHS()->printPretty(OS, Helper,
2092 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002093 if (C->getRHS()) {
2094 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002095 C->getRHS()->printPretty(OS, Helper,
2096 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002097 }
Mike Stump079bd722010-01-19 22:00:14 +00002098 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002099 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002100 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002101 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002102 if (CS->getExceptionDecl())
2103 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2104 0);
2105 else
2106 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002107 OS << ")";
2108
2109 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002110 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002111
Ted Kremenek9cffe732007-08-29 23:20:49 +00002112 OS << ":\n";
2113 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002114
Ted Kremenekfddd5182007-08-21 21:42:03 +00002115 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002116 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002117
Ted Kremenek42a509f2007-08-31 21:30:12 +00002118 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2119 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002120
Ted Kremenek9cffe732007-08-29 23:20:49 +00002121 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002122 if (print_edges)
2123 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002124
Ted Kremeneka95d3752008-09-13 05:16:45 +00002125 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002126
Ted Kremenek42a509f2007-08-31 21:30:12 +00002127 if (Helper)
2128 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002129
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002130 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002131 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002132
Ted Kremenek9cffe732007-08-29 23:20:49 +00002133 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002134 if (B.getTerminator()) {
2135 if (print_edges)
2136 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002137
Ted Kremenek9cffe732007-08-29 23:20:49 +00002138 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002139
Ted Kremenek42a509f2007-08-31 21:30:12 +00002140 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002141
Chris Lattnere4f21422009-06-30 01:26:17 +00002142 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2143 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002144 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002145 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002146 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002147
Ted Kremenek9cffe732007-08-29 23:20:49 +00002148 if (print_edges) {
2149 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002150 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002151 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002152
Ted Kremenek42a509f2007-08-31 21:30:12 +00002153 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2154 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002155
Ted Kremenek42a509f2007-08-31 21:30:12 +00002156 if (i == 8 || (i-8) == 0)
2157 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002158
Ted Kremenek9cffe732007-08-29 23:20:49 +00002159 OS << " B" << (*I)->getBlockID();
2160 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002161
Ted Kremenek42a509f2007-08-31 21:30:12 +00002162 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002163
Ted Kremenek42a509f2007-08-31 21:30:12 +00002164 // Print the successors of this block.
2165 OS << " Successors (" << B.succ_size() << "):";
2166 i = 0;
2167
2168 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2169 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002170
Ted Kremenek42a509f2007-08-31 21:30:12 +00002171 if (i == 8 || (i-8) % 10 == 0)
2172 OS << "\n ";
2173
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002174 if (*I)
2175 OS << " B" << (*I)->getBlockID();
2176 else
2177 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002178 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002179
Ted Kremenek9cffe732007-08-29 23:20:49 +00002180 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002181 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002182}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002183
Ted Kremenek42a509f2007-08-31 21:30:12 +00002184
2185/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002186void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002187
2188/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002189void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2190 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002191
Ted Kremenek42a509f2007-08-31 21:30:12 +00002192 // Print the entry block.
2193 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002194
Ted Kremenek42a509f2007-08-31 21:30:12 +00002195 // Iterate through the CFGBlocks and print them one by one.
2196 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2197 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002198 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002199 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002200
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002201 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002202 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002203
Ted Kremenek42a509f2007-08-31 21:30:12 +00002204 // Print the exit block.
2205 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002206 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002207}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002208
2209/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002210void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2211 print(llvm::errs(), cfg, LO);
2212}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002213
2214/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2215/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002216void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2217 const LangOptions &LO) const {
2218 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002219 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002220}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002221
Ted Kremeneka2925852008-01-30 23:02:42 +00002222/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002223void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002224 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002225 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002226 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2227}
2228
Ted Kremenek390e48b2008-11-12 21:11:49 +00002229Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002230
Ted Kremenek411cdee2008-04-16 21:10:48 +00002231 if (!Terminator)
2232 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002233
Ted Kremenek411cdee2008-04-16 21:10:48 +00002234 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002235
Ted Kremenek411cdee2008-04-16 21:10:48 +00002236 switch (Terminator->getStmtClass()) {
2237 default:
2238 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002239
Ted Kremenek411cdee2008-04-16 21:10:48 +00002240 case Stmt::ForStmtClass:
2241 E = cast<ForStmt>(Terminator)->getCond();
2242 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002243
Ted Kremenek411cdee2008-04-16 21:10:48 +00002244 case Stmt::WhileStmtClass:
2245 E = cast<WhileStmt>(Terminator)->getCond();
2246 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002247
Ted Kremenek411cdee2008-04-16 21:10:48 +00002248 case Stmt::DoStmtClass:
2249 E = cast<DoStmt>(Terminator)->getCond();
2250 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002251
Ted Kremenek411cdee2008-04-16 21:10:48 +00002252 case Stmt::IfStmtClass:
2253 E = cast<IfStmt>(Terminator)->getCond();
2254 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002255
Ted Kremenek411cdee2008-04-16 21:10:48 +00002256 case Stmt::ChooseExprClass:
2257 E = cast<ChooseExpr>(Terminator)->getCond();
2258 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002259
Ted Kremenek411cdee2008-04-16 21:10:48 +00002260 case Stmt::IndirectGotoStmtClass:
2261 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2262 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002263
Ted Kremenek411cdee2008-04-16 21:10:48 +00002264 case Stmt::SwitchStmtClass:
2265 E = cast<SwitchStmt>(Terminator)->getCond();
2266 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002267
Ted Kremenek411cdee2008-04-16 21:10:48 +00002268 case Stmt::ConditionalOperatorClass:
2269 E = cast<ConditionalOperator>(Terminator)->getCond();
2270 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002271
Ted Kremenek411cdee2008-04-16 21:10:48 +00002272 case Stmt::BinaryOperatorClass: // '&&' and '||'
2273 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002274 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002275
Ted Kremenek390e48b2008-11-12 21:11:49 +00002276 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002277 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002278 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002279
Ted Kremenek411cdee2008-04-16 21:10:48 +00002280 return E ? E->IgnoreParens() : NULL;
2281}
2282
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002283bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002284
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002285 if (!Terminator)
2286 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002287
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002288 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002289
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002290 switch (Terminator->getStmtClass()) {
2291 default:
2292 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002293
2294 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002295 case Stmt::WhileStmtClass:
2296 case Stmt::DoStmtClass:
2297 case Stmt::IfStmtClass:
2298 case Stmt::ChooseExprClass:
2299 case Stmt::ConditionalOperatorClass:
2300 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002301 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002302 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002303
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002304 return E ? E->IgnoreParens() : NULL;
2305}
2306
Ted Kremeneka2925852008-01-30 23:02:42 +00002307
Ted Kremenek7dba8602007-08-29 21:56:09 +00002308//===----------------------------------------------------------------------===//
2309// CFG Graphviz Visualization
2310//===----------------------------------------------------------------------===//
2311
Ted Kremenek42a509f2007-08-31 21:30:12 +00002312
2313#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002314static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002315#endif
2316
Chris Lattnere4f21422009-06-30 01:26:17 +00002317void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002318#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002319 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002320 GraphHelper = &H;
2321 llvm::ViewGraph(this,"CFG");
2322 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002323#endif
2324}
2325
Ted Kremenek7dba8602007-08-29 21:56:09 +00002326namespace llvm {
2327template<>
2328struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002329
2330 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2331
2332 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002333
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002334#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002335 std::string OutSStr;
2336 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002337 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002338 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002339
2340 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2341
2342 // Process string output to make it nicer...
2343 for (unsigned i = 0; i != OutStr.length(); ++i)
2344 if (OutStr[i] == '\n') { // Left justify
2345 OutStr[i] = '\\';
2346 OutStr.insert(OutStr.begin()+i+1, 'l');
2347 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002348
Ted Kremenek7dba8602007-08-29 21:56:09 +00002349 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002350#else
2351 return "";
2352#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002353 }
2354};
2355} // end namespace llvm