blob: 3d68551aba289505f223012a52cf8062f230405f [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekfddd5182007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenek852274d2009-12-16 03:18:58 +000038
39class AddStmtChoice {
40public:
Ted Kremenek5ba290a2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramer792bea92010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000047
Benjamin Kramer792bea92010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
49 bool asLValue() const { return k >= AlwaysAddAsLValue; }
Ted Kremenek5ba290a2010-03-02 21:43:54 +000050
Ted Kremenek852274d2009-12-16 03:18:58 +000051private:
Benjamin Kramer792bea92010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek852274d2009-12-16 03:18:58 +000053};
Mike Stump6d9828c2009-07-17 01:31:16 +000054
Ted Kremeneka34ea072008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000056/// The builder is stateful: an instance of the builder should be used to only
57/// construct a single CFG.
58///
59/// Example usage:
60///
61/// CFGBuilder builder;
62/// CFG* cfg = builder.BuildAST(stmt1);
63///
Mike Stump6d9828c2009-07-17 01:31:16 +000064/// CFG construction is done via a recursive walk of an AST. We actually parse
65/// the AST in reverse order so that the successor of a basic block is
66/// constructed prior to its predecessor. This allows us to nicely capture
67/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000072
Ted Kremenekfddd5182007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000080
Ted Kremenek19bb3562007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000084
85 // A list of blocks that end with a "goto" that must be backpatched to their
86 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000089
Ted Kremenek19bb3562007-08-28 19:26:49 +000090 // A list of labels whose address has been taken (for indirect gotos).
91 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
92 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000093
94public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000095 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
96 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000097 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +000098 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
99 TryTerminatedBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000100
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000101 // buildCFG - Used by external clients to construct the CFG.
Mike Stump4c45aa12010-01-21 15:20:48 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C, bool AddEHEdges,
103 bool AddScopes);
Mike Stump6d9828c2009-07-17 01:31:16 +0000104
Ted Kremenek4f880632009-07-17 22:18:43 +0000105private:
106 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000107 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
108 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
109 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000110 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000111 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
112 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
113 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Ted Kremenek852274d2009-12-16 03:18:58 +0000114 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000115 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000116 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000117 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000118 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000119 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000120 CFGBlock *VisitDeclStmt(DeclStmt *DS);
121 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000122 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
123 CFGBlock *VisitDoStmt(DoStmt *D);
124 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000125 CFGBlock *VisitGotoStmt(GotoStmt* G);
126 CFGBlock *VisitIfStmt(IfStmt *I);
127 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
128 CFGBlock *VisitLabelStmt(LabelStmt *L);
129 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
130 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
131 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
132 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
133 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
134 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000135 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
136 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000137 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
138 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000139
Ted Kremenek852274d2009-12-16 03:18:58 +0000140 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
141 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000142 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000143
Ted Kremenek274f4332008-04-28 18:00:46 +0000144 // NYS == Not Yet Supported
145 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000146 badCFG = true;
147 return Block;
148 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000149
Mike Stump079bd722010-01-19 22:00:14 +0000150 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
151 if (!AddScopes)
152 return B;
153
154 if (B == 0)
155 B = createBlock();
156 B->StartScope(S, cfg->getBumpVectorContext());
157 return B;
158 }
159
160 void EndScope(Stmt *S) {
161 if (!AddScopes)
162 return;
163
164 if (Block == 0)
165 Block = createBlock();
166 Block->EndScope(S, cfg->getBumpVectorContext());
167 }
168
Ted Kremenek4f880632009-07-17 22:18:43 +0000169 void autoCreateBlock() { if (!Block) Block = createBlock(); }
170 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000171 bool FinishBlock(CFGBlock* B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000172 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
173 return Visit(S, asc);
174 }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000175
Ted Kremenek852274d2009-12-16 03:18:58 +0000176 void AppendStmt(CFGBlock *B, Stmt *S,
177 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
178 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000179 }
180
181 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
182 B->addSuccessor(S, cfg->getBumpVectorContext());
183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000185 /// TryResult - a class representing a variant over the values
186 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
187 /// and is used by the CFGBuilder to decide if a branch condition
188 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000189 class TryResult {
190 int X;
191 public:
192 TryResult(bool b) : X(b ? 1 : 0) {}
193 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek941fde82009-07-24 04:47:11 +0000195 bool isTrue() const { return X == 1; }
196 bool isFalse() const { return X == 0; }
197 bool isKnown() const { return X >= 0; }
198 void negate() {
199 assert(isKnown());
200 X ^= 0x1;
201 }
202 };
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Mike Stump00998a02009-07-23 23:25:26 +0000204 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
205 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000206 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000207 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000208 if (!S->isTypeDependent() && !S->isValueDependent() &&
209 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000210 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000211
212 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000213 }
214
Ted Kremenek4102af92008-03-13 03:04:22 +0000215 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000216
217 // True iff EH edges on CallExprs should be added to the CFG.
218 bool AddEHEdges;
219
220 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump079bd722010-01-19 22:00:14 +0000221 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000222};
Mike Stump6d9828c2009-07-17 01:31:16 +0000223
Douglas Gregor898574e2008-12-05 23:32:09 +0000224// FIXME: Add support for dependent-sized array types in C++?
225// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000226static VariableArrayType* FindVA(Type* t) {
227 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
228 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
229 if (vat->getSizeExpr())
230 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000231
Ted Kremenek610a09e2008-09-26 22:58:57 +0000232 t = vt->getElementType().getTypePtr();
233 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000234
Ted Kremenek610a09e2008-09-26 22:58:57 +0000235 return 0;
236}
Mike Stump6d9828c2009-07-17 01:31:16 +0000237
238/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
239/// arbitrary statement. Examples include a single expression or a function
240/// body (compound statement). The ownership of the returned CFG is
241/// transferred to the caller. If CFG construction fails, this method returns
242/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000243CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Mike Stump55f988e2010-01-21 17:21:23 +0000244 bool addehedges, bool AddScopes) {
245 AddEHEdges = addehedges;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000246 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000247 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000248 if (!Statement)
249 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000250
Mike Stump079bd722010-01-19 22:00:14 +0000251 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000252 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000253
254 // Create an empty block that will serve as the exit block for the CFG. Since
255 // this is the first block added to the CFG, it will be implicitly registered
256 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000257 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000258 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000259 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000260
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000261 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000262 CFGBlock* B = addStmt(Statement);
Mike Stumpb978a442010-01-21 02:21:40 +0000263
264 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
265 // FIXME: Add code for base initializers and member initializers.
266 (void)CD;
267 }
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000268 if (!B)
269 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000270
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000271 if (B) {
272 // Finalize the last constructed block. This usually involves reversing the
273 // order of the statements in the block.
274 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000275
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000276 // Backpatch the gotos whose label -> block mappings we didn't know when we
277 // encountered them.
278 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000279 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000280
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000281 CFGBlock* B = *I;
282 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
283 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000284
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000285 // If there is no target for the goto, then we are looking at an
286 // incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000287 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000288
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000289 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000290 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000291
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000292 // Add successors to the Indirect Goto Dispatch block (if we have one).
293 if (CFGBlock* B = cfg->getIndirectGotoBlock())
294 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
295 E = AddressTakenLabels.end(); I != E; ++I ) {
296
297 // Lookup the target block.
298 LabelMapTy::iterator LI = LabelMap.find(*I);
299
300 // If there is no target block that contains label, then we are looking
301 // at an incomplete AST. Handle this by not registering a successor.
302 if (LI == LabelMap.end()) continue;
303
304 AddSuccessor(B, LI->second);
305 }
306
307 Succ = B;
308 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000309
310 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000311 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000312
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000313 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000314}
Mike Stump6d9828c2009-07-17 01:31:16 +0000315
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000316/// createBlock - Used to lazily create blocks that are connected
317/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000318CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000319 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000320 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000321 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000322 return B;
323}
Mike Stump6d9828c2009-07-17 01:31:16 +0000324
Ted Kremenek6c249722009-09-24 18:45:41 +0000325/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000326bool CFGBuilder::FinishBlock(CFGBlock* B) {
327 if (badCFG)
328 return false;
329
Ted Kremenek4f880632009-07-17 22:18:43 +0000330 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000331 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000332}
333
Ted Kremenek4f880632009-07-17 22:18:43 +0000334/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000335/// blocks for ternary operators, &&, and ||. We also process "," and
336/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000337CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000338tryAgain:
339 switch (S->getStmtClass()) {
340 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000341 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000342
343 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000344 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek4f880632009-07-17 22:18:43 +0000346 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000347 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek4f880632009-07-17 22:18:43 +0000349 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000350 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000351
Ted Kremenek4f880632009-07-17 22:18:43 +0000352 case Stmt::BreakStmtClass:
353 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek4f880632009-07-17 22:18:43 +0000355 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000356 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Ted Kremenek4f880632009-07-17 22:18:43 +0000358 case Stmt::CaseStmtClass:
359 return VisitCaseStmt(cast<CaseStmt>(S));
360
361 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000362 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek4f880632009-07-17 22:18:43 +0000364 case Stmt::CompoundStmtClass:
365 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek4f880632009-07-17 22:18:43 +0000367 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000368 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek4f880632009-07-17 22:18:43 +0000370 case Stmt::ContinueStmtClass:
371 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek021c8af2010-01-19 20:40:33 +0000373 case Stmt::CXXCatchStmtClass:
374 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
375
376 case Stmt::CXXThrowExprClass:
377 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
378
379 case Stmt::CXXTryStmtClass:
380 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
381
Ted Kremenek4f880632009-07-17 22:18:43 +0000382 case Stmt::DeclStmtClass:
383 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek4f880632009-07-17 22:18:43 +0000385 case Stmt::DefaultStmtClass:
386 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek4f880632009-07-17 22:18:43 +0000388 case Stmt::DoStmtClass:
389 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Ted Kremenek4f880632009-07-17 22:18:43 +0000391 case Stmt::ForStmtClass:
392 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek4f880632009-07-17 22:18:43 +0000394 case Stmt::GotoStmtClass:
395 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek4f880632009-07-17 22:18:43 +0000397 case Stmt::IfStmtClass:
398 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek4f880632009-07-17 22:18:43 +0000400 case Stmt::IndirectGotoStmtClass:
401 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek4f880632009-07-17 22:18:43 +0000403 case Stmt::LabelStmtClass:
404 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Ted Kremenek4f880632009-07-17 22:18:43 +0000406 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000407 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
408
Ted Kremenek4f880632009-07-17 22:18:43 +0000409 case Stmt::ObjCAtSynchronizedStmtClass:
410 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek4f880632009-07-17 22:18:43 +0000412 case Stmt::ObjCAtThrowStmtClass:
413 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek4f880632009-07-17 22:18:43 +0000415 case Stmt::ObjCAtTryStmtClass:
416 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek4f880632009-07-17 22:18:43 +0000418 case Stmt::ObjCForCollectionStmtClass:
419 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek4f880632009-07-17 22:18:43 +0000421 case Stmt::ParenExprClass:
422 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000423 goto tryAgain;
424
Ted Kremenek4f880632009-07-17 22:18:43 +0000425 case Stmt::NullStmtClass:
426 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek4f880632009-07-17 22:18:43 +0000428 case Stmt::ReturnStmtClass:
429 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremenek4f880632009-07-17 22:18:43 +0000431 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000432 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Ted Kremenek4f880632009-07-17 22:18:43 +0000434 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000435 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek4f880632009-07-17 22:18:43 +0000437 case Stmt::SwitchStmtClass:
438 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremenek4f880632009-07-17 22:18:43 +0000440 case Stmt::WhileStmtClass:
441 return VisitWhileStmt(cast<WhileStmt>(S));
442 }
443}
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek852274d2009-12-16 03:18:58 +0000445CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
446 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000447 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000448 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek4f880632009-07-17 22:18:43 +0000451 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000452}
Mike Stump6d9828c2009-07-17 01:31:16 +0000453
Ted Kremenek4f880632009-07-17 22:18:43 +0000454/// VisitChildren - Visit the children of a Stmt.
455CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
456 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000457 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000458 E = Terminator->child_end(); I != E; ++I) {
459 if (*I) B = Visit(*I);
460 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000461 return B;
462}
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek852274d2009-12-16 03:18:58 +0000464CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
465 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000466 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000467
Ted Kremenek852274d2009-12-16 03:18:58 +0000468 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000469 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000470 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000471 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000472
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000473 return Block;
474}
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenek852274d2009-12-16 03:18:58 +0000476CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
477 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000478 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000479 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000480 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek4f880632009-07-17 22:18:43 +0000482 if (!FinishBlock(ConfluenceBlock))
483 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Ted Kremenek4f880632009-07-17 22:18:43 +0000485 // create the block evaluating the LHS
486 CFGBlock* LHSBlock = createBlock(false);
487 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Ted Kremenek4f880632009-07-17 22:18:43 +0000489 // create the block evaluating the RHS
490 Succ = ConfluenceBlock;
491 Block = NULL;
492 CFGBlock* RHSBlock = addStmt(B->getRHS());
493 if (!FinishBlock(RHSBlock))
494 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Mike Stump00998a02009-07-23 23:25:26 +0000496 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000497 TryResult KnownVal = TryEvaluateBool(B->getLHS());
498 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
499 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000500
Ted Kremenek4f880632009-07-17 22:18:43 +0000501 // Now link the LHSBlock with RHSBlock.
502 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000503 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
504 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000505 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000506 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000507 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
508 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Ted Kremenek4f880632009-07-17 22:18:43 +0000511 // Generate the blocks for evaluating the LHS.
512 Block = LHSBlock;
513 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000514 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000515 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000516 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000517 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000518 addStmt(B->getRHS());
519 return addStmt(B->getLHS());
520 }
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Ted Kremenek852274d2009-12-16 03:18:58 +0000522 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000523}
524
Ted Kremenek852274d2009-12-16 03:18:58 +0000525CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
526 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000527 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000528 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000529 }
530 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000531}
532
Ted Kremenek4f880632009-07-17 22:18:43 +0000533CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
534 // "break" is a control-flow statement. Thus we stop processing the current
535 // block.
536 if (Block && !FinishBlock(Block))
537 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Ted Kremenek4f880632009-07-17 22:18:43 +0000539 // Now create a new block that ends with the break statement.
540 Block = createBlock(false);
541 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Ted Kremenek4f880632009-07-17 22:18:43 +0000543 // If there is no target for the break, then we are looking at an incomplete
544 // AST. This means that the CFG cannot be constructed.
545 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000546 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000547 else
548 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
550
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000551 return Block;
552}
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Mike Stump4c45aa12010-01-21 15:20:48 +0000554static bool CanThrow(Expr *E) {
555 QualType Ty = E->getType();
556 if (Ty->isFunctionPointerType())
557 Ty = Ty->getAs<PointerType>()->getPointeeType();
558 else if (Ty->isBlockPointerType())
559 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
560
561 const FunctionType *FT = Ty->getAs<FunctionType>();
562 if (FT) {
563 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
564 if (Proto->hasEmptyExceptionSpec())
565 return false;
566 }
567 return true;
568}
569
Ted Kremenek852274d2009-12-16 03:18:58 +0000570CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000571 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000572 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000573 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000574 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000575 }
Mike Stump24556362009-07-25 21:26:53 +0000576
Mike Stump4c45aa12010-01-21 15:20:48 +0000577 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000578
579 // Languages without exceptions are assumed to not throw.
580 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000581 if (AddEHEdges)
582 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000583 }
584
585 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000586 if (FD->hasAttr<NoReturnAttr>())
587 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000588 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000589 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000590 }
Mike Stump24556362009-07-25 21:26:53 +0000591
Mike Stump4c45aa12010-01-21 15:20:48 +0000592 if (!CanThrow(C->getCallee()))
593 AddEHEdge = false;
594
595 if (!NoReturn && !AddEHEdge)
Zhongxing Xu91071662010-02-24 02:19:28 +0000596 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Mike Stump079bd722010-01-19 22:00:14 +0000598 if (Block) {
599 Succ = Block;
600 if (!FinishBlock(Block))
601 return 0;
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Mike Stump079bd722010-01-19 22:00:14 +0000604 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000605 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000606
Mike Stump079bd722010-01-19 22:00:14 +0000607 if (NoReturn) {
608 // Wire this to the exit block directly.
609 AddSuccessor(Block, &cfg->getExit());
610 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000611 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000612 // Add exceptional edges.
613 if (TryTerminatedBlock)
614 AddSuccessor(Block, TryTerminatedBlock);
615 else
616 AddSuccessor(Block, &cfg->getExit());
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Mike Stump24556362009-07-25 21:26:53 +0000619 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000620}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000621
Ted Kremenek852274d2009-12-16 03:18:58 +0000622CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
623 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000624 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000625 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000626 if (!FinishBlock(ConfluenceBlock))
627 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000629 Succ = ConfluenceBlock;
630 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000631 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000632 if (!FinishBlock(LHSBlock))
633 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000635 Succ = ConfluenceBlock;
636 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000637 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000638 if (!FinishBlock(RHSBlock))
639 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000641 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000642 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000643 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000644 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
645 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000646 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000647 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000648}
Mike Stump1eb44332009-09-09 15:08:12 +0000649
650
651CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000652 EndScope(C);
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000655
656 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
657 I != E; ++I ) {
658 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000660 if (badCFG)
661 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000662 }
Mike Stump079bd722010-01-19 22:00:14 +0000663
664 LastBlock = StartScope(C, LastBlock);
665
Ted Kremenek4f880632009-07-17 22:18:43 +0000666 return LastBlock;
667}
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremenek852274d2009-12-16 03:18:58 +0000669CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
670 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000671 // Create the confluence block that will "merge" the results of the ternary
672 // expression.
673 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000674 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000675 if (!FinishBlock(ConfluenceBlock))
676 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000678 // Create a block for the LHS expression if there is an LHS expression. A
679 // GCC extension allows LHS to be NULL, causing the condition to be the
680 // value that is returned instead.
681 // e.g: x ?: y is shorthand for: x ? x : y;
682 Succ = ConfluenceBlock;
683 Block = NULL;
684 CFGBlock* LHSBlock = NULL;
685 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000686 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000687 if (!FinishBlock(LHSBlock))
688 return 0;
689 Block = NULL;
690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000692 // Create the block for the RHS expression.
693 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000694 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000695 if (!FinishBlock(RHSBlock))
696 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000698 // Create the block that will contain the condition.
699 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Mike Stump00998a02009-07-23 23:25:26 +0000701 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000702 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000703 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000704 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000705 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000706 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000707 // If we know the condition is false, add NULL as the successor for
708 // the block containing the condition. In this case, the confluence
709 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000710 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000711 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000712 } else {
713 // If we have no LHS expression, add the ConfluenceBlock as a direct
714 // successor for the block containing the condition. Moreover, we need to
715 // reverse the order of the predecessors in the ConfluenceBlock because
716 // the RHSBlock will have been added to the succcessors already, and we
717 // want the first predecessor to the the block containing the expression
718 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000719 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000720 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000721 std::reverse(ConfluenceBlock->pred_begin(),
722 ConfluenceBlock->pred_end());
723 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000726 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000727 Block->setTerminator(C);
728 return addStmt(C->getCond());
729}
730
Ted Kremenek4f880632009-07-17 22:18:43 +0000731CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
732 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000733
Ted Kremenek4f880632009-07-17 22:18:43 +0000734 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000735 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000736 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek4f880632009-07-17 22:18:43 +0000739 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek4f880632009-07-17 22:18:43 +0000741 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
742 typedef llvm::SmallVector<Decl*,10> BufTy;
743 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek4f880632009-07-17 22:18:43 +0000745 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
746 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
747 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
748 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremenek4f880632009-07-17 22:18:43 +0000750 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
751 // automatically freed with the CFG.
752 DeclGroupRef DG(*I);
753 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000754 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000755 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek4f880632009-07-17 22:18:43 +0000757 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000758 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000759 B = VisitDeclSubExpr(D);
760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
762 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000763}
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek4f880632009-07-17 22:18:43 +0000765/// VisitDeclSubExpr - Utility method to add block-level expressions for
766/// initializers in Decls.
767CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
768 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000769
Ted Kremenek4f880632009-07-17 22:18:43 +0000770 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenek4f880632009-07-17 22:18:43 +0000772 if (!VD)
773 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek4f880632009-07-17 22:18:43 +0000775 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek4f880632009-07-17 22:18:43 +0000777 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000778 AddStmtChoice::Kind k =
779 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
780 : AddStmtChoice::NotAlwaysAdd;
781 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000782 }
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Ted Kremenek4f880632009-07-17 22:18:43 +0000784 // If the type of VD is a VLA, then we must process its size expressions.
785 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
786 VA = FindVA(VA->getElementType().getTypePtr()))
787 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000790}
791
792CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000793 // We may see an if statement in the middle of a basic block, or it may be the
794 // first statement we are processing. In either case, we create a new basic
795 // block. First, we create the blocks for the then...else statements, and
796 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000797 // middle of a block, we stop processing that block. That block is then the
798 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000799
800 // The block we were proccessing is now finished. Make it the successor
801 // block.
802 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000803 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000804 if (!FinishBlock(Block))
805 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000806 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000807
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000808 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000809 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000810
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000811 if (Stmt* Else = I->getElse()) {
812 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000813
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000814 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000815 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000816 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000817 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000818
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000819 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
820 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000821 else if (Block) {
822 if (!FinishBlock(ElseBlock))
823 return 0;
824 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000825 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000826
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000827 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000828 CFGBlock* ThenBlock;
829 {
830 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000831 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000832 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000833 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000834 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000835
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000836 if (!ThenBlock) {
837 // We can reach here if the "then" body has all NullStmts.
838 // Create an empty block so we can distinguish between true and false
839 // branches in path-sensitive analyses.
840 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000841 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000842 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000843 if (!FinishBlock(ThenBlock))
844 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000845 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000846 }
847
Mike Stump6d9828c2009-07-17 01:31:16 +0000848 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000849 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000850
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000851 // Set the terminator of the new block to the If statement.
852 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000853
Mike Stump00998a02009-07-23 23:25:26 +0000854 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000855 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000856
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000857 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000858 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
859 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000860
861 // Add the condition as the last statement in the new block. This may create
862 // new blocks as the condition may contain control-flow. Any newly created
863 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000864 Block = addStmt(I->getCond());
865
866 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
867 // and the condition variable initialization to the CFG.
868 if (VarDecl *VD = I->getConditionVariable()) {
869 if (Expr *Init = VD->getInit()) {
870 autoCreateBlock();
871 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
872 addStmt(Init);
873 }
874 }
875
876 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000877}
Mike Stump6d9828c2009-07-17 01:31:16 +0000878
879
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000880CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000881 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000882 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000883 // NOTE: If a "return" appears in the middle of a block, this means that the
884 // code afterwards is DEAD (unreachable). We still keep a basic block
885 // for that code; a simple "mark-and-sweep" from the entry block will be
886 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000887 if (Block)
888 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000889
890 // Create the new block.
891 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000892
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000893 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000894 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000895
896 // Add the return statement to the block. This may create new blocks if R
897 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000898 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000899}
900
901CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
902 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000903 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000904 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000905
Ted Kremenek4f880632009-07-17 22:18:43 +0000906 if (!LabelBlock) // This can happen when the body is empty, i.e.
907 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000908
Ted Kremenek4f880632009-07-17 22:18:43 +0000909 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000910 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000911
912 // Labels partition blocks, so this is the end of the basic block we were
913 // processing (L is the block's label). Because this is label (and we have
914 // already processed the substatement) there is no extra control-flow to worry
915 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000916 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000917 if (!FinishBlock(LabelBlock))
918 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000919
920 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000921 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000922
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000923 // This block is now the implicit successor of other blocks.
924 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000925
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000926 return LabelBlock;
927}
928
929CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000930 // Goto is a control-flow statement. Thus we stop processing the current
931 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000932 if (Block)
933 FinishBlock(Block);
934
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000935 Block = createBlock(false);
936 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000937
938 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000939 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000940
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000941 if (I == LabelMap.end())
942 // We will need to backpatch this block later.
943 BackpatchBlocks.push_back(Block);
944 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000945 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000946
Mike Stump6d9828c2009-07-17 01:31:16 +0000947 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000948}
949
950CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000951 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000952
Mike Stumpfefb9f72009-07-21 01:12:51 +0000953 // "for" is a control-flow statement. Thus we stop processing the current
954 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000955 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000956 if (!FinishBlock(Block))
957 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000958 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000959 } else
960 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000961
962 // Because of short-circuit evaluation, the condition of the loop can span
963 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
964 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000965 CFGBlock* ExitConditionBlock = createBlock(false);
966 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000967
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000968 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000969 ExitConditionBlock->setTerminator(F);
970
971 // Now add the actual condition to the condition block. Because the condition
972 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000973 if (Stmt* C = F->getCond()) {
974 Block = ExitConditionBlock;
975 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000976 assert(Block == EntryConditionBlock);
977
978 // If this block contains a condition variable, add both the condition
979 // variable and initializer to the CFG.
980 if (VarDecl *VD = F->getConditionVariable()) {
981 if (Expr *Init = VD->getInit()) {
982 autoCreateBlock();
983 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
984 EntryConditionBlock = addStmt(Init);
985 assert(Block == EntryConditionBlock);
986 }
987 }
988
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000989 if (Block) {
990 if (!FinishBlock(EntryConditionBlock))
991 return 0;
992 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000993 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000994
Mike Stump6d9828c2009-07-17 01:31:16 +0000995 // The condition block is the implicit successor for the loop body as well as
996 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000997 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000998
Mike Stump00998a02009-07-23 23:25:26 +0000999 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001000 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Mike Stump00998a02009-07-23 23:25:26 +00001002 if (F->getCond())
1003 KnownVal = TryEvaluateBool(F->getCond());
1004
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001005 // Now create the loop body.
1006 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001007 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001008
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001009 // Save the current values for Block, Succ, and continue and break targets
1010 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001011 save_continue(ContinueTargetBlock),
1012 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001013
Ted Kremenekaf603f72007-08-30 18:39:40 +00001014 // Create a new block to contain the (bottom) of the loop body.
1015 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001016
Ted Kremeneke9334502008-09-04 21:48:47 +00001017 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001018 // Generate increment code in its own basic block. This is the target of
1019 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001020 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001021 } else {
1022 // No increment code. Create a special, empty, block that is used as the
1023 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001024 assert(Succ == EntryConditionBlock);
1025 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001026 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001027
Ted Kremenek3575f842009-04-28 00:51:56 +00001028 // Finish up the increment (or empty) block if it hasn't been already.
1029 if (Block) {
1030 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001031 if (!FinishBlock(Block))
1032 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001033 Block = 0;
1034 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001035
Ted Kremenek3575f842009-04-28 00:51:56 +00001036 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001037
Ted Kremenek3575f842009-04-28 00:51:56 +00001038 // The starting block for the loop increment is the block that should
1039 // represent the 'loop target' for looping back to the start of the loop.
1040 ContinueTargetBlock->setLoopTarget(F);
1041
Ted Kremeneke9334502008-09-04 21:48:47 +00001042 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +00001043 BreakTargetBlock = LoopSuccessor;
1044
1045 // Now populate the body block, and in the process create new blocks as we
1046 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001047 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001048
1049 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001050 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001051 else if (Block && !FinishBlock(BodyBlock))
1052 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001053
Ted Kremenek941fde82009-07-24 04:47:11 +00001054 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001055 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001056 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001057
Ted Kremenek941fde82009-07-24 04:47:11 +00001058 // Link up the condition block with the code that follows the loop. (the
1059 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001060 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001061
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001062 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001063 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001064 if (Stmt* I = F->getInit()) {
1065 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001066 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001067 } else {
1068 // There is no loop initialization. We are thus basically a while loop.
1069 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001070 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001071 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001072 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001073 }
1074}
1075
Ted Kremenek514de5a2008-11-11 17:10:00 +00001076CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1077 // Objective-C fast enumeration 'for' statements:
1078 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1079 //
1080 // for ( Type newVariable in collection_expression ) { statements }
1081 //
1082 // becomes:
1083 //
1084 // prologue:
1085 // 1. collection_expression
1086 // T. jump to loop_entry
1087 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001088 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001089 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1090 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1091 // TB:
1092 // statements
1093 // T. jump to loop_entry
1094 // FB:
1095 // what comes after
1096 //
1097 // and
1098 //
1099 // Type existingItem;
1100 // for ( existingItem in expression ) { statements }
1101 //
1102 // becomes:
1103 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001104 // the same with newVariable replaced with existingItem; the binding works
1105 // the same except that for one ObjCForCollectionStmt::getElement() returns
1106 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001107 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001108
Ted Kremenek514de5a2008-11-11 17:10:00 +00001109 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001110
Ted Kremenek514de5a2008-11-11 17:10:00 +00001111 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001112 if (!FinishBlock(Block))
1113 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001114 LoopSuccessor = Block;
1115 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001116 } else
1117 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001118
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001119 // Build the condition blocks.
1120 CFGBlock* ExitConditionBlock = createBlock(false);
1121 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001122
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001123 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001124 ExitConditionBlock->setTerminator(S);
1125
1126 // The last statement in the block should be the ObjCForCollectionStmt, which
1127 // performs the actual binding to 'element' and determines if there are any
1128 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001129 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001130 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001131
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001132 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001133 // generate new blocks as necesary. We DON'T add the statement by default to
1134 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001135 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001136 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001137 if (!FinishBlock(EntryConditionBlock))
1138 return 0;
1139 Block = 0;
1140 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001141
1142 // The condition block is the implicit successor for the loop body as well as
1143 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001144 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001145
Ted Kremenek514de5a2008-11-11 17:10:00 +00001146 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001147 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001148 // Save the current values for Succ, continue and break targets.
1149 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001150 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1151
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001152 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001153 ContinueTargetBlock = EntryConditionBlock;
1154
Ted Kremenek4f880632009-07-17 22:18:43 +00001155 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001156
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001157 if (!BodyBlock)
1158 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001159 else if (Block) {
1160 if (!FinishBlock(BodyBlock))
1161 return 0;
1162 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001163
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001164 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001165 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001166 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001167
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001168 // Link up the condition block with the code that follows the loop.
1169 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001170 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001171
Ted Kremenek514de5a2008-11-11 17:10:00 +00001172 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001173 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001174 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001175}
1176
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001177CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1178 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001179
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001180 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001181 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001182
Ted Kremenekda5348e2009-05-05 23:11:51 +00001183 // The sync body starts its own basic block. This makes it a little easier
1184 // for diagnostic clients.
1185 if (SyncBlock) {
1186 if (!FinishBlock(SyncBlock))
1187 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001188
Ted Kremenekda5348e2009-05-05 23:11:51 +00001189 Block = 0;
1190 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001191
Ted Kremenekda5348e2009-05-05 23:11:51 +00001192 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001193
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001194 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001195 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001196}
Mike Stump6d9828c2009-07-17 01:31:16 +00001197
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001198CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001199 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001200 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001201}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001202
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001203CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001204 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001205
Mike Stumpfefb9f72009-07-21 01:12:51 +00001206 // "while" is a control-flow statement. Thus we stop processing the current
1207 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001208 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001209 if (!FinishBlock(Block))
1210 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001211 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001212 } else
1213 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001214
1215 // Because of short-circuit evaluation, the condition of the loop can span
1216 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1217 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001218 CFGBlock* ExitConditionBlock = createBlock(false);
1219 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001220
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001221 // Set the terminator for the "exit" condition block.
1222 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001223
1224 // Now add the actual condition to the condition block. Because the condition
1225 // itself may contain control-flow, new blocks may be created. Thus we update
1226 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001227 if (Stmt* C = W->getCond()) {
1228 Block = ExitConditionBlock;
1229 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001230 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001231
1232 // If this block contains a condition variable, add both the condition
1233 // variable and initializer to the CFG.
1234 if (VarDecl *VD = W->getConditionVariable()) {
1235 if (Expr *Init = VD->getInit()) {
1236 autoCreateBlock();
1237 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1238 EntryConditionBlock = addStmt(Init);
1239 assert(Block == EntryConditionBlock);
1240 }
1241 }
1242
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001243 if (Block) {
1244 if (!FinishBlock(EntryConditionBlock))
1245 return 0;
1246 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001247 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001248
1249 // The condition block is the implicit successor for the loop body as well as
1250 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001251 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
Mike Stump00998a02009-07-23 23:25:26 +00001253 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001254 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001255
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001256 // Process the loop body.
1257 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001258 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001259
1260 // Save the current values for Block, Succ, and continue and break targets
1261 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1262 save_continue(ContinueTargetBlock),
1263 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001264
Mike Stump6d9828c2009-07-17 01:31:16 +00001265 // Create an empty block to represent the transition block for looping back
1266 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001267 Block = 0;
1268 assert(Succ == EntryConditionBlock);
1269 Succ = createBlock();
1270 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001271 ContinueTargetBlock = Succ;
1272
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001273 // All breaks should go to the code following the loop.
1274 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001275
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001276 // NULL out Block to force lazy instantiation of blocks for the body.
1277 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001278
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001279 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001280 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001281
Ted Kremenekaf603f72007-08-30 18:39:40 +00001282 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001283 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001284 else if (Block) {
1285 if (!FinishBlock(BodyBlock))
1286 return 0;
1287 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001288
Ted Kremenek941fde82009-07-24 04:47:11 +00001289 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001290 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001291 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
Ted Kremenek941fde82009-07-24 04:47:11 +00001293 // Link up the condition block with the code that follows the loop. (the
1294 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001295 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001296
1297 // There can be no more statements in the condition block since we loop back
1298 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001299 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001300
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001301 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001302 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001303 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001304}
Mike Stump1eb44332009-09-09 15:08:12 +00001305
1306
Ted Kremenek4f880632009-07-17 22:18:43 +00001307CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1308 // FIXME: For now we pretend that @catch and the code it contains does not
1309 // exit.
1310 return Block;
1311}
Mike Stump6d9828c2009-07-17 01:31:16 +00001312
Ted Kremenek2fda5042008-12-09 20:20:09 +00001313CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1314 // FIXME: This isn't complete. We basically treat @throw like a return
1315 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001316
Ted Kremenek6c249722009-09-24 18:45:41 +00001317 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001318 if (Block && !FinishBlock(Block))
1319 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
Ted Kremenek2fda5042008-12-09 20:20:09 +00001321 // Create the new block.
1322 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001323
Ted Kremenek2fda5042008-12-09 20:20:09 +00001324 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001325 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001326
1327 // Add the statement to the block. This may create new blocks if S contains
1328 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001329 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001330}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001331
Mike Stump0979d802009-07-22 22:56:04 +00001332CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001333 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001334 if (Block && !FinishBlock(Block))
1335 return 0;
1336
1337 // Create the new block.
1338 Block = createBlock(false);
1339
Mike Stump5d1d2022010-01-19 02:20:09 +00001340 if (TryTerminatedBlock)
1341 // The current try statement is the only successor.
1342 AddSuccessor(Block, TryTerminatedBlock);
1343 else
1344 // otherwise the Exit block is the only successor.
1345 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001346
1347 // Add the statement to the block. This may create new blocks if S contains
1348 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001349 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001350}
1351
Ted Kremenek4f880632009-07-17 22:18:43 +00001352CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001353 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001354
Mike Stump8f9893a2009-07-21 01:27:50 +00001355 // "do...while" is a control-flow statement. Thus we stop processing the
1356 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001357 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001358 if (!FinishBlock(Block))
1359 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001360 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001361 } else
1362 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001363
1364 // Because of short-circuit evaluation, the condition of the loop can span
1365 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1366 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001367 CFGBlock* ExitConditionBlock = createBlock(false);
1368 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001369
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001370 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001371 ExitConditionBlock->setTerminator(D);
1372
1373 // Now add the actual condition to the condition block. Because the condition
1374 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001375 if (Stmt* C = D->getCond()) {
1376 Block = ExitConditionBlock;
1377 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001378 if (Block) {
1379 if (!FinishBlock(EntryConditionBlock))
1380 return 0;
1381 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001382 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001383
Ted Kremenek54827132008-02-27 07:20:00 +00001384 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001385 Succ = EntryConditionBlock;
1386
Mike Stump00998a02009-07-23 23:25:26 +00001387 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001388 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001389
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001390 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001391 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001392 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001393 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001394
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001395 // Save the current values for Block, Succ, and continue and break targets
1396 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001397 save_continue(ContinueTargetBlock),
1398 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001399
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001400 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001401 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001402
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001403 // All breaks should go to the code following the loop.
1404 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001405
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001406 // NULL out Block to force lazy instantiation of blocks for the body.
1407 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001408
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001409 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001410 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001411
Ted Kremenekaf603f72007-08-30 18:39:40 +00001412 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001413 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001414 else if (Block) {
1415 if (!FinishBlock(BodyBlock))
1416 return 0;
1417 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001418
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001419 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001420 // ExitConditionBlock to represent the "loop back" transition. Create an
1421 // empty block to represent the transition block for looping back to the
1422 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001423 // FIXME: Can we do this more efficiently without adding another block?
1424 Block = NULL;
1425 Succ = BodyBlock;
1426 CFGBlock *LoopBackBlock = createBlock();
1427 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001428
Ted Kremenek941fde82009-07-24 04:47:11 +00001429 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001430 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001431 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenek941fde82009-07-24 04:47:11 +00001433 // Link up the condition block with the code that follows the loop.
1434 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001435 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001436
1437 // There can be no more statements in the body block(s) since we loop back to
1438 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001439 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001440
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001441 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001442 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001443 return BodyBlock;
1444}
1445
1446CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1447 // "continue" is a control-flow statement. Thus we stop processing the
1448 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001449 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001450 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001451
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001452 // Now create a new block that ends with the continue statement.
1453 Block = createBlock(false);
1454 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001455
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001456 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001457 // incomplete AST. This means the CFG cannot be constructed.
1458 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001459 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001460 else
1461 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001462
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001463 return Block;
1464}
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001466CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001467 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001468
Ted Kremenek852274d2009-12-16 03:18:58 +00001469 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001470 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001471 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Ted Kremenek4f880632009-07-17 22:18:43 +00001474 // VLA types have expressions that must be evaluated.
1475 if (E->isArgumentType()) {
1476 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1477 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1478 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001479 }
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Mike Stump6d9828c2009-07-17 01:31:16 +00001481 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001482}
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek4f880632009-07-17 22:18:43 +00001484/// VisitStmtExpr - Utility method to handle (nested) statement
1485/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001486CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1487 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001488 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001489 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001490 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001491 return VisitCompoundStmt(SE->getSubStmt());
1492}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001493
Ted Kremenek411cdee2008-04-16 21:10:48 +00001494CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001495 // "switch" is a control-flow statement. Thus we stop processing the current
1496 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001497 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001498
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001499 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001500 if (!FinishBlock(Block))
1501 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001502 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001503 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001504
1505 // Save the current "switch" context.
1506 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001507 save_break(BreakTargetBlock),
1508 save_default(DefaultCaseBlock);
1509
Mike Stump6d9828c2009-07-17 01:31:16 +00001510 // Set the "default" case to be the block after the switch statement. If the
1511 // switch statement contains a "default:", this value will be overwritten with
1512 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001513 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001514
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001515 // Create a new block that will contain the switch statement.
1516 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001517
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001518 // Now process the switch body. The code after the switch is the implicit
1519 // successor.
1520 Succ = SwitchSuccessor;
1521 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001522
1523 // When visiting the body, the case statements should automatically get linked
1524 // up to the switch. We also don't keep a pointer to the body, since all
1525 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001526 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001527 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001528 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001529 if (Block) {
1530 if (!FinishBlock(BodyBlock))
1531 return 0;
1532 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001533
Mike Stump6d9828c2009-07-17 01:31:16 +00001534 // If we have no "default:" case, the default transition is to the code
1535 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001536 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001537
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001538 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001539 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001540 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001541 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001542 Block = addStmt(Terminator->getCond());
1543
1544 // Finally, if the SwitchStmt contains a condition variable, add both the
1545 // SwitchStmt and the condition variable initialization to the CFG.
1546 if (VarDecl *VD = Terminator->getConditionVariable()) {
1547 if (Expr *Init = VD->getInit()) {
1548 autoCreateBlock();
1549 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1550 addStmt(Init);
1551 }
1552 }
1553
1554 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001555}
1556
Ted Kremenek4f880632009-07-17 22:18:43 +00001557CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001558 // CaseStmts are essentially labels, so they are the first statement in a
1559 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001560
Ted Kremenek4f880632009-07-17 22:18:43 +00001561 if (CS->getSubStmt())
1562 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001564 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001565 if (!CaseBlock)
1566 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
1568 // Cases statements partition blocks, so this is the top of the basic block we
1569 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001570 CaseBlock->setLabel(CS);
1571
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001572 if (!FinishBlock(CaseBlock))
1573 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001574
1575 // Add this block to the list of successors for the block with the switch
1576 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001577 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001578 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001579
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001580 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1581 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001582
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001583 // This block is now the implicit successor of other blocks.
1584 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001585
Ted Kremenek2677ea82008-03-15 07:45:02 +00001586 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001587}
Mike Stump6d9828c2009-07-17 01:31:16 +00001588
Ted Kremenek411cdee2008-04-16 21:10:48 +00001589CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001590 if (Terminator->getSubStmt())
1591 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001593 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001594
1595 if (!DefaultCaseBlock)
1596 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001597
1598 // Default statements partition blocks, so this is the top of the basic block
1599 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001600 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001602 if (!FinishBlock(DefaultCaseBlock))
1603 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001604
Mike Stump6d9828c2009-07-17 01:31:16 +00001605 // Unlike case statements, we don't add the default block to the successors
1606 // for the switch statement immediately. This is done when we finish
1607 // processing the switch statement. This allows for the default case
1608 // (including a fall-through to the code after the switch statement) to always
1609 // be the last successor of a switch-terminated block.
1610
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001611 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1612 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001613
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001614 // This block is now the implicit successor of other blocks.
1615 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001616
1617 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001618}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001619
Mike Stump5d1d2022010-01-19 02:20:09 +00001620CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1621 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1622 // current block.
1623 CFGBlock* TrySuccessor = NULL;
1624
1625 if (Block) {
1626 if (!FinishBlock(Block))
1627 return 0;
1628 TrySuccessor = Block;
1629 } else TrySuccessor = Succ;
1630
Mike Stumpa1f93632010-01-20 01:15:34 +00001631 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001632
1633 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001634 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001635 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001636 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001637
Mike Stumpa1f93632010-01-20 01:15:34 +00001638 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001639 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1640 // The code after the try is the implicit successor.
1641 Succ = TrySuccessor;
1642 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001643 if (CS->getExceptionDecl() == 0) {
1644 HasCatchAll = true;
1645 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001646 Block = NULL;
1647 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1648 if (CatchBlock == 0)
1649 return 0;
1650 // Add this block to the list of successors for the block with the try
1651 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001652 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001653 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001654 if (!HasCatchAll) {
1655 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001656 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001657 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001658 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001659 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001660
1661 // The code after the try is the implicit successor.
1662 Succ = TrySuccessor;
1663
Mike Stumpf00cca52010-01-20 01:30:58 +00001664 // Save the current "try" context.
1665 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1666 TryTerminatedBlock = NewTryTerminatedBlock;
1667
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001668 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001669 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001670 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001671 return Block;
1672}
1673
1674CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1675 // CXXCatchStmt are treated like labels, so they are the first statement in a
1676 // block.
1677
1678 if (CS->getHandlerBlock())
1679 addStmt(CS->getHandlerBlock());
1680
1681 CFGBlock* CatchBlock = Block;
1682 if (!CatchBlock)
1683 CatchBlock = createBlock();
1684
1685 CatchBlock->setLabel(CS);
1686
1687 if (!FinishBlock(CatchBlock))
1688 return 0;
1689
1690 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1691 Block = NULL;
1692
1693 return CatchBlock;
1694}
1695
Ted Kremenek19bb3562007-08-28 19:26:49 +00001696CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001697 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001698 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001699
Ted Kremenek19bb3562007-08-28 19:26:49 +00001700 if (!IBlock) {
1701 IBlock = createBlock(false);
1702 cfg->setIndirectGotoBlock(IBlock);
1703 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001704
Ted Kremenek19bb3562007-08-28 19:26:49 +00001705 // IndirectGoto is a control-flow statement. Thus we stop processing the
1706 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001707 if (Block && !FinishBlock(Block))
1708 return 0;
1709
Ted Kremenek19bb3562007-08-28 19:26:49 +00001710 Block = createBlock(false);
1711 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001712 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001713 return addStmt(I->getTarget());
1714}
1715
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001716} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001717
Mike Stump6d9828c2009-07-17 01:31:16 +00001718/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1719/// no successors or predecessors. If this is the first block created in the
1720/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001721CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001722 bool first_block = begin() == end();
1723
1724 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001725 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1726 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1727 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001728
1729 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001730 if (first_block)
1731 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001732
1733 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001734 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001735}
1736
Ted Kremenek026473c2007-08-23 16:51:22 +00001737/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1738/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001739CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump4c45aa12010-01-21 15:20:48 +00001740 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001741 CFGBuilder Builder;
Mike Stump4c45aa12010-01-21 15:20:48 +00001742 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001743}
1744
Ted Kremenek63f58872007-10-01 19:33:33 +00001745//===----------------------------------------------------------------------===//
1746// CFG: Queries for BlkExprs.
1747//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001748
Ted Kremenek63f58872007-10-01 19:33:33 +00001749namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001750 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001751}
1752
Ted Kremenek8a693662009-12-23 23:37:10 +00001753static void FindSubExprAssignments(Stmt *S,
1754 llvm::SmallPtrSet<Expr*,50>& Set) {
1755 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001756 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001757
Ted Kremenek8a693662009-12-23 23:37:10 +00001758 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1759 Stmt *child = *I;
1760 if (!child)
1761 continue;
1762
1763 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001764 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001765
Ted Kremenek8a693662009-12-23 23:37:10 +00001766 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001767 }
1768}
1769
Ted Kremenek63f58872007-10-01 19:33:33 +00001770static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1771 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001772
1773 // Look for assignments that are used as subexpressions. These are the only
1774 // assignments that we want to *possibly* register as a block-level
1775 // expression. Basically, if an assignment occurs both in a subexpression and
1776 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001777 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001778
Ted Kremenek63f58872007-10-01 19:33:33 +00001779 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001780 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001781 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001782
Ted Kremenek411cdee2008-04-16 21:10:48 +00001783 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001784
1785 // Iterate over the statements again on identify the Expr* and Stmt* at the
1786 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001787
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001788 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001789 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001790
Ted Kremenek411cdee2008-04-16 21:10:48 +00001791 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001792 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001793 // expression are really "statements" whose value is never used by
1794 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001795 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001796 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001797 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1798 // Special handling for statement expressions. The last statement in
1799 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001800 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001801 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001802 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001803 (*M)[C->body_back()] = x;
1804 }
1805 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001806
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001807 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001808 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001809 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001810
Ted Kremenek411cdee2008-04-16 21:10:48 +00001811 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001812
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001813 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001814
Ted Kremenek390e48b2008-11-12 21:11:49 +00001815 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001816 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001817 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001818 }
1819 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001820
Ted Kremenek63f58872007-10-01 19:33:33 +00001821 return M;
1822}
1823
Ted Kremenek86946742008-01-17 20:48:37 +00001824CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1825 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001826 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001827
Ted Kremenek63f58872007-10-01 19:33:33 +00001828 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001829 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001830 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001831}
1832
1833unsigned CFG::getNumBlkExprs() {
1834 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1835 return M->size();
1836 else {
1837 // We assume callers interested in the number of BlkExprs will want
1838 // the map constructed if it doesn't already exist.
1839 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1840 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1841 }
1842}
1843
Ted Kremenek274f4332008-04-28 18:00:46 +00001844//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001845// Cleanup: CFG dstor.
1846//===----------------------------------------------------------------------===//
1847
Ted Kremenek63f58872007-10-01 19:33:33 +00001848CFG::~CFG() {
1849 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1850}
Mike Stump6d9828c2009-07-17 01:31:16 +00001851
Ted Kremenek7dba8602007-08-29 21:56:09 +00001852//===----------------------------------------------------------------------===//
1853// CFG pretty printing
1854//===----------------------------------------------------------------------===//
1855
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001856namespace {
1857
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001858class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001859 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1860 StmtMapTy StmtMap;
1861 signed CurrentBlock;
1862 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001863 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001864public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001865
Chris Lattnere4f21422009-06-30 01:26:17 +00001866 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1867 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001868 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1869 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001870 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001871 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001872 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001873 }
1874 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001875
Ted Kremenek42a509f2007-08-31 21:30:12 +00001876 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001877
Chris Lattnere4f21422009-06-30 01:26:17 +00001878 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001879 void setBlockID(signed i) { CurrentBlock = i; }
1880 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Ted Kremeneka95d3752008-09-13 05:16:45 +00001882 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001883
Ted Kremenek411cdee2008-04-16 21:10:48 +00001884 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001885
1886 if (I == StmtMap.end())
1887 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001888
1889 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001890 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001891 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001892 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001893
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001894 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001895 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001896 }
1897};
Chris Lattnere4f21422009-06-30 01:26:17 +00001898} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001899
Chris Lattnere4f21422009-06-30 01:26:17 +00001900
1901namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001902class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001903 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001904
Ted Kremeneka95d3752008-09-13 05:16:45 +00001905 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001906 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001907 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001908public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001909 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001910 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001911 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001912
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001913 void VisitIfStmt(IfStmt* I) {
1914 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001915 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001916 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001917
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001918 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001919 void VisitStmt(Stmt* Terminator) {
1920 Terminator->printPretty(OS, Helper, Policy);
1921 }
1922
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001923 void VisitForStmt(ForStmt* F) {
1924 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001925 if (F->getInit())
1926 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001927 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001928 if (Stmt* C = F->getCond())
1929 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001930 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001931 if (F->getInc())
1932 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001933 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001934 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001935
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001936 void VisitWhileStmt(WhileStmt* W) {
1937 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001938 if (Stmt* C = W->getCond())
1939 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001940 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001941
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001942 void VisitDoStmt(DoStmt* D) {
1943 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001944 if (Stmt* C = D->getCond())
1945 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001946 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001947
Ted Kremenek411cdee2008-04-16 21:10:48 +00001948 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001949 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001950 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001951 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001952
Mike Stump5d1d2022010-01-19 02:20:09 +00001953 void VisitCXXTryStmt(CXXTryStmt* CS) {
1954 OS << "try ...";
1955 }
1956
Ted Kremenek805e9a82007-08-31 21:49:40 +00001957 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001958 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001959 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001960 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001961
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001962 void VisitChooseExpr(ChooseExpr* C) {
1963 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001964 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001965 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001966 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001967
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001968 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1969 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001970 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001971 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Ted Kremenek805e9a82007-08-31 21:49:40 +00001973 void VisitBinaryOperator(BinaryOperator* B) {
1974 if (!B->isLogicalOp()) {
1975 VisitExpr(B);
1976 return;
1977 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001978
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001979 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001980
Ted Kremenek805e9a82007-08-31 21:49:40 +00001981 switch (B->getOpcode()) {
1982 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001983 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001984 return;
1985 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001986 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001987 return;
1988 default:
1989 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001990 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001991 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001992
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001993 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001994 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001995 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001996};
Chris Lattnere4f21422009-06-30 01:26:17 +00001997} // end anonymous namespace
1998
Mike Stump6d9828c2009-07-17 01:31:16 +00001999
Chris Lattnere4f21422009-06-30 01:26:17 +00002000static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002001 const CFGElement &E) {
2002 Stmt *Terminator = E;
2003
2004 if (E.asStartScope()) {
2005 OS << "start scope\n";
2006 return;
2007 }
2008 if (E.asEndScope()) {
2009 OS << "end scope\n";
2010 return;
2011 }
2012
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002013 if (Helper) {
2014 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002015 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002016 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002017
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002018 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002019 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002020 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002021 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002022 return;
2023 }
2024 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002025
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002026 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002027 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002028 if (B->getOpcode() == BinaryOperator::Comma) {
2029 OS << "... , ";
2030 Helper->handledStmt(B->getRHS(),OS);
2031 OS << '\n';
2032 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002033 }
2034 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002035 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002036
Chris Lattnere4f21422009-06-30 01:26:17 +00002037 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002038
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002039 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002040 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002041}
Mike Stump6d9828c2009-07-17 01:31:16 +00002042
Chris Lattnere4f21422009-06-30 01:26:17 +00002043static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2044 const CFGBlock& B,
2045 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002046
Ted Kremenek42a509f2007-08-31 21:30:12 +00002047 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002048
Ted Kremenek7dba8602007-08-29 21:56:09 +00002049 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002050 OS << "\n [ B" << B.getBlockID();
2051
Ted Kremenek42a509f2007-08-31 21:30:12 +00002052 if (&B == &cfg->getEntry())
2053 OS << " (ENTRY) ]\n";
2054 else if (&B == &cfg->getExit())
2055 OS << " (EXIT) ]\n";
2056 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002057 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002058 else
2059 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002060
Ted Kremenek9cffe732007-08-29 23:20:49 +00002061 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002062 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002063
2064 if (print_edges)
2065 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002066
Mike Stump079bd722010-01-19 22:00:14 +00002067 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002068 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002069 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002070 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002071 C->getLHS()->printPretty(OS, Helper,
2072 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002073 if (C->getRHS()) {
2074 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002075 C->getRHS()->printPretty(OS, Helper,
2076 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002077 }
Mike Stump079bd722010-01-19 22:00:14 +00002078 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002079 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002080 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002081 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002082 if (CS->getExceptionDecl())
2083 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2084 0);
2085 else
2086 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002087 OS << ")";
2088
2089 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002090 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002091
Ted Kremenek9cffe732007-08-29 23:20:49 +00002092 OS << ":\n";
2093 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002094
Ted Kremenekfddd5182007-08-21 21:42:03 +00002095 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002096 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002097
Ted Kremenek42a509f2007-08-31 21:30:12 +00002098 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2099 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002100
Ted Kremenek9cffe732007-08-29 23:20:49 +00002101 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002102 if (print_edges)
2103 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002104
Ted Kremeneka95d3752008-09-13 05:16:45 +00002105 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002106
Ted Kremenek42a509f2007-08-31 21:30:12 +00002107 if (Helper)
2108 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002109
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002110 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002111 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002112
Ted Kremenek9cffe732007-08-29 23:20:49 +00002113 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002114 if (B.getTerminator()) {
2115 if (print_edges)
2116 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002117
Ted Kremenek9cffe732007-08-29 23:20:49 +00002118 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002119
Ted Kremenek42a509f2007-08-31 21:30:12 +00002120 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002121
Chris Lattnere4f21422009-06-30 01:26:17 +00002122 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2123 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002124 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002125 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002126 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002127
Ted Kremenek9cffe732007-08-29 23:20:49 +00002128 if (print_edges) {
2129 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002130 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002131 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002132
Ted Kremenek42a509f2007-08-31 21:30:12 +00002133 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2134 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002135
Ted Kremenek42a509f2007-08-31 21:30:12 +00002136 if (i == 8 || (i-8) == 0)
2137 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002138
Ted Kremenek9cffe732007-08-29 23:20:49 +00002139 OS << " B" << (*I)->getBlockID();
2140 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002141
Ted Kremenek42a509f2007-08-31 21:30:12 +00002142 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002143
Ted Kremenek42a509f2007-08-31 21:30:12 +00002144 // Print the successors of this block.
2145 OS << " Successors (" << B.succ_size() << "):";
2146 i = 0;
2147
2148 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2149 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002150
Ted Kremenek42a509f2007-08-31 21:30:12 +00002151 if (i == 8 || (i-8) % 10 == 0)
2152 OS << "\n ";
2153
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002154 if (*I)
2155 OS << " B" << (*I)->getBlockID();
2156 else
2157 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002158 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002159
Ted Kremenek9cffe732007-08-29 23:20:49 +00002160 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002161 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002162}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002163
Ted Kremenek42a509f2007-08-31 21:30:12 +00002164
2165/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002166void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002167
2168/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002169void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2170 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002171
Ted Kremenek42a509f2007-08-31 21:30:12 +00002172 // Print the entry block.
2173 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002174
Ted Kremenek42a509f2007-08-31 21:30:12 +00002175 // Iterate through the CFGBlocks and print them one by one.
2176 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2177 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002178 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002179 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002180
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002181 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002182 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002183
Ted Kremenek42a509f2007-08-31 21:30:12 +00002184 // Print the exit block.
2185 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002186 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002187}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002188
2189/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002190void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2191 print(llvm::errs(), cfg, LO);
2192}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002193
2194/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2195/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002196void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2197 const LangOptions &LO) const {
2198 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002199 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002200}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002201
Ted Kremeneka2925852008-01-30 23:02:42 +00002202/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002203void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002204 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002205 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002206 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2207}
2208
Ted Kremenek390e48b2008-11-12 21:11:49 +00002209Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002210
Ted Kremenek411cdee2008-04-16 21:10:48 +00002211 if (!Terminator)
2212 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002213
Ted Kremenek411cdee2008-04-16 21:10:48 +00002214 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002215
Ted Kremenek411cdee2008-04-16 21:10:48 +00002216 switch (Terminator->getStmtClass()) {
2217 default:
2218 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002219
Ted Kremenek411cdee2008-04-16 21:10:48 +00002220 case Stmt::ForStmtClass:
2221 E = cast<ForStmt>(Terminator)->getCond();
2222 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002223
Ted Kremenek411cdee2008-04-16 21:10:48 +00002224 case Stmt::WhileStmtClass:
2225 E = cast<WhileStmt>(Terminator)->getCond();
2226 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002227
Ted Kremenek411cdee2008-04-16 21:10:48 +00002228 case Stmt::DoStmtClass:
2229 E = cast<DoStmt>(Terminator)->getCond();
2230 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002231
Ted Kremenek411cdee2008-04-16 21:10:48 +00002232 case Stmt::IfStmtClass:
2233 E = cast<IfStmt>(Terminator)->getCond();
2234 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002235
Ted Kremenek411cdee2008-04-16 21:10:48 +00002236 case Stmt::ChooseExprClass:
2237 E = cast<ChooseExpr>(Terminator)->getCond();
2238 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002239
Ted Kremenek411cdee2008-04-16 21:10:48 +00002240 case Stmt::IndirectGotoStmtClass:
2241 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2242 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002243
Ted Kremenek411cdee2008-04-16 21:10:48 +00002244 case Stmt::SwitchStmtClass:
2245 E = cast<SwitchStmt>(Terminator)->getCond();
2246 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002247
Ted Kremenek411cdee2008-04-16 21:10:48 +00002248 case Stmt::ConditionalOperatorClass:
2249 E = cast<ConditionalOperator>(Terminator)->getCond();
2250 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002251
Ted Kremenek411cdee2008-04-16 21:10:48 +00002252 case Stmt::BinaryOperatorClass: // '&&' and '||'
2253 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002254 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002255
Ted Kremenek390e48b2008-11-12 21:11:49 +00002256 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002257 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002258 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002259
Ted Kremenek411cdee2008-04-16 21:10:48 +00002260 return E ? E->IgnoreParens() : NULL;
2261}
2262
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002263bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002264
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002265 if (!Terminator)
2266 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002267
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002268 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002269
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002270 switch (Terminator->getStmtClass()) {
2271 default:
2272 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002273
2274 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002275 case Stmt::WhileStmtClass:
2276 case Stmt::DoStmtClass:
2277 case Stmt::IfStmtClass:
2278 case Stmt::ChooseExprClass:
2279 case Stmt::ConditionalOperatorClass:
2280 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002281 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002282 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002283
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002284 return E ? E->IgnoreParens() : NULL;
2285}
2286
Ted Kremeneka2925852008-01-30 23:02:42 +00002287
Ted Kremenek7dba8602007-08-29 21:56:09 +00002288//===----------------------------------------------------------------------===//
2289// CFG Graphviz Visualization
2290//===----------------------------------------------------------------------===//
2291
Ted Kremenek42a509f2007-08-31 21:30:12 +00002292
2293#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002294static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002295#endif
2296
Chris Lattnere4f21422009-06-30 01:26:17 +00002297void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002298#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002299 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002300 GraphHelper = &H;
2301 llvm::ViewGraph(this,"CFG");
2302 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002303#endif
2304}
2305
Ted Kremenek7dba8602007-08-29 21:56:09 +00002306namespace llvm {
2307template<>
2308struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002309
2310 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2311
2312 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002313
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002314#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002315 std::string OutSStr;
2316 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002317 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002318 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002319
2320 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2321
2322 // Process string output to make it nicer...
2323 for (unsigned i = 0; i != OutStr.length(); ++i)
2324 if (OutStr[i] == '\n') { // Left justify
2325 OutStr[i] = '\\';
2326 OutStr.insert(OutStr.begin()+i+1, 'l');
2327 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002328
Ted Kremenek7dba8602007-08-29 21:56:09 +00002329 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002330#else
2331 return "";
2332#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002333 }
2334};
2335} // end namespace llvm