blob: d4f64bc17869d66c2230b6ef893314a726a7736a [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:
41 enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
42public:
43 AddStmtChoice(Kind kind) : k(kind) {}
44 bool alwaysAdd() const { return k != NotAlwaysAdd; }
45 bool asLValue() const { return k == AlwaysAddAsLValue; }
46private:
47 Kind k;
48};
Mike Stump6d9828c2009-07-17 01:31:16 +000049
Ted Kremeneka34ea072008-08-04 22:51:42 +000050/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000051/// The builder is stateful: an instance of the builder should be used to only
52/// construct a single CFG.
53///
54/// Example usage:
55///
56/// CFGBuilder builder;
57/// CFG* cfg = builder.BuildAST(stmt1);
58///
Mike Stump6d9828c2009-07-17 01:31:16 +000059/// CFG construction is done via a recursive walk of an AST. We actually parse
60/// the AST in reverse order so that the successor of a basic block is
61/// constructed prior to its predecessor. This allows us to nicely capture
62/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000063///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000064class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000065 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000066 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000067
Ted Kremenekfddd5182007-08-21 21:42:03 +000068 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000069 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000070 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000071 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000072 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000073 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +000074 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000075
Ted Kremenek19bb3562007-08-28 19:26:49 +000076 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000077 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
78 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000079
80 // A list of blocks that end with a "goto" that must be backpatched to their
81 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000082 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000083 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000084
Ted Kremenek19bb3562007-08-28 19:26:49 +000085 // A list of labels whose address has been taken (for indirect gotos).
86 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
87 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000088
89public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000090 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
91 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000092 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +000093 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
94 TryTerminatedBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +000095
Ted Kremenekd4fdee32007-08-23 21:42:29 +000096 // buildCFG - Used by external clients to construct the CFG.
Mike Stump4c45aa12010-01-21 15:20:48 +000097 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, bool AddEHEdges,
98 bool AddScopes);
Mike Stump6d9828c2009-07-17 01:31:16 +000099
Ted Kremenek4f880632009-07-17 22:18:43 +0000100private:
101 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000102 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
103 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
104 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000105 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000106 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000107 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000108 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000109 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000110 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
111 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000112 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump5d1d2022010-01-19 02:20:09 +0000113 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
Mike Stump0979d802009-07-22 22:56:04 +0000114 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Mike Stump5d1d2022010-01-19 02:20:09 +0000115 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Ted Kremenek4f880632009-07-17 22:18:43 +0000116 CFGBlock *VisitDeclStmt(DeclStmt *DS);
117 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000118 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
119 CFGBlock *VisitDoStmt(DoStmt *D);
120 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000121 CFGBlock *VisitGotoStmt(GotoStmt* G);
122 CFGBlock *VisitIfStmt(IfStmt *I);
123 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
124 CFGBlock *VisitLabelStmt(LabelStmt *L);
125 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
126 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
127 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
128 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
129 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
130 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000131 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
132 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000133 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
134 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000135
Ted Kremenek852274d2009-12-16 03:18:58 +0000136 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
137 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000138 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000139
Ted Kremenek274f4332008-04-28 18:00:46 +0000140 // NYS == Not Yet Supported
141 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000142 badCFG = true;
143 return Block;
144 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000145
Mike Stump079bd722010-01-19 22:00:14 +0000146 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
147 if (!AddScopes)
148 return B;
149
150 if (B == 0)
151 B = createBlock();
152 B->StartScope(S, cfg->getBumpVectorContext());
153 return B;
154 }
155
156 void EndScope(Stmt *S) {
157 if (!AddScopes)
158 return;
159
160 if (Block == 0)
161 Block = createBlock();
162 Block->EndScope(S, cfg->getBumpVectorContext());
163 }
164
Ted Kremenek4f880632009-07-17 22:18:43 +0000165 void autoCreateBlock() { if (!Block) Block = createBlock(); }
166 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000167 bool FinishBlock(CFGBlock* B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000168 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
169 return Visit(S, asc);
170 }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000171
Ted Kremenek852274d2009-12-16 03:18:58 +0000172 void AppendStmt(CFGBlock *B, Stmt *S,
173 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
174 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000175 }
176
177 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
178 B->addSuccessor(S, cfg->getBumpVectorContext());
179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000181 /// TryResult - a class representing a variant over the values
182 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
183 /// and is used by the CFGBuilder to decide if a branch condition
184 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000185 class TryResult {
186 int X;
187 public:
188 TryResult(bool b) : X(b ? 1 : 0) {}
189 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Ted Kremenek941fde82009-07-24 04:47:11 +0000191 bool isTrue() const { return X == 1; }
192 bool isFalse() const { return X == 0; }
193 bool isKnown() const { return X >= 0; }
194 void negate() {
195 assert(isKnown());
196 X ^= 0x1;
197 }
198 };
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Mike Stump00998a02009-07-23 23:25:26 +0000200 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
201 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000202 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000203 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000204 if (!S->isTypeDependent() && !S->isValueDependent() &&
205 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000206 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000207
208 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000209 }
210
Ted Kremenek4102af92008-03-13 03:04:22 +0000211 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000212
213 // True iff EH edges on CallExprs should be added to the CFG.
214 bool AddEHEdges;
215
216 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump079bd722010-01-19 22:00:14 +0000217 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000218};
Mike Stump6d9828c2009-07-17 01:31:16 +0000219
Douglas Gregor898574e2008-12-05 23:32:09 +0000220// FIXME: Add support for dependent-sized array types in C++?
221// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000222static VariableArrayType* FindVA(Type* t) {
223 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
224 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
225 if (vat->getSizeExpr())
226 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000227
Ted Kremenek610a09e2008-09-26 22:58:57 +0000228 t = vt->getElementType().getTypePtr();
229 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000230
Ted Kremenek610a09e2008-09-26 22:58:57 +0000231 return 0;
232}
Mike Stump6d9828c2009-07-17 01:31:16 +0000233
234/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
235/// arbitrary statement. Examples include a single expression or a function
236/// body (compound statement). The ownership of the returned CFG is
237/// transferred to the caller. If CFG construction fails, this method returns
238/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000239CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Mike Stump55f988e2010-01-21 17:21:23 +0000240 bool addehedges, bool AddScopes) {
241 AddEHEdges = addehedges;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000242 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000243 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000244 if (!Statement)
245 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000246
Mike Stump079bd722010-01-19 22:00:14 +0000247 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000248 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000249
250 // Create an empty block that will serve as the exit block for the CFG. Since
251 // this is the first block added to the CFG, it will be implicitly registered
252 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000253 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000254 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000255 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000256
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000257 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000258 CFGBlock* B = addStmt(Statement);
Mike Stumpb978a442010-01-21 02:21:40 +0000259
260 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
261 // FIXME: Add code for base initializers and member initializers.
262 (void)CD;
263 }
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000264 if (!B)
265 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000266
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000267 if (B) {
268 // Finalize the last constructed block. This usually involves reversing the
269 // order of the statements in the block.
270 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000271
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000272 // Backpatch the gotos whose label -> block mappings we didn't know when we
273 // encountered them.
274 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000275 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000276
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000277 CFGBlock* B = *I;
278 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
279 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000280
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000281 // If there is no target for the goto, then we are looking at an
282 // incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000283 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000284
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000285 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000286 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000287
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000288 // Add successors to the Indirect Goto Dispatch block (if we have one).
289 if (CFGBlock* B = cfg->getIndirectGotoBlock())
290 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
291 E = AddressTakenLabels.end(); I != E; ++I ) {
292
293 // Lookup the target block.
294 LabelMapTy::iterator LI = LabelMap.find(*I);
295
296 // If there is no target block that contains label, then we are looking
297 // at an incomplete AST. Handle this by not registering a successor.
298 if (LI == LabelMap.end()) continue;
299
300 AddSuccessor(B, LI->second);
301 }
302
303 Succ = B;
304 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000305
306 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000307 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000308
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000309 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000310}
Mike Stump6d9828c2009-07-17 01:31:16 +0000311
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000312/// createBlock - Used to lazily create blocks that are connected
313/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000314CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000315 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000316 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000317 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000318 return B;
319}
Mike Stump6d9828c2009-07-17 01:31:16 +0000320
Ted Kremenek6c249722009-09-24 18:45:41 +0000321/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000322bool CFGBuilder::FinishBlock(CFGBlock* B) {
323 if (badCFG)
324 return false;
325
Ted Kremenek4f880632009-07-17 22:18:43 +0000326 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000327 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000328}
329
Ted Kremenek4f880632009-07-17 22:18:43 +0000330/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000331/// blocks for ternary operators, &&, and ||. We also process "," and
332/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000333CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000334tryAgain:
335 switch (S->getStmtClass()) {
336 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000337 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000338
339 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000340 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek4f880632009-07-17 22:18:43 +0000342 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000343 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek4f880632009-07-17 22:18:43 +0000345 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000346 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000347
Ted Kremenek4f880632009-07-17 22:18:43 +0000348 case Stmt::BreakStmtClass:
349 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek4f880632009-07-17 22:18:43 +0000351 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000352 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek4f880632009-07-17 22:18:43 +0000354 case Stmt::CaseStmtClass:
355 return VisitCaseStmt(cast<CaseStmt>(S));
356
357 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000358 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek4f880632009-07-17 22:18:43 +0000360 case Stmt::CompoundStmtClass:
361 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek4f880632009-07-17 22:18:43 +0000363 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000364 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek4f880632009-07-17 22:18:43 +0000366 case Stmt::ContinueStmtClass:
367 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek021c8af2010-01-19 20:40:33 +0000369 case Stmt::CXXCatchStmtClass:
370 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
371
372 case Stmt::CXXThrowExprClass:
373 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
374
375 case Stmt::CXXTryStmtClass:
376 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
377
Ted Kremenek4f880632009-07-17 22:18:43 +0000378 case Stmt::DeclStmtClass:
379 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381 case Stmt::DefaultStmtClass:
382 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek4f880632009-07-17 22:18:43 +0000384 case Stmt::DoStmtClass:
385 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek4f880632009-07-17 22:18:43 +0000387 case Stmt::ForStmtClass:
388 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek4f880632009-07-17 22:18:43 +0000390 case Stmt::GotoStmtClass:
391 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek4f880632009-07-17 22:18:43 +0000393 case Stmt::IfStmtClass:
394 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek4f880632009-07-17 22:18:43 +0000396 case Stmt::IndirectGotoStmtClass:
397 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek4f880632009-07-17 22:18:43 +0000399 case Stmt::LabelStmtClass:
400 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek4f880632009-07-17 22:18:43 +0000402 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000403 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
404
Ted Kremenek4f880632009-07-17 22:18:43 +0000405 case Stmt::ObjCAtSynchronizedStmtClass:
406 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek4f880632009-07-17 22:18:43 +0000408 case Stmt::ObjCAtThrowStmtClass:
409 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek4f880632009-07-17 22:18:43 +0000411 case Stmt::ObjCAtTryStmtClass:
412 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremenek4f880632009-07-17 22:18:43 +0000414 case Stmt::ObjCForCollectionStmtClass:
415 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek4f880632009-07-17 22:18:43 +0000417 case Stmt::ParenExprClass:
418 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000419 goto tryAgain;
420
Ted Kremenek4f880632009-07-17 22:18:43 +0000421 case Stmt::NullStmtClass:
422 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 case Stmt::ReturnStmtClass:
425 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek4f880632009-07-17 22:18:43 +0000427 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000428 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek4f880632009-07-17 22:18:43 +0000430 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000431 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek4f880632009-07-17 22:18:43 +0000433 case Stmt::SwitchStmtClass:
434 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Ted Kremenek4f880632009-07-17 22:18:43 +0000436 case Stmt::WhileStmtClass:
437 return VisitWhileStmt(cast<WhileStmt>(S));
438 }
439}
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek852274d2009-12-16 03:18:58 +0000441CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
442 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000443 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000444 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Ted Kremenek4f880632009-07-17 22:18:43 +0000447 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000448}
Mike Stump6d9828c2009-07-17 01:31:16 +0000449
Ted Kremenek4f880632009-07-17 22:18:43 +0000450/// VisitChildren - Visit the children of a Stmt.
451CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
452 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000453 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000454 E = Terminator->child_end(); I != E; ++I) {
455 if (*I) B = Visit(*I);
456 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000457 return B;
458}
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek852274d2009-12-16 03:18:58 +0000460CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
461 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000462 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000463
Ted Kremenek852274d2009-12-16 03:18:58 +0000464 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000465 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000466 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000467 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000468
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000469 return Block;
470}
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenek852274d2009-12-16 03:18:58 +0000472CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
473 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000474 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000475 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000476 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek4f880632009-07-17 22:18:43 +0000478 if (!FinishBlock(ConfluenceBlock))
479 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek4f880632009-07-17 22:18:43 +0000481 // create the block evaluating the LHS
482 CFGBlock* LHSBlock = createBlock(false);
483 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek4f880632009-07-17 22:18:43 +0000485 // create the block evaluating the RHS
486 Succ = ConfluenceBlock;
487 Block = NULL;
488 CFGBlock* RHSBlock = addStmt(B->getRHS());
489 if (!FinishBlock(RHSBlock))
490 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Mike Stump00998a02009-07-23 23:25:26 +0000492 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000493 TryResult KnownVal = TryEvaluateBool(B->getLHS());
494 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
495 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000496
Ted Kremenek4f880632009-07-17 22:18:43 +0000497 // Now link the LHSBlock with RHSBlock.
498 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000499 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
500 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000501 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000502 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000503 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
504 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Ted Kremenek4f880632009-07-17 22:18:43 +0000507 // Generate the blocks for evaluating the LHS.
508 Block = LHSBlock;
509 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000510 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000511 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000512 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000513 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000514 addStmt(B->getRHS());
515 return addStmt(B->getLHS());
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Ted Kremenek852274d2009-12-16 03:18:58 +0000518 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000519}
520
Ted Kremenek852274d2009-12-16 03:18:58 +0000521CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
522 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000523 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000524 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000525 }
526 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000527}
528
Ted Kremenek4f880632009-07-17 22:18:43 +0000529CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
530 // "break" is a control-flow statement. Thus we stop processing the current
531 // block.
532 if (Block && !FinishBlock(Block))
533 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Ted Kremenek4f880632009-07-17 22:18:43 +0000535 // Now create a new block that ends with the break statement.
536 Block = createBlock(false);
537 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek4f880632009-07-17 22:18:43 +0000539 // If there is no target for the break, then we are looking at an incomplete
540 // AST. This means that the CFG cannot be constructed.
541 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000542 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000543 else
544 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
546
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000547 return Block;
548}
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Mike Stump4c45aa12010-01-21 15:20:48 +0000550static bool CanThrow(Expr *E) {
551 QualType Ty = E->getType();
552 if (Ty->isFunctionPointerType())
553 Ty = Ty->getAs<PointerType>()->getPointeeType();
554 else if (Ty->isBlockPointerType())
555 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
556
557 const FunctionType *FT = Ty->getAs<FunctionType>();
558 if (FT) {
559 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
560 if (Proto->hasEmptyExceptionSpec())
561 return false;
562 }
563 return true;
564}
565
Ted Kremenek852274d2009-12-16 03:18:58 +0000566CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000567 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000568 bool NoReturn = false;
569 if (C->getCallee()->getType().getNoReturnAttr()) {
570 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000571 }
Mike Stump24556362009-07-25 21:26:53 +0000572
Mike Stump4c45aa12010-01-21 15:20:48 +0000573 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000574
575 // Languages without exceptions are assumed to not throw.
576 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000577 if (AddEHEdges)
578 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000579 }
580
581 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000582 if (FD->hasAttr<NoReturnAttr>())
583 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000584 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000585 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000586 }
Mike Stump24556362009-07-25 21:26:53 +0000587
Mike Stump4c45aa12010-01-21 15:20:48 +0000588 if (!CanThrow(C->getCallee()))
589 AddEHEdge = false;
590
591 if (!NoReturn && !AddEHEdge)
Zhongxing Xu91071662010-02-24 02:19:28 +0000592 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Mike Stump079bd722010-01-19 22:00:14 +0000594 if (Block) {
595 Succ = Block;
596 if (!FinishBlock(Block))
597 return 0;
598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Mike Stump079bd722010-01-19 22:00:14 +0000600 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000601 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000602
Mike Stump079bd722010-01-19 22:00:14 +0000603 if (NoReturn) {
604 // Wire this to the exit block directly.
605 AddSuccessor(Block, &cfg->getExit());
606 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000607 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000608 // Add exceptional edges.
609 if (TryTerminatedBlock)
610 AddSuccessor(Block, TryTerminatedBlock);
611 else
612 AddSuccessor(Block, &cfg->getExit());
613 }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Mike Stump24556362009-07-25 21:26:53 +0000615 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000616}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000617
Ted Kremenek852274d2009-12-16 03:18:58 +0000618CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
619 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000620 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000621 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000622 if (!FinishBlock(ConfluenceBlock))
623 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000625 Succ = ConfluenceBlock;
626 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000627 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000628 if (!FinishBlock(LHSBlock))
629 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000631 Succ = ConfluenceBlock;
632 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000633 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000634 if (!FinishBlock(RHSBlock))
635 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000637 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000638 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000639 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000640 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
641 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000642 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000643 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000644}
Mike Stump1eb44332009-09-09 15:08:12 +0000645
646
647CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000648 EndScope(C);
649
Mike Stump1eb44332009-09-09 15:08:12 +0000650 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000651
652 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
653 I != E; ++I ) {
654 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000656 if (badCFG)
657 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000658 }
Mike Stump079bd722010-01-19 22:00:14 +0000659
660 LastBlock = StartScope(C, LastBlock);
661
Ted Kremenek4f880632009-07-17 22:18:43 +0000662 return LastBlock;
663}
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Ted Kremenek852274d2009-12-16 03:18:58 +0000665CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
666 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000667 // Create the confluence block that will "merge" the results of the ternary
668 // expression.
669 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000670 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000671 if (!FinishBlock(ConfluenceBlock))
672 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000674 // Create a block for the LHS expression if there is an LHS expression. A
675 // GCC extension allows LHS to be NULL, causing the condition to be the
676 // value that is returned instead.
677 // e.g: x ?: y is shorthand for: x ? x : y;
678 Succ = ConfluenceBlock;
679 Block = NULL;
680 CFGBlock* LHSBlock = NULL;
681 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000682 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000683 if (!FinishBlock(LHSBlock))
684 return 0;
685 Block = NULL;
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000688 // Create the block for the RHS expression.
689 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000690 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000691 if (!FinishBlock(RHSBlock))
692 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000694 // Create the block that will contain the condition.
695 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Mike Stump00998a02009-07-23 23:25:26 +0000697 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000698 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000699 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000700 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000701 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000702 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000703 // If we know the condition is false, add NULL as the successor for
704 // the block containing the condition. In this case, the confluence
705 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000706 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000707 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000708 } else {
709 // If we have no LHS expression, add the ConfluenceBlock as a direct
710 // successor for the block containing the condition. Moreover, we need to
711 // reverse the order of the predecessors in the ConfluenceBlock because
712 // the RHSBlock will have been added to the succcessors already, and we
713 // want the first predecessor to the the block containing the expression
714 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000715 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000716 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000717 std::reverse(ConfluenceBlock->pred_begin(),
718 ConfluenceBlock->pred_end());
719 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000722 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000723 Block->setTerminator(C);
724 return addStmt(C->getCond());
725}
726
Ted Kremenek4f880632009-07-17 22:18:43 +0000727CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
728 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000729
Ted Kremenek4f880632009-07-17 22:18:43 +0000730 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000731 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000732 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek4f880632009-07-17 22:18:43 +0000735 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek4f880632009-07-17 22:18:43 +0000737 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
738 typedef llvm::SmallVector<Decl*,10> BufTy;
739 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek4f880632009-07-17 22:18:43 +0000741 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
742 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
743 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
744 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek4f880632009-07-17 22:18:43 +0000746 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
747 // automatically freed with the CFG.
748 DeclGroupRef DG(*I);
749 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000750 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000751 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenek4f880632009-07-17 22:18:43 +0000753 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000754 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000755 B = VisitDeclSubExpr(D);
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
758 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000759}
Mike Stump1eb44332009-09-09 15:08:12 +0000760
Ted Kremenek4f880632009-07-17 22:18:43 +0000761/// VisitDeclSubExpr - Utility method to add block-level expressions for
762/// initializers in Decls.
763CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
764 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000765
Ted Kremenek4f880632009-07-17 22:18:43 +0000766 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek4f880632009-07-17 22:18:43 +0000768 if (!VD)
769 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Ted Kremenek4f880632009-07-17 22:18:43 +0000771 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek4f880632009-07-17 22:18:43 +0000773 if (Init) {
774 // Optimization: Don't create separate block-level statements for literals.
775 switch (Init->getStmtClass()) {
776 case Stmt::IntegerLiteralClass:
777 case Stmt::CharacterLiteralClass:
778 case Stmt::StringLiteralClass:
779 break;
780 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000781 Block = addStmt(Init,
782 VD->getType()->isReferenceType()
783 ? AddStmtChoice::AlwaysAddAsLValue
784 : AddStmtChoice::AlwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000785 }
786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenek4f880632009-07-17 22:18:43 +0000788 // If the type of VD is a VLA, then we must process its size expressions.
789 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
790 VA = FindVA(VA->getElementType().getTypePtr()))
791 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Ted Kremenek4f880632009-07-17 22:18:43 +0000793 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000794}
795
796CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000797 // We may see an if statement in the middle of a basic block, or it may be the
798 // first statement we are processing. In either case, we create a new basic
799 // block. First, we create the blocks for the then...else statements, and
800 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000801 // middle of a block, we stop processing that block. That block is then the
802 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000803
804 // The block we were proccessing is now finished. Make it the successor
805 // block.
806 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000807 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000808 if (!FinishBlock(Block))
809 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000810 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000811
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000812 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000813 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000814
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000815 if (Stmt* Else = I->getElse()) {
816 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000817
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000818 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000819 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000820 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000821 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000822
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000823 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
824 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000825 else if (Block) {
826 if (!FinishBlock(ElseBlock))
827 return 0;
828 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000829 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000830
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000831 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000832 CFGBlock* ThenBlock;
833 {
834 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000835 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000836 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000837 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000838 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000839
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000840 if (!ThenBlock) {
841 // We can reach here if the "then" body has all NullStmts.
842 // Create an empty block so we can distinguish between true and false
843 // branches in path-sensitive analyses.
844 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000845 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000846 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000847 if (!FinishBlock(ThenBlock))
848 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000849 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000850 }
851
Mike Stump6d9828c2009-07-17 01:31:16 +0000852 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000853 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000854
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000855 // Set the terminator of the new block to the If statement.
856 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000857
Mike Stump00998a02009-07-23 23:25:26 +0000858 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000859 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000860
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000861 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000862 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
863 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000864
865 // Add the condition as the last statement in the new block. This may create
866 // new blocks as the condition may contain control-flow. Any newly created
867 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000868 Block = addStmt(I->getCond());
869
870 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
871 // and the condition variable initialization to the CFG.
872 if (VarDecl *VD = I->getConditionVariable()) {
873 if (Expr *Init = VD->getInit()) {
874 autoCreateBlock();
875 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
876 addStmt(Init);
877 }
878 }
879
880 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000881}
Mike Stump6d9828c2009-07-17 01:31:16 +0000882
883
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000884CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000885 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000886 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000887 // NOTE: If a "return" appears in the middle of a block, this means that the
888 // code afterwards is DEAD (unreachable). We still keep a basic block
889 // for that code; a simple "mark-and-sweep" from the entry block will be
890 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000891 if (Block)
892 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000893
894 // Create the new block.
895 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000896
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000897 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000898 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000899
900 // Add the return statement to the block. This may create new blocks if R
901 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000902 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000903}
904
905CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
906 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000907 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000908 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000909
Ted Kremenek4f880632009-07-17 22:18:43 +0000910 if (!LabelBlock) // This can happen when the body is empty, i.e.
911 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000912
Ted Kremenek4f880632009-07-17 22:18:43 +0000913 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000914 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000915
916 // Labels partition blocks, so this is the end of the basic block we were
917 // processing (L is the block's label). Because this is label (and we have
918 // already processed the substatement) there is no extra control-flow to worry
919 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000920 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000921 if (!FinishBlock(LabelBlock))
922 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000923
924 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000925 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000926
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000927 // This block is now the implicit successor of other blocks.
928 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000929
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000930 return LabelBlock;
931}
932
933CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000934 // Goto is a control-flow statement. Thus we stop processing the current
935 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000936 if (Block)
937 FinishBlock(Block);
938
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000939 Block = createBlock(false);
940 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000941
942 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000943 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000944
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000945 if (I == LabelMap.end())
946 // We will need to backpatch this block later.
947 BackpatchBlocks.push_back(Block);
948 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000949 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000950
Mike Stump6d9828c2009-07-17 01:31:16 +0000951 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000952}
953
954CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000955 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000956
Mike Stumpfefb9f72009-07-21 01:12:51 +0000957 // "for" is a control-flow statement. Thus we stop processing the current
958 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000959 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000960 if (!FinishBlock(Block))
961 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000962 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000963 } else
964 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000965
966 // Because of short-circuit evaluation, the condition of the loop can span
967 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
968 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000969 CFGBlock* ExitConditionBlock = createBlock(false);
970 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000971
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000972 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000973 ExitConditionBlock->setTerminator(F);
974
975 // Now add the actual condition to the condition block. Because the condition
976 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000977 if (Stmt* C = F->getCond()) {
978 Block = ExitConditionBlock;
979 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000980 assert(Block == EntryConditionBlock);
981
982 // If this block contains a condition variable, add both the condition
983 // variable and initializer to the CFG.
984 if (VarDecl *VD = F->getConditionVariable()) {
985 if (Expr *Init = VD->getInit()) {
986 autoCreateBlock();
987 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
988 EntryConditionBlock = addStmt(Init);
989 assert(Block == EntryConditionBlock);
990 }
991 }
992
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000993 if (Block) {
994 if (!FinishBlock(EntryConditionBlock))
995 return 0;
996 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000997 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000998
Mike Stump6d9828c2009-07-17 01:31:16 +0000999 // The condition block is the implicit successor for the loop body as well as
1000 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001001 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001002
Mike Stump00998a02009-07-23 23:25:26 +00001003 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001004 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Mike Stump00998a02009-07-23 23:25:26 +00001006 if (F->getCond())
1007 KnownVal = TryEvaluateBool(F->getCond());
1008
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001009 // Now create the loop body.
1010 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001011 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001012
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001013 // Save the current values for Block, Succ, and continue and break targets
1014 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001015 save_continue(ContinueTargetBlock),
1016 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001017
Ted Kremenekaf603f72007-08-30 18:39:40 +00001018 // Create a new block to contain the (bottom) of the loop body.
1019 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001020
Ted Kremeneke9334502008-09-04 21:48:47 +00001021 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001022 // Generate increment code in its own basic block. This is the target of
1023 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001024 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001025 } else {
1026 // No increment code. Create a special, empty, block that is used as the
1027 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001028 assert(Succ == EntryConditionBlock);
1029 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001030 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001031
Ted Kremenek3575f842009-04-28 00:51:56 +00001032 // Finish up the increment (or empty) block if it hasn't been already.
1033 if (Block) {
1034 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001035 if (!FinishBlock(Block))
1036 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001037 Block = 0;
1038 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001039
Ted Kremenek3575f842009-04-28 00:51:56 +00001040 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001041
Ted Kremenek3575f842009-04-28 00:51:56 +00001042 // The starting block for the loop increment is the block that should
1043 // represent the 'loop target' for looping back to the start of the loop.
1044 ContinueTargetBlock->setLoopTarget(F);
1045
Ted Kremeneke9334502008-09-04 21:48:47 +00001046 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +00001047 BreakTargetBlock = LoopSuccessor;
1048
1049 // Now populate the body block, and in the process create new blocks as we
1050 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001051 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001052
1053 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001054 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001055 else if (Block && !FinishBlock(BodyBlock))
1056 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001057
Ted Kremenek941fde82009-07-24 04:47:11 +00001058 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001059 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001060 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001061
Ted Kremenek941fde82009-07-24 04:47:11 +00001062 // Link up the condition block with the code that follows the loop. (the
1063 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001064 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001065
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001066 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001067 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001068 if (Stmt* I = F->getInit()) {
1069 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001070 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001071 } else {
1072 // There is no loop initialization. We are thus basically a while loop.
1073 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001074 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001075 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001076 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001077 }
1078}
1079
Ted Kremenek514de5a2008-11-11 17:10:00 +00001080CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1081 // Objective-C fast enumeration 'for' statements:
1082 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1083 //
1084 // for ( Type newVariable in collection_expression ) { statements }
1085 //
1086 // becomes:
1087 //
1088 // prologue:
1089 // 1. collection_expression
1090 // T. jump to loop_entry
1091 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001092 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001093 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1094 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1095 // TB:
1096 // statements
1097 // T. jump to loop_entry
1098 // FB:
1099 // what comes after
1100 //
1101 // and
1102 //
1103 // Type existingItem;
1104 // for ( existingItem in expression ) { statements }
1105 //
1106 // becomes:
1107 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001108 // the same with newVariable replaced with existingItem; the binding works
1109 // the same except that for one ObjCForCollectionStmt::getElement() returns
1110 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001111 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001112
Ted Kremenek514de5a2008-11-11 17:10:00 +00001113 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001114
Ted Kremenek514de5a2008-11-11 17:10:00 +00001115 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001116 if (!FinishBlock(Block))
1117 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001118 LoopSuccessor = Block;
1119 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001120 } else
1121 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001122
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001123 // Build the condition blocks.
1124 CFGBlock* ExitConditionBlock = createBlock(false);
1125 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001126
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001127 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001128 ExitConditionBlock->setTerminator(S);
1129
1130 // The last statement in the block should be the ObjCForCollectionStmt, which
1131 // performs the actual binding to 'element' and determines if there are any
1132 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001133 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001134 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001135
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001136 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001137 // generate new blocks as necesary. We DON'T add the statement by default to
1138 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001139 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001140 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001141 if (!FinishBlock(EntryConditionBlock))
1142 return 0;
1143 Block = 0;
1144 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001145
1146 // The condition block is the implicit successor for the loop body as well as
1147 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001148 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001149
Ted Kremenek514de5a2008-11-11 17:10:00 +00001150 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001151 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001152 // Save the current values for Succ, continue and break targets.
1153 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001154 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1155
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001156 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001157 ContinueTargetBlock = EntryConditionBlock;
1158
Ted Kremenek4f880632009-07-17 22:18:43 +00001159 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001160
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001161 if (!BodyBlock)
1162 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001163 else if (Block) {
1164 if (!FinishBlock(BodyBlock))
1165 return 0;
1166 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001167
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001168 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001169 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001170 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001171
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001172 // Link up the condition block with the code that follows the loop.
1173 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001174 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001175
Ted Kremenek514de5a2008-11-11 17:10:00 +00001176 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001177 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001178 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001179}
1180
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001181CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1182 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001183
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001184 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001185 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001186
Ted Kremenekda5348e2009-05-05 23:11:51 +00001187 // The sync body starts its own basic block. This makes it a little easier
1188 // for diagnostic clients.
1189 if (SyncBlock) {
1190 if (!FinishBlock(SyncBlock))
1191 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001192
Ted Kremenekda5348e2009-05-05 23:11:51 +00001193 Block = 0;
1194 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001195
Ted Kremenekda5348e2009-05-05 23:11:51 +00001196 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001197
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001198 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001199 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001200}
Mike Stump6d9828c2009-07-17 01:31:16 +00001201
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001202CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001203 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001204 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001205}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001206
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001207CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001208 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001209
Mike Stumpfefb9f72009-07-21 01:12:51 +00001210 // "while" is a control-flow statement. Thus we stop processing the current
1211 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001212 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001213 if (!FinishBlock(Block))
1214 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001215 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001216 } else
1217 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001218
1219 // Because of short-circuit evaluation, the condition of the loop can span
1220 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1221 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001222 CFGBlock* ExitConditionBlock = createBlock(false);
1223 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001224
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001225 // Set the terminator for the "exit" condition block.
1226 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001227
1228 // Now add the actual condition to the condition block. Because the condition
1229 // itself may contain control-flow, new blocks may be created. Thus we update
1230 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001231 if (Stmt* C = W->getCond()) {
1232 Block = ExitConditionBlock;
1233 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001234 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001235
1236 // If this block contains a condition variable, add both the condition
1237 // variable and initializer to the CFG.
1238 if (VarDecl *VD = W->getConditionVariable()) {
1239 if (Expr *Init = VD->getInit()) {
1240 autoCreateBlock();
1241 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1242 EntryConditionBlock = addStmt(Init);
1243 assert(Block == EntryConditionBlock);
1244 }
1245 }
1246
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001247 if (Block) {
1248 if (!FinishBlock(EntryConditionBlock))
1249 return 0;
1250 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001251 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
1253 // The condition block is the implicit successor for the loop body as well as
1254 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001255 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001256
Mike Stump00998a02009-07-23 23:25:26 +00001257 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001258 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001259
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001260 // Process the loop body.
1261 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001262 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001263
1264 // Save the current values for Block, Succ, and continue and break targets
1265 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1266 save_continue(ContinueTargetBlock),
1267 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001268
Mike Stump6d9828c2009-07-17 01:31:16 +00001269 // Create an empty block to represent the transition block for looping back
1270 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001271 Block = 0;
1272 assert(Succ == EntryConditionBlock);
1273 Succ = createBlock();
1274 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001275 ContinueTargetBlock = Succ;
1276
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001277 // All breaks should go to the code following the loop.
1278 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001279
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 // NULL out Block to force lazy instantiation of blocks for the body.
1281 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001282
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001283 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001284 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001285
Ted Kremenekaf603f72007-08-30 18:39:40 +00001286 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001287 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001288 else if (Block) {
1289 if (!FinishBlock(BodyBlock))
1290 return 0;
1291 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
Ted Kremenek941fde82009-07-24 04:47:11 +00001293 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001294 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001295 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001296
Ted Kremenek941fde82009-07-24 04:47:11 +00001297 // Link up the condition block with the code that follows the loop. (the
1298 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001299 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001300
1301 // There can be no more statements in the condition block since we loop back
1302 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001303 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001304
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001305 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001306 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001307 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001308}
Mike Stump1eb44332009-09-09 15:08:12 +00001309
1310
Ted Kremenek4f880632009-07-17 22:18:43 +00001311CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1312 // FIXME: For now we pretend that @catch and the code it contains does not
1313 // exit.
1314 return Block;
1315}
Mike Stump6d9828c2009-07-17 01:31:16 +00001316
Ted Kremenek2fda5042008-12-09 20:20:09 +00001317CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1318 // FIXME: This isn't complete. We basically treat @throw like a return
1319 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
Ted Kremenek6c249722009-09-24 18:45:41 +00001321 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001322 if (Block && !FinishBlock(Block))
1323 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
Ted Kremenek2fda5042008-12-09 20:20:09 +00001325 // Create the new block.
1326 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001327
Ted Kremenek2fda5042008-12-09 20:20:09 +00001328 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001329 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001330
1331 // Add the statement to the block. This may create new blocks if S contains
1332 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001333 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001334}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001335
Mike Stump0979d802009-07-22 22:56:04 +00001336CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001337 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001338 if (Block && !FinishBlock(Block))
1339 return 0;
1340
1341 // Create the new block.
1342 Block = createBlock(false);
1343
Mike Stump5d1d2022010-01-19 02:20:09 +00001344 if (TryTerminatedBlock)
1345 // The current try statement is the only successor.
1346 AddSuccessor(Block, TryTerminatedBlock);
1347 else
1348 // otherwise the Exit block is the only successor.
1349 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001350
1351 // Add the statement to the block. This may create new blocks if S contains
1352 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001353 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001354}
1355
Ted Kremenek4f880632009-07-17 22:18:43 +00001356CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001357 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001358
Mike Stump8f9893a2009-07-21 01:27:50 +00001359 // "do...while" is a control-flow statement. Thus we stop processing the
1360 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001361 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001362 if (!FinishBlock(Block))
1363 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001364 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001365 } else
1366 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001367
1368 // Because of short-circuit evaluation, the condition of the loop can span
1369 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1370 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001371 CFGBlock* ExitConditionBlock = createBlock(false);
1372 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001373
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001374 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001375 ExitConditionBlock->setTerminator(D);
1376
1377 // Now add the actual condition to the condition block. Because the condition
1378 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001379 if (Stmt* C = D->getCond()) {
1380 Block = ExitConditionBlock;
1381 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001382 if (Block) {
1383 if (!FinishBlock(EntryConditionBlock))
1384 return 0;
1385 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001386 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001387
Ted Kremenek54827132008-02-27 07:20:00 +00001388 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001389 Succ = EntryConditionBlock;
1390
Mike Stump00998a02009-07-23 23:25:26 +00001391 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001392 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001393
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001394 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001395 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001396 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001397 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001398
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001399 // Save the current values for Block, Succ, and continue and break targets
1400 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001401 save_continue(ContinueTargetBlock),
1402 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001403
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001404 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001405 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001406
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001407 // All breaks should go to the code following the loop.
1408 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001409
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001410 // NULL out Block to force lazy instantiation of blocks for the body.
1411 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001412
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001413 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001414 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001415
Ted Kremenekaf603f72007-08-30 18:39:40 +00001416 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001417 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001418 else if (Block) {
1419 if (!FinishBlock(BodyBlock))
1420 return 0;
1421 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001422
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001423 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001424 // ExitConditionBlock to represent the "loop back" transition. Create an
1425 // empty block to represent the transition block for looping back to the
1426 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001427 // FIXME: Can we do this more efficiently without adding another block?
1428 Block = NULL;
1429 Succ = BodyBlock;
1430 CFGBlock *LoopBackBlock = createBlock();
1431 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenek941fde82009-07-24 04:47:11 +00001433 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001434 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001435 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001436
Ted Kremenek941fde82009-07-24 04:47:11 +00001437 // Link up the condition block with the code that follows the loop.
1438 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001439 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001440
1441 // There can be no more statements in the body block(s) since we loop back to
1442 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001443 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001445 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001446 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001447 return BodyBlock;
1448}
1449
1450CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1451 // "continue" is a control-flow statement. Thus we stop processing the
1452 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001453 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001454 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001455
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001456 // Now create a new block that ends with the continue statement.
1457 Block = createBlock(false);
1458 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001459
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001460 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001461 // incomplete AST. This means the CFG cannot be constructed.
1462 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001463 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001464 else
1465 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001466
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001467 return Block;
1468}
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001470CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001471 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001472
Ted Kremenek852274d2009-12-16 03:18:58 +00001473 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001474 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001475 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001476 }
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Ted Kremenek4f880632009-07-17 22:18:43 +00001478 // VLA types have expressions that must be evaluated.
1479 if (E->isArgumentType()) {
1480 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1481 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1482 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001483 }
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Mike Stump6d9828c2009-07-17 01:31:16 +00001485 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001486}
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek4f880632009-07-17 22:18:43 +00001488/// VisitStmtExpr - Utility method to handle (nested) statement
1489/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001490CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1491 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001492 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001493 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001494 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001495 return VisitCompoundStmt(SE->getSubStmt());
1496}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001497
Ted Kremenek411cdee2008-04-16 21:10:48 +00001498CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001499 // "switch" is a control-flow statement. Thus we stop processing the current
1500 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001501 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001502
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001503 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001504 if (!FinishBlock(Block))
1505 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001506 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001507 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001508
1509 // Save the current "switch" context.
1510 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001511 save_break(BreakTargetBlock),
1512 save_default(DefaultCaseBlock);
1513
Mike Stump6d9828c2009-07-17 01:31:16 +00001514 // Set the "default" case to be the block after the switch statement. If the
1515 // switch statement contains a "default:", this value will be overwritten with
1516 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001517 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001518
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001519 // Create a new block that will contain the switch statement.
1520 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001521
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001522 // Now process the switch body. The code after the switch is the implicit
1523 // successor.
1524 Succ = SwitchSuccessor;
1525 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001526
1527 // When visiting the body, the case statements should automatically get linked
1528 // up to the switch. We also don't keep a pointer to the body, since all
1529 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001530 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001531 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001532 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001533 if (Block) {
1534 if (!FinishBlock(BodyBlock))
1535 return 0;
1536 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001537
Mike Stump6d9828c2009-07-17 01:31:16 +00001538 // If we have no "default:" case, the default transition is to the code
1539 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001540 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001541
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001542 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001543 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001544 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001545 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001546 Block = addStmt(Terminator->getCond());
1547
1548 // Finally, if the SwitchStmt contains a condition variable, add both the
1549 // SwitchStmt and the condition variable initialization to the CFG.
1550 if (VarDecl *VD = Terminator->getConditionVariable()) {
1551 if (Expr *Init = VD->getInit()) {
1552 autoCreateBlock();
1553 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1554 addStmt(Init);
1555 }
1556 }
1557
1558 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001559}
1560
Ted Kremenek4f880632009-07-17 22:18:43 +00001561CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001562 // CaseStmts are essentially labels, so they are the first statement in a
1563 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001564
Ted Kremenek4f880632009-07-17 22:18:43 +00001565 if (CS->getSubStmt())
1566 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001568 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001569 if (!CaseBlock)
1570 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001571
1572 // Cases statements partition blocks, so this is the top of the basic block we
1573 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001574 CaseBlock->setLabel(CS);
1575
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001576 if (!FinishBlock(CaseBlock))
1577 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001578
1579 // Add this block to the list of successors for the block with the switch
1580 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001581 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001582 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001583
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001584 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1585 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001586
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001587 // This block is now the implicit successor of other blocks.
1588 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Ted Kremenek2677ea82008-03-15 07:45:02 +00001590 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001591}
Mike Stump6d9828c2009-07-17 01:31:16 +00001592
Ted Kremenek411cdee2008-04-16 21:10:48 +00001593CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001594 if (Terminator->getSubStmt())
1595 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001597 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001598
1599 if (!DefaultCaseBlock)
1600 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001601
1602 // Default statements partition blocks, so this is the top of the basic block
1603 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001604 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001606 if (!FinishBlock(DefaultCaseBlock))
1607 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001608
Mike Stump6d9828c2009-07-17 01:31:16 +00001609 // Unlike case statements, we don't add the default block to the successors
1610 // for the switch statement immediately. This is done when we finish
1611 // processing the switch statement. This allows for the default case
1612 // (including a fall-through to the code after the switch statement) to always
1613 // be the last successor of a switch-terminated block.
1614
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001615 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1616 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001618 // This block is now the implicit successor of other blocks.
1619 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
1621 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001622}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001623
Mike Stump5d1d2022010-01-19 02:20:09 +00001624CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1625 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1626 // current block.
1627 CFGBlock* TrySuccessor = NULL;
1628
1629 if (Block) {
1630 if (!FinishBlock(Block))
1631 return 0;
1632 TrySuccessor = Block;
1633 } else TrySuccessor = Succ;
1634
Mike Stumpa1f93632010-01-20 01:15:34 +00001635 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001636
1637 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001638 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001639 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001640 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001641
Mike Stumpa1f93632010-01-20 01:15:34 +00001642 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001643 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1644 // The code after the try is the implicit successor.
1645 Succ = TrySuccessor;
1646 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001647 if (CS->getExceptionDecl() == 0) {
1648 HasCatchAll = true;
1649 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001650 Block = NULL;
1651 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1652 if (CatchBlock == 0)
1653 return 0;
1654 // Add this block to the list of successors for the block with the try
1655 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001656 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001657 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001658 if (!HasCatchAll) {
1659 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001660 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001661 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001662 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001663 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001664
1665 // The code after the try is the implicit successor.
1666 Succ = TrySuccessor;
1667
Mike Stumpf00cca52010-01-20 01:30:58 +00001668 // Save the current "try" context.
1669 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1670 TryTerminatedBlock = NewTryTerminatedBlock;
1671
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001672 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001673 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001674 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001675 return Block;
1676}
1677
1678CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1679 // CXXCatchStmt are treated like labels, so they are the first statement in a
1680 // block.
1681
1682 if (CS->getHandlerBlock())
1683 addStmt(CS->getHandlerBlock());
1684
1685 CFGBlock* CatchBlock = Block;
1686 if (!CatchBlock)
1687 CatchBlock = createBlock();
1688
1689 CatchBlock->setLabel(CS);
1690
1691 if (!FinishBlock(CatchBlock))
1692 return 0;
1693
1694 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1695 Block = NULL;
1696
1697 return CatchBlock;
1698}
1699
Ted Kremenek19bb3562007-08-28 19:26:49 +00001700CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001701 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001702 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001703
Ted Kremenek19bb3562007-08-28 19:26:49 +00001704 if (!IBlock) {
1705 IBlock = createBlock(false);
1706 cfg->setIndirectGotoBlock(IBlock);
1707 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001708
Ted Kremenek19bb3562007-08-28 19:26:49 +00001709 // IndirectGoto is a control-flow statement. Thus we stop processing the
1710 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001711 if (Block && !FinishBlock(Block))
1712 return 0;
1713
Ted Kremenek19bb3562007-08-28 19:26:49 +00001714 Block = createBlock(false);
1715 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001716 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001717 return addStmt(I->getTarget());
1718}
1719
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001720} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001721
Mike Stump6d9828c2009-07-17 01:31:16 +00001722/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1723/// no successors or predecessors. If this is the first block created in the
1724/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001725CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001726 bool first_block = begin() == end();
1727
1728 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001729 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1730 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1731 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001732
1733 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001734 if (first_block)
1735 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001736
1737 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001738 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001739}
1740
Ted Kremenek026473c2007-08-23 16:51:22 +00001741/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1742/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001743CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump4c45aa12010-01-21 15:20:48 +00001744 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001745 CFGBuilder Builder;
Mike Stump4c45aa12010-01-21 15:20:48 +00001746 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001747}
1748
Ted Kremenek63f58872007-10-01 19:33:33 +00001749//===----------------------------------------------------------------------===//
1750// CFG: Queries for BlkExprs.
1751//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001752
Ted Kremenek63f58872007-10-01 19:33:33 +00001753namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001754 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001755}
1756
Ted Kremenek8a693662009-12-23 23:37:10 +00001757static void FindSubExprAssignments(Stmt *S,
1758 llvm::SmallPtrSet<Expr*,50>& Set) {
1759 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001760 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001761
Ted Kremenek8a693662009-12-23 23:37:10 +00001762 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1763 Stmt *child = *I;
1764 if (!child)
1765 continue;
1766
1767 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001768 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001769
Ted Kremenek8a693662009-12-23 23:37:10 +00001770 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001771 }
1772}
1773
Ted Kremenek63f58872007-10-01 19:33:33 +00001774static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1775 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001776
1777 // Look for assignments that are used as subexpressions. These are the only
1778 // assignments that we want to *possibly* register as a block-level
1779 // expression. Basically, if an assignment occurs both in a subexpression and
1780 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001781 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001782
Ted Kremenek63f58872007-10-01 19:33:33 +00001783 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001784 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001785 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001786
Ted Kremenek411cdee2008-04-16 21:10:48 +00001787 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001788
1789 // Iterate over the statements again on identify the Expr* and Stmt* at the
1790 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001791
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001792 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001793 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001794
Ted Kremenek411cdee2008-04-16 21:10:48 +00001795 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001796 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001797 // expression are really "statements" whose value is never used by
1798 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001799 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001800 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001801 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1802 // Special handling for statement expressions. The last statement in
1803 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001804 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001805 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001806 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001807 (*M)[C->body_back()] = x;
1808 }
1809 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001810
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001811 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001812 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001813 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001814
Ted Kremenek411cdee2008-04-16 21:10:48 +00001815 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001816
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001817 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001818
Ted Kremenek390e48b2008-11-12 21:11:49 +00001819 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001820 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001821 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001822 }
1823 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001824
Ted Kremenek63f58872007-10-01 19:33:33 +00001825 return M;
1826}
1827
Ted Kremenek86946742008-01-17 20:48:37 +00001828CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1829 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001830 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001831
Ted Kremenek63f58872007-10-01 19:33:33 +00001832 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001833 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001834 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001835}
1836
1837unsigned CFG::getNumBlkExprs() {
1838 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1839 return M->size();
1840 else {
1841 // We assume callers interested in the number of BlkExprs will want
1842 // the map constructed if it doesn't already exist.
1843 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1844 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1845 }
1846}
1847
Ted Kremenek274f4332008-04-28 18:00:46 +00001848//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001849// Cleanup: CFG dstor.
1850//===----------------------------------------------------------------------===//
1851
Ted Kremenek63f58872007-10-01 19:33:33 +00001852CFG::~CFG() {
1853 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1854}
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
Ted Kremenek7dba8602007-08-29 21:56:09 +00001856//===----------------------------------------------------------------------===//
1857// CFG pretty printing
1858//===----------------------------------------------------------------------===//
1859
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001860namespace {
1861
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001862class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001863 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1864 StmtMapTy StmtMap;
1865 signed CurrentBlock;
1866 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001867 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001868public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001869
Chris Lattnere4f21422009-06-30 01:26:17 +00001870 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1871 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001872 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1873 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001874 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001875 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001876 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001877 }
1878 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001879
Ted Kremenek42a509f2007-08-31 21:30:12 +00001880 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Chris Lattnere4f21422009-06-30 01:26:17 +00001882 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001883 void setBlockID(signed i) { CurrentBlock = i; }
1884 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001885
Ted Kremeneka95d3752008-09-13 05:16:45 +00001886 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001887
Ted Kremenek411cdee2008-04-16 21:10:48 +00001888 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001889
1890 if (I == StmtMap.end())
1891 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001892
1893 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001894 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001895 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001896 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001897
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001898 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001899 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001900 }
1901};
Chris Lattnere4f21422009-06-30 01:26:17 +00001902} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001903
Chris Lattnere4f21422009-06-30 01:26:17 +00001904
1905namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001906class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001907 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001908
Ted Kremeneka95d3752008-09-13 05:16:45 +00001909 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001910 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001911 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001912public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001913 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001914 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001915 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001917 void VisitIfStmt(IfStmt* I) {
1918 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001919 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001920 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001921
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001922 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001923 void VisitStmt(Stmt* Terminator) {
1924 Terminator->printPretty(OS, Helper, Policy);
1925 }
1926
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001927 void VisitForStmt(ForStmt* F) {
1928 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001929 if (F->getInit())
1930 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001931 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001932 if (Stmt* C = F->getCond())
1933 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001934 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001935 if (F->getInc())
1936 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001937 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001938 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001939
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001940 void VisitWhileStmt(WhileStmt* W) {
1941 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001942 if (Stmt* C = W->getCond())
1943 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001944 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001945
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001946 void VisitDoStmt(DoStmt* D) {
1947 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001948 if (Stmt* C = D->getCond())
1949 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001950 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001951
Ted Kremenek411cdee2008-04-16 21:10:48 +00001952 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001953 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001954 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001955 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001956
Mike Stump5d1d2022010-01-19 02:20:09 +00001957 void VisitCXXTryStmt(CXXTryStmt* CS) {
1958 OS << "try ...";
1959 }
1960
Ted Kremenek805e9a82007-08-31 21:49:40 +00001961 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001962 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001963 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001964 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001965
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001966 void VisitChooseExpr(ChooseExpr* C) {
1967 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001968 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001969 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001970 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001971
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001972 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1973 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001974 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001975 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001976
Ted Kremenek805e9a82007-08-31 21:49:40 +00001977 void VisitBinaryOperator(BinaryOperator* B) {
1978 if (!B->isLogicalOp()) {
1979 VisitExpr(B);
1980 return;
1981 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001983 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001984
Ted Kremenek805e9a82007-08-31 21:49:40 +00001985 switch (B->getOpcode()) {
1986 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001987 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001988 return;
1989 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001990 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001991 return;
1992 default:
1993 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001994 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001995 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001996
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001997 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001998 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001999 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002000};
Chris Lattnere4f21422009-06-30 01:26:17 +00002001} // end anonymous namespace
2002
Mike Stump6d9828c2009-07-17 01:31:16 +00002003
Chris Lattnere4f21422009-06-30 01:26:17 +00002004static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002005 const CFGElement &E) {
2006 Stmt *Terminator = E;
2007
2008 if (E.asStartScope()) {
2009 OS << "start scope\n";
2010 return;
2011 }
2012 if (E.asEndScope()) {
2013 OS << "end scope\n";
2014 return;
2015 }
2016
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002017 if (Helper) {
2018 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002019 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002020 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002021
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002022 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002023 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002024 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002025 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002026 return;
2027 }
2028 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002029
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002030 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002031 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002032 if (B->getOpcode() == BinaryOperator::Comma) {
2033 OS << "... , ";
2034 Helper->handledStmt(B->getRHS(),OS);
2035 OS << '\n';
2036 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002037 }
2038 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002039 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002040
Chris Lattnere4f21422009-06-30 01:26:17 +00002041 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002042
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002043 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002044 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002045}
Mike Stump6d9828c2009-07-17 01:31:16 +00002046
Chris Lattnere4f21422009-06-30 01:26:17 +00002047static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2048 const CFGBlock& B,
2049 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002050
Ted Kremenek42a509f2007-08-31 21:30:12 +00002051 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002052
Ted Kremenek7dba8602007-08-29 21:56:09 +00002053 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002054 OS << "\n [ B" << B.getBlockID();
2055
Ted Kremenek42a509f2007-08-31 21:30:12 +00002056 if (&B == &cfg->getEntry())
2057 OS << " (ENTRY) ]\n";
2058 else if (&B == &cfg->getExit())
2059 OS << " (EXIT) ]\n";
2060 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002061 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002062 else
2063 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002064
Ted Kremenek9cffe732007-08-29 23:20:49 +00002065 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002066 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002067
2068 if (print_edges)
2069 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002070
Mike Stump079bd722010-01-19 22:00:14 +00002071 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002072 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002073 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002074 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002075 C->getLHS()->printPretty(OS, Helper,
2076 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002077 if (C->getRHS()) {
2078 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002079 C->getRHS()->printPretty(OS, Helper,
2080 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002081 }
Mike Stump079bd722010-01-19 22:00:14 +00002082 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002083 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002084 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002085 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002086 if (CS->getExceptionDecl())
2087 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2088 0);
2089 else
2090 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002091 OS << ")";
2092
2093 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002094 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002095
Ted Kremenek9cffe732007-08-29 23:20:49 +00002096 OS << ":\n";
2097 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002098
Ted Kremenekfddd5182007-08-21 21:42:03 +00002099 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002100 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002101
Ted Kremenek42a509f2007-08-31 21:30:12 +00002102 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2103 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002104
Ted Kremenek9cffe732007-08-29 23:20:49 +00002105 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002106 if (print_edges)
2107 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002108
Ted Kremeneka95d3752008-09-13 05:16:45 +00002109 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002110
Ted Kremenek42a509f2007-08-31 21:30:12 +00002111 if (Helper)
2112 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002113
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002114 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002115 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002116
Ted Kremenek9cffe732007-08-29 23:20:49 +00002117 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002118 if (B.getTerminator()) {
2119 if (print_edges)
2120 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002121
Ted Kremenek9cffe732007-08-29 23:20:49 +00002122 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002123
Ted Kremenek42a509f2007-08-31 21:30:12 +00002124 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002125
Chris Lattnere4f21422009-06-30 01:26:17 +00002126 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2127 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002128 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002129 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002130 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002131
Ted Kremenek9cffe732007-08-29 23:20:49 +00002132 if (print_edges) {
2133 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002134 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002135 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002136
Ted Kremenek42a509f2007-08-31 21:30:12 +00002137 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2138 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002139
Ted Kremenek42a509f2007-08-31 21:30:12 +00002140 if (i == 8 || (i-8) == 0)
2141 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002142
Ted Kremenek9cffe732007-08-29 23:20:49 +00002143 OS << " B" << (*I)->getBlockID();
2144 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002145
Ted Kremenek42a509f2007-08-31 21:30:12 +00002146 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002147
Ted Kremenek42a509f2007-08-31 21:30:12 +00002148 // Print the successors of this block.
2149 OS << " Successors (" << B.succ_size() << "):";
2150 i = 0;
2151
2152 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2153 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002154
Ted Kremenek42a509f2007-08-31 21:30:12 +00002155 if (i == 8 || (i-8) % 10 == 0)
2156 OS << "\n ";
2157
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002158 if (*I)
2159 OS << " B" << (*I)->getBlockID();
2160 else
2161 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002162 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002163
Ted Kremenek9cffe732007-08-29 23:20:49 +00002164 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002165 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002166}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002167
Ted Kremenek42a509f2007-08-31 21:30:12 +00002168
2169/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002170void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002171
2172/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002173void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2174 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002175
Ted Kremenek42a509f2007-08-31 21:30:12 +00002176 // Print the entry block.
2177 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002178
Ted Kremenek42a509f2007-08-31 21:30:12 +00002179 // Iterate through the CFGBlocks and print them one by one.
2180 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2181 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002182 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002183 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002184
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002185 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002186 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002187
Ted Kremenek42a509f2007-08-31 21:30:12 +00002188 // Print the exit block.
2189 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002190 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002191}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002192
2193/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002194void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2195 print(llvm::errs(), cfg, LO);
2196}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002197
2198/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2199/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002200void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2201 const LangOptions &LO) const {
2202 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002203 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002204}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002205
Ted Kremeneka2925852008-01-30 23:02:42 +00002206/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002207void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002208 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002209 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002210 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2211}
2212
Ted Kremenek390e48b2008-11-12 21:11:49 +00002213Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002214
Ted Kremenek411cdee2008-04-16 21:10:48 +00002215 if (!Terminator)
2216 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002217
Ted Kremenek411cdee2008-04-16 21:10:48 +00002218 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002219
Ted Kremenek411cdee2008-04-16 21:10:48 +00002220 switch (Terminator->getStmtClass()) {
2221 default:
2222 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002223
Ted Kremenek411cdee2008-04-16 21:10:48 +00002224 case Stmt::ForStmtClass:
2225 E = cast<ForStmt>(Terminator)->getCond();
2226 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002227
Ted Kremenek411cdee2008-04-16 21:10:48 +00002228 case Stmt::WhileStmtClass:
2229 E = cast<WhileStmt>(Terminator)->getCond();
2230 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002231
Ted Kremenek411cdee2008-04-16 21:10:48 +00002232 case Stmt::DoStmtClass:
2233 E = cast<DoStmt>(Terminator)->getCond();
2234 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002235
Ted Kremenek411cdee2008-04-16 21:10:48 +00002236 case Stmt::IfStmtClass:
2237 E = cast<IfStmt>(Terminator)->getCond();
2238 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002239
Ted Kremenek411cdee2008-04-16 21:10:48 +00002240 case Stmt::ChooseExprClass:
2241 E = cast<ChooseExpr>(Terminator)->getCond();
2242 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002243
Ted Kremenek411cdee2008-04-16 21:10:48 +00002244 case Stmt::IndirectGotoStmtClass:
2245 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2246 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002247
Ted Kremenek411cdee2008-04-16 21:10:48 +00002248 case Stmt::SwitchStmtClass:
2249 E = cast<SwitchStmt>(Terminator)->getCond();
2250 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002251
Ted Kremenek411cdee2008-04-16 21:10:48 +00002252 case Stmt::ConditionalOperatorClass:
2253 E = cast<ConditionalOperator>(Terminator)->getCond();
2254 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002255
Ted Kremenek411cdee2008-04-16 21:10:48 +00002256 case Stmt::BinaryOperatorClass: // '&&' and '||'
2257 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002258 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002259
Ted Kremenek390e48b2008-11-12 21:11:49 +00002260 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002261 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002262 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002263
Ted Kremenek411cdee2008-04-16 21:10:48 +00002264 return E ? E->IgnoreParens() : NULL;
2265}
2266
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002267bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002269 if (!Terminator)
2270 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002271
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002272 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002273
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002274 switch (Terminator->getStmtClass()) {
2275 default:
2276 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002277
2278 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002279 case Stmt::WhileStmtClass:
2280 case Stmt::DoStmtClass:
2281 case Stmt::IfStmtClass:
2282 case Stmt::ChooseExprClass:
2283 case Stmt::ConditionalOperatorClass:
2284 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002285 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002286 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002287
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002288 return E ? E->IgnoreParens() : NULL;
2289}
2290
Ted Kremeneka2925852008-01-30 23:02:42 +00002291
Ted Kremenek7dba8602007-08-29 21:56:09 +00002292//===----------------------------------------------------------------------===//
2293// CFG Graphviz Visualization
2294//===----------------------------------------------------------------------===//
2295
Ted Kremenek42a509f2007-08-31 21:30:12 +00002296
2297#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002298static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002299#endif
2300
Chris Lattnere4f21422009-06-30 01:26:17 +00002301void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002302#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002303 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002304 GraphHelper = &H;
2305 llvm::ViewGraph(this,"CFG");
2306 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002307#endif
2308}
2309
Ted Kremenek7dba8602007-08-29 21:56:09 +00002310namespace llvm {
2311template<>
2312struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002313
2314 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2315
2316 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002317
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002318#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002319 std::string OutSStr;
2320 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002321 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002322 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002323
2324 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2325
2326 // Process string output to make it nicer...
2327 for (unsigned i = 0; i != OutStr.length(); ++i)
2328 if (OutStr[i] == '\n') { // Left justify
2329 OutStr[i] = '\\';
2330 OutStr.insert(OutStr.begin()+i+1, 'l');
2331 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002332
Ted Kremenek7dba8602007-08-29 21:56:09 +00002333 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002334#else
2335 return "";
2336#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002337 }
2338};
2339} // end namespace llvm