blob: f94f6b3f127fe6d06d6503b44138e1061759f317 [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 Kremenek852274d2009-12-16 03:18:58 +0000111 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000112 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000113 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000114 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000115 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
116 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000117 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump5d1d2022010-01-19 02:20:09 +0000118 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
Mike Stump0979d802009-07-22 22:56:04 +0000119 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Mike Stump5d1d2022010-01-19 02:20:09 +0000120 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Ted Kremenek4f880632009-07-17 22:18:43 +0000121 CFGBlock *VisitDeclStmt(DeclStmt *DS);
122 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000123 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
124 CFGBlock *VisitDoStmt(DoStmt *D);
125 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000126 CFGBlock *VisitGotoStmt(GotoStmt* G);
127 CFGBlock *VisitIfStmt(IfStmt *I);
128 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
129 CFGBlock *VisitLabelStmt(LabelStmt *L);
130 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
131 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
132 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
133 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
134 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
135 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000136 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
137 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000138 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
139 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000140
Ted Kremenek852274d2009-12-16 03:18:58 +0000141 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
142 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000143 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000144
Ted Kremenek274f4332008-04-28 18:00:46 +0000145 // NYS == Not Yet Supported
146 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000147 badCFG = true;
148 return Block;
149 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000150
Mike Stump079bd722010-01-19 22:00:14 +0000151 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
152 if (!AddScopes)
153 return B;
154
155 if (B == 0)
156 B = createBlock();
157 B->StartScope(S, cfg->getBumpVectorContext());
158 return B;
159 }
160
161 void EndScope(Stmt *S) {
162 if (!AddScopes)
163 return;
164
165 if (Block == 0)
166 Block = createBlock();
167 Block->EndScope(S, cfg->getBumpVectorContext());
168 }
169
Ted Kremenek4f880632009-07-17 22:18:43 +0000170 void autoCreateBlock() { if (!Block) Block = createBlock(); }
171 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000172 bool FinishBlock(CFGBlock* B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000173 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
174 return Visit(S, asc);
175 }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000176
Ted Kremenek852274d2009-12-16 03:18:58 +0000177 void AppendStmt(CFGBlock *B, Stmt *S,
178 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
179 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000180 }
181
182 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
183 B->addSuccessor(S, cfg->getBumpVectorContext());
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000186 /// TryResult - a class representing a variant over the values
187 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
188 /// and is used by the CFGBuilder to decide if a branch condition
189 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000190 class TryResult {
191 int X;
192 public:
193 TryResult(bool b) : X(b ? 1 : 0) {}
194 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Ted Kremenek941fde82009-07-24 04:47:11 +0000196 bool isTrue() const { return X == 1; }
197 bool isFalse() const { return X == 0; }
198 bool isKnown() const { return X >= 0; }
199 void negate() {
200 assert(isKnown());
201 X ^= 0x1;
202 }
203 };
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Mike Stump00998a02009-07-23 23:25:26 +0000205 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
206 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000207 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000208 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000209 if (!S->isTypeDependent() && !S->isValueDependent() &&
210 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000211 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000212
213 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000214 }
215
Ted Kremenek4102af92008-03-13 03:04:22 +0000216 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000217
218 // True iff EH edges on CallExprs should be added to the CFG.
219 bool AddEHEdges;
220
221 // True iff scope start and scope end notes should be added to the CFG.
Mike Stump079bd722010-01-19 22:00:14 +0000222 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000223};
Mike Stump6d9828c2009-07-17 01:31:16 +0000224
Douglas Gregor898574e2008-12-05 23:32:09 +0000225// FIXME: Add support for dependent-sized array types in C++?
226// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000227static VariableArrayType* FindVA(Type* t) {
228 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
229 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
230 if (vat->getSizeExpr())
231 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000232
Ted Kremenek610a09e2008-09-26 22:58:57 +0000233 t = vt->getElementType().getTypePtr();
234 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000235
Ted Kremenek610a09e2008-09-26 22:58:57 +0000236 return 0;
237}
Mike Stump6d9828c2009-07-17 01:31:16 +0000238
239/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
240/// arbitrary statement. Examples include a single expression or a function
241/// body (compound statement). The ownership of the returned CFG is
242/// transferred to the caller. If CFG construction fails, this method returns
243/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000244CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Mike Stump55f988e2010-01-21 17:21:23 +0000245 bool addehedges, bool AddScopes) {
246 AddEHEdges = addehedges;
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000247 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000248 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000249 if (!Statement)
250 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000251
Mike Stump079bd722010-01-19 22:00:14 +0000252 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000253 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000254
255 // Create an empty block that will serve as the exit block for the CFG. Since
256 // this is the first block added to the CFG, it will be implicitly registered
257 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000258 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000259 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000260 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000261
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000262 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000263 CFGBlock* B = addStmt(Statement);
Mike Stumpb978a442010-01-21 02:21:40 +0000264
265 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
266 // FIXME: Add code for base initializers and member initializers.
267 (void)CD;
268 }
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000269 if (!B)
270 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000271
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000272 if (B) {
273 // Finalize the last constructed block. This usually involves reversing the
274 // order of the statements in the block.
275 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000276
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000277 // Backpatch the gotos whose label -> block mappings we didn't know when we
278 // encountered them.
279 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000280 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000281
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000282 CFGBlock* B = *I;
283 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
284 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000285
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000286 // If there is no target for the goto, then we are looking at an
287 // incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000288 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000289
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000290 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000291 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000292
Daniel Dunbarc3daac52010-02-22 05:58:59 +0000293 // Add successors to the Indirect Goto Dispatch block (if we have one).
294 if (CFGBlock* B = cfg->getIndirectGotoBlock())
295 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
296 E = AddressTakenLabels.end(); I != E; ++I ) {
297
298 // Lookup the target block.
299 LabelMapTy::iterator LI = LabelMap.find(*I);
300
301 // If there is no target block that contains label, then we are looking
302 // at an incomplete AST. Handle this by not registering a successor.
303 if (LI == LabelMap.end()) continue;
304
305 AddSuccessor(B, LI->second);
306 }
307
308 Succ = B;
309 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000310
311 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000312 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000313
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000314 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000315}
Mike Stump6d9828c2009-07-17 01:31:16 +0000316
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000317/// createBlock - Used to lazily create blocks that are connected
318/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000319CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000320 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000321 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000322 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000323 return B;
324}
Mike Stump6d9828c2009-07-17 01:31:16 +0000325
Ted Kremenek6c249722009-09-24 18:45:41 +0000326/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000327bool CFGBuilder::FinishBlock(CFGBlock* B) {
328 if (badCFG)
329 return false;
330
Ted Kremenek4f880632009-07-17 22:18:43 +0000331 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000332 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000333}
334
Ted Kremenek4f880632009-07-17 22:18:43 +0000335/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000336/// blocks for ternary operators, &&, and ||. We also process "," and
337/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000338CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000339tryAgain:
340 switch (S->getStmtClass()) {
341 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000342 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000343
344 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000345 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Ted Kremenek4f880632009-07-17 22:18:43 +0000347 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000348 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek4f880632009-07-17 22:18:43 +0000350 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000351 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000352
Ted Kremenek4f880632009-07-17 22:18:43 +0000353 case Stmt::BreakStmtClass:
354 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000357 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Ted Kremenek4f880632009-07-17 22:18:43 +0000359 case Stmt::CaseStmtClass:
360 return VisitCaseStmt(cast<CaseStmt>(S));
361
362 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000363 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Ted Kremenek4f880632009-07-17 22:18:43 +0000365 case Stmt::CompoundStmtClass:
366 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek4f880632009-07-17 22:18:43 +0000368 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000369 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Ted Kremenek4f880632009-07-17 22:18:43 +0000371 case Stmt::ContinueStmtClass:
372 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Ted Kremenek021c8af2010-01-19 20:40:33 +0000374 case Stmt::CXXCatchStmtClass:
375 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
376
377 case Stmt::CXXThrowExprClass:
378 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
379
380 case Stmt::CXXTryStmtClass:
381 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
382
Ted Kremenek4f880632009-07-17 22:18:43 +0000383 case Stmt::DeclStmtClass:
384 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek4f880632009-07-17 22:18:43 +0000386 case Stmt::DefaultStmtClass:
387 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek4f880632009-07-17 22:18:43 +0000389 case Stmt::DoStmtClass:
390 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek4f880632009-07-17 22:18:43 +0000392 case Stmt::ForStmtClass:
393 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek4f880632009-07-17 22:18:43 +0000395 case Stmt::GotoStmtClass:
396 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek4f880632009-07-17 22:18:43 +0000398 case Stmt::IfStmtClass:
399 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremenek4f880632009-07-17 22:18:43 +0000401 case Stmt::IndirectGotoStmtClass:
402 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Ted Kremenek4f880632009-07-17 22:18:43 +0000404 case Stmt::LabelStmtClass:
405 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Ted Kremenek4f880632009-07-17 22:18:43 +0000407 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000408 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
409
Ted Kremenek4f880632009-07-17 22:18:43 +0000410 case Stmt::ObjCAtSynchronizedStmtClass:
411 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek4f880632009-07-17 22:18:43 +0000413 case Stmt::ObjCAtThrowStmtClass:
414 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek4f880632009-07-17 22:18:43 +0000416 case Stmt::ObjCAtTryStmtClass:
417 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek4f880632009-07-17 22:18:43 +0000419 case Stmt::ObjCForCollectionStmtClass:
420 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek4f880632009-07-17 22:18:43 +0000422 case Stmt::ParenExprClass:
423 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000424 goto tryAgain;
425
Ted Kremenek4f880632009-07-17 22:18:43 +0000426 case Stmt::NullStmtClass:
427 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Ted Kremenek4f880632009-07-17 22:18:43 +0000429 case Stmt::ReturnStmtClass:
430 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek4f880632009-07-17 22:18:43 +0000432 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000433 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek4f880632009-07-17 22:18:43 +0000435 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000436 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek4f880632009-07-17 22:18:43 +0000438 case Stmt::SwitchStmtClass:
439 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Ted Kremenek4f880632009-07-17 22:18:43 +0000441 case Stmt::WhileStmtClass:
442 return VisitWhileStmt(cast<WhileStmt>(S));
443 }
444}
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek852274d2009-12-16 03:18:58 +0000446CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
447 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000448 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000449 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek4f880632009-07-17 22:18:43 +0000452 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000453}
Mike Stump6d9828c2009-07-17 01:31:16 +0000454
Ted Kremenek4f880632009-07-17 22:18:43 +0000455/// VisitChildren - Visit the children of a Stmt.
456CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
457 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000458 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000459 E = Terminator->child_end(); I != E; ++I) {
460 if (*I) B = Visit(*I);
461 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000462 return B;
463}
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Ted Kremenek852274d2009-12-16 03:18:58 +0000465CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
466 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000467 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000468
Ted Kremenek852274d2009-12-16 03:18:58 +0000469 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000470 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000471 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000472 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000473
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000474 return Block;
475}
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Ted Kremenek852274d2009-12-16 03:18:58 +0000477CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
478 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000479 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000480 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000481 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek4f880632009-07-17 22:18:43 +0000483 if (!FinishBlock(ConfluenceBlock))
484 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek4f880632009-07-17 22:18:43 +0000486 // create the block evaluating the LHS
487 CFGBlock* LHSBlock = createBlock(false);
488 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Ted Kremenek4f880632009-07-17 22:18:43 +0000490 // create the block evaluating the RHS
491 Succ = ConfluenceBlock;
492 Block = NULL;
493 CFGBlock* RHSBlock = addStmt(B->getRHS());
494 if (!FinishBlock(RHSBlock))
495 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Mike Stump00998a02009-07-23 23:25:26 +0000497 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000498 TryResult KnownVal = TryEvaluateBool(B->getLHS());
499 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
500 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000501
Ted Kremenek4f880632009-07-17 22:18:43 +0000502 // Now link the LHSBlock with RHSBlock.
503 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000504 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
505 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000506 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000507 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000508 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
509 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek4f880632009-07-17 22:18:43 +0000512 // Generate the blocks for evaluating the LHS.
513 Block = LHSBlock;
514 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000515 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000516 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000517 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000518 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000519 addStmt(B->getRHS());
520 return addStmt(B->getLHS());
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Ted Kremenek852274d2009-12-16 03:18:58 +0000523 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000524}
525
Ted Kremenek852274d2009-12-16 03:18:58 +0000526CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
527 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000528 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000529 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000530 }
531 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000532}
533
Ted Kremenek4f880632009-07-17 22:18:43 +0000534CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
535 // "break" is a control-flow statement. Thus we stop processing the current
536 // block.
537 if (Block && !FinishBlock(Block))
538 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek4f880632009-07-17 22:18:43 +0000540 // Now create a new block that ends with the break statement.
541 Block = createBlock(false);
542 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Ted Kremenek4f880632009-07-17 22:18:43 +0000544 // If there is no target for the break, then we are looking at an incomplete
545 // AST. This means that the CFG cannot be constructed.
546 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000547 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000548 else
549 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
551
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000552 return Block;
553}
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Mike Stump4c45aa12010-01-21 15:20:48 +0000555static bool CanThrow(Expr *E) {
556 QualType Ty = E->getType();
557 if (Ty->isFunctionPointerType())
558 Ty = Ty->getAs<PointerType>()->getPointeeType();
559 else if (Ty->isBlockPointerType())
560 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
561
562 const FunctionType *FT = Ty->getAs<FunctionType>();
563 if (FT) {
564 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
565 if (Proto->hasEmptyExceptionSpec())
566 return false;
567 }
568 return true;
569}
570
Ted Kremenek852274d2009-12-16 03:18:58 +0000571CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000572 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000573 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000574 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000575 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000576 }
Mike Stump24556362009-07-25 21:26:53 +0000577
Mike Stump4c45aa12010-01-21 15:20:48 +0000578 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000579
580 // Languages without exceptions are assumed to not throw.
581 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000582 if (AddEHEdges)
583 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000584 }
585
586 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000587 if (FD->hasAttr<NoReturnAttr>())
588 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000589 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000590 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000591 }
Mike Stump24556362009-07-25 21:26:53 +0000592
Mike Stump4c45aa12010-01-21 15:20:48 +0000593 if (!CanThrow(C->getCallee()))
594 AddEHEdge = false;
595
596 if (!NoReturn && !AddEHEdge)
Zhongxing Xu91071662010-02-24 02:19:28 +0000597 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Mike Stump079bd722010-01-19 22:00:14 +0000599 if (Block) {
600 Succ = Block;
601 if (!FinishBlock(Block))
602 return 0;
603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Mike Stump079bd722010-01-19 22:00:14 +0000605 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000606 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000607
Mike Stump079bd722010-01-19 22:00:14 +0000608 if (NoReturn) {
609 // Wire this to the exit block directly.
610 AddSuccessor(Block, &cfg->getExit());
611 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000612 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000613 // Add exceptional edges.
614 if (TryTerminatedBlock)
615 AddSuccessor(Block, TryTerminatedBlock);
616 else
617 AddSuccessor(Block, &cfg->getExit());
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Mike Stump24556362009-07-25 21:26:53 +0000620 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000621}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000622
Ted Kremenek852274d2009-12-16 03:18:58 +0000623CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
624 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000625 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000626 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000627 if (!FinishBlock(ConfluenceBlock))
628 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000630 Succ = ConfluenceBlock;
631 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000632 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000633 if (!FinishBlock(LHSBlock))
634 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000636 Succ = ConfluenceBlock;
637 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000638 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000639 if (!FinishBlock(RHSBlock))
640 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000642 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000643 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000644 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000645 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
646 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000647 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000648 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000649}
Mike Stump1eb44332009-09-09 15:08:12 +0000650
651
652CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000653 EndScope(C);
654
Mike Stump1eb44332009-09-09 15:08:12 +0000655 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000656
657 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
658 I != E; ++I ) {
659 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000661 if (badCFG)
662 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000663 }
Mike Stump079bd722010-01-19 22:00:14 +0000664
665 LastBlock = StartScope(C, LastBlock);
666
Ted Kremenek4f880632009-07-17 22:18:43 +0000667 return LastBlock;
668}
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremenek852274d2009-12-16 03:18:58 +0000670CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
671 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000672 // Create the confluence block that will "merge" the results of the ternary
673 // expression.
674 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000675 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000676 if (!FinishBlock(ConfluenceBlock))
677 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000679 // Create a block for the LHS expression if there is an LHS expression. A
680 // GCC extension allows LHS to be NULL, causing the condition to be the
681 // value that is returned instead.
682 // e.g: x ?: y is shorthand for: x ? x : y;
683 Succ = ConfluenceBlock;
684 Block = NULL;
685 CFGBlock* LHSBlock = NULL;
686 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000687 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000688 if (!FinishBlock(LHSBlock))
689 return 0;
690 Block = NULL;
691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000693 // Create the block for the RHS expression.
694 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000695 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000696 if (!FinishBlock(RHSBlock))
697 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000699 // Create the block that will contain the condition.
700 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Mike Stump00998a02009-07-23 23:25:26 +0000702 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000703 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000704 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000705 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000706 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000707 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000708 // If we know the condition is false, add NULL as the successor for
709 // the block containing the condition. In this case, the confluence
710 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000711 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000712 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000713 } else {
714 // If we have no LHS expression, add the ConfluenceBlock as a direct
715 // successor for the block containing the condition. Moreover, we need to
716 // reverse the order of the predecessors in the ConfluenceBlock because
717 // the RHSBlock will have been added to the succcessors already, and we
718 // want the first predecessor to the the block containing the expression
719 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000720 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000721 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000722 std::reverse(ConfluenceBlock->pred_begin(),
723 ConfluenceBlock->pred_end());
724 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000727 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000728 Block->setTerminator(C);
729 return addStmt(C->getCond());
730}
731
Ted Kremenek4f880632009-07-17 22:18:43 +0000732CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
733 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000734
Ted Kremenek4f880632009-07-17 22:18:43 +0000735 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000736 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000737 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek4f880632009-07-17 22:18:43 +0000740 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek4f880632009-07-17 22:18:43 +0000742 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
743 typedef llvm::SmallVector<Decl*,10> BufTy;
744 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek4f880632009-07-17 22:18:43 +0000746 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
747 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
748 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
749 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremenek4f880632009-07-17 22:18:43 +0000751 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
752 // automatically freed with the CFG.
753 DeclGroupRef DG(*I);
754 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000755 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000756 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek4f880632009-07-17 22:18:43 +0000758 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000759 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000760 B = VisitDeclSubExpr(D);
761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762
763 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000764}
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Ted Kremenek4f880632009-07-17 22:18:43 +0000766/// VisitDeclSubExpr - Utility method to add block-level expressions for
767/// initializers in Decls.
768CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
769 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000770
Ted Kremenek4f880632009-07-17 22:18:43 +0000771 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek4f880632009-07-17 22:18:43 +0000773 if (!VD)
774 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek4f880632009-07-17 22:18:43 +0000776 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Ted Kremenek4f880632009-07-17 22:18:43 +0000778 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000779 AddStmtChoice::Kind k =
780 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
781 : AddStmtChoice::NotAlwaysAdd;
782 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek4f880632009-07-17 22:18:43 +0000785 // If the type of VD is a VLA, then we must process its size expressions.
786 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
787 VA = FindVA(VA->getElementType().getTypePtr()))
788 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek4f880632009-07-17 22:18:43 +0000790 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000791}
792
793CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000794 // We may see an if statement in the middle of a basic block, or it may be the
795 // first statement we are processing. In either case, we create a new basic
796 // block. First, we create the blocks for the then...else statements, and
797 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000798 // middle of a block, we stop processing that block. That block is then the
799 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000800
801 // The block we were proccessing is now finished. Make it the successor
802 // block.
803 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000804 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000805 if (!FinishBlock(Block))
806 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000807 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000808
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000809 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000810 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000811
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000812 if (Stmt* Else = I->getElse()) {
813 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000814
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000815 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000816 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000817 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000818 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000819
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000820 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
821 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000822 else if (Block) {
823 if (!FinishBlock(ElseBlock))
824 return 0;
825 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000827
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000828 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000829 CFGBlock* ThenBlock;
830 {
831 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000832 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000833 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000834 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000835 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000836
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000837 if (!ThenBlock) {
838 // We can reach here if the "then" body has all NullStmts.
839 // Create an empty block so we can distinguish between true and false
840 // branches in path-sensitive analyses.
841 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000842 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000843 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000844 if (!FinishBlock(ThenBlock))
845 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000846 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000847 }
848
Mike Stump6d9828c2009-07-17 01:31:16 +0000849 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000850 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000851
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000852 // Set the terminator of the new block to the If statement.
853 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000854
Mike Stump00998a02009-07-23 23:25:26 +0000855 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000856 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000857
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000858 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000859 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
860 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000861
862 // Add the condition as the last statement in the new block. This may create
863 // new blocks as the condition may contain control-flow. Any newly created
864 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000865 Block = addStmt(I->getCond());
866
867 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
868 // and the condition variable initialization to the CFG.
869 if (VarDecl *VD = I->getConditionVariable()) {
870 if (Expr *Init = VD->getInit()) {
871 autoCreateBlock();
872 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
873 addStmt(Init);
874 }
875 }
876
877 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000878}
Mike Stump6d9828c2009-07-17 01:31:16 +0000879
880
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000881CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000882 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000883 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000884 // NOTE: If a "return" appears in the middle of a block, this means that the
885 // code afterwards is DEAD (unreachable). We still keep a basic block
886 // for that code; a simple "mark-and-sweep" from the entry block will be
887 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000888 if (Block)
889 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000890
891 // Create the new block.
892 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000893
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000894 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000895 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000896
897 // Add the return statement to the block. This may create new blocks if R
898 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000899 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000900}
901
902CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
903 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000904 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000905 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000906
Ted Kremenek4f880632009-07-17 22:18:43 +0000907 if (!LabelBlock) // This can happen when the body is empty, i.e.
908 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000909
Ted Kremenek4f880632009-07-17 22:18:43 +0000910 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000911 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000912
913 // Labels partition blocks, so this is the end of the basic block we were
914 // processing (L is the block's label). Because this is label (and we have
915 // already processed the substatement) there is no extra control-flow to worry
916 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000917 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000918 if (!FinishBlock(LabelBlock))
919 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000920
921 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000922 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000923
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000924 // This block is now the implicit successor of other blocks.
925 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000926
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000927 return LabelBlock;
928}
929
930CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000931 // Goto is a control-flow statement. Thus we stop processing the current
932 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000933 if (Block)
934 FinishBlock(Block);
935
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000936 Block = createBlock(false);
937 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000938
939 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000940 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000941
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000942 if (I == LabelMap.end())
943 // We will need to backpatch this block later.
944 BackpatchBlocks.push_back(Block);
945 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000946 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000947
Mike Stump6d9828c2009-07-17 01:31:16 +0000948 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000949}
950
951CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000952 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000953
Mike Stumpfefb9f72009-07-21 01:12:51 +0000954 // "for" is a control-flow statement. Thus we stop processing the current
955 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000956 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000957 if (!FinishBlock(Block))
958 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000959 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000960 } else
961 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000962
963 // Because of short-circuit evaluation, the condition of the loop can span
964 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
965 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000966 CFGBlock* ExitConditionBlock = createBlock(false);
967 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000968
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000969 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000970 ExitConditionBlock->setTerminator(F);
971
972 // Now add the actual condition to the condition block. Because the condition
973 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000974 if (Stmt* C = F->getCond()) {
975 Block = ExitConditionBlock;
976 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000977 assert(Block == EntryConditionBlock);
978
979 // If this block contains a condition variable, add both the condition
980 // variable and initializer to the CFG.
981 if (VarDecl *VD = F->getConditionVariable()) {
982 if (Expr *Init = VD->getInit()) {
983 autoCreateBlock();
984 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
985 EntryConditionBlock = addStmt(Init);
986 assert(Block == EntryConditionBlock);
987 }
988 }
989
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000990 if (Block) {
991 if (!FinishBlock(EntryConditionBlock))
992 return 0;
993 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000994 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000995
Mike Stump6d9828c2009-07-17 01:31:16 +0000996 // The condition block is the implicit successor for the loop body as well as
997 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000998 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000999
Mike Stump00998a02009-07-23 23:25:26 +00001000 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001001 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Mike Stump00998a02009-07-23 23:25:26 +00001003 if (F->getCond())
1004 KnownVal = TryEvaluateBool(F->getCond());
1005
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001006 // Now create the loop body.
1007 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001008 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001009
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001010 // Save the current values for Block, Succ, and continue and break targets
1011 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001012 save_continue(ContinueTargetBlock),
1013 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001014
Ted Kremenekaf603f72007-08-30 18:39:40 +00001015 // Create a new block to contain the (bottom) of the loop body.
1016 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001017
Ted Kremeneke9334502008-09-04 21:48:47 +00001018 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001019 // Generate increment code in its own basic block. This is the target of
1020 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001021 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001022 } else {
1023 // No increment code. Create a special, empty, block that is used as the
1024 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001025 assert(Succ == EntryConditionBlock);
1026 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001027 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001028
Ted Kremenek3575f842009-04-28 00:51:56 +00001029 // Finish up the increment (or empty) block if it hasn't been already.
1030 if (Block) {
1031 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001032 if (!FinishBlock(Block))
1033 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001034 Block = 0;
1035 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001036
Ted Kremenek3575f842009-04-28 00:51:56 +00001037 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001038
Ted Kremenek3575f842009-04-28 00:51:56 +00001039 // The starting block for the loop increment is the block that should
1040 // represent the 'loop target' for looping back to the start of the loop.
1041 ContinueTargetBlock->setLoopTarget(F);
1042
Ted Kremeneke9334502008-09-04 21:48:47 +00001043 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +00001044 BreakTargetBlock = LoopSuccessor;
1045
1046 // Now populate the body block, and in the process create new blocks as we
1047 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001048 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001049
1050 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001051 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001052 else if (Block && !FinishBlock(BodyBlock))
1053 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001054
Ted Kremenek941fde82009-07-24 04:47:11 +00001055 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001056 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001057 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001058
Ted Kremenek941fde82009-07-24 04:47:11 +00001059 // Link up the condition block with the code that follows the loop. (the
1060 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001061 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001062
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001063 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001064 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001065 if (Stmt* I = F->getInit()) {
1066 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001067 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001068 } else {
1069 // There is no loop initialization. We are thus basically a while loop.
1070 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001071 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001072 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001073 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001074 }
1075}
1076
Ted Kremenek514de5a2008-11-11 17:10:00 +00001077CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1078 // Objective-C fast enumeration 'for' statements:
1079 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1080 //
1081 // for ( Type newVariable in collection_expression ) { statements }
1082 //
1083 // becomes:
1084 //
1085 // prologue:
1086 // 1. collection_expression
1087 // T. jump to loop_entry
1088 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001089 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001090 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1091 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1092 // TB:
1093 // statements
1094 // T. jump to loop_entry
1095 // FB:
1096 // what comes after
1097 //
1098 // and
1099 //
1100 // Type existingItem;
1101 // for ( existingItem in expression ) { statements }
1102 //
1103 // becomes:
1104 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001105 // the same with newVariable replaced with existingItem; the binding works
1106 // the same except that for one ObjCForCollectionStmt::getElement() returns
1107 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001108 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001109
Ted Kremenek514de5a2008-11-11 17:10:00 +00001110 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001111
Ted Kremenek514de5a2008-11-11 17:10:00 +00001112 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001113 if (!FinishBlock(Block))
1114 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001115 LoopSuccessor = Block;
1116 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001117 } else
1118 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001119
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001120 // Build the condition blocks.
1121 CFGBlock* ExitConditionBlock = createBlock(false);
1122 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001123
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001124 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001125 ExitConditionBlock->setTerminator(S);
1126
1127 // The last statement in the block should be the ObjCForCollectionStmt, which
1128 // performs the actual binding to 'element' and determines if there are any
1129 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001130 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001131 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001132
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001133 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001134 // generate new blocks as necesary. We DON'T add the statement by default to
1135 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001136 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001137 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001138 if (!FinishBlock(EntryConditionBlock))
1139 return 0;
1140 Block = 0;
1141 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001142
1143 // The condition block is the implicit successor for the loop body as well as
1144 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001145 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001146
Ted Kremenek514de5a2008-11-11 17:10:00 +00001147 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001148 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001149 // Save the current values for Succ, continue and break targets.
1150 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001151 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1152
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001153 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001154 ContinueTargetBlock = EntryConditionBlock;
1155
Ted Kremenek4f880632009-07-17 22:18:43 +00001156 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001157
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001158 if (!BodyBlock)
1159 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001160 else if (Block) {
1161 if (!FinishBlock(BodyBlock))
1162 return 0;
1163 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001164
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001165 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001166 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001167 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001168
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001169 // Link up the condition block with the code that follows the loop.
1170 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001171 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001172
Ted Kremenek514de5a2008-11-11 17:10:00 +00001173 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001174 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001175 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001176}
1177
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001178CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1179 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001180
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001181 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001182 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001183
Ted Kremenekda5348e2009-05-05 23:11:51 +00001184 // The sync body starts its own basic block. This makes it a little easier
1185 // for diagnostic clients.
1186 if (SyncBlock) {
1187 if (!FinishBlock(SyncBlock))
1188 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001189
Ted Kremenekda5348e2009-05-05 23:11:51 +00001190 Block = 0;
1191 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001192
Ted Kremenekda5348e2009-05-05 23:11:51 +00001193 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001194
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001195 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001196 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001197}
Mike Stump6d9828c2009-07-17 01:31:16 +00001198
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001199CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001200 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001201 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001202}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001203
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001204CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001205 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001206
Mike Stumpfefb9f72009-07-21 01:12:51 +00001207 // "while" is a control-flow statement. Thus we stop processing the current
1208 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001209 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001210 if (!FinishBlock(Block))
1211 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001212 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001213 } else
1214 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001215
1216 // Because of short-circuit evaluation, the condition of the loop can span
1217 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1218 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001219 CFGBlock* ExitConditionBlock = createBlock(false);
1220 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001221
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001222 // Set the terminator for the "exit" condition block.
1223 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001224
1225 // Now add the actual condition to the condition block. Because the condition
1226 // itself may contain control-flow, new blocks may be created. Thus we update
1227 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001228 if (Stmt* C = W->getCond()) {
1229 Block = ExitConditionBlock;
1230 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001231 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001232
1233 // If this block contains a condition variable, add both the condition
1234 // variable and initializer to the CFG.
1235 if (VarDecl *VD = W->getConditionVariable()) {
1236 if (Expr *Init = VD->getInit()) {
1237 autoCreateBlock();
1238 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1239 EntryConditionBlock = addStmt(Init);
1240 assert(Block == EntryConditionBlock);
1241 }
1242 }
1243
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001244 if (Block) {
1245 if (!FinishBlock(EntryConditionBlock))
1246 return 0;
1247 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001248 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001249
1250 // The condition block is the implicit successor for the loop body as well as
1251 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001252 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001253
Mike Stump00998a02009-07-23 23:25:26 +00001254 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001255 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001256
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001257 // Process the loop body.
1258 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001259 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001260
1261 // Save the current values for Block, Succ, and continue and break targets
1262 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1263 save_continue(ContinueTargetBlock),
1264 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001265
Mike Stump6d9828c2009-07-17 01:31:16 +00001266 // Create an empty block to represent the transition block for looping back
1267 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001268 Block = 0;
1269 assert(Succ == EntryConditionBlock);
1270 Succ = createBlock();
1271 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001272 ContinueTargetBlock = Succ;
1273
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001274 // All breaks should go to the code following the loop.
1275 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001276
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001277 // NULL out Block to force lazy instantiation of blocks for the body.
1278 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001279
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001280 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001281 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001282
Ted Kremenekaf603f72007-08-30 18:39:40 +00001283 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001284 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001285 else if (Block) {
1286 if (!FinishBlock(BodyBlock))
1287 return 0;
1288 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001289
Ted Kremenek941fde82009-07-24 04:47:11 +00001290 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001291 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001292 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001293
Ted Kremenek941fde82009-07-24 04:47:11 +00001294 // Link up the condition block with the code that follows the loop. (the
1295 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001296 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001297
1298 // There can be no more statements in the condition block since we loop back
1299 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001300 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001301
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001302 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001303 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001304 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001305}
Mike Stump1eb44332009-09-09 15:08:12 +00001306
1307
Ted Kremenek4f880632009-07-17 22:18:43 +00001308CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1309 // FIXME: For now we pretend that @catch and the code it contains does not
1310 // exit.
1311 return Block;
1312}
Mike Stump6d9828c2009-07-17 01:31:16 +00001313
Ted Kremenek2fda5042008-12-09 20:20:09 +00001314CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1315 // FIXME: This isn't complete. We basically treat @throw like a return
1316 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001317
Ted Kremenek6c249722009-09-24 18:45:41 +00001318 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001319 if (Block && !FinishBlock(Block))
1320 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001321
Ted Kremenek2fda5042008-12-09 20:20:09 +00001322 // Create the new block.
1323 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
Ted Kremenek2fda5042008-12-09 20:20:09 +00001325 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001326 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001327
1328 // Add the statement to the block. This may create new blocks if S contains
1329 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001330 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001331}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001332
Mike Stump0979d802009-07-22 22:56:04 +00001333CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001334 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001335 if (Block && !FinishBlock(Block))
1336 return 0;
1337
1338 // Create the new block.
1339 Block = createBlock(false);
1340
Mike Stump5d1d2022010-01-19 02:20:09 +00001341 if (TryTerminatedBlock)
1342 // The current try statement is the only successor.
1343 AddSuccessor(Block, TryTerminatedBlock);
1344 else
1345 // otherwise the Exit block is the only successor.
1346 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001347
1348 // Add the statement to the block. This may create new blocks if S contains
1349 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001350 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001351}
1352
Ted Kremenek4f880632009-07-17 22:18:43 +00001353CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001354 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001355
Mike Stump8f9893a2009-07-21 01:27:50 +00001356 // "do...while" is a control-flow statement. Thus we stop processing the
1357 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001358 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001359 if (!FinishBlock(Block))
1360 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001361 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001362 } else
1363 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001364
1365 // Because of short-circuit evaluation, the condition of the loop can span
1366 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1367 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001368 CFGBlock* ExitConditionBlock = createBlock(false);
1369 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001370
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001371 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001372 ExitConditionBlock->setTerminator(D);
1373
1374 // Now add the actual condition to the condition block. Because the condition
1375 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001376 if (Stmt* C = D->getCond()) {
1377 Block = ExitConditionBlock;
1378 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001379 if (Block) {
1380 if (!FinishBlock(EntryConditionBlock))
1381 return 0;
1382 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001383 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001384
Ted Kremenek54827132008-02-27 07:20:00 +00001385 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001386 Succ = EntryConditionBlock;
1387
Mike Stump00998a02009-07-23 23:25:26 +00001388 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001389 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001390
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001391 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001392 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001393 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001394 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001395
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001396 // Save the current values for Block, Succ, and continue and break targets
1397 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001398 save_continue(ContinueTargetBlock),
1399 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001400
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001401 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001402 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001403
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001404 // All breaks should go to the code following the loop.
1405 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001406
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001407 // NULL out Block to force lazy instantiation of blocks for the body.
1408 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001409
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001410 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001411 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001412
Ted Kremenekaf603f72007-08-30 18:39:40 +00001413 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001414 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001415 else if (Block) {
1416 if (!FinishBlock(BodyBlock))
1417 return 0;
1418 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001419
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001420 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001421 // ExitConditionBlock to represent the "loop back" transition. Create an
1422 // empty block to represent the transition block for looping back to the
1423 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001424 // FIXME: Can we do this more efficiently without adding another block?
1425 Block = NULL;
1426 Succ = BodyBlock;
1427 CFGBlock *LoopBackBlock = createBlock();
1428 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001429
Ted Kremenek941fde82009-07-24 04:47:11 +00001430 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001431 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001432 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001433
Ted Kremenek941fde82009-07-24 04:47:11 +00001434 // Link up the condition block with the code that follows the loop.
1435 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001436 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001437
1438 // There can be no more statements in the body block(s) since we loop back to
1439 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001440 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001441
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001442 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001443 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001444 return BodyBlock;
1445}
1446
1447CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1448 // "continue" is a control-flow statement. Thus we stop processing the
1449 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001450 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001451 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001452
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001453 // Now create a new block that ends with the continue statement.
1454 Block = createBlock(false);
1455 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001456
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001457 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001458 // incomplete AST. This means the CFG cannot be constructed.
1459 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001460 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001461 else
1462 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001463
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001464 return Block;
1465}
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001467CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001468 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001469
Ted Kremenek852274d2009-12-16 03:18:58 +00001470 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001471 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001472 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001473 }
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Ted Kremenek4f880632009-07-17 22:18:43 +00001475 // VLA types have expressions that must be evaluated.
1476 if (E->isArgumentType()) {
1477 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1478 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1479 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001480 }
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Mike Stump6d9828c2009-07-17 01:31:16 +00001482 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001483}
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek4f880632009-07-17 22:18:43 +00001485/// VisitStmtExpr - Utility method to handle (nested) statement
1486/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001487CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1488 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001489 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001490 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001491 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001492 return VisitCompoundStmt(SE->getSubStmt());
1493}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001494
Ted Kremenek411cdee2008-04-16 21:10:48 +00001495CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001496 // "switch" is a control-flow statement. Thus we stop processing the current
1497 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001498 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001499
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001500 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001501 if (!FinishBlock(Block))
1502 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001503 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001504 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001505
1506 // Save the current "switch" context.
1507 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001508 save_break(BreakTargetBlock),
1509 save_default(DefaultCaseBlock);
1510
Mike Stump6d9828c2009-07-17 01:31:16 +00001511 // Set the "default" case to be the block after the switch statement. If the
1512 // switch statement contains a "default:", this value will be overwritten with
1513 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001514 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001515
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001516 // Create a new block that will contain the switch statement.
1517 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001518
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001519 // Now process the switch body. The code after the switch is the implicit
1520 // successor.
1521 Succ = SwitchSuccessor;
1522 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001523
1524 // When visiting the body, the case statements should automatically get linked
1525 // up to the switch. We also don't keep a pointer to the body, since all
1526 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001527 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001528 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001529 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001530 if (Block) {
1531 if (!FinishBlock(BodyBlock))
1532 return 0;
1533 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001534
Mike Stump6d9828c2009-07-17 01:31:16 +00001535 // If we have no "default:" case, the default transition is to the code
1536 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001537 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001538
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001539 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001540 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001541 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001542 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001543 Block = addStmt(Terminator->getCond());
1544
1545 // Finally, if the SwitchStmt contains a condition variable, add both the
1546 // SwitchStmt and the condition variable initialization to the CFG.
1547 if (VarDecl *VD = Terminator->getConditionVariable()) {
1548 if (Expr *Init = VD->getInit()) {
1549 autoCreateBlock();
1550 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1551 addStmt(Init);
1552 }
1553 }
1554
1555 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001556}
1557
Ted Kremenek4f880632009-07-17 22:18:43 +00001558CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001559 // CaseStmts are essentially labels, so they are the first statement in a
1560 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001561
Ted Kremenek4f880632009-07-17 22:18:43 +00001562 if (CS->getSubStmt())
1563 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001565 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001566 if (!CaseBlock)
1567 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001568
1569 // Cases statements partition blocks, so this is the top of the basic block we
1570 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001571 CaseBlock->setLabel(CS);
1572
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001573 if (!FinishBlock(CaseBlock))
1574 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001575
1576 // Add this block to the list of successors for the block with the switch
1577 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001578 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001579 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001580
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001581 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1582 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001583
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001584 // This block is now the implicit successor of other blocks.
1585 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001586
Ted Kremenek2677ea82008-03-15 07:45:02 +00001587 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001588}
Mike Stump6d9828c2009-07-17 01:31:16 +00001589
Ted Kremenek411cdee2008-04-16 21:10:48 +00001590CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001591 if (Terminator->getSubStmt())
1592 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001594 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001595
1596 if (!DefaultCaseBlock)
1597 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001598
1599 // Default statements partition blocks, so this is the top of the basic block
1600 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001601 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001603 if (!FinishBlock(DefaultCaseBlock))
1604 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001605
Mike Stump6d9828c2009-07-17 01:31:16 +00001606 // Unlike case statements, we don't add the default block to the successors
1607 // for the switch statement immediately. This is done when we finish
1608 // processing the switch statement. This allows for the default case
1609 // (including a fall-through to the code after the switch statement) to always
1610 // be the last successor of a switch-terminated block.
1611
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001612 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1613 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001614
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001615 // This block is now the implicit successor of other blocks.
1616 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001617
1618 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001619}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001620
Mike Stump5d1d2022010-01-19 02:20:09 +00001621CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1622 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1623 // current block.
1624 CFGBlock* TrySuccessor = NULL;
1625
1626 if (Block) {
1627 if (!FinishBlock(Block))
1628 return 0;
1629 TrySuccessor = Block;
1630 } else TrySuccessor = Succ;
1631
Mike Stumpa1f93632010-01-20 01:15:34 +00001632 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001633
1634 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001635 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001636 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001637 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001638
Mike Stumpa1f93632010-01-20 01:15:34 +00001639 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001640 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1641 // The code after the try is the implicit successor.
1642 Succ = TrySuccessor;
1643 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001644 if (CS->getExceptionDecl() == 0) {
1645 HasCatchAll = true;
1646 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001647 Block = NULL;
1648 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1649 if (CatchBlock == 0)
1650 return 0;
1651 // Add this block to the list of successors for the block with the try
1652 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001653 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001654 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001655 if (!HasCatchAll) {
1656 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001657 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001658 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001659 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001660 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001661
1662 // The code after the try is the implicit successor.
1663 Succ = TrySuccessor;
1664
Mike Stumpf00cca52010-01-20 01:30:58 +00001665 // Save the current "try" context.
1666 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1667 TryTerminatedBlock = NewTryTerminatedBlock;
1668
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001669 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001670 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001671 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001672 return Block;
1673}
1674
1675CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1676 // CXXCatchStmt are treated like labels, so they are the first statement in a
1677 // block.
1678
1679 if (CS->getHandlerBlock())
1680 addStmt(CS->getHandlerBlock());
1681
1682 CFGBlock* CatchBlock = Block;
1683 if (!CatchBlock)
1684 CatchBlock = createBlock();
1685
1686 CatchBlock->setLabel(CS);
1687
1688 if (!FinishBlock(CatchBlock))
1689 return 0;
1690
1691 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1692 Block = NULL;
1693
1694 return CatchBlock;
1695}
1696
Ted Kremenek19bb3562007-08-28 19:26:49 +00001697CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001698 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001699 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001700
Ted Kremenek19bb3562007-08-28 19:26:49 +00001701 if (!IBlock) {
1702 IBlock = createBlock(false);
1703 cfg->setIndirectGotoBlock(IBlock);
1704 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001705
Ted Kremenek19bb3562007-08-28 19:26:49 +00001706 // IndirectGoto is a control-flow statement. Thus we stop processing the
1707 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001708 if (Block && !FinishBlock(Block))
1709 return 0;
1710
Ted Kremenek19bb3562007-08-28 19:26:49 +00001711 Block = createBlock(false);
1712 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001713 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001714 return addStmt(I->getTarget());
1715}
1716
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001717} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001718
Mike Stump6d9828c2009-07-17 01:31:16 +00001719/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1720/// no successors or predecessors. If this is the first block created in the
1721/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001722CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001723 bool first_block = begin() == end();
1724
1725 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001726 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1727 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1728 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001729
1730 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001731 if (first_block)
1732 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001733
1734 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001735 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001736}
1737
Ted Kremenek026473c2007-08-23 16:51:22 +00001738/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1739/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001740CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Mike Stump4c45aa12010-01-21 15:20:48 +00001741 bool AddEHEdges, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001742 CFGBuilder Builder;
Mike Stump4c45aa12010-01-21 15:20:48 +00001743 return Builder.buildCFG(D, Statement, C, AddEHEdges, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001744}
1745
Ted Kremenek63f58872007-10-01 19:33:33 +00001746//===----------------------------------------------------------------------===//
1747// CFG: Queries for BlkExprs.
1748//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001749
Ted Kremenek63f58872007-10-01 19:33:33 +00001750namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001751 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001752}
1753
Ted Kremenek8a693662009-12-23 23:37:10 +00001754static void FindSubExprAssignments(Stmt *S,
1755 llvm::SmallPtrSet<Expr*,50>& Set) {
1756 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001757 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001758
Ted Kremenek8a693662009-12-23 23:37:10 +00001759 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1760 Stmt *child = *I;
1761 if (!child)
1762 continue;
1763
1764 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001765 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001766
Ted Kremenek8a693662009-12-23 23:37:10 +00001767 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001768 }
1769}
1770
Ted Kremenek63f58872007-10-01 19:33:33 +00001771static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1772 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001773
1774 // Look for assignments that are used as subexpressions. These are the only
1775 // assignments that we want to *possibly* register as a block-level
1776 // expression. Basically, if an assignment occurs both in a subexpression and
1777 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001778 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001779
Ted Kremenek63f58872007-10-01 19:33:33 +00001780 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001781 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001782 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001783
Ted Kremenek411cdee2008-04-16 21:10:48 +00001784 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001785
1786 // Iterate over the statements again on identify the Expr* and Stmt* at the
1787 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001788
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001789 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001790 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001791
Ted Kremenek411cdee2008-04-16 21:10:48 +00001792 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001793 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001794 // expression are really "statements" whose value is never used by
1795 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001796 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001797 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001798 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1799 // Special handling for statement expressions. The last statement in
1800 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001801 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001802 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001803 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001804 (*M)[C->body_back()] = x;
1805 }
1806 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001807
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001808 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001809 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001810 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001811
Ted Kremenek411cdee2008-04-16 21:10:48 +00001812 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001813
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001814 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001815
Ted Kremenek390e48b2008-11-12 21:11:49 +00001816 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001817 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001818 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001819 }
1820 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001821
Ted Kremenek63f58872007-10-01 19:33:33 +00001822 return M;
1823}
1824
Ted Kremenek86946742008-01-17 20:48:37 +00001825CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1826 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001827 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001828
Ted Kremenek63f58872007-10-01 19:33:33 +00001829 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001830 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001831 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001832}
1833
1834unsigned CFG::getNumBlkExprs() {
1835 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1836 return M->size();
1837 else {
1838 // We assume callers interested in the number of BlkExprs will want
1839 // the map constructed if it doesn't already exist.
1840 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1841 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1842 }
1843}
1844
Ted Kremenek274f4332008-04-28 18:00:46 +00001845//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001846// Cleanup: CFG dstor.
1847//===----------------------------------------------------------------------===//
1848
Ted Kremenek63f58872007-10-01 19:33:33 +00001849CFG::~CFG() {
1850 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1851}
Mike Stump6d9828c2009-07-17 01:31:16 +00001852
Ted Kremenek7dba8602007-08-29 21:56:09 +00001853//===----------------------------------------------------------------------===//
1854// CFG pretty printing
1855//===----------------------------------------------------------------------===//
1856
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001857namespace {
1858
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001859class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001860 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1861 StmtMapTy StmtMap;
1862 signed CurrentBlock;
1863 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001864 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001865public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001866
Chris Lattnere4f21422009-06-30 01:26:17 +00001867 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1868 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001869 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1870 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001871 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001872 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001873 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001874 }
1875 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001876
Ted Kremenek42a509f2007-08-31 21:30:12 +00001877 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001878
Chris Lattnere4f21422009-06-30 01:26:17 +00001879 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001880 void setBlockID(signed i) { CurrentBlock = i; }
1881 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001882
Ted Kremeneka95d3752008-09-13 05:16:45 +00001883 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001884
Ted Kremenek411cdee2008-04-16 21:10:48 +00001885 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001886
1887 if (I == StmtMap.end())
1888 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001889
1890 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001891 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001892 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001893 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001894
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001895 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001896 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001897 }
1898};
Chris Lattnere4f21422009-06-30 01:26:17 +00001899} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001900
Chris Lattnere4f21422009-06-30 01:26:17 +00001901
1902namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001903class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001904 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001905
Ted Kremeneka95d3752008-09-13 05:16:45 +00001906 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001907 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001908 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001909public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001910 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001911 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001912 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001913
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001914 void VisitIfStmt(IfStmt* I) {
1915 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001916 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001917 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001918
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001919 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001920 void VisitStmt(Stmt* Terminator) {
1921 Terminator->printPretty(OS, Helper, Policy);
1922 }
1923
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001924 void VisitForStmt(ForStmt* F) {
1925 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001926 if (F->getInit())
1927 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001928 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001929 if (Stmt* C = F->getCond())
1930 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001931 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001932 if (F->getInc())
1933 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001934 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001935 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001936
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001937 void VisitWhileStmt(WhileStmt* W) {
1938 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001939 if (Stmt* C = W->getCond())
1940 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001941 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001942
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001943 void VisitDoStmt(DoStmt* D) {
1944 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001945 if (Stmt* C = D->getCond())
1946 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001947 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001948
Ted Kremenek411cdee2008-04-16 21:10:48 +00001949 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001950 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001951 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001952 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001953
Mike Stump5d1d2022010-01-19 02:20:09 +00001954 void VisitCXXTryStmt(CXXTryStmt* CS) {
1955 OS << "try ...";
1956 }
1957
Ted Kremenek805e9a82007-08-31 21:49:40 +00001958 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001959 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001960 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001961 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001962
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001963 void VisitChooseExpr(ChooseExpr* C) {
1964 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001965 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001966 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001967 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001969 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1970 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001971 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001972 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001973
Ted Kremenek805e9a82007-08-31 21:49:40 +00001974 void VisitBinaryOperator(BinaryOperator* B) {
1975 if (!B->isLogicalOp()) {
1976 VisitExpr(B);
1977 return;
1978 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001979
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001980 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001981
Ted Kremenek805e9a82007-08-31 21:49:40 +00001982 switch (B->getOpcode()) {
1983 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001984 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001985 return;
1986 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001987 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001988 return;
1989 default:
1990 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001991 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001992 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001993
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001994 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001995 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001996 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001997};
Chris Lattnere4f21422009-06-30 01:26:17 +00001998} // end anonymous namespace
1999
Mike Stump6d9828c2009-07-17 01:31:16 +00002000
Chris Lattnere4f21422009-06-30 01:26:17 +00002001static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002002 const CFGElement &E) {
2003 Stmt *Terminator = E;
2004
2005 if (E.asStartScope()) {
2006 OS << "start scope\n";
2007 return;
2008 }
2009 if (E.asEndScope()) {
2010 OS << "end scope\n";
2011 return;
2012 }
2013
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002014 if (Helper) {
2015 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002016 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002017 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002018
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002019 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002020 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002021 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002022 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002023 return;
2024 }
2025 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002026
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002027 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002028 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002029 if (B->getOpcode() == BinaryOperator::Comma) {
2030 OS << "... , ";
2031 Helper->handledStmt(B->getRHS(),OS);
2032 OS << '\n';
2033 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002034 }
2035 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002036 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002037
Chris Lattnere4f21422009-06-30 01:26:17 +00002038 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002039
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002040 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002041 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002042}
Mike Stump6d9828c2009-07-17 01:31:16 +00002043
Chris Lattnere4f21422009-06-30 01:26:17 +00002044static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2045 const CFGBlock& B,
2046 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002047
Ted Kremenek42a509f2007-08-31 21:30:12 +00002048 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002049
Ted Kremenek7dba8602007-08-29 21:56:09 +00002050 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002051 OS << "\n [ B" << B.getBlockID();
2052
Ted Kremenek42a509f2007-08-31 21:30:12 +00002053 if (&B == &cfg->getEntry())
2054 OS << " (ENTRY) ]\n";
2055 else if (&B == &cfg->getExit())
2056 OS << " (EXIT) ]\n";
2057 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002058 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002059 else
2060 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002061
Ted Kremenek9cffe732007-08-29 23:20:49 +00002062 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002063 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002064
2065 if (print_edges)
2066 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002067
Mike Stump079bd722010-01-19 22:00:14 +00002068 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002069 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002070 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002071 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002072 C->getLHS()->printPretty(OS, Helper,
2073 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002074 if (C->getRHS()) {
2075 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002076 C->getRHS()->printPretty(OS, Helper,
2077 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002078 }
Mike Stump079bd722010-01-19 22:00:14 +00002079 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002080 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002081 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002082 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002083 if (CS->getExceptionDecl())
2084 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2085 0);
2086 else
2087 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002088 OS << ")";
2089
2090 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002091 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002092
Ted Kremenek9cffe732007-08-29 23:20:49 +00002093 OS << ":\n";
2094 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002095
Ted Kremenekfddd5182007-08-21 21:42:03 +00002096 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002097 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002098
Ted Kremenek42a509f2007-08-31 21:30:12 +00002099 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2100 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002101
Ted Kremenek9cffe732007-08-29 23:20:49 +00002102 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002103 if (print_edges)
2104 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002105
Ted Kremeneka95d3752008-09-13 05:16:45 +00002106 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002107
Ted Kremenek42a509f2007-08-31 21:30:12 +00002108 if (Helper)
2109 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002110
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002111 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002112 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002113
Ted Kremenek9cffe732007-08-29 23:20:49 +00002114 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002115 if (B.getTerminator()) {
2116 if (print_edges)
2117 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002118
Ted Kremenek9cffe732007-08-29 23:20:49 +00002119 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002120
Ted Kremenek42a509f2007-08-31 21:30:12 +00002121 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002122
Chris Lattnere4f21422009-06-30 01:26:17 +00002123 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2124 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002125 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002126 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002127 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002128
Ted Kremenek9cffe732007-08-29 23:20:49 +00002129 if (print_edges) {
2130 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002131 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002132 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002133
Ted Kremenek42a509f2007-08-31 21:30:12 +00002134 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2135 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002136
Ted Kremenek42a509f2007-08-31 21:30:12 +00002137 if (i == 8 || (i-8) == 0)
2138 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002139
Ted Kremenek9cffe732007-08-29 23:20:49 +00002140 OS << " B" << (*I)->getBlockID();
2141 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002142
Ted Kremenek42a509f2007-08-31 21:30:12 +00002143 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002144
Ted Kremenek42a509f2007-08-31 21:30:12 +00002145 // Print the successors of this block.
2146 OS << " Successors (" << B.succ_size() << "):";
2147 i = 0;
2148
2149 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2150 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002151
Ted Kremenek42a509f2007-08-31 21:30:12 +00002152 if (i == 8 || (i-8) % 10 == 0)
2153 OS << "\n ";
2154
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002155 if (*I)
2156 OS << " B" << (*I)->getBlockID();
2157 else
2158 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002159 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002160
Ted Kremenek9cffe732007-08-29 23:20:49 +00002161 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002162 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002163}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002164
Ted Kremenek42a509f2007-08-31 21:30:12 +00002165
2166/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002167void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002168
2169/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002170void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2171 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002172
Ted Kremenek42a509f2007-08-31 21:30:12 +00002173 // Print the entry block.
2174 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002175
Ted Kremenek42a509f2007-08-31 21:30:12 +00002176 // Iterate through the CFGBlocks and print them one by one.
2177 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2178 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002179 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002180 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002181
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002182 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002183 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002184
Ted Kremenek42a509f2007-08-31 21:30:12 +00002185 // Print the exit block.
2186 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002187 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002188}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002189
2190/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002191void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2192 print(llvm::errs(), cfg, LO);
2193}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002194
2195/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2196/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002197void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2198 const LangOptions &LO) const {
2199 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002200 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002201}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002202
Ted Kremeneka2925852008-01-30 23:02:42 +00002203/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002204void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002205 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002206 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002207 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2208}
2209
Ted Kremenek390e48b2008-11-12 21:11:49 +00002210Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002211
Ted Kremenek411cdee2008-04-16 21:10:48 +00002212 if (!Terminator)
2213 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002214
Ted Kremenek411cdee2008-04-16 21:10:48 +00002215 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002216
Ted Kremenek411cdee2008-04-16 21:10:48 +00002217 switch (Terminator->getStmtClass()) {
2218 default:
2219 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002220
Ted Kremenek411cdee2008-04-16 21:10:48 +00002221 case Stmt::ForStmtClass:
2222 E = cast<ForStmt>(Terminator)->getCond();
2223 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002224
Ted Kremenek411cdee2008-04-16 21:10:48 +00002225 case Stmt::WhileStmtClass:
2226 E = cast<WhileStmt>(Terminator)->getCond();
2227 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002228
Ted Kremenek411cdee2008-04-16 21:10:48 +00002229 case Stmt::DoStmtClass:
2230 E = cast<DoStmt>(Terminator)->getCond();
2231 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002232
Ted Kremenek411cdee2008-04-16 21:10:48 +00002233 case Stmt::IfStmtClass:
2234 E = cast<IfStmt>(Terminator)->getCond();
2235 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002236
Ted Kremenek411cdee2008-04-16 21:10:48 +00002237 case Stmt::ChooseExprClass:
2238 E = cast<ChooseExpr>(Terminator)->getCond();
2239 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002240
Ted Kremenek411cdee2008-04-16 21:10:48 +00002241 case Stmt::IndirectGotoStmtClass:
2242 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2243 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002244
Ted Kremenek411cdee2008-04-16 21:10:48 +00002245 case Stmt::SwitchStmtClass:
2246 E = cast<SwitchStmt>(Terminator)->getCond();
2247 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002248
Ted Kremenek411cdee2008-04-16 21:10:48 +00002249 case Stmt::ConditionalOperatorClass:
2250 E = cast<ConditionalOperator>(Terminator)->getCond();
2251 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002252
Ted Kremenek411cdee2008-04-16 21:10:48 +00002253 case Stmt::BinaryOperatorClass: // '&&' and '||'
2254 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002255 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002256
Ted Kremenek390e48b2008-11-12 21:11:49 +00002257 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002258 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002259 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002260
Ted Kremenek411cdee2008-04-16 21:10:48 +00002261 return E ? E->IgnoreParens() : NULL;
2262}
2263
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002264bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002265
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002266 if (!Terminator)
2267 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002269 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002270
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002271 switch (Terminator->getStmtClass()) {
2272 default:
2273 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002274
2275 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002276 case Stmt::WhileStmtClass:
2277 case Stmt::DoStmtClass:
2278 case Stmt::IfStmtClass:
2279 case Stmt::ChooseExprClass:
2280 case Stmt::ConditionalOperatorClass:
2281 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002282 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002283 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002284
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002285 return E ? E->IgnoreParens() : NULL;
2286}
2287
Ted Kremeneka2925852008-01-30 23:02:42 +00002288
Ted Kremenek7dba8602007-08-29 21:56:09 +00002289//===----------------------------------------------------------------------===//
2290// CFG Graphviz Visualization
2291//===----------------------------------------------------------------------===//
2292
Ted Kremenek42a509f2007-08-31 21:30:12 +00002293
2294#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002295static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002296#endif
2297
Chris Lattnere4f21422009-06-30 01:26:17 +00002298void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002299#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002300 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002301 GraphHelper = &H;
2302 llvm::ViewGraph(this,"CFG");
2303 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002304#endif
2305}
2306
Ted Kremenek7dba8602007-08-29 21:56:09 +00002307namespace llvm {
2308template<>
2309struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002310
2311 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2312
2313 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002314
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002315#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002316 std::string OutSStr;
2317 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002318 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002319 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002320
2321 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2322
2323 // Process string output to make it nicer...
2324 for (unsigned i = 0; i != OutStr.length(); ++i)
2325 if (OutStr[i] == '\n') { // Left justify
2326 OutStr[i] = '\\';
2327 OutStr.insert(OutStr.begin()+i+1, 'l');
2328 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002329
Ted Kremenek7dba8602007-08-29 21:56:09 +00002330 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002331#else
2332 return "";
2333#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002334 }
2335};
2336} // end namespace llvm