blob: 97bf675180be0fd2d0a7115f4be22ea11f6fe94c [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"
Ted Kremenekc310e932007-08-21 22:06:14 +000017#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000018#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000019#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/Allocator.h"
21#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000022#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000023#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000024#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000025
Ted Kremenekfddd5182007-08-21 21:42:03 +000026using namespace clang;
27
28namespace {
29
Douglas Gregor4afa39d2009-01-20 01:17:11 +000030static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000031 if (VarDecl* VD = dyn_cast<VarDecl>(D))
32 if (Expr* Ex = VD->getInit())
33 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000034
35 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000036}
Ted Kremenek852274d2009-12-16 03:18:58 +000037
38class AddStmtChoice {
39public:
40 enum Kind { NotAlwaysAdd = 0, AlwaysAdd, AlwaysAddAsLValue };
41public:
42 AddStmtChoice(Kind kind) : k(kind) {}
43 bool alwaysAdd() const { return k != NotAlwaysAdd; }
44 bool asLValue() const { return k == AlwaysAddAsLValue; }
45private:
46 Kind k;
47};
Mike Stump6d9828c2009-07-17 01:31:16 +000048
Ted Kremeneka34ea072008-08-04 22:51:42 +000049/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000050/// The builder is stateful: an instance of the builder should be used to only
51/// construct a single CFG.
52///
53/// Example usage:
54///
55/// CFGBuilder builder;
56/// CFG* cfg = builder.BuildAST(stmt1);
57///
Mike Stump6d9828c2009-07-17 01:31:16 +000058/// CFG construction is done via a recursive walk of an AST. We actually parse
59/// the AST in reverse order so that the successor of a basic block is
60/// constructed prior to its predecessor. This allows us to nicely capture
61/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000062///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000063class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000064 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000065 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000066
Ted Kremenekfddd5182007-08-21 21:42:03 +000067 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000068 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000069 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000070 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000071 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000072 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +000073 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000074
Ted Kremenek19bb3562007-08-28 19:26:49 +000075 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000076 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
77 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000078
79 // A list of blocks that end with a "goto" that must be backpatched to their
80 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000081 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000082 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000083
Ted Kremenek19bb3562007-08-28 19:26:49 +000084 // A list of labels whose address has been taken (for indirect gotos).
85 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
86 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000087
88public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000089 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
90 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000091 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +000092 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
93 TryTerminatedBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +000094
Ted Kremenekd4fdee32007-08-23 21:42:29 +000095 // buildCFG - Used by external clients to construct the CFG.
Mike Stump079bd722010-01-19 22:00:14 +000096 CFG* buildCFG(Stmt *Statement, ASTContext *C, bool AddScopes);
Mike Stump6d9828c2009-07-17 01:31:16 +000097
Ted Kremenek4f880632009-07-17 22:18:43 +000098private:
99 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000100 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
101 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
102 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000103 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000104 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000105 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000106 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000107 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000108 CFGBlock *VisitConditionalOperator(ConditionalOperator *C,
109 AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000110 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump5d1d2022010-01-19 02:20:09 +0000111 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
Mike Stump0979d802009-07-22 22:56:04 +0000112 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Mike Stump5d1d2022010-01-19 02:20:09 +0000113 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Ted Kremenek4f880632009-07-17 22:18:43 +0000114 CFGBlock *VisitDeclStmt(DeclStmt *DS);
115 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000116 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
117 CFGBlock *VisitDoStmt(DoStmt *D);
118 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000119 CFGBlock *VisitGotoStmt(GotoStmt* G);
120 CFGBlock *VisitIfStmt(IfStmt *I);
121 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
122 CFGBlock *VisitLabelStmt(LabelStmt *L);
123 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
124 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
125 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
126 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
127 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
128 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000129 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
130 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000131 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
132 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000133
Ted Kremenek852274d2009-12-16 03:18:58 +0000134 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
135 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000136 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000137
Ted Kremenek274f4332008-04-28 18:00:46 +0000138 // NYS == Not Yet Supported
139 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000140 badCFG = true;
141 return Block;
142 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000143
Mike Stump079bd722010-01-19 22:00:14 +0000144 CFGBlock *StartScope(Stmt *S, CFGBlock *B) {
145 if (!AddScopes)
146 return B;
147
148 if (B == 0)
149 B = createBlock();
150 B->StartScope(S, cfg->getBumpVectorContext());
151 return B;
152 }
153
154 void EndScope(Stmt *S) {
155 if (!AddScopes)
156 return;
157
158 if (Block == 0)
159 Block = createBlock();
160 Block->EndScope(S, cfg->getBumpVectorContext());
161 }
162
Ted Kremenek4f880632009-07-17 22:18:43 +0000163 void autoCreateBlock() { if (!Block) Block = createBlock(); }
164 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000165 bool FinishBlock(CFGBlock* B);
Ted Kremenek852274d2009-12-16 03:18:58 +0000166 CFGBlock *addStmt(Stmt *S, AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
167 return Visit(S, asc);
168 }
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000169
Ted Kremenek852274d2009-12-16 03:18:58 +0000170 void AppendStmt(CFGBlock *B, Stmt *S,
171 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
172 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000173 }
174
175 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
176 B->addSuccessor(S, cfg->getBumpVectorContext());
177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000179 /// TryResult - a class representing a variant over the values
180 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
181 /// and is used by the CFGBuilder to decide if a branch condition
182 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000183 class TryResult {
184 int X;
185 public:
186 TryResult(bool b) : X(b ? 1 : 0) {}
187 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenek941fde82009-07-24 04:47:11 +0000189 bool isTrue() const { return X == 1; }
190 bool isFalse() const { return X == 0; }
191 bool isKnown() const { return X >= 0; }
192 void negate() {
193 assert(isKnown());
194 X ^= 0x1;
195 }
196 };
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Mike Stump00998a02009-07-23 23:25:26 +0000198 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
199 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000200 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000201 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000202 if (!S->isTypeDependent() && !S->isValueDependent() &&
203 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000204 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000205
206 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000207 }
208
Ted Kremenek4102af92008-03-13 03:04:22 +0000209 bool badCFG;
Mike Stump079bd722010-01-19 22:00:14 +0000210 bool AddScopes;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000211};
Mike Stump6d9828c2009-07-17 01:31:16 +0000212
Douglas Gregor898574e2008-12-05 23:32:09 +0000213// FIXME: Add support for dependent-sized array types in C++?
214// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000215static VariableArrayType* FindVA(Type* t) {
216 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
217 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
218 if (vat->getSizeExpr())
219 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000220
Ted Kremenek610a09e2008-09-26 22:58:57 +0000221 t = vt->getElementType().getTypePtr();
222 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000223
Ted Kremenek610a09e2008-09-26 22:58:57 +0000224 return 0;
225}
Mike Stump6d9828c2009-07-17 01:31:16 +0000226
227/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
228/// arbitrary statement. Examples include a single expression or a function
229/// body (compound statement). The ownership of the returned CFG is
230/// transferred to the caller. If CFG construction fails, this method returns
231/// NULL.
Mike Stump079bd722010-01-19 22:00:14 +0000232CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C, bool AddScopes) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000233 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000234 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000235 if (!Statement)
236 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000237
Mike Stump079bd722010-01-19 22:00:14 +0000238 this->AddScopes = AddScopes;
Ted Kremenek4102af92008-03-13 03:04:22 +0000239 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000240
241 // Create an empty block that will serve as the exit block for the CFG. Since
242 // this is the first block added to the CFG, it will be implicitly registered
243 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000244 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000245 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000246 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000247
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000248 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000249 CFGBlock* B = addStmt(Statement);
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000250 if (!B)
251 B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000252
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000253 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000254 // Finalize the last constructed block. This usually involves reversing the
255 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000256 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000257
258 // Backpatch the gotos whose label -> block mappings we didn't know when we
259 // encountered them.
260 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000261 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000262
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000263 CFGBlock* B = *I;
264 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
265 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
266
267 // If there is no target for the goto, then we are looking at an
268 // incomplete AST. Handle this by not registering a successor.
269 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000270
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000271 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000272 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000273
Ted Kremenek19bb3562007-08-28 19:26:49 +0000274 // Add successors to the Indirect Goto Dispatch block (if we have one).
275 if (CFGBlock* B = cfg->getIndirectGotoBlock())
276 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
277 E = AddressTakenLabels.end(); I != E; ++I ) {
278
279 // Lookup the target block.
280 LabelMapTy::iterator LI = LabelMap.find(*I);
281
282 // If there is no target block that contains label, then we are looking
283 // at an incomplete AST. Handle this by not registering a successor.
284 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000285
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000286 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000287 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000288
Ted Kremenek94b33162007-09-17 16:18:02 +0000289 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000290 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000291
292 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000293 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000294
Ted Kremenekda9b30e2009-10-20 23:59:28 +0000295 return badCFG ? NULL : cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000296}
Mike Stump6d9828c2009-07-17 01:31:16 +0000297
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000298/// createBlock - Used to lazily create blocks that are connected
299/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000300CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000301 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000302 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000303 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000304 return B;
305}
Mike Stump6d9828c2009-07-17 01:31:16 +0000306
Ted Kremenek6c249722009-09-24 18:45:41 +0000307/// FinishBlock - "Finalize" the block by checking if we have a bad CFG.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000308bool CFGBuilder::FinishBlock(CFGBlock* B) {
309 if (badCFG)
310 return false;
311
Ted Kremenek4f880632009-07-17 22:18:43 +0000312 assert(B);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000313 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000314}
315
Ted Kremenek4f880632009-07-17 22:18:43 +0000316/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000317/// blocks for ternary operators, &&, and ||. We also process "," and
318/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000319CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000320tryAgain:
321 switch (S->getStmtClass()) {
322 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000323 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000324
325 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000326 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek4f880632009-07-17 22:18:43 +0000328 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000329 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremenek4f880632009-07-17 22:18:43 +0000331 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000332 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000333
Ted Kremenek4f880632009-07-17 22:18:43 +0000334 case Stmt::BreakStmtClass:
335 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenek4f880632009-07-17 22:18:43 +0000337 case Stmt::CallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000338 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek4f880632009-07-17 22:18:43 +0000340 case Stmt::CaseStmtClass:
341 return VisitCaseStmt(cast<CaseStmt>(S));
342
343 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000344 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek4f880632009-07-17 22:18:43 +0000346 case Stmt::CompoundStmtClass:
347 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek4f880632009-07-17 22:18:43 +0000349 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000350 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremenek4f880632009-07-17 22:18:43 +0000352 case Stmt::ContinueStmtClass:
353 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremenek021c8af2010-01-19 20:40:33 +0000355 case Stmt::CXXCatchStmtClass:
356 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
357
358 case Stmt::CXXThrowExprClass:
359 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
360
361 case Stmt::CXXTryStmtClass:
362 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
363
Ted Kremenek4f880632009-07-17 22:18:43 +0000364 case Stmt::DeclStmtClass:
365 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek4f880632009-07-17 22:18:43 +0000367 case Stmt::DefaultStmtClass:
368 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek4f880632009-07-17 22:18:43 +0000370 case Stmt::DoStmtClass:
371 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Ted Kremenek4f880632009-07-17 22:18:43 +0000373 case Stmt::ForStmtClass:
374 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek4f880632009-07-17 22:18:43 +0000376 case Stmt::GotoStmtClass:
377 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Ted Kremenek4f880632009-07-17 22:18:43 +0000379 case Stmt::IfStmtClass:
380 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek4f880632009-07-17 22:18:43 +0000382 case Stmt::IndirectGotoStmtClass:
383 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek4f880632009-07-17 22:18:43 +0000385 case Stmt::LabelStmtClass:
386 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek4f880632009-07-17 22:18:43 +0000388 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000389 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
390
Ted Kremenek4f880632009-07-17 22:18:43 +0000391 case Stmt::ObjCAtSynchronizedStmtClass:
392 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Ted Kremenek4f880632009-07-17 22:18:43 +0000394 case Stmt::ObjCAtThrowStmtClass:
395 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek4f880632009-07-17 22:18:43 +0000397 case Stmt::ObjCAtTryStmtClass:
398 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek4f880632009-07-17 22:18:43 +0000400 case Stmt::ObjCForCollectionStmtClass:
401 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek4f880632009-07-17 22:18:43 +0000403 case Stmt::ParenExprClass:
404 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000405 goto tryAgain;
406
Ted Kremenek4f880632009-07-17 22:18:43 +0000407 case Stmt::NullStmtClass:
408 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Ted Kremenek4f880632009-07-17 22:18:43 +0000410 case Stmt::ReturnStmtClass:
411 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Ted Kremenek4f880632009-07-17 22:18:43 +0000413 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000414 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Ted Kremenek4f880632009-07-17 22:18:43 +0000416 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000417 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek4f880632009-07-17 22:18:43 +0000419 case Stmt::SwitchStmtClass:
420 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Ted Kremenek4f880632009-07-17 22:18:43 +0000422 case Stmt::WhileStmtClass:
423 return VisitWhileStmt(cast<WhileStmt>(S));
424 }
425}
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek852274d2009-12-16 03:18:58 +0000427CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
428 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000429 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000430 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek4f880632009-07-17 22:18:43 +0000433 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000434}
Mike Stump6d9828c2009-07-17 01:31:16 +0000435
Ted Kremenek4f880632009-07-17 22:18:43 +0000436/// VisitChildren - Visit the children of a Stmt.
437CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
438 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000439 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000440 E = Terminator->child_end(); I != E; ++I) {
441 if (*I) B = Visit(*I);
442 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000443 return B;
444}
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremenek852274d2009-12-16 03:18:58 +0000446CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
447 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000448 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000449
Ted Kremenek852274d2009-12-16 03:18:58 +0000450 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000451 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000452 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000453 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000454
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000455 return Block;
456}
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Ted Kremenek852274d2009-12-16 03:18:58 +0000458CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
459 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000460 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000461 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000462 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek4f880632009-07-17 22:18:43 +0000464 if (!FinishBlock(ConfluenceBlock))
465 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Ted Kremenek4f880632009-07-17 22:18:43 +0000467 // create the block evaluating the LHS
468 CFGBlock* LHSBlock = createBlock(false);
469 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek4f880632009-07-17 22:18:43 +0000471 // create the block evaluating the RHS
472 Succ = ConfluenceBlock;
473 Block = NULL;
474 CFGBlock* RHSBlock = addStmt(B->getRHS());
475 if (!FinishBlock(RHSBlock))
476 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Mike Stump00998a02009-07-23 23:25:26 +0000478 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000479 TryResult KnownVal = TryEvaluateBool(B->getLHS());
480 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
481 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000482
Ted Kremenek4f880632009-07-17 22:18:43 +0000483 // Now link the LHSBlock with RHSBlock.
484 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000485 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
486 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000487 } else {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000488 assert(B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000489 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
490 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek4f880632009-07-17 22:18:43 +0000493 // Generate the blocks for evaluating the LHS.
494 Block = LHSBlock;
495 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000496 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000497 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000498 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000499 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000500 addStmt(B->getRHS());
501 return addStmt(B->getLHS());
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Ted Kremenek852274d2009-12-16 03:18:58 +0000504 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000505}
506
Ted Kremenek852274d2009-12-16 03:18:58 +0000507CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
508 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000509 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000510 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000511 }
512 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000513}
514
Ted Kremenek4f880632009-07-17 22:18:43 +0000515CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
516 // "break" is a control-flow statement. Thus we stop processing the current
517 // block.
518 if (Block && !FinishBlock(Block))
519 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek4f880632009-07-17 22:18:43 +0000521 // Now create a new block that ends with the break statement.
522 Block = createBlock(false);
523 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Ted Kremenek4f880632009-07-17 22:18:43 +0000525 // If there is no target for the break, then we are looking at an incomplete
526 // AST. This means that the CFG cannot be constructed.
527 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000528 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000529 else
530 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
532
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000533 return Block;
534}
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek852274d2009-12-16 03:18:58 +0000536CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000537 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000538 bool NoReturn = false;
539 if (C->getCallee()->getType().getNoReturnAttr()) {
540 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000541 }
Mike Stump24556362009-07-25 21:26:53 +0000542
Mike Stump079bd722010-01-19 22:00:14 +0000543 bool CanThrow = false;
544
545 // Languages without exceptions are assumed to not throw.
546 if (Context->getLangOptions().Exceptions) {
547 CanThrow = true;
548 }
549
550 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000551 if (FD->hasAttr<NoReturnAttr>())
552 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000553 if (FD->hasAttr<NoThrowAttr>())
554 CanThrow = false;
555 }
Mike Stump24556362009-07-25 21:26:53 +0000556
Mike Stump079bd722010-01-19 22:00:14 +0000557 if (!NoReturn && !CanThrow)
Ted Kremenek852274d2009-12-16 03:18:58 +0000558 return VisitStmt(C, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Mike Stump079bd722010-01-19 22:00:14 +0000560 if (Block) {
561 Succ = Block;
562 if (!FinishBlock(Block))
563 return 0;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Mike Stump079bd722010-01-19 22:00:14 +0000566 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000567 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000568
Mike Stump079bd722010-01-19 22:00:14 +0000569 if (NoReturn) {
570 // Wire this to the exit block directly.
571 AddSuccessor(Block, &cfg->getExit());
572 }
573 if (CanThrow) {
574 // Add exceptional edges.
575 if (TryTerminatedBlock)
576 AddSuccessor(Block, TryTerminatedBlock);
577 else
578 AddSuccessor(Block, &cfg->getExit());
579 }
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Mike Stump24556362009-07-25 21:26:53 +0000581 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000582}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000583
Ted Kremenek852274d2009-12-16 03:18:58 +0000584CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
585 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000586 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000587 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000588 if (!FinishBlock(ConfluenceBlock))
589 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000591 Succ = ConfluenceBlock;
592 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000593 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000594 if (!FinishBlock(LHSBlock))
595 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000597 Succ = ConfluenceBlock;
598 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000599 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000600 if (!FinishBlock(RHSBlock))
601 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000603 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000604 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000605 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000606 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
607 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000608 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000609 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000610}
Mike Stump1eb44332009-09-09 15:08:12 +0000611
612
613CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
Mike Stump079bd722010-01-19 22:00:14 +0000614 EndScope(C);
615
Mike Stump1eb44332009-09-09 15:08:12 +0000616 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000617
618 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
619 I != E; ++I ) {
620 LastBlock = addStmt(*I);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000622 if (badCFG)
623 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000624 }
Mike Stump079bd722010-01-19 22:00:14 +0000625
626 LastBlock = StartScope(C, LastBlock);
627
Ted Kremenek4f880632009-07-17 22:18:43 +0000628 return LastBlock;
629}
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Ted Kremenek852274d2009-12-16 03:18:58 +0000631CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
632 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000633 // Create the confluence block that will "merge" the results of the ternary
634 // expression.
635 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000636 AppendStmt(ConfluenceBlock, C, asc);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000637 if (!FinishBlock(ConfluenceBlock))
638 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000640 // Create a block for the LHS expression if there is an LHS expression. A
641 // GCC extension allows LHS to be NULL, causing the condition to be the
642 // value that is returned instead.
643 // e.g: x ?: y is shorthand for: x ? x : y;
644 Succ = ConfluenceBlock;
645 Block = NULL;
646 CFGBlock* LHSBlock = NULL;
647 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000648 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000649 if (!FinishBlock(LHSBlock))
650 return 0;
651 Block = NULL;
652 }
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000654 // Create the block for the RHS expression.
655 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000656 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000657 if (!FinishBlock(RHSBlock))
658 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000660 // Create the block that will contain the condition.
661 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Mike Stump00998a02009-07-23 23:25:26 +0000663 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000664 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000665 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000666 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000667 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000668 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000669 // If we know the condition is false, add NULL as the successor for
670 // the block containing the condition. In this case, the confluence
671 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000672 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000673 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000674 } else {
675 // If we have no LHS expression, add the ConfluenceBlock as a direct
676 // successor for the block containing the condition. Moreover, we need to
677 // reverse the order of the predecessors in the ConfluenceBlock because
678 // the RHSBlock will have been added to the succcessors already, and we
679 // want the first predecessor to the the block containing the expression
680 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000681 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000682 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000683 std::reverse(ConfluenceBlock->pred_begin(),
684 ConfluenceBlock->pred_end());
685 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000688 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000689 Block->setTerminator(C);
690 return addStmt(C->getCond());
691}
692
Ted Kremenek4f880632009-07-17 22:18:43 +0000693CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
694 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000695
Ted Kremenek4f880632009-07-17 22:18:43 +0000696 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000697 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000698 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek4f880632009-07-17 22:18:43 +0000701 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Ted Kremenek4f880632009-07-17 22:18:43 +0000703 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
704 typedef llvm::SmallVector<Decl*,10> BufTy;
705 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek4f880632009-07-17 22:18:43 +0000707 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
708 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
709 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
710 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek4f880632009-07-17 22:18:43 +0000712 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
713 // automatically freed with the CFG.
714 DeclGroupRef DG(*I);
715 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000716 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000717 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek4f880632009-07-17 22:18:43 +0000719 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000720 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000721 B = VisitDeclSubExpr(D);
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
724 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000725}
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek4f880632009-07-17 22:18:43 +0000727/// VisitDeclSubExpr - Utility method to add block-level expressions for
728/// initializers in Decls.
729CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
730 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000731
Ted Kremenek4f880632009-07-17 22:18:43 +0000732 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek4f880632009-07-17 22:18:43 +0000734 if (!VD)
735 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek4f880632009-07-17 22:18:43 +0000737 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Ted Kremenek4f880632009-07-17 22:18:43 +0000739 if (Init) {
740 // Optimization: Don't create separate block-level statements for literals.
741 switch (Init->getStmtClass()) {
742 case Stmt::IntegerLiteralClass:
743 case Stmt::CharacterLiteralClass:
744 case Stmt::StringLiteralClass:
745 break;
746 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000747 Block = addStmt(Init,
748 VD->getType()->isReferenceType()
749 ? AddStmtChoice::AlwaysAddAsLValue
750 : AddStmtChoice::AlwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000751 }
752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenek4f880632009-07-17 22:18:43 +0000754 // If the type of VD is a VLA, then we must process its size expressions.
755 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
756 VA = FindVA(VA->getElementType().getTypePtr()))
757 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Ted Kremenek4f880632009-07-17 22:18:43 +0000759 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000760}
761
762CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000763 // We may see an if statement in the middle of a basic block, or it may be the
764 // first statement we are processing. In either case, we create a new basic
765 // block. First, we create the blocks for the then...else statements, and
766 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000767 // middle of a block, we stop processing that block. That block is then the
768 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000769
770 // The block we were proccessing is now finished. Make it the successor
771 // block.
772 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000773 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000774 if (!FinishBlock(Block))
775 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000776 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000777
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000778 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000779 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000780
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000781 if (Stmt* Else = I->getElse()) {
782 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000783
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000784 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000785 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000786 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000787 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000788
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000789 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
790 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000791 else if (Block) {
792 if (!FinishBlock(ElseBlock))
793 return 0;
794 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000795 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000796
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000797 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000798 CFGBlock* ThenBlock;
799 {
800 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000801 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000802 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000803 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000804 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000805
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000806 if (!ThenBlock) {
807 // We can reach here if the "then" body has all NullStmts.
808 // Create an empty block so we can distinguish between true and false
809 // branches in path-sensitive analyses.
810 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000811 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000812 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000813 if (!FinishBlock(ThenBlock))
814 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000815 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000816 }
817
Mike Stump6d9828c2009-07-17 01:31:16 +0000818 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000819 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000820
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000821 // Set the terminator of the new block to the If statement.
822 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000823
Mike Stump00998a02009-07-23 23:25:26 +0000824 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000825 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000826
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000827 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000828 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
829 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000830
831 // Add the condition as the last statement in the new block. This may create
832 // new blocks as the condition may contain control-flow. Any newly created
833 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000834 Block = addStmt(I->getCond());
835
836 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
837 // and the condition variable initialization to the CFG.
838 if (VarDecl *VD = I->getConditionVariable()) {
839 if (Expr *Init = VD->getInit()) {
840 autoCreateBlock();
841 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
842 addStmt(Init);
843 }
844 }
845
846 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000847}
Mike Stump6d9828c2009-07-17 01:31:16 +0000848
849
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000850CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000851 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000852 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000853 // NOTE: If a "return" appears in the middle of a block, this means that the
854 // code afterwards is DEAD (unreachable). We still keep a basic block
855 // for that code; a simple "mark-and-sweep" from the entry block will be
856 // able to report such dead blocks.
Ted Kremenek6c249722009-09-24 18:45:41 +0000857 if (Block)
858 FinishBlock(Block);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000859
860 // Create the new block.
861 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000862
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000863 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000864 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000865
866 // Add the return statement to the block. This may create new blocks if R
867 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000868 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000869}
870
871CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
872 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000873 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000874 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000875
Ted Kremenek4f880632009-07-17 22:18:43 +0000876 if (!LabelBlock) // This can happen when the body is empty, i.e.
877 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000878
Ted Kremenek4f880632009-07-17 22:18:43 +0000879 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000880 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000881
882 // Labels partition blocks, so this is the end of the basic block we were
883 // processing (L is the block's label). Because this is label (and we have
884 // already processed the substatement) there is no extra control-flow to worry
885 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000886 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000887 if (!FinishBlock(LabelBlock))
888 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000889
890 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000891 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000892
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000893 // This block is now the implicit successor of other blocks.
894 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000895
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000896 return LabelBlock;
897}
898
899CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000900 // Goto is a control-flow statement. Thus we stop processing the current
901 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000902 if (Block)
903 FinishBlock(Block);
904
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000905 Block = createBlock(false);
906 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000907
908 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000909 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000910
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000911 if (I == LabelMap.end())
912 // We will need to backpatch this block later.
913 BackpatchBlocks.push_back(Block);
914 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000915 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000916
Mike Stump6d9828c2009-07-17 01:31:16 +0000917 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000918}
919
920CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000921 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000922
Mike Stumpfefb9f72009-07-21 01:12:51 +0000923 // "for" is a control-flow statement. Thus we stop processing the current
924 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000925 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000926 if (!FinishBlock(Block))
927 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000928 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000929 } else
930 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000931
932 // Because of short-circuit evaluation, the condition of the loop can span
933 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
934 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000935 CFGBlock* ExitConditionBlock = createBlock(false);
936 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000937
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000938 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000939 ExitConditionBlock->setTerminator(F);
940
941 // Now add the actual condition to the condition block. Because the condition
942 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000943 if (Stmt* C = F->getCond()) {
944 Block = ExitConditionBlock;
945 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000946 assert(Block == EntryConditionBlock);
947
948 // If this block contains a condition variable, add both the condition
949 // variable and initializer to the CFG.
950 if (VarDecl *VD = F->getConditionVariable()) {
951 if (Expr *Init = VD->getInit()) {
952 autoCreateBlock();
953 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
954 EntryConditionBlock = addStmt(Init);
955 assert(Block == EntryConditionBlock);
956 }
957 }
958
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000959 if (Block) {
960 if (!FinishBlock(EntryConditionBlock))
961 return 0;
962 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000963 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000964
Mike Stump6d9828c2009-07-17 01:31:16 +0000965 // The condition block is the implicit successor for the loop body as well as
966 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000967 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000968
Mike Stump00998a02009-07-23 23:25:26 +0000969 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000970 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Mike Stump00998a02009-07-23 23:25:26 +0000972 if (F->getCond())
973 KnownVal = TryEvaluateBool(F->getCond());
974
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000975 // Now create the loop body.
976 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000977 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000978
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000979 // Save the current values for Block, Succ, and continue and break targets
980 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +0000981 save_continue(ContinueTargetBlock),
982 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000983
Ted Kremenekaf603f72007-08-30 18:39:40 +0000984 // Create a new block to contain the (bottom) of the loop body.
985 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000986
Ted Kremeneke9334502008-09-04 21:48:47 +0000987 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000988 // Generate increment code in its own basic block. This is the target of
989 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +0000990 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000991 } else {
992 // No increment code. Create a special, empty, block that is used as the
993 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000994 assert(Succ == EntryConditionBlock);
995 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000996 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000997
Ted Kremenek3575f842009-04-28 00:51:56 +0000998 // Finish up the increment (or empty) block if it hasn't been already.
999 if (Block) {
1000 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001001 if (!FinishBlock(Block))
1002 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001003 Block = 0;
1004 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001005
Ted Kremenek3575f842009-04-28 00:51:56 +00001006 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001007
Ted Kremenek3575f842009-04-28 00:51:56 +00001008 // The starting block for the loop increment is the block that should
1009 // represent the 'loop target' for looping back to the start of the loop.
1010 ContinueTargetBlock->setLoopTarget(F);
1011
Ted Kremeneke9334502008-09-04 21:48:47 +00001012 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +00001013 BreakTargetBlock = LoopSuccessor;
1014
1015 // Now populate the body block, and in the process create new blocks as we
1016 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001017 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001018
1019 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001020 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +00001021 else if (Block && !FinishBlock(BodyBlock))
1022 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001023
Ted Kremenek941fde82009-07-24 04:47:11 +00001024 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001025 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001026 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001027
Ted Kremenek941fde82009-07-24 04:47:11 +00001028 // Link up the condition block with the code that follows the loop. (the
1029 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001030 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001031
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001032 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001033 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001034 if (Stmt* I = F->getInit()) {
1035 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001036 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001037 } else {
1038 // There is no loop initialization. We are thus basically a while loop.
1039 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001040 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001041 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001042 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001043 }
1044}
1045
Ted Kremenek514de5a2008-11-11 17:10:00 +00001046CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1047 // Objective-C fast enumeration 'for' statements:
1048 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1049 //
1050 // for ( Type newVariable in collection_expression ) { statements }
1051 //
1052 // becomes:
1053 //
1054 // prologue:
1055 // 1. collection_expression
1056 // T. jump to loop_entry
1057 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001058 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001059 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1060 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1061 // TB:
1062 // statements
1063 // T. jump to loop_entry
1064 // FB:
1065 // what comes after
1066 //
1067 // and
1068 //
1069 // Type existingItem;
1070 // for ( existingItem in expression ) { statements }
1071 //
1072 // becomes:
1073 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001074 // the same with newVariable replaced with existingItem; the binding works
1075 // the same except that for one ObjCForCollectionStmt::getElement() returns
1076 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001077 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001078
Ted Kremenek514de5a2008-11-11 17:10:00 +00001079 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001080
Ted Kremenek514de5a2008-11-11 17:10:00 +00001081 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001082 if (!FinishBlock(Block))
1083 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001084 LoopSuccessor = Block;
1085 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001086 } else
1087 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001088
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001089 // Build the condition blocks.
1090 CFGBlock* ExitConditionBlock = createBlock(false);
1091 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001092
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001093 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001094 ExitConditionBlock->setTerminator(S);
1095
1096 // The last statement in the block should be the ObjCForCollectionStmt, which
1097 // performs the actual binding to 'element' and determines if there are any
1098 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001099 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001100 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001101
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001102 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001103 // generate new blocks as necesary. We DON'T add the statement by default to
1104 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001105 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001106 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001107 if (!FinishBlock(EntryConditionBlock))
1108 return 0;
1109 Block = 0;
1110 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001111
1112 // The condition block is the implicit successor for the loop body as well as
1113 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001114 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001115
Ted Kremenek514de5a2008-11-11 17:10:00 +00001116 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001117 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001118 // Save the current values for Succ, continue and break targets.
1119 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001120 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1121
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001122 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001123 ContinueTargetBlock = EntryConditionBlock;
1124
Ted Kremenek4f880632009-07-17 22:18:43 +00001125 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001126
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001127 if (!BodyBlock)
1128 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001129 else if (Block) {
1130 if (!FinishBlock(BodyBlock))
1131 return 0;
1132 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001133
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001134 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001135 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001136 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001137
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001138 // Link up the condition block with the code that follows the loop.
1139 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001140 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001141
Ted Kremenek514de5a2008-11-11 17:10:00 +00001142 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001143 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001144 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001145}
1146
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001147CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1148 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001149
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001150 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001151 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001152
Ted Kremenekda5348e2009-05-05 23:11:51 +00001153 // The sync body starts its own basic block. This makes it a little easier
1154 // for diagnostic clients.
1155 if (SyncBlock) {
1156 if (!FinishBlock(SyncBlock))
1157 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001158
Ted Kremenekda5348e2009-05-05 23:11:51 +00001159 Block = 0;
1160 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001161
Ted Kremenekda5348e2009-05-05 23:11:51 +00001162 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001163
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001164 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001165 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001166}
Mike Stump6d9828c2009-07-17 01:31:16 +00001167
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001168CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001169 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001170 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001171}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001172
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001173CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001174 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001175
Mike Stumpfefb9f72009-07-21 01:12:51 +00001176 // "while" is a control-flow statement. Thus we stop processing the current
1177 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001178 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001179 if (!FinishBlock(Block))
1180 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001181 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001182 } else
1183 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001184
1185 // Because of short-circuit evaluation, the condition of the loop can span
1186 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1187 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001188 CFGBlock* ExitConditionBlock = createBlock(false);
1189 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001190
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001191 // Set the terminator for the "exit" condition block.
1192 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001193
1194 // Now add the actual condition to the condition block. Because the condition
1195 // itself may contain control-flow, new blocks may be created. Thus we update
1196 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001197 if (Stmt* C = W->getCond()) {
1198 Block = ExitConditionBlock;
1199 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001200 assert(Block == EntryConditionBlock);
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001201
1202 // If this block contains a condition variable, add both the condition
1203 // variable and initializer to the CFG.
1204 if (VarDecl *VD = W->getConditionVariable()) {
1205 if (Expr *Init = VD->getInit()) {
1206 autoCreateBlock();
1207 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1208 EntryConditionBlock = addStmt(Init);
1209 assert(Block == EntryConditionBlock);
1210 }
1211 }
1212
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001213 if (Block) {
1214 if (!FinishBlock(EntryConditionBlock))
1215 return 0;
1216 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001217 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001218
1219 // The condition block is the implicit successor for the loop body as well as
1220 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001221 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001222
Mike Stump00998a02009-07-23 23:25:26 +00001223 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001224 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001225
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001226 // Process the loop body.
1227 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001228 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001229
1230 // Save the current values for Block, Succ, and continue and break targets
1231 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1232 save_continue(ContinueTargetBlock),
1233 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001234
Mike Stump6d9828c2009-07-17 01:31:16 +00001235 // Create an empty block to represent the transition block for looping back
1236 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001237 Block = 0;
1238 assert(Succ == EntryConditionBlock);
1239 Succ = createBlock();
1240 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001241 ContinueTargetBlock = Succ;
1242
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001243 // All breaks should go to the code following the loop.
1244 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001245
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001246 // NULL out Block to force lazy instantiation of blocks for the body.
1247 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001248
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001249 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001250 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001251
Ted Kremenekaf603f72007-08-30 18:39:40 +00001252 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001253 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001254 else if (Block) {
1255 if (!FinishBlock(BodyBlock))
1256 return 0;
1257 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001258
Ted Kremenek941fde82009-07-24 04:47:11 +00001259 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001260 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001261 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001262
Ted Kremenek941fde82009-07-24 04:47:11 +00001263 // Link up the condition block with the code that follows the loop. (the
1264 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001265 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001266
1267 // There can be no more statements in the condition block since we loop back
1268 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001269 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001270
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001271 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001272 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001273 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001274}
Mike Stump1eb44332009-09-09 15:08:12 +00001275
1276
Ted Kremenek4f880632009-07-17 22:18:43 +00001277CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1278 // FIXME: For now we pretend that @catch and the code it contains does not
1279 // exit.
1280 return Block;
1281}
Mike Stump6d9828c2009-07-17 01:31:16 +00001282
Ted Kremenek2fda5042008-12-09 20:20:09 +00001283CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1284 // FIXME: This isn't complete. We basically treat @throw like a return
1285 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001286
Ted Kremenek6c249722009-09-24 18:45:41 +00001287 // If we were in the middle of a block we stop processing that block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001288 if (Block && !FinishBlock(Block))
1289 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001290
Ted Kremenek2fda5042008-12-09 20:20:09 +00001291 // Create the new block.
1292 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001293
Ted Kremenek2fda5042008-12-09 20:20:09 +00001294 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001295 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001296
1297 // Add the statement to the block. This may create new blocks if S contains
1298 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001299 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001300}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001301
Mike Stump0979d802009-07-22 22:56:04 +00001302CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001303 // If we were in the middle of a block we stop processing that block.
Mike Stump0979d802009-07-22 22:56:04 +00001304 if (Block && !FinishBlock(Block))
1305 return 0;
1306
1307 // Create the new block.
1308 Block = createBlock(false);
1309
Mike Stump5d1d2022010-01-19 02:20:09 +00001310 if (TryTerminatedBlock)
1311 // The current try statement is the only successor.
1312 AddSuccessor(Block, TryTerminatedBlock);
1313 else
1314 // otherwise the Exit block is the only successor.
1315 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001316
1317 // Add the statement to the block. This may create new blocks if S contains
1318 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001319 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001320}
1321
Ted Kremenek4f880632009-07-17 22:18:43 +00001322CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001323 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
Mike Stump8f9893a2009-07-21 01:27:50 +00001325 // "do...while" is a control-flow statement. Thus we stop processing the
1326 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001327 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001328 if (!FinishBlock(Block))
1329 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001330 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001331 } else
1332 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001333
1334 // Because of short-circuit evaluation, the condition of the loop can span
1335 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1336 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001337 CFGBlock* ExitConditionBlock = createBlock(false);
1338 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001339
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001340 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001341 ExitConditionBlock->setTerminator(D);
1342
1343 // Now add the actual condition to the condition block. Because the condition
1344 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001345 if (Stmt* C = D->getCond()) {
1346 Block = ExitConditionBlock;
1347 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001348 if (Block) {
1349 if (!FinishBlock(EntryConditionBlock))
1350 return 0;
1351 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001352 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001353
Ted Kremenek54827132008-02-27 07:20:00 +00001354 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001355 Succ = EntryConditionBlock;
1356
Mike Stump00998a02009-07-23 23:25:26 +00001357 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001358 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001359
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001360 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001361 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001362 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001363 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001364
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001365 // Save the current values for Block, Succ, and continue and break targets
1366 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001367 save_continue(ContinueTargetBlock),
1368 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001369
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001370 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001371 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001372
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001373 // All breaks should go to the code following the loop.
1374 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001375
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001376 // NULL out Block to force lazy instantiation of blocks for the body.
1377 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001378
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001379 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001380 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001381
Ted Kremenekaf603f72007-08-30 18:39:40 +00001382 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001383 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001384 else if (Block) {
1385 if (!FinishBlock(BodyBlock))
1386 return 0;
1387 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001388
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001389 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001390 // ExitConditionBlock to represent the "loop back" transition. Create an
1391 // empty block to represent the transition block for looping back to the
1392 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001393 // FIXME: Can we do this more efficiently without adding another block?
1394 Block = NULL;
1395 Succ = BodyBlock;
1396 CFGBlock *LoopBackBlock = createBlock();
1397 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001398
Ted Kremenek941fde82009-07-24 04:47:11 +00001399 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001400 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001401 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001402
Ted Kremenek941fde82009-07-24 04:47:11 +00001403 // Link up the condition block with the code that follows the loop.
1404 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001405 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001406
1407 // There can be no more statements in the body block(s) since we loop back to
1408 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001409 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001410
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001411 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001412 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001413 return BodyBlock;
1414}
1415
1416CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1417 // "continue" is a control-flow statement. Thus we stop processing the
1418 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001419 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001420 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001421
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001422 // Now create a new block that ends with the continue statement.
1423 Block = createBlock(false);
1424 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001425
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001426 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001427 // incomplete AST. This means the CFG cannot be constructed.
1428 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001429 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001430 else
1431 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433 return Block;
1434}
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001436CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001437 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001438
Ted Kremenek852274d2009-12-16 03:18:58 +00001439 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001440 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001441 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Ted Kremenek4f880632009-07-17 22:18:43 +00001444 // VLA types have expressions that must be evaluated.
1445 if (E->isArgumentType()) {
1446 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1447 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1448 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001449 }
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Mike Stump6d9828c2009-07-17 01:31:16 +00001451 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001452}
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Ted Kremenek4f880632009-07-17 22:18:43 +00001454/// VisitStmtExpr - Utility method to handle (nested) statement
1455/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001456CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1457 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001458 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001459 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001460 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001461 return VisitCompoundStmt(SE->getSubStmt());
1462}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001463
Ted Kremenek411cdee2008-04-16 21:10:48 +00001464CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001465 // "switch" is a control-flow statement. Thus we stop processing the current
1466 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001467 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001468
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001469 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001470 if (!FinishBlock(Block))
1471 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001473 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001474
1475 // Save the current "switch" context.
1476 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001477 save_break(BreakTargetBlock),
1478 save_default(DefaultCaseBlock);
1479
Mike Stump6d9828c2009-07-17 01:31:16 +00001480 // Set the "default" case to be the block after the switch statement. If the
1481 // switch statement contains a "default:", this value will be overwritten with
1482 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001483 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001484
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001485 // Create a new block that will contain the switch statement.
1486 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001487
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001488 // Now process the switch body. The code after the switch is the implicit
1489 // successor.
1490 Succ = SwitchSuccessor;
1491 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001492
1493 // When visiting the body, the case statements should automatically get linked
1494 // up to the switch. We also don't keep a pointer to the body, since all
1495 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001496 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001497 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001498 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001499 if (Block) {
1500 if (!FinishBlock(BodyBlock))
1501 return 0;
1502 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001503
Mike Stump6d9828c2009-07-17 01:31:16 +00001504 // If we have no "default:" case, the default transition is to the code
1505 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001506 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001507
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001508 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001509 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001510 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001511 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001512 Block = addStmt(Terminator->getCond());
1513
1514 // Finally, if the SwitchStmt contains a condition variable, add both the
1515 // SwitchStmt and the condition variable initialization to the CFG.
1516 if (VarDecl *VD = Terminator->getConditionVariable()) {
1517 if (Expr *Init = VD->getInit()) {
1518 autoCreateBlock();
1519 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1520 addStmt(Init);
1521 }
1522 }
1523
1524 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001525}
1526
Ted Kremenek4f880632009-07-17 22:18:43 +00001527CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001528 // CaseStmts are essentially labels, so they are the first statement in a
1529 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001530
Ted Kremenek4f880632009-07-17 22:18:43 +00001531 if (CS->getSubStmt())
1532 addStmt(CS->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001534 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001535 if (!CaseBlock)
1536 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001537
1538 // Cases statements partition blocks, so this is the top of the basic block we
1539 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001540 CaseBlock->setLabel(CS);
1541
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001542 if (!FinishBlock(CaseBlock))
1543 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001544
1545 // Add this block to the list of successors for the block with the switch
1546 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001547 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001548 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001549
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001550 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1551 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001552
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001553 // This block is now the implicit successor of other blocks.
1554 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001555
Ted Kremenek2677ea82008-03-15 07:45:02 +00001556 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001557}
Mike Stump6d9828c2009-07-17 01:31:16 +00001558
Ted Kremenek411cdee2008-04-16 21:10:48 +00001559CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001560 if (Terminator->getSubStmt())
1561 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001563 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001564
1565 if (!DefaultCaseBlock)
1566 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001567
1568 // Default statements partition blocks, so this is the top of the basic block
1569 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001570 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001572 if (!FinishBlock(DefaultCaseBlock))
1573 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001574
Mike Stump6d9828c2009-07-17 01:31:16 +00001575 // Unlike case statements, we don't add the default block to the successors
1576 // for the switch statement immediately. This is done when we finish
1577 // processing the switch statement. This allows for the default case
1578 // (including a fall-through to the code after the switch statement) to always
1579 // be the last successor of a switch-terminated block.
1580
Ted Kremenekeef5a9a2008-02-13 22:05:39 +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 Kremenekeef5a9a2008-02-13 22:05:39 +00001584 // This block is now the implicit successor of other blocks.
1585 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001586
1587 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001588}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001589
Mike Stump5d1d2022010-01-19 02:20:09 +00001590CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1591 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1592 // current block.
1593 CFGBlock* TrySuccessor = NULL;
1594
1595 if (Block) {
1596 if (!FinishBlock(Block))
1597 return 0;
1598 TrySuccessor = Block;
1599 } else TrySuccessor = Succ;
1600
Mike Stumpa1f93632010-01-20 01:15:34 +00001601 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001602
1603 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001604 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001605 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001606 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001607
Mike Stumpa1f93632010-01-20 01:15:34 +00001608 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001609 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1610 // The code after the try is the implicit successor.
1611 Succ = TrySuccessor;
1612 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001613 if (CS->getExceptionDecl() == 0) {
1614 HasCatchAll = true;
1615 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001616 Block = NULL;
1617 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1618 if (CatchBlock == 0)
1619 return 0;
1620 // Add this block to the list of successors for the block with the try
1621 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001622 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001623 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001624 if (!HasCatchAll) {
1625 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001626 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001627 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001628 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001629 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001630
1631 // The code after the try is the implicit successor.
1632 Succ = TrySuccessor;
1633
Mike Stumpf00cca52010-01-20 01:30:58 +00001634 // Save the current "try" context.
1635 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1636 TryTerminatedBlock = NewTryTerminatedBlock;
1637
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001638 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001639 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001640 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001641 return Block;
1642}
1643
1644CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1645 // CXXCatchStmt are treated like labels, so they are the first statement in a
1646 // block.
1647
1648 if (CS->getHandlerBlock())
1649 addStmt(CS->getHandlerBlock());
1650
1651 CFGBlock* CatchBlock = Block;
1652 if (!CatchBlock)
1653 CatchBlock = createBlock();
1654
1655 CatchBlock->setLabel(CS);
1656
1657 if (!FinishBlock(CatchBlock))
1658 return 0;
1659
1660 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1661 Block = NULL;
1662
1663 return CatchBlock;
1664}
1665
Ted Kremenek19bb3562007-08-28 19:26:49 +00001666CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001667 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001668 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001669
Ted Kremenek19bb3562007-08-28 19:26:49 +00001670 if (!IBlock) {
1671 IBlock = createBlock(false);
1672 cfg->setIndirectGotoBlock(IBlock);
1673 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001674
Ted Kremenek19bb3562007-08-28 19:26:49 +00001675 // IndirectGoto is a control-flow statement. Thus we stop processing the
1676 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001677 if (Block && !FinishBlock(Block))
1678 return 0;
1679
Ted Kremenek19bb3562007-08-28 19:26:49 +00001680 Block = createBlock(false);
1681 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001682 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001683 return addStmt(I->getTarget());
1684}
1685
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001686} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001687
Mike Stump6d9828c2009-07-17 01:31:16 +00001688/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1689/// no successors or predecessors. If this is the first block created in the
1690/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001691CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001692 bool first_block = begin() == end();
1693
1694 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001695 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1696 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1697 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001698
1699 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001700 if (first_block)
1701 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001702
1703 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001704 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001705}
1706
Ted Kremenek026473c2007-08-23 16:51:22 +00001707/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1708/// CFG is returned to the caller.
Mike Stump079bd722010-01-19 22:00:14 +00001709CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C, bool AddScopes) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001710 CFGBuilder Builder;
Mike Stump079bd722010-01-19 22:00:14 +00001711 return Builder.buildCFG(Statement, C, AddScopes);
Ted Kremenek026473c2007-08-23 16:51:22 +00001712}
1713
Ted Kremenek63f58872007-10-01 19:33:33 +00001714//===----------------------------------------------------------------------===//
1715// CFG: Queries for BlkExprs.
1716//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001717
Ted Kremenek63f58872007-10-01 19:33:33 +00001718namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001719 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001720}
1721
Ted Kremenek8a693662009-12-23 23:37:10 +00001722static void FindSubExprAssignments(Stmt *S,
1723 llvm::SmallPtrSet<Expr*,50>& Set) {
1724 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001725 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001726
Ted Kremenek8a693662009-12-23 23:37:10 +00001727 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
1728 Stmt *child = *I;
1729 if (!child)
1730 continue;
1731
1732 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001733 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001734
Ted Kremenek8a693662009-12-23 23:37:10 +00001735 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001736 }
1737}
1738
Ted Kremenek63f58872007-10-01 19:33:33 +00001739static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1740 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001741
1742 // Look for assignments that are used as subexpressions. These are the only
1743 // assignments that we want to *possibly* register as a block-level
1744 // expression. Basically, if an assignment occurs both in a subexpression and
1745 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001746 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001747
Ted Kremenek63f58872007-10-01 19:33:33 +00001748 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001749 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001750 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001751
Ted Kremenek411cdee2008-04-16 21:10:48 +00001752 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001753
1754 // Iterate over the statements again on identify the Expr* and Stmt* at the
1755 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001756
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001757 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001758 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001759
Ted Kremenek411cdee2008-04-16 21:10:48 +00001760 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001761 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001762 // expression are really "statements" whose value is never used by
1763 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001764 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001765 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001766 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1767 // Special handling for statement expressions. The last statement in
1768 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001769 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001770 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001771 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001772 (*M)[C->body_back()] = x;
1773 }
1774 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001775
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001776 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001777 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001778 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001779
Ted Kremenek411cdee2008-04-16 21:10:48 +00001780 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001781
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001782 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001783
Ted Kremenek390e48b2008-11-12 21:11:49 +00001784 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001785 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001786 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001787 }
1788 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001789
Ted Kremenek63f58872007-10-01 19:33:33 +00001790 return M;
1791}
1792
Ted Kremenek86946742008-01-17 20:48:37 +00001793CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1794 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001795 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001796
Ted Kremenek63f58872007-10-01 19:33:33 +00001797 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001798 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001799 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001800}
1801
1802unsigned CFG::getNumBlkExprs() {
1803 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1804 return M->size();
1805 else {
1806 // We assume callers interested in the number of BlkExprs will want
1807 // the map constructed if it doesn't already exist.
1808 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1809 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1810 }
1811}
1812
Ted Kremenek274f4332008-04-28 18:00:46 +00001813//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001814// Cleanup: CFG dstor.
1815//===----------------------------------------------------------------------===//
1816
Ted Kremenek63f58872007-10-01 19:33:33 +00001817CFG::~CFG() {
1818 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1819}
Mike Stump6d9828c2009-07-17 01:31:16 +00001820
Ted Kremenek7dba8602007-08-29 21:56:09 +00001821//===----------------------------------------------------------------------===//
1822// CFG pretty printing
1823//===----------------------------------------------------------------------===//
1824
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001825namespace {
1826
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001827class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001828 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1829 StmtMapTy StmtMap;
1830 signed CurrentBlock;
1831 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001832 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001833public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001834
Chris Lattnere4f21422009-06-30 01:26:17 +00001835 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1836 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001837 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1838 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001839 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001840 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001841 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001842 }
1843 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001844
Ted Kremenek42a509f2007-08-31 21:30:12 +00001845 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001846
Chris Lattnere4f21422009-06-30 01:26:17 +00001847 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001848 void setBlockID(signed i) { CurrentBlock = i; }
1849 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001850
Ted Kremeneka95d3752008-09-13 05:16:45 +00001851 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001852
Ted Kremenek411cdee2008-04-16 21:10:48 +00001853 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001854
1855 if (I == StmtMap.end())
1856 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001857
1858 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001859 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001860 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001861 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001862
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001863 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001864 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001865 }
1866};
Chris Lattnere4f21422009-06-30 01:26:17 +00001867} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001868
Chris Lattnere4f21422009-06-30 01:26:17 +00001869
1870namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001871class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001872 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001873
Ted Kremeneka95d3752008-09-13 05:16:45 +00001874 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001875 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001876 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001877public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001878 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001879 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001880 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001882 void VisitIfStmt(IfStmt* I) {
1883 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001884 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001885 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001886
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001887 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001888 void VisitStmt(Stmt* Terminator) {
1889 Terminator->printPretty(OS, Helper, Policy);
1890 }
1891
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001892 void VisitForStmt(ForStmt* F) {
1893 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001894 if (F->getInit())
1895 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00001896 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001897 if (Stmt* C = F->getCond())
1898 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001899 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001900 if (F->getInc())
1901 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001902 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001903 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001904
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001905 void VisitWhileStmt(WhileStmt* W) {
1906 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001907 if (Stmt* C = W->getCond())
1908 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001909 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001910
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001911 void VisitDoStmt(DoStmt* D) {
1912 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001913 if (Stmt* C = D->getCond())
1914 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001915 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001916
Ted Kremenek411cdee2008-04-16 21:10:48 +00001917 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001918 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001919 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001920 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001921
Mike Stump5d1d2022010-01-19 02:20:09 +00001922 void VisitCXXTryStmt(CXXTryStmt* CS) {
1923 OS << "try ...";
1924 }
1925
Ted Kremenek805e9a82007-08-31 21:49:40 +00001926 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001927 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001928 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001929 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001930
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001931 void VisitChooseExpr(ChooseExpr* C) {
1932 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001933 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001934 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001935 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001936
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001937 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1938 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001939 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001940 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001941
Ted Kremenek805e9a82007-08-31 21:49:40 +00001942 void VisitBinaryOperator(BinaryOperator* B) {
1943 if (!B->isLogicalOp()) {
1944 VisitExpr(B);
1945 return;
1946 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001947
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001948 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001949
Ted Kremenek805e9a82007-08-31 21:49:40 +00001950 switch (B->getOpcode()) {
1951 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001952 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001953 return;
1954 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001955 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001956 return;
1957 default:
1958 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001959 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001960 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001961
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001962 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001963 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001964 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001965};
Chris Lattnere4f21422009-06-30 01:26:17 +00001966} // end anonymous namespace
1967
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Chris Lattnere4f21422009-06-30 01:26:17 +00001969static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00001970 const CFGElement &E) {
1971 Stmt *Terminator = E;
1972
1973 if (E.asStartScope()) {
1974 OS << "start scope\n";
1975 return;
1976 }
1977 if (E.asEndScope()) {
1978 OS << "end scope\n";
1979 return;
1980 }
1981
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001982 if (Helper) {
1983 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001984 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001985 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001986
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001987 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001988 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001989 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001990 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001991 return;
1992 }
1993 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001995 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001996 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001997 if (B->getOpcode() == BinaryOperator::Comma) {
1998 OS << "... , ";
1999 Helper->handledStmt(B->getRHS(),OS);
2000 OS << '\n';
2001 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002002 }
2003 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002004 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002005
Chris Lattnere4f21422009-06-30 01:26:17 +00002006 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00002007
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002008 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00002009 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002010}
Mike Stump6d9828c2009-07-17 01:31:16 +00002011
Chris Lattnere4f21422009-06-30 01:26:17 +00002012static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2013 const CFGBlock& B,
2014 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002015
Ted Kremenek42a509f2007-08-31 21:30:12 +00002016 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002017
Ted Kremenek7dba8602007-08-29 21:56:09 +00002018 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002019 OS << "\n [ B" << B.getBlockID();
2020
Ted Kremenek42a509f2007-08-31 21:30:12 +00002021 if (&B == &cfg->getEntry())
2022 OS << " (ENTRY) ]\n";
2023 else if (&B == &cfg->getExit())
2024 OS << " (EXIT) ]\n";
2025 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002026 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002027 else
2028 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002029
Ted Kremenek9cffe732007-08-29 23:20:49 +00002030 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002031 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002032
2033 if (print_edges)
2034 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002035
Mike Stump079bd722010-01-19 22:00:14 +00002036 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002037 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002038 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002039 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002040 C->getLHS()->printPretty(OS, Helper,
2041 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002042 if (C->getRHS()) {
2043 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002044 C->getRHS()->printPretty(OS, Helper,
2045 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002046 }
Mike Stump079bd722010-01-19 22:00:14 +00002047 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002048 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002049 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002050 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002051 if (CS->getExceptionDecl())
2052 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2053 0);
2054 else
2055 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002056 OS << ")";
2057
2058 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002059 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002060
Ted Kremenek9cffe732007-08-29 23:20:49 +00002061 OS << ":\n";
2062 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002063
Ted Kremenekfddd5182007-08-21 21:42:03 +00002064 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002065 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002066
Ted Kremenek42a509f2007-08-31 21:30:12 +00002067 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2068 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002069
Ted Kremenek9cffe732007-08-29 23:20:49 +00002070 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002071 if (print_edges)
2072 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002073
Ted Kremeneka95d3752008-09-13 05:16:45 +00002074 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002075
Ted Kremenek42a509f2007-08-31 21:30:12 +00002076 if (Helper)
2077 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002078
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002079 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002080 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002081
Ted Kremenek9cffe732007-08-29 23:20:49 +00002082 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002083 if (B.getTerminator()) {
2084 if (print_edges)
2085 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002086
Ted Kremenek9cffe732007-08-29 23:20:49 +00002087 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002088
Ted Kremenek42a509f2007-08-31 21:30:12 +00002089 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002090
Chris Lattnere4f21422009-06-30 01:26:17 +00002091 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2092 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002093 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002094 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002095 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002096
Ted Kremenek9cffe732007-08-29 23:20:49 +00002097 if (print_edges) {
2098 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002099 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002100 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002101
Ted Kremenek42a509f2007-08-31 21:30:12 +00002102 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2103 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002104
Ted Kremenek42a509f2007-08-31 21:30:12 +00002105 if (i == 8 || (i-8) == 0)
2106 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002107
Ted Kremenek9cffe732007-08-29 23:20:49 +00002108 OS << " B" << (*I)->getBlockID();
2109 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002110
Ted Kremenek42a509f2007-08-31 21:30:12 +00002111 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002112
Ted Kremenek42a509f2007-08-31 21:30:12 +00002113 // Print the successors of this block.
2114 OS << " Successors (" << B.succ_size() << "):";
2115 i = 0;
2116
2117 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2118 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002119
Ted Kremenek42a509f2007-08-31 21:30:12 +00002120 if (i == 8 || (i-8) % 10 == 0)
2121 OS << "\n ";
2122
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002123 if (*I)
2124 OS << " B" << (*I)->getBlockID();
2125 else
2126 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002127 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002128
Ted Kremenek9cffe732007-08-29 23:20:49 +00002129 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002130 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002131}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002132
Ted Kremenek42a509f2007-08-31 21:30:12 +00002133
2134/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002135void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002136
2137/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002138void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2139 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002140
Ted Kremenek42a509f2007-08-31 21:30:12 +00002141 // Print the entry block.
2142 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002143
Ted Kremenek42a509f2007-08-31 21:30:12 +00002144 // Iterate through the CFGBlocks and print them one by one.
2145 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2146 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002147 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002148 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002149
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002150 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002151 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002152
Ted Kremenek42a509f2007-08-31 21:30:12 +00002153 // Print the exit block.
2154 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002155 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002156}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002157
2158/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002159void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2160 print(llvm::errs(), cfg, LO);
2161}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002162
2163/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2164/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002165void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2166 const LangOptions &LO) const {
2167 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002168 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002169}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002170
Ted Kremeneka2925852008-01-30 23:02:42 +00002171/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002172void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002173 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002174 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002175 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2176}
2177
Ted Kremenek390e48b2008-11-12 21:11:49 +00002178Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002179
Ted Kremenek411cdee2008-04-16 21:10:48 +00002180 if (!Terminator)
2181 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002182
Ted Kremenek411cdee2008-04-16 21:10:48 +00002183 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002184
Ted Kremenek411cdee2008-04-16 21:10:48 +00002185 switch (Terminator->getStmtClass()) {
2186 default:
2187 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002188
Ted Kremenek411cdee2008-04-16 21:10:48 +00002189 case Stmt::ForStmtClass:
2190 E = cast<ForStmt>(Terminator)->getCond();
2191 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002192
Ted Kremenek411cdee2008-04-16 21:10:48 +00002193 case Stmt::WhileStmtClass:
2194 E = cast<WhileStmt>(Terminator)->getCond();
2195 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002196
Ted Kremenek411cdee2008-04-16 21:10:48 +00002197 case Stmt::DoStmtClass:
2198 E = cast<DoStmt>(Terminator)->getCond();
2199 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002200
Ted Kremenek411cdee2008-04-16 21:10:48 +00002201 case Stmt::IfStmtClass:
2202 E = cast<IfStmt>(Terminator)->getCond();
2203 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002204
Ted Kremenek411cdee2008-04-16 21:10:48 +00002205 case Stmt::ChooseExprClass:
2206 E = cast<ChooseExpr>(Terminator)->getCond();
2207 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002208
Ted Kremenek411cdee2008-04-16 21:10:48 +00002209 case Stmt::IndirectGotoStmtClass:
2210 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2211 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002212
Ted Kremenek411cdee2008-04-16 21:10:48 +00002213 case Stmt::SwitchStmtClass:
2214 E = cast<SwitchStmt>(Terminator)->getCond();
2215 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002216
Ted Kremenek411cdee2008-04-16 21:10:48 +00002217 case Stmt::ConditionalOperatorClass:
2218 E = cast<ConditionalOperator>(Terminator)->getCond();
2219 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002220
Ted Kremenek411cdee2008-04-16 21:10:48 +00002221 case Stmt::BinaryOperatorClass: // '&&' and '||'
2222 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002223 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002224
Ted Kremenek390e48b2008-11-12 21:11:49 +00002225 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002226 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002227 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002228
Ted Kremenek411cdee2008-04-16 21:10:48 +00002229 return E ? E->IgnoreParens() : NULL;
2230}
2231
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002232bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002233
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002234 if (!Terminator)
2235 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002236
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002237 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002238
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002239 switch (Terminator->getStmtClass()) {
2240 default:
2241 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002242
2243 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002244 case Stmt::WhileStmtClass:
2245 case Stmt::DoStmtClass:
2246 case Stmt::IfStmtClass:
2247 case Stmt::ChooseExprClass:
2248 case Stmt::ConditionalOperatorClass:
2249 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002250 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002251 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002252
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002253 return E ? E->IgnoreParens() : NULL;
2254}
2255
Ted Kremeneka2925852008-01-30 23:02:42 +00002256
Ted Kremenek7dba8602007-08-29 21:56:09 +00002257//===----------------------------------------------------------------------===//
2258// CFG Graphviz Visualization
2259//===----------------------------------------------------------------------===//
2260
Ted Kremenek42a509f2007-08-31 21:30:12 +00002261
2262#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002263static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002264#endif
2265
Chris Lattnere4f21422009-06-30 01:26:17 +00002266void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002267#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002268 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002269 GraphHelper = &H;
2270 llvm::ViewGraph(this,"CFG");
2271 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002272#endif
2273}
2274
Ted Kremenek7dba8602007-08-29 21:56:09 +00002275namespace llvm {
2276template<>
2277struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002278
2279 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2280
2281 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002282
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002283#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002284 std::string OutSStr;
2285 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002286 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002287 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002288
2289 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2290
2291 // Process string output to make it nicer...
2292 for (unsigned i = 0; i != OutStr.length(); ++i)
2293 if (OutStr[i] == '\n') { // Left justify
2294 OutStr[i] = '\\';
2295 OutStr.insert(OutStr.begin()+i+1, 'l');
2296 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002297
Ted Kremenek7dba8602007-08-29 21:56:09 +00002298 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002299#else
2300 return "";
2301#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002302 }
2303};
2304} // end namespace llvm