blob: c4406858e5f8504a2fffd6969226ce6e4380ce00 [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"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000019#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000020#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek7dba8602007-08-29 21:56:09 +000021#include "llvm/Support/GraphWriter.h"
Ted Kremenek7e3a89d2007-12-17 19:35:20 +000022#include "llvm/Support/Streams.h"
Ted Kremenek6fa9b882008-01-08 18:15:10 +000023#include "llvm/Support/Compiler.h"
Ted Kremenek274f4332008-04-28 18:00:46 +000024#include <llvm/Support/Allocator.h>
Ted Kremeneka95d3752008-09-13 05:16:45 +000025#include <llvm/Support/Format.h>
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Mike Stump6d9828c2009-07-17 01:31:16 +000038
Ted Kremeneka34ea072008-08-04 22:51:42 +000039/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000040/// The builder is stateful: an instance of the builder should be used to only
41/// construct a single CFG.
42///
43/// Example usage:
44///
45/// CFGBuilder builder;
46/// CFG* cfg = builder.BuildAST(stmt1);
47///
Mike Stump6d9828c2009-07-17 01:31:16 +000048/// CFG construction is done via a recursive walk of an AST. We actually parse
49/// the AST in reverse order so that the successor of a basic block is
50/// constructed prior to its predecessor. This allows us to nicely capture
51/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000052///
Ted Kremenek4f880632009-07-17 22:18:43 +000053class VISIBILITY_HIDDEN CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000054 ASTContext *Context;
Ted Kremenekfddd5182007-08-21 21:42:03 +000055 CFG* cfg;
56 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000057 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000058 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000059 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000060 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000061 CFGBlock* DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000062
Ted Kremenek19bb3562007-08-28 19:26:49 +000063 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000064 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
65 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000066
67 // A list of blocks that end with a "goto" that must be backpatched to their
68 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000069 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000070 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000071
Ted Kremenek19bb3562007-08-28 19:26:49 +000072 // A list of labels whose address has been taken (for indirect gotos).
73 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
74 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000075
76public:
Ted Kremenek026473c2007-08-23 16:51:22 +000077 explicit CFGBuilder() : cfg(NULL), Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000078 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000079 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL) {
Ted Kremenekfddd5182007-08-21 21:42:03 +000080 // Create an empty CFG.
Mike Stump6d9828c2009-07-17 01:31:16 +000081 cfg = new CFG();
Ted Kremenekfddd5182007-08-21 21:42:03 +000082 }
Mike Stump6d9828c2009-07-17 01:31:16 +000083
Ted Kremenekfddd5182007-08-21 21:42:03 +000084 ~CFGBuilder() { delete cfg; }
Mike Stump6d9828c2009-07-17 01:31:16 +000085
Ted Kremenekd4fdee32007-08-23 21:42:29 +000086 // buildCFG - Used by external clients to construct the CFG.
Mike Stumpe5af3ce2009-07-20 23:24:15 +000087 CFG* buildCFG(Stmt *Statement, ASTContext *C);
Mike Stump6d9828c2009-07-17 01:31:16 +000088
Ted Kremenek4f880632009-07-17 22:18:43 +000089private:
90 // Visitors to walk an AST and construct the CFG.
91 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd);
92 CFGBlock *VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd);
Ted Kremenek13fc08a2009-07-18 00:47:21 +000093 CFGBlock *VisitBlockExpr(BlockExpr* E, bool alwaysAdd);
94 CFGBlock *VisitBlockDeclRefExpr(BlockDeclRefExpr* E, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +000095 CFGBlock *VisitBreakStmt(BreakStmt *B);
96 CFGBlock *VisitCallExpr(CallExpr *C, bool alwaysAdd);
97 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +000098 CFGBlock *VisitChooseExpr(ChooseExpr *C);
99 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
100 CFGBlock *VisitConditionalOperator(ConditionalOperator *C);
101 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Mike Stump0979d802009-07-22 22:56:04 +0000102 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
Ted Kremenek4f880632009-07-17 22:18:43 +0000103 CFGBlock *VisitDeclStmt(DeclStmt *DS);
104 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000105 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
106 CFGBlock *VisitDoStmt(DoStmt *D);
107 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000108 CFGBlock *VisitGotoStmt(GotoStmt* G);
109 CFGBlock *VisitIfStmt(IfStmt *I);
110 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
111 CFGBlock *VisitLabelStmt(LabelStmt *L);
112 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
113 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
114 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
115 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
116 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
117 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000118 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, bool alwaysAdd);
119 CFGBlock *VisitStmtExpr(StmtExpr *S, bool alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000120 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
121 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000122
Ted Kremenek4f880632009-07-17 22:18:43 +0000123 CFGBlock *Visit(Stmt *S, bool alwaysAdd = false);
124 CFGBlock *VisitStmt(Stmt *S, bool alwaysAdd);
125 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000126
Ted Kremenek274f4332008-04-28 18:00:46 +0000127 // NYS == Not Yet Supported
128 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000129 badCFG = true;
130 return Block;
131 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000132
Ted Kremenek4f880632009-07-17 22:18:43 +0000133 void autoCreateBlock() { if (!Block) Block = createBlock(); }
134 CFGBlock *createBlock(bool add_successor = true);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000135 bool FinishBlock(CFGBlock* B);
Ted Kremenek4f880632009-07-17 22:18:43 +0000136 CFGBlock *addStmt(Stmt *S) { return Visit(S, true); }
137
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000138
139 /// TryResult - a class representing a variant over the values
140 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
141 /// and is used by the CFGBuilder to decide if a branch condition
142 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000143 class TryResult {
144 int X;
145 public:
146 TryResult(bool b) : X(b ? 1 : 0) {}
147 TryResult() : X(-1) {}
148
149 bool isTrue() const { return X == 1; }
150 bool isFalse() const { return X == 0; }
151 bool isKnown() const { return X >= 0; }
152 void negate() {
153 assert(isKnown());
154 X ^= 0x1;
155 }
156 };
157
Mike Stump00998a02009-07-23 23:25:26 +0000158 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
159 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000160 TryResult TryEvaluateBool(Expr *S) {
Mike Stump00998a02009-07-23 23:25:26 +0000161 Expr::EvalResult Result;
Ted Kremenek941fde82009-07-24 04:47:11 +0000162 if (S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000163 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000164
165 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000166 }
167
Ted Kremenek4102af92008-03-13 03:04:22 +0000168 bool badCFG;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000169};
Mike Stump6d9828c2009-07-17 01:31:16 +0000170
Douglas Gregor898574e2008-12-05 23:32:09 +0000171// FIXME: Add support for dependent-sized array types in C++?
172// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000173static VariableArrayType* FindVA(Type* t) {
174 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
175 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
176 if (vat->getSizeExpr())
177 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000178
Ted Kremenek610a09e2008-09-26 22:58:57 +0000179 t = vt->getElementType().getTypePtr();
180 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000181
Ted Kremenek610a09e2008-09-26 22:58:57 +0000182 return 0;
183}
Mike Stump6d9828c2009-07-17 01:31:16 +0000184
185/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
186/// arbitrary statement. Examples include a single expression or a function
187/// body (compound statement). The ownership of the returned CFG is
188/// transferred to the caller. If CFG construction fails, this method returns
189/// NULL.
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000190CFG* CFGBuilder::buildCFG(Stmt* Statement, ASTContext* C) {
191 Context = C;
Ted Kremenek4f880632009-07-17 22:18:43 +0000192 assert(cfg);
193 if (!Statement)
194 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000195
Ted Kremenek4102af92008-03-13 03:04:22 +0000196 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000197
198 // Create an empty block that will serve as the exit block for the CFG. Since
199 // this is the first block added to the CFG, it will be implicitly registered
200 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000201 Succ = createBlock();
202 assert (Succ == &cfg->getExit());
203 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000204
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000205 // Visit the statements and create the CFG.
Ted Kremenek4f880632009-07-17 22:18:43 +0000206 CFGBlock* B = addStmt(Statement);
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000207 if (!B) B = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000208
Ted Kremenek0d99ecf2008-02-27 17:33:02 +0000209 if (B) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000210 // Finalize the last constructed block. This usually involves reversing the
211 // order of the statements in the block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000212 if (Block) FinishBlock(B);
Mike Stump6d9828c2009-07-17 01:31:16 +0000213
214 // Backpatch the gotos whose label -> block mappings we didn't know when we
215 // encountered them.
216 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000217 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000218
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000219 CFGBlock* B = *I;
220 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
221 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
222
223 // If there is no target for the goto, then we are looking at an
224 // incomplete AST. Handle this by not registering a successor.
225 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000226
227 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000228 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000229
Ted Kremenek19bb3562007-08-28 19:26:49 +0000230 // Add successors to the Indirect Goto Dispatch block (if we have one).
231 if (CFGBlock* B = cfg->getIndirectGotoBlock())
232 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
233 E = AddressTakenLabels.end(); I != E; ++I ) {
234
235 // Lookup the target block.
236 LabelMapTy::iterator LI = LabelMap.find(*I);
237
238 // If there is no target block that contains label, then we are looking
239 // at an incomplete AST. Handle this by not registering a successor.
240 if (LI == LabelMap.end()) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +0000241
242 B->addSuccessor(LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000243 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000244
Ted Kremenek94b33162007-09-17 16:18:02 +0000245 Succ = B;
Ted Kremenek322f58d2007-09-26 21:23:31 +0000246 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000247
248 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000249 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000250
Ted Kremenek4102af92008-03-13 03:04:22 +0000251 if (badCFG) {
252 delete cfg;
253 cfg = NULL;
254 return NULL;
255 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000256
257 // NULL out cfg so that repeated calls to the builder will fail and that the
258 // ownership of the constructed CFG is passed to the caller.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000259 CFG* t = cfg;
260 cfg = NULL;
261 return t;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000262}
Mike Stump6d9828c2009-07-17 01:31:16 +0000263
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000264/// createBlock - Used to lazily create blocks that are connected
265/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000266CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000267 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000268 if (add_successor && Succ)
269 B->addSuccessor(Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000270 return B;
271}
Mike Stump6d9828c2009-07-17 01:31:16 +0000272
273/// FinishBlock - When the last statement has been added to the block, we must
274/// reverse the statements because they have been inserted in reverse order.
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000275bool CFGBuilder::FinishBlock(CFGBlock* B) {
276 if (badCFG)
277 return false;
278
Ted Kremenek4f880632009-07-17 22:18:43 +0000279 assert(B);
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000280 B->reverseStmts();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000281 return true;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000282}
283
Ted Kremenek4f880632009-07-17 22:18:43 +0000284/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000285/// blocks for ternary operators, &&, and ||. We also process "," and
286/// DeclStmts (which may contain nested control-flow).
Ted Kremenek4f880632009-07-17 22:18:43 +0000287CFGBlock* CFGBuilder::Visit(Stmt * S, bool alwaysAdd) {
288tryAgain:
289 switch (S->getStmtClass()) {
290 default:
291 return VisitStmt(S, alwaysAdd);
292
293 case Stmt::AddrLabelExprClass:
294 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), alwaysAdd);
295
296 case Stmt::BinaryOperatorClass:
297 return VisitBinaryOperator(cast<BinaryOperator>(S), alwaysAdd);
298
299 case Stmt::BlockExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000300 return VisitBlockExpr(cast<BlockExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000301
302 case Stmt::BlockDeclRefExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000303 return VisitBlockDeclRefExpr(cast<BlockDeclRefExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000304
305 case Stmt::BreakStmtClass:
306 return VisitBreakStmt(cast<BreakStmt>(S));
307
308 case Stmt::CallExprClass:
309 return VisitCallExpr(cast<CallExpr>(S), alwaysAdd);
310
311 case Stmt::CaseStmtClass:
312 return VisitCaseStmt(cast<CaseStmt>(S));
313
314 case Stmt::ChooseExprClass:
315 return VisitChooseExpr(cast<ChooseExpr>(S));
316
317 case Stmt::CompoundStmtClass:
318 return VisitCompoundStmt(cast<CompoundStmt>(S));
319
320 case Stmt::ConditionalOperatorClass:
321 return VisitConditionalOperator(cast<ConditionalOperator>(S));
322
323 case Stmt::ContinueStmtClass:
324 return VisitContinueStmt(cast<ContinueStmt>(S));
325
326 case Stmt::DeclStmtClass:
327 return VisitDeclStmt(cast<DeclStmt>(S));
328
329 case Stmt::DefaultStmtClass:
330 return VisitDefaultStmt(cast<DefaultStmt>(S));
331
332 case Stmt::DoStmtClass:
333 return VisitDoStmt(cast<DoStmt>(S));
334
335 case Stmt::ForStmtClass:
336 return VisitForStmt(cast<ForStmt>(S));
337
338 case Stmt::GotoStmtClass:
339 return VisitGotoStmt(cast<GotoStmt>(S));
340
341 case Stmt::IfStmtClass:
342 return VisitIfStmt(cast<IfStmt>(S));
343
344 case Stmt::IndirectGotoStmtClass:
345 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
346
347 case Stmt::LabelStmtClass:
348 return VisitLabelStmt(cast<LabelStmt>(S));
349
350 case Stmt::ObjCAtCatchStmtClass:
351 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
352
Mike Stump0979d802009-07-22 22:56:04 +0000353 case Stmt::CXXThrowExprClass:
354 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
355
Ted Kremenek4f880632009-07-17 22:18:43 +0000356 case Stmt::ObjCAtSynchronizedStmtClass:
357 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
358
359 case Stmt::ObjCAtThrowStmtClass:
360 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
361
362 case Stmt::ObjCAtTryStmtClass:
363 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
364
365 case Stmt::ObjCForCollectionStmtClass:
366 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
367
368 case Stmt::ParenExprClass:
369 S = cast<ParenExpr>(S)->getSubExpr();
370 goto tryAgain;
371
372 case Stmt::NullStmtClass:
373 return Block;
374
375 case Stmt::ReturnStmtClass:
376 return VisitReturnStmt(cast<ReturnStmt>(S));
377
378 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000379 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000380
381 case Stmt::StmtExprClass:
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000382 return VisitStmtExpr(cast<StmtExpr>(S), alwaysAdd);
Ted Kremenek4f880632009-07-17 22:18:43 +0000383
384 case Stmt::SwitchStmtClass:
385 return VisitSwitchStmt(cast<SwitchStmt>(S));
386
387 case Stmt::WhileStmtClass:
388 return VisitWhileStmt(cast<WhileStmt>(S));
389 }
390}
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000391
Ted Kremenek4f880632009-07-17 22:18:43 +0000392CFGBlock *CFGBuilder::VisitStmt(Stmt *S, bool alwaysAdd) {
393 if (alwaysAdd) {
394 autoCreateBlock();
395 Block->appendStmt(S);
Mike Stump6d9828c2009-07-17 01:31:16 +0000396 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000397
398 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000399}
Mike Stump6d9828c2009-07-17 01:31:16 +0000400
Ted Kremenek4f880632009-07-17 22:18:43 +0000401/// VisitChildren - Visit the children of a Stmt.
402CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
403 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000404 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000405 E = Terminator->child_end(); I != E; ++I) {
406 if (*I) B = Visit(*I);
407 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000408 return B;
409}
Ted Kremenek4f880632009-07-17 22:18:43 +0000410
411CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A, bool alwaysAdd) {
412 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000413
Ted Kremenek4f880632009-07-17 22:18:43 +0000414 if (alwaysAdd) {
415 autoCreateBlock();
416 Block->appendStmt(A);
417 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000418
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000419 return Block;
420}
Ted Kremenek4f880632009-07-17 22:18:43 +0000421
422CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B, bool alwaysAdd) {
423 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
425 ConfluenceBlock->appendStmt(B);
426
427 if (!FinishBlock(ConfluenceBlock))
428 return 0;
429
430 // create the block evaluating the LHS
431 CFGBlock* LHSBlock = createBlock(false);
432 LHSBlock->setTerminator(B);
433
434 // create the block evaluating the RHS
435 Succ = ConfluenceBlock;
436 Block = NULL;
437 CFGBlock* RHSBlock = addStmt(B->getRHS());
438 if (!FinishBlock(RHSBlock))
439 return 0;
440
Mike Stump00998a02009-07-23 23:25:26 +0000441 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000442 TryResult KnownVal = TryEvaluateBool(B->getLHS());
443 if (KnownVal.isKnown() && (B->getOpcode() == BinaryOperator::LOr))
444 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000445
Ted Kremenek4f880632009-07-17 22:18:43 +0000446 // Now link the LHSBlock with RHSBlock.
447 if (B->getOpcode() == BinaryOperator::LOr) {
Ted Kremenek941fde82009-07-24 04:47:11 +0000448 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
449 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
450 } else {
Ted Kremenek4f880632009-07-17 22:18:43 +0000451 assert (B->getOpcode() == BinaryOperator::LAnd);
Ted Kremenek941fde82009-07-24 04:47:11 +0000452 LHSBlock->addSuccessor(KnownVal.isFalse() ? NULL : RHSBlock);
453 LHSBlock->addSuccessor(KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000454 }
455
456 // Generate the blocks for evaluating the LHS.
457 Block = LHSBlock;
458 return addStmt(B->getLHS());
459 }
460 else if (B->getOpcode() == BinaryOperator::Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000461 autoCreateBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000462 Block->appendStmt(B);
463 addStmt(B->getRHS());
464 return addStmt(B->getLHS());
465 }
466
467 return VisitStmt(B, alwaysAdd);
468}
469
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000470CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr* E, bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000471 // FIXME
472 return NYS();
473}
474
Ted Kremenek13fc08a2009-07-18 00:47:21 +0000475CFGBlock *CFGBuilder::VisitBlockDeclRefExpr(BlockDeclRefExpr* E,
476 bool alwaysAdd) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000477 // FIXME
478 return NYS();
479}
480
481CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
482 // "break" is a control-flow statement. Thus we stop processing the current
483 // block.
484 if (Block && !FinishBlock(Block))
485 return 0;
486
487 // Now create a new block that ends with the break statement.
488 Block = createBlock(false);
489 Block->setTerminator(B);
490
491 // If there is no target for the break, then we are looking at an incomplete
492 // AST. This means that the CFG cannot be constructed.
493 if (BreakTargetBlock)
494 Block->addSuccessor(BreakTargetBlock);
495 else
496 badCFG = true;
497
498
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000499 return Block;
500}
Ted Kremenek4f880632009-07-17 22:18:43 +0000501
502CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, bool alwaysAdd) {
503 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000504 bool NoReturn = false;
505 if (C->getCallee()->getType().getNoReturnAttr()) {
506 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000507 }
Mike Stump24556362009-07-25 21:26:53 +0000508
509 if (FunctionDecl *FD = C->getDirectCallee())
510 if (FD->hasAttr<NoReturnAttr>())
511 NoReturn = true;
512
513 if (!NoReturn)
514 return VisitStmt(C, alwaysAdd);
515
516 if (Block && !FinishBlock(Block))
517 return 0;
518
519 // Create new block with no successor for the remaining pieces.
520 Block = createBlock(false);
521 Block->appendStmt(C);
522
523 // Wire this to the exit block directly.
524 Block->addSuccessor(&cfg->getExit());
Ted Kremenek4f880632009-07-17 22:18:43 +0000525
Mike Stump24556362009-07-25 21:26:53 +0000526 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000527}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000528
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000529CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C) {
530 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
531 ConfluenceBlock->appendStmt(C);
532 if (!FinishBlock(ConfluenceBlock))
533 return 0;
534
535 Succ = ConfluenceBlock;
536 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000537 CFGBlock* LHSBlock = addStmt(C->getLHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000538 if (!FinishBlock(LHSBlock))
539 return 0;
540
541 Succ = ConfluenceBlock;
542 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000543 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000544 if (!FinishBlock(RHSBlock))
545 return 0;
546
547 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000548 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000549 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
550 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
551 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000552 Block->setTerminator(C);
553 return addStmt(C->getCond());
554}
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000555
Ted Kremenek4f880632009-07-17 22:18:43 +0000556
557CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
558 CFGBlock* LastBlock = Block;
559
560 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
561 I != E; ++I ) {
562 LastBlock = addStmt(*I);
563 }
564 return LastBlock;
565}
566
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000567CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C) {
568 // Create the confluence block that will "merge" the results of the ternary
569 // expression.
570 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
571 ConfluenceBlock->appendStmt(C);
572 if (!FinishBlock(ConfluenceBlock))
573 return 0;
574
575 // Create a block for the LHS expression if there is an LHS expression. A
576 // GCC extension allows LHS to be NULL, causing the condition to be the
577 // value that is returned instead.
578 // e.g: x ?: y is shorthand for: x ? x : y;
579 Succ = ConfluenceBlock;
580 Block = NULL;
581 CFGBlock* LHSBlock = NULL;
582 if (C->getLHS()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000583 LHSBlock = addStmt(C->getLHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000584 if (!FinishBlock(LHSBlock))
585 return 0;
586 Block = NULL;
587 }
588
589 // Create the block for the RHS expression.
590 Succ = ConfluenceBlock;
Ted Kremenek4f880632009-07-17 22:18:43 +0000591 CFGBlock* RHSBlock = addStmt(C->getRHS());
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000592 if (!FinishBlock(RHSBlock))
593 return 0;
594
595 // Create the block that will contain the condition.
596 Block = createBlock(false);
597
Mike Stump00998a02009-07-23 23:25:26 +0000598 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000599 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000600 if (LHSBlock) {
Ted Kremenek941fde82009-07-24 04:47:11 +0000601 Block->addSuccessor(KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000602 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000603 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000604 // If we know the condition is false, add NULL as the successor for
605 // the block containing the condition. In this case, the confluence
606 // block will have just one predecessor.
607 Block->addSuccessor(0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000608 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000609 } else {
610 // If we have no LHS expression, add the ConfluenceBlock as a direct
611 // successor for the block containing the condition. Moreover, we need to
612 // reverse the order of the predecessors in the ConfluenceBlock because
613 // the RHSBlock will have been added to the succcessors already, and we
614 // want the first predecessor to the the block containing the expression
615 // for the case when the ternary expression evaluates to true.
616 Block->addSuccessor(ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000617 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000618 std::reverse(ConfluenceBlock->pred_begin(),
619 ConfluenceBlock->pred_end());
620 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000621 }
622
Ted Kremenek941fde82009-07-24 04:47:11 +0000623 Block->addSuccessor(KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000624 Block->setTerminator(C);
625 return addStmt(C->getCond());
626}
627
Ted Kremenek4f880632009-07-17 22:18:43 +0000628CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
629 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000630
Ted Kremenek4f880632009-07-17 22:18:43 +0000631 if (DS->isSingleDecl()) {
632 Block->appendStmt(DS);
633 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000634 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000635
636 CFGBlock *B = 0;
637
638 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
639 typedef llvm::SmallVector<Decl*,10> BufTy;
640 BufTy Buf(DS->decl_begin(), DS->decl_end());
641
642 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
643 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
644 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
645 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
646
647 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
648 // automatically freed with the CFG.
649 DeclGroupRef DG(*I);
650 Decl *D = *I;
651 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
652 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
653
654 // Append the fake DeclStmt to block.
655 Block->appendStmt(DSNew);
656 B = VisitDeclSubExpr(D);
657 }
658
659 return B;
660}
661
662/// VisitDeclSubExpr - Utility method to add block-level expressions for
663/// initializers in Decls.
664CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
665 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000666
Ted Kremenek4f880632009-07-17 22:18:43 +0000667 VarDecl *VD = dyn_cast<VarDecl>(D);
668
669 if (!VD)
670 return Block;
671
672 Expr *Init = VD->getInit();
673
674 if (Init) {
675 // Optimization: Don't create separate block-level statements for literals.
676 switch (Init->getStmtClass()) {
677 case Stmt::IntegerLiteralClass:
678 case Stmt::CharacterLiteralClass:
679 case Stmt::StringLiteralClass:
680 break;
681 default:
682 Block = addStmt(Init);
683 }
684 }
685
686 // If the type of VD is a VLA, then we must process its size expressions.
687 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
688 VA = FindVA(VA->getElementType().getTypePtr()))
689 Block = addStmt(VA->getSizeExpr());
690
691 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000692}
693
694CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000695 // We may see an if statement in the middle of a basic block, or it may be the
696 // first statement we are processing. In either case, we create a new basic
697 // block. First, we create the blocks for the then...else statements, and
698 // then we create the block containing the if statement. If we were in the
699 // middle of a block, we stop processing that block and reverse its
700 // statements. That block is then the implicit successor for the "then" and
701 // "else" clauses.
702
703 // The block we were proccessing is now finished. Make it the successor
704 // block.
705 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000706 Succ = Block;
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000707 if (!FinishBlock(Block))
708 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000709 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000710
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000711 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000712 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000713
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000714 if (Stmt* Else = I->getElse()) {
715 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000716
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000717 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000718 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000719 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000720 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000721
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000722 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
723 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000724 else if (Block) {
725 if (!FinishBlock(ElseBlock))
726 return 0;
727 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000728 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000729
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000730 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000731 CFGBlock* ThenBlock;
732 {
733 Stmt* Then = I->getThen();
734 assert (Then);
735 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000736 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000737 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000738
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000739 if (!ThenBlock) {
740 // We can reach here if the "then" body has all NullStmts.
741 // Create an empty block so we can distinguish between true and false
742 // branches in path-sensitive analyses.
743 ThenBlock = createBlock(false);
744 ThenBlock->addSuccessor(sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000745 } else if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000746 if (!FinishBlock(ThenBlock))
747 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000748 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000749 }
750
Mike Stump6d9828c2009-07-17 01:31:16 +0000751 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000752 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000753
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000754 // Set the terminator of the new block to the If statement.
755 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000756
Mike Stump00998a02009-07-23 23:25:26 +0000757 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000758 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000759
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000760 // Now add the successors.
Ted Kremenek941fde82009-07-24 04:47:11 +0000761 Block->addSuccessor(KnownVal.isFalse() ? NULL : ThenBlock);
762 Block->addSuccessor(KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000763
764 // Add the condition as the last statement in the new block. This may create
765 // new blocks as the condition may contain control-flow. Any newly created
766 // blocks will be pointed to be "Block".
Ted Kremenek4f880632009-07-17 22:18:43 +0000767 return addStmt(I->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000768}
Mike Stump6d9828c2009-07-17 01:31:16 +0000769
770
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000771CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000772 // If we were in the middle of a block we stop processing that block and
773 // reverse its statements.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000774 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000775 // NOTE: If a "return" appears in the middle of a block, this means that the
776 // code afterwards is DEAD (unreachable). We still keep a basic block
777 // for that code; a simple "mark-and-sweep" from the entry block will be
778 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000779 if (Block) FinishBlock(Block);
780
781 // Create the new block.
782 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000783
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000784 // The Exit block is the only successor.
785 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000786
787 // Add the return statement to the block. This may create new blocks if R
788 // contains control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 return VisitStmt(R, true);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000790}
791
792CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
793 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000794 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000795 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000796
Ted Kremenek4f880632009-07-17 22:18:43 +0000797 if (!LabelBlock) // This can happen when the body is empty, i.e.
798 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000799
Ted Kremenek4f880632009-07-17 22:18:43 +0000800 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000801 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000802
803 // Labels partition blocks, so this is the end of the basic block we were
804 // processing (L is the block's label). Because this is label (and we have
805 // already processed the substatement) there is no extra control-flow to worry
806 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000807 LabelBlock->setLabel(L);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000808 if (!FinishBlock(LabelBlock))
809 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000810
811 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000812 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000813
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000814 // This block is now the implicit successor of other blocks.
815 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000816
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000817 return LabelBlock;
818}
819
820CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000821 // Goto is a control-flow statement. Thus we stop processing the current
822 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000823 if (Block)
824 FinishBlock(Block);
825
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 Block = createBlock(false);
827 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000828
829 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000830 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000831
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000832 if (I == LabelMap.end())
833 // We will need to backpatch this block later.
834 BackpatchBlocks.push_back(Block);
835 else
836 Block->addSuccessor(I->second);
837
Mike Stump6d9828c2009-07-17 01:31:16 +0000838 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000839}
840
841CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000843
Mike Stumpfefb9f72009-07-21 01:12:51 +0000844 // "for" is a control-flow statement. Thus we stop processing the current
845 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000846 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000847 if (!FinishBlock(Block))
848 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000849 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000850 } else
851 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000852
853 // Because of short-circuit evaluation, the condition of the loop can span
854 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
855 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000856 CFGBlock* ExitConditionBlock = createBlock(false);
857 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000858
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000859 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000860 ExitConditionBlock->setTerminator(F);
861
862 // Now add the actual condition to the condition block. Because the condition
863 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000864 if (Stmt* C = F->getCond()) {
865 Block = ExitConditionBlock;
866 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000867 if (Block) {
868 if (!FinishBlock(EntryConditionBlock))
869 return 0;
870 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000871 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000872
Mike Stump6d9828c2009-07-17 01:31:16 +0000873 // The condition block is the implicit successor for the loop body as well as
874 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000875 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000876
Mike Stump00998a02009-07-23 23:25:26 +0000877 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000878 TryResult KnownVal(true);
879
Mike Stump00998a02009-07-23 23:25:26 +0000880 if (F->getCond())
881 KnownVal = TryEvaluateBool(F->getCond());
882
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000883 // Now create the loop body.
884 {
885 assert (F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +0000886
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000887 // Save the current values for Block, Succ, and continue and break targets
888 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
889 save_continue(ContinueTargetBlock),
Mike Stump6d9828c2009-07-17 01:31:16 +0000890 save_break(BreakTargetBlock);
891
Ted Kremenekaf603f72007-08-30 18:39:40 +0000892 // Create a new block to contain the (bottom) of the loop body.
893 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000894
Ted Kremeneke9334502008-09-04 21:48:47 +0000895 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000896 // Generate increment code in its own basic block. This is the target of
897 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +0000898 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000899 } else {
900 // No increment code. Create a special, empty, block that is used as the
901 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +0000902 assert(Succ == EntryConditionBlock);
903 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +0000904 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000905
Ted Kremenek3575f842009-04-28 00:51:56 +0000906 // Finish up the increment (or empty) block if it hasn't been already.
907 if (Block) {
908 assert(Block == Succ);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000909 if (!FinishBlock(Block))
910 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +0000911 Block = 0;
912 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000913
Ted Kremenek3575f842009-04-28 00:51:56 +0000914 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000915
Ted Kremenek3575f842009-04-28 00:51:56 +0000916 // The starting block for the loop increment is the block that should
917 // represent the 'loop target' for looping back to the start of the loop.
918 ContinueTargetBlock->setLoopTarget(F);
919
Ted Kremeneke9334502008-09-04 21:48:47 +0000920 // All breaks should go to the code following the loop.
Mike Stump6d9828c2009-07-17 01:31:16 +0000921 BreakTargetBlock = LoopSuccessor;
922
923 // Now populate the body block, and in the process create new blocks as we
924 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +0000925 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +0000926
927 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +0000928 BodyBlock = EntryConditionBlock; // can happen for "for (...;...; ) ;"
Ted Kremenek941fde82009-07-24 04:47:11 +0000929 else if (Block && !FinishBlock(BodyBlock))
930 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000931
Ted Kremenek941fde82009-07-24 04:47:11 +0000932 // This new body block is a successor to our "exit" condition block.
933 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000934 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000935
Ted Kremenek941fde82009-07-24 04:47:11 +0000936 // Link up the condition block with the code that follows the loop. (the
937 // false branch).
938 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +0000939
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000940 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +0000941 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000942 if (Stmt* I = F->getInit()) {
943 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000944 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000945 } else {
946 // There is no loop initialization. We are thus basically a while loop.
947 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000948 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +0000949 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000950 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000951 }
952}
953
Ted Kremenek514de5a2008-11-11 17:10:00 +0000954CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
955 // Objective-C fast enumeration 'for' statements:
956 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
957 //
958 // for ( Type newVariable in collection_expression ) { statements }
959 //
960 // becomes:
961 //
962 // prologue:
963 // 1. collection_expression
964 // T. jump to loop_entry
965 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000966 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +0000967 // 1. ObjCForCollectionStmt [performs binding to newVariable]
968 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
969 // TB:
970 // statements
971 // T. jump to loop_entry
972 // FB:
973 // what comes after
974 //
975 // and
976 //
977 // Type existingItem;
978 // for ( existingItem in expression ) { statements }
979 //
980 // becomes:
981 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000982 // the same with newVariable replaced with existingItem; the binding works
983 // the same except that for one ObjCForCollectionStmt::getElement() returns
984 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +0000985 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000986
Ted Kremenek514de5a2008-11-11 17:10:00 +0000987 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000988
Ted Kremenek514de5a2008-11-11 17:10:00 +0000989 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000990 if (!FinishBlock(Block))
991 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +0000992 LoopSuccessor = Block;
993 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +0000994 } else
995 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000996
Ted Kremenek4cb3a852008-11-14 01:57:41 +0000997 // Build the condition blocks.
998 CFGBlock* ExitConditionBlock = createBlock(false);
999 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001000
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001001 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001002 ExitConditionBlock->setTerminator(S);
1003
1004 // The last statement in the block should be the ObjCForCollectionStmt, which
1005 // performs the actual binding to 'element' and determines if there are any
1006 // more items in the collection.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001007 ExitConditionBlock->appendStmt(S);
1008 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001009
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001010 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001011 // generate new blocks as necesary. We DON'T add the statement by default to
1012 // the CFG unless it contains control-flow.
Ted Kremenek4f880632009-07-17 22:18:43 +00001013 EntryConditionBlock = Visit(S->getElement(), false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001014 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001015 if (!FinishBlock(EntryConditionBlock))
1016 return 0;
1017 Block = 0;
1018 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001019
1020 // The condition block is the implicit successor for the loop body as well as
1021 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001022 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001023
Ted Kremenek514de5a2008-11-11 17:10:00 +00001024 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001025 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001026 // Save the current values for Succ, continue and break targets.
1027 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001028 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1029
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001030 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001031 ContinueTargetBlock = EntryConditionBlock;
1032
Ted Kremenek4f880632009-07-17 22:18:43 +00001033 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001034
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001035 if (!BodyBlock)
1036 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001037 else if (Block) {
1038 if (!FinishBlock(BodyBlock))
1039 return 0;
1040 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001041
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001042 // This new body block is a successor to our "exit" condition block.
1043 ExitConditionBlock->addSuccessor(BodyBlock);
1044 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001045
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001046 // Link up the condition block with the code that follows the loop.
1047 // (the false branch).
1048 ExitConditionBlock->addSuccessor(LoopSuccessor);
1049
Ted Kremenek514de5a2008-11-11 17:10:00 +00001050 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001051 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001052 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001053}
1054
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001055CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1056 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001057
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001058 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001059 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001060
Ted Kremenekda5348e2009-05-05 23:11:51 +00001061 // The sync body starts its own basic block. This makes it a little easier
1062 // for diagnostic clients.
1063 if (SyncBlock) {
1064 if (!FinishBlock(SyncBlock))
1065 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001066
Ted Kremenekda5348e2009-05-05 23:11:51 +00001067 Block = 0;
1068 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001069
Ted Kremenekda5348e2009-05-05 23:11:51 +00001070 Succ = SyncBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001071
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001072 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001073 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001074}
Mike Stump6d9828c2009-07-17 01:31:16 +00001075
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001076CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001077 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001078 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001079}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001080
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001081CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001082 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001083
Mike Stumpfefb9f72009-07-21 01:12:51 +00001084 // "while" is a control-flow statement. Thus we stop processing the current
1085 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001086 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001087 if (!FinishBlock(Block))
1088 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001089 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001090 } else
1091 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001092
1093 // Because of short-circuit evaluation, the condition of the loop can span
1094 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1095 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001096 CFGBlock* ExitConditionBlock = createBlock(false);
1097 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001098
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001099 // Set the terminator for the "exit" condition block.
1100 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001101
1102 // Now add the actual condition to the condition block. Because the condition
1103 // itself may contain control-flow, new blocks may be created. Thus we update
1104 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001105 if (Stmt* C = W->getCond()) {
1106 Block = ExitConditionBlock;
1107 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001108 assert(Block == EntryConditionBlock);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001109 if (Block) {
1110 if (!FinishBlock(EntryConditionBlock))
1111 return 0;
1112 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001113 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001114
1115 // The condition block is the implicit successor for the loop body as well as
1116 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001117 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001118
Mike Stump00998a02009-07-23 23:25:26 +00001119 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001120 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001121
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001122 // Process the loop body.
1123 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001124 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001125
1126 // Save the current values for Block, Succ, and continue and break targets
1127 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1128 save_continue(ContinueTargetBlock),
1129 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001130
Mike Stump6d9828c2009-07-17 01:31:16 +00001131 // Create an empty block to represent the transition block for looping back
1132 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001133 Block = 0;
1134 assert(Succ == EntryConditionBlock);
1135 Succ = createBlock();
1136 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001137 ContinueTargetBlock = Succ;
1138
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001139 // All breaks should go to the code following the loop.
1140 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001141
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001142 // NULL out Block to force lazy instantiation of blocks for the body.
1143 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001144
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001145 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001146 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001147
Ted Kremenekaf603f72007-08-30 18:39:40 +00001148 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001149 BodyBlock = EntryConditionBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001150 else if (Block) {
1151 if (!FinishBlock(BodyBlock))
1152 return 0;
1153 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001154
Ted Kremenek941fde82009-07-24 04:47:11 +00001155 // Add the loop body entry as a successor to the condition.
1156 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001157 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001158
Ted Kremenek941fde82009-07-24 04:47:11 +00001159 // Link up the condition block with the code that follows the loop. (the
1160 // false branch).
1161 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001162
1163 // There can be no more statements in the condition block since we loop back
1164 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001165 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001166
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001167 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001168 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001169 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001170}
Ted Kremenek4f880632009-07-17 22:18:43 +00001171
1172
1173CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1174 // FIXME: For now we pretend that @catch and the code it contains does not
1175 // exit.
1176 return Block;
1177}
Mike Stump6d9828c2009-07-17 01:31:16 +00001178
Ted Kremenek2fda5042008-12-09 20:20:09 +00001179CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1180 // FIXME: This isn't complete. We basically treat @throw like a return
1181 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001182
1183 // If we were in the middle of a block we stop processing that block and
1184 // reverse its statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001185 if (Block && !FinishBlock(Block))
1186 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001187
Ted Kremenek2fda5042008-12-09 20:20:09 +00001188 // Create the new block.
1189 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001190
Ted Kremenek2fda5042008-12-09 20:20:09 +00001191 // The Exit block is the only successor.
1192 Block->addSuccessor(&cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001193
1194 // Add the statement to the block. This may create new blocks if S contains
1195 // control-flow (short-circuit operations).
Ted Kremenek4f880632009-07-17 22:18:43 +00001196 return VisitStmt(S, true);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001197}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001198
Mike Stump0979d802009-07-22 22:56:04 +00001199CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
1200 // If we were in the middle of a block we stop processing that block and
1201 // reverse its statements.
1202 if (Block && !FinishBlock(Block))
1203 return 0;
1204
1205 // Create the new block.
1206 Block = createBlock(false);
1207
1208 // The Exit block is the only successor.
1209 Block->addSuccessor(&cfg->getExit());
1210
1211 // Add the statement to the block. This may create new blocks if S contains
1212 // control-flow (short-circuit operations).
1213 return VisitStmt(T, true);
1214}
1215
Ted Kremenek4f880632009-07-17 22:18:43 +00001216CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001217 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001218
Mike Stump8f9893a2009-07-21 01:27:50 +00001219 // "do...while" is a control-flow statement. Thus we stop processing the
1220 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001221 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001222 if (!FinishBlock(Block))
1223 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001224 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001225 } else
1226 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001227
1228 // Because of short-circuit evaluation, the condition of the loop can span
1229 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1230 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001231 CFGBlock* ExitConditionBlock = createBlock(false);
1232 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001233
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001234 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001235 ExitConditionBlock->setTerminator(D);
1236
1237 // Now add the actual condition to the condition block. Because the condition
1238 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001239 if (Stmt* C = D->getCond()) {
1240 Block = ExitConditionBlock;
1241 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001242 if (Block) {
1243 if (!FinishBlock(EntryConditionBlock))
1244 return 0;
1245 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001246 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001247
Ted Kremenek54827132008-02-27 07:20:00 +00001248 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001249 Succ = EntryConditionBlock;
1250
Mike Stump00998a02009-07-23 23:25:26 +00001251 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001252 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001253
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001254 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001255 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001256 {
1257 assert (D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001258
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001259 // Save the current values for Block, Succ, and continue and break targets
1260 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1261 save_continue(ContinueTargetBlock),
1262 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001263
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001264 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001265 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001266
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001267 // All breaks should go to the code following the loop.
1268 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001269
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001270 // NULL out Block to force lazy instantiation of blocks for the body.
1271 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001272
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001273 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001274 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001275
Ted Kremenekaf603f72007-08-30 18:39:40 +00001276 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001277 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001278 else if (Block) {
1279 if (!FinishBlock(BodyBlock))
1280 return 0;
1281 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001282
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001283 // Add an intermediate block between the BodyBlock and the
Mike Stump6d9828c2009-07-17 01:31:16 +00001284 // ExitConditionBlock to represent the "loop back" transition. Create an
1285 // empty block to represent the transition block for looping back to the
1286 // head of the loop.
Ted Kremenek8f08c9d2009-04-28 04:22:00 +00001287 // FIXME: Can we do this more efficiently without adding another block?
1288 Block = NULL;
1289 Succ = BodyBlock;
1290 CFGBlock *LoopBackBlock = createBlock();
1291 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001292
Ted Kremenek941fde82009-07-24 04:47:11 +00001293 // Add the loop body entry as a successor to the condition.
1294 ExitConditionBlock->addSuccessor(KnownVal.isFalse() ? NULL : LoopBackBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001295 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001296
Ted Kremenek941fde82009-07-24 04:47:11 +00001297 // Link up the condition block with the code that follows the loop.
1298 // (the false branch).
1299 ExitConditionBlock->addSuccessor(KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001300
1301 // There can be no more statements in the body block(s) since we loop back to
1302 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001303 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001304
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001305 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001306 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001307 return BodyBlock;
1308}
1309
1310CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1311 // "continue" is a control-flow statement. Thus we stop processing the
1312 // current block.
Ted Kremenek4f880632009-07-17 22:18:43 +00001313 if (Block && !FinishBlock(Block))
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001314 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001315
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001316 // Now create a new block that ends with the continue statement.
1317 Block = createBlock(false);
1318 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001319
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001320 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001321 // incomplete AST. This means the CFG cannot be constructed.
1322 if (ContinueTargetBlock)
1323 Block->addSuccessor(ContinueTargetBlock);
1324 else
1325 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001326
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001327 return Block;
1328}
Ted Kremenek4f880632009-07-17 22:18:43 +00001329
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001330CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
1331 bool alwaysAdd) {
1332
1333 if (alwaysAdd) {
1334 autoCreateBlock();
1335 Block->appendStmt(E);
1336 }
1337
Ted Kremenek4f880632009-07-17 22:18:43 +00001338 // VLA types have expressions that must be evaluated.
1339 if (E->isArgumentType()) {
1340 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1341 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1342 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001343 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001344
Mike Stump6d9828c2009-07-17 01:31:16 +00001345 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001346}
Ted Kremenek4f880632009-07-17 22:18:43 +00001347
1348/// VisitStmtExpr - Utility method to handle (nested) statement
1349/// expressions (a GCC extension).
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001350CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, bool alwaysAdd) {
1351 if (alwaysAdd) {
1352 autoCreateBlock();
1353 Block->appendStmt(SE);
1354 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001355 return VisitCompoundStmt(SE->getSubStmt());
1356}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001357
Ted Kremenek411cdee2008-04-16 21:10:48 +00001358CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001359 // "switch" is a control-flow statement. Thus we stop processing the current
1360 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001361 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001362
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001363 if (Block) {
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001364 if (!FinishBlock(Block))
1365 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001366 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001367 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001368
1369 // Save the current "switch" context.
1370 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001371 save_break(BreakTargetBlock),
1372 save_default(DefaultCaseBlock);
1373
Mike Stump6d9828c2009-07-17 01:31:16 +00001374 // Set the "default" case to be the block after the switch statement. If the
1375 // switch statement contains a "default:", this value will be overwritten with
1376 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001377 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001378
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001379 // Create a new block that will contain the switch statement.
1380 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001381
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001382 // Now process the switch body. The code after the switch is the implicit
1383 // successor.
1384 Succ = SwitchSuccessor;
1385 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001386
1387 // When visiting the body, the case statements should automatically get linked
1388 // up to the switch. We also don't keep a pointer to the body, since all
1389 // control-flow from the switch goes to case/default statements.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001390 assert (Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001391 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +00001392 CFGBlock *BodyBlock = addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001393 if (Block) {
1394 if (!FinishBlock(BodyBlock))
1395 return 0;
1396 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001397
Mike Stump6d9828c2009-07-17 01:31:16 +00001398 // If we have no "default:" case, the default transition is to the code
1399 // following the switch body.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001400 SwitchTerminatedBlock->addSuccessor(DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001401
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001402 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001403 SwitchTerminatedBlock->setTerminator(Terminator);
1404 assert (Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001405 Block = SwitchTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001406
Ted Kremenek411cdee2008-04-16 21:10:48 +00001407 return addStmt(Terminator->getCond());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001408}
1409
Ted Kremenek4f880632009-07-17 22:18:43 +00001410CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001411 // CaseStmts are essentially labels, so they are the first statement in a
1412 // block.
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001413
Ted Kremenek4f880632009-07-17 22:18:43 +00001414 if (CS->getSubStmt())
1415 addStmt(CS->getSubStmt());
1416
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001417 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001418 if (!CaseBlock)
1419 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001420
1421 // Cases statements partition blocks, so this is the top of the basic block we
1422 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001423 CaseBlock->setLabel(CS);
1424
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001425 if (!FinishBlock(CaseBlock))
1426 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001427
1428 // Add this block to the list of successors for the block with the switch
1429 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001430 assert(SwitchTerminatedBlock);
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001431 SwitchTerminatedBlock->addSuccessor(CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1434 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001435
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001436 // This block is now the implicit successor of other blocks.
1437 Succ = CaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001438
Ted Kremenek2677ea82008-03-15 07:45:02 +00001439 return CaseBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001440}
Mike Stump6d9828c2009-07-17 01:31:16 +00001441
Ted Kremenek411cdee2008-04-16 21:10:48 +00001442CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001443 if (Terminator->getSubStmt())
1444 addStmt(Terminator->getSubStmt());
1445
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001446 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001447
1448 if (!DefaultCaseBlock)
1449 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001450
1451 // Default statements partition blocks, so this is the top of the basic block
1452 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001453 DefaultCaseBlock->setLabel(Terminator);
Ted Kremenek4f880632009-07-17 22:18:43 +00001454
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001455 if (!FinishBlock(DefaultCaseBlock))
1456 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001457
Mike Stump6d9828c2009-07-17 01:31:16 +00001458 // Unlike case statements, we don't add the default block to the successors
1459 // for the switch statement immediately. This is done when we finish
1460 // processing the switch statement. This allows for the default case
1461 // (including a fall-through to the code after the switch statement) to always
1462 // be the last successor of a switch-terminated block.
1463
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001464 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1465 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001466
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001467 // This block is now the implicit successor of other blocks.
1468 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001469
1470 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001471}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472
Ted Kremenek19bb3562007-08-28 19:26:49 +00001473CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001474 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001475 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001476
Ted Kremenek19bb3562007-08-28 19:26:49 +00001477 if (!IBlock) {
1478 IBlock = createBlock(false);
1479 cfg->setIndirectGotoBlock(IBlock);
1480 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001481
Ted Kremenek19bb3562007-08-28 19:26:49 +00001482 // IndirectGoto is a control-flow statement. Thus we stop processing the
1483 // current block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +00001484 if (Block && !FinishBlock(Block))
1485 return 0;
1486
Ted Kremenek19bb3562007-08-28 19:26:49 +00001487 Block = createBlock(false);
1488 Block->setTerminator(I);
1489 Block->addSuccessor(IBlock);
1490 return addStmt(I->getTarget());
1491}
1492
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001493} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001494
Mike Stump6d9828c2009-07-17 01:31:16 +00001495/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1496/// no successors or predecessors. If this is the first block created in the
1497/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001498CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001499 bool first_block = begin() == end();
1500
1501 // Create the block.
Ted Kremenek94382522007-09-05 20:02:05 +00001502 Blocks.push_front(CFGBlock(NumBlockIDs++));
Ted Kremenek026473c2007-08-23 16:51:22 +00001503
1504 // If this is the first block, set it as the Entry and Exit.
1505 if (first_block) Entry = Exit = &front();
1506
1507 // Return the block.
1508 return &front();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001509}
1510
Ted Kremenek026473c2007-08-23 16:51:22 +00001511/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1512/// CFG is returned to the caller.
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001513CFG* CFG::buildCFG(Stmt* Statement, ASTContext *C) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001514 CFGBuilder Builder;
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001515 return Builder.buildCFG(Statement, C);
Ted Kremenek026473c2007-08-23 16:51:22 +00001516}
1517
1518/// reverseStmts - Reverses the orders of statements within a CFGBlock.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001519void CFGBlock::reverseStmts() { std::reverse(Stmts.begin(),Stmts.end()); }
1520
Ted Kremenek63f58872007-10-01 19:33:33 +00001521//===----------------------------------------------------------------------===//
1522// CFG: Queries for BlkExprs.
1523//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001524
Ted Kremenek63f58872007-10-01 19:33:33 +00001525namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001526 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001527}
1528
Ted Kremenek411cdee2008-04-16 21:10:48 +00001529static void FindSubExprAssignments(Stmt* Terminator, llvm::SmallPtrSet<Expr*,50>& Set) {
1530 if (!Terminator)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001531 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001532
Ted Kremenek411cdee2008-04-16 21:10:48 +00001533 for (Stmt::child_iterator I=Terminator->child_begin(), E=Terminator->child_end(); I!=E; ++I) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001534 if (!*I) continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001535
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001536 if (BinaryOperator* B = dyn_cast<BinaryOperator>(*I))
1537 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001538
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001539 FindSubExprAssignments(*I, Set);
1540 }
1541}
1542
Ted Kremenek63f58872007-10-01 19:33:33 +00001543static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1544 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001545
1546 // Look for assignments that are used as subexpressions. These are the only
1547 // assignments that we want to *possibly* register as a block-level
1548 // expression. Basically, if an assignment occurs both in a subexpression and
1549 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001550 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001551
Ted Kremenek63f58872007-10-01 19:33:33 +00001552 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
1553 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001554 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001555
Ted Kremenek411cdee2008-04-16 21:10:48 +00001556 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001557
1558 // Iterate over the statements again on identify the Expr* and Stmt* at the
1559 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001560
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001561 for (CFGBlock::iterator BI=I->begin(), EI=I->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001562 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001563
Ted Kremenek411cdee2008-04-16 21:10:48 +00001564 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001565 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001566 // expression are really "statements" whose value is never used by
1567 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001568 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001569 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001570 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1571 // Special handling for statement expressions. The last statement in
1572 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001573 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001574 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001575 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001576 (*M)[C->body_back()] = x;
1577 }
1578 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001579
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001580 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001581 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001582 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001583
Ted Kremenek411cdee2008-04-16 21:10:48 +00001584 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001585
Ted Kremenek390e48b2008-11-12 21:11:49 +00001586 Stmt* S = I->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001587
Ted Kremenek390e48b2008-11-12 21:11:49 +00001588 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001589 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001590 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001591 }
1592 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001593
Ted Kremenek63f58872007-10-01 19:33:33 +00001594 return M;
1595}
1596
Ted Kremenek86946742008-01-17 20:48:37 +00001597CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1598 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001599 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001600
Ted Kremenek63f58872007-10-01 19:33:33 +00001601 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001602 BlkExprMapTy::iterator I = M->find(S);
Mike Stump6d9828c2009-07-17 01:31:16 +00001603
Ted Kremenek63f58872007-10-01 19:33:33 +00001604 if (I == M->end()) return CFG::BlkExprNumTy();
1605 else return CFG::BlkExprNumTy(I->second);
1606}
1607
1608unsigned CFG::getNumBlkExprs() {
1609 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1610 return M->size();
1611 else {
1612 // We assume callers interested in the number of BlkExprs will want
1613 // the map constructed if it doesn't already exist.
1614 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1615 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1616 }
1617}
1618
Ted Kremenek274f4332008-04-28 18:00:46 +00001619//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001620// Cleanup: CFG dstor.
1621//===----------------------------------------------------------------------===//
1622
Ted Kremenek63f58872007-10-01 19:33:33 +00001623CFG::~CFG() {
1624 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1625}
Mike Stump6d9828c2009-07-17 01:31:16 +00001626
Ted Kremenek7dba8602007-08-29 21:56:09 +00001627//===----------------------------------------------------------------------===//
1628// CFG pretty printing
1629//===----------------------------------------------------------------------===//
1630
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001631namespace {
1632
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001633class VISIBILITY_HIDDEN StmtPrinterHelper : public PrinterHelper {
Mike Stump6d9828c2009-07-17 01:31:16 +00001634
Ted Kremenek42a509f2007-08-31 21:30:12 +00001635 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1636 StmtMapTy StmtMap;
1637 signed CurrentBlock;
1638 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001639 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001640public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001641
Chris Lattnere4f21422009-06-30 01:26:17 +00001642 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1643 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001644 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1645 unsigned j = 1;
1646 for (CFGBlock::const_iterator BI = I->begin(), BEnd = I->end() ;
1647 BI != BEnd; ++BI, ++j )
1648 StmtMap[*BI] = std::make_pair(I->getBlockID(),j);
1649 }
1650 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001651
Ted Kremenek42a509f2007-08-31 21:30:12 +00001652 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001653
Chris Lattnere4f21422009-06-30 01:26:17 +00001654 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001655 void setBlockID(signed i) { CurrentBlock = i; }
1656 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001657
Ted Kremeneka95d3752008-09-13 05:16:45 +00001658 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001659
Ted Kremenek411cdee2008-04-16 21:10:48 +00001660 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001661
1662 if (I == StmtMap.end())
1663 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001664
1665 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek42a509f2007-08-31 21:30:12 +00001666 && I->second.second == CurrentStmt)
1667 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001668
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001669 OS << "[B" << I->second.first << "." << I->second.second << "]";
1670 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001671 }
1672};
Chris Lattnere4f21422009-06-30 01:26:17 +00001673} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001674
Chris Lattnere4f21422009-06-30 01:26:17 +00001675
1676namespace {
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001677class VISIBILITY_HIDDEN CFGBlockTerminatorPrint
1678 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001679
Ted Kremeneka95d3752008-09-13 05:16:45 +00001680 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001681 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001682 PrintingPolicy Policy;
1683
Ted Kremenek42a509f2007-08-31 21:30:12 +00001684public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001685 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00001686 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001687 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001688
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001689 void VisitIfStmt(IfStmt* I) {
1690 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001691 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001692 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001693
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001694 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00001695 void VisitStmt(Stmt* Terminator) {
1696 Terminator->printPretty(OS, Helper, Policy);
1697 }
1698
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001699 void VisitForStmt(ForStmt* F) {
1700 OS << "for (" ;
Ted Kremenek535bb202007-08-30 21:28:02 +00001701 if (F->getInit()) OS << "...";
1702 OS << "; ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001703 if (Stmt* C = F->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00001704 OS << "; ";
1705 if (F->getInc()) OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00001706 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001707 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001708
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001709 void VisitWhileStmt(WhileStmt* W) {
1710 OS << "while " ;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001711 if (Stmt* C = W->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001712 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001713
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001714 void VisitDoStmt(DoStmt* D) {
1715 OS << "do ... while ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001716 if (Stmt* C = D->getCond()) C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001717 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001718
Ted Kremenek411cdee2008-04-16 21:10:48 +00001719 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001720 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001721 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00001722 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001723
Ted Kremenek805e9a82007-08-31 21:49:40 +00001724 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001725 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001726 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001727 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001728
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001729 void VisitChooseExpr(ChooseExpr* C) {
1730 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001731 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00001732 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00001733 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001734
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001735 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
1736 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001737 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001738 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001739
Ted Kremenek805e9a82007-08-31 21:49:40 +00001740 void VisitBinaryOperator(BinaryOperator* B) {
1741 if (!B->isLogicalOp()) {
1742 VisitExpr(B);
1743 return;
1744 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001745
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001746 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001747
Ted Kremenek805e9a82007-08-31 21:49:40 +00001748 switch (B->getOpcode()) {
1749 case BinaryOperator::LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00001750 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001751 return;
1752 case BinaryOperator::LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00001753 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00001754 return;
1755 default:
1756 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001757 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00001758 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001759
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00001760 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001761 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00001762 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001763};
Chris Lattnere4f21422009-06-30 01:26:17 +00001764} // end anonymous namespace
1765
Mike Stump6d9828c2009-07-17 01:31:16 +00001766
Chris Lattnere4f21422009-06-30 01:26:17 +00001767static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
1768 Stmt* Terminator) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001769 if (Helper) {
1770 // special printing for statement-expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001771 if (StmtExpr* SE = dyn_cast<StmtExpr>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001772 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00001773
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001774 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00001775 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00001776 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00001777 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001778 return;
1779 }
1780 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001781
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001782 // special printing for comma expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001783 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Terminator)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001784 if (B->getOpcode() == BinaryOperator::Comma) {
1785 OS << "... , ";
1786 Helper->handledStmt(B->getRHS(),OS);
1787 OS << '\n';
1788 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001789 }
1790 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001791 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001792
Chris Lattnere4f21422009-06-30 01:26:17 +00001793 Terminator->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
Mike Stump6d9828c2009-07-17 01:31:16 +00001794
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001795 // Expressions need a newline.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001796 if (isa<Expr>(Terminator)) OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001797}
Mike Stump6d9828c2009-07-17 01:31:16 +00001798
Chris Lattnere4f21422009-06-30 01:26:17 +00001799static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
1800 const CFGBlock& B,
1801 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001802
Ted Kremenek42a509f2007-08-31 21:30:12 +00001803 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00001804
Ted Kremenek7dba8602007-08-29 21:56:09 +00001805 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00001806 OS << "\n [ B" << B.getBlockID();
1807
Ted Kremenek42a509f2007-08-31 21:30:12 +00001808 if (&B == &cfg->getEntry())
1809 OS << " (ENTRY) ]\n";
1810 else if (&B == &cfg->getExit())
1811 OS << " (EXIT) ]\n";
1812 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00001813 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001814 else
1815 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00001816
Ted Kremenek9cffe732007-08-29 23:20:49 +00001817 // Print the label of this block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001818 if (Stmt* Terminator = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001819
1820 if (print_edges)
1821 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001822
Ted Kremenek411cdee2008-04-16 21:10:48 +00001823 if (LabelStmt* L = dyn_cast<LabelStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001824 OS << L->getName();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001825 else if (CaseStmt* C = dyn_cast<CaseStmt>(Terminator)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00001826 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001827 C->getLHS()->printPretty(OS, Helper,
1828 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001829 if (C->getRHS()) {
1830 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00001831 C->getRHS()->printPretty(OS, Helper,
1832 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00001833 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001834 } else if (isa<DefaultStmt>(Terminator))
Ted Kremenek9cffe732007-08-29 23:20:49 +00001835 OS << "default";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001836 else
1837 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00001838
Ted Kremenek9cffe732007-08-29 23:20:49 +00001839 OS << ":\n";
1840 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001841
Ted Kremenekfddd5182007-08-21 21:42:03 +00001842 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00001843 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00001844
Ted Kremenek42a509f2007-08-31 21:30:12 +00001845 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
1846 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
Ted Kremenek9cffe732007-08-29 23:20:49 +00001848 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001849 if (print_edges)
1850 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001851
Ted Kremeneka95d3752008-09-13 05:16:45 +00001852 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001853
Ted Kremenek42a509f2007-08-31 21:30:12 +00001854 if (Helper)
1855 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00001856
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001857 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00001858 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001859
Ted Kremenek9cffe732007-08-29 23:20:49 +00001860 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001861 if (B.getTerminator()) {
1862 if (print_edges)
1863 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001864
Ted Kremenek9cffe732007-08-29 23:20:49 +00001865 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001866
Ted Kremenek42a509f2007-08-31 21:30:12 +00001867 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00001868
Chris Lattnere4f21422009-06-30 01:26:17 +00001869 CFGBlockTerminatorPrint TPrinter(OS, Helper,
1870 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00001871 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00001872 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001873 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001874
Ted Kremenek9cffe732007-08-29 23:20:49 +00001875 if (print_edges) {
1876 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00001877 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00001878 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00001879
Ted Kremenek42a509f2007-08-31 21:30:12 +00001880 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
1881 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001882
Ted Kremenek42a509f2007-08-31 21:30:12 +00001883 if (i == 8 || (i-8) == 0)
1884 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00001885
Ted Kremenek9cffe732007-08-29 23:20:49 +00001886 OS << " B" << (*I)->getBlockID();
1887 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001888
Ted Kremenek42a509f2007-08-31 21:30:12 +00001889 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00001890
Ted Kremenek42a509f2007-08-31 21:30:12 +00001891 // Print the successors of this block.
1892 OS << " Successors (" << B.succ_size() << "):";
1893 i = 0;
1894
1895 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
1896 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001897
Ted Kremenek42a509f2007-08-31 21:30:12 +00001898 if (i == 8 || (i-8) % 10 == 0)
1899 OS << "\n ";
1900
Mike Stumpe5af3ce2009-07-20 23:24:15 +00001901 if (*I)
1902 OS << " B" << (*I)->getBlockID();
1903 else
1904 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00001905 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001906
Ted Kremenek9cffe732007-08-29 23:20:49 +00001907 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00001908 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001909}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001910
Ted Kremenek42a509f2007-08-31 21:30:12 +00001911
1912/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001913void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001914
1915/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00001916void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
1917 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00001918
Ted Kremenek42a509f2007-08-31 21:30:12 +00001919 // Print the entry block.
1920 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00001921
Ted Kremenek42a509f2007-08-31 21:30:12 +00001922 // Iterate through the CFGBlocks and print them one by one.
1923 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
1924 // Skip the entry block, because we already printed it.
1925 if (&(*I) == &getEntry() || &(*I) == &getExit())
1926 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001927
Ted Kremenek42a509f2007-08-31 21:30:12 +00001928 print_block(OS, this, *I, &Helper, true);
1929 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001930
Ted Kremenek42a509f2007-08-31 21:30:12 +00001931 // Print the exit block.
1932 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00001933 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00001934}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001935
1936/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00001937void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
1938 print(llvm::errs(), cfg, LO);
1939}
Ted Kremenek42a509f2007-08-31 21:30:12 +00001940
1941/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
1942/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00001943void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
1944 const LangOptions &LO) const {
1945 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001946 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00001947}
Ted Kremenek7dba8602007-08-29 21:56:09 +00001948
Ted Kremeneka2925852008-01-30 23:02:42 +00001949/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00001950void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00001951 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00001952 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00001953 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
1954}
1955
Ted Kremenek390e48b2008-11-12 21:11:49 +00001956Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00001957
Ted Kremenek411cdee2008-04-16 21:10:48 +00001958 if (!Terminator)
1959 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001960
Ted Kremenek411cdee2008-04-16 21:10:48 +00001961 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001962
Ted Kremenek411cdee2008-04-16 21:10:48 +00001963 switch (Terminator->getStmtClass()) {
1964 default:
1965 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001966
Ted Kremenek411cdee2008-04-16 21:10:48 +00001967 case Stmt::ForStmtClass:
1968 E = cast<ForStmt>(Terminator)->getCond();
1969 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001970
Ted Kremenek411cdee2008-04-16 21:10:48 +00001971 case Stmt::WhileStmtClass:
1972 E = cast<WhileStmt>(Terminator)->getCond();
1973 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001974
Ted Kremenek411cdee2008-04-16 21:10:48 +00001975 case Stmt::DoStmtClass:
1976 E = cast<DoStmt>(Terminator)->getCond();
1977 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001978
Ted Kremenek411cdee2008-04-16 21:10:48 +00001979 case Stmt::IfStmtClass:
1980 E = cast<IfStmt>(Terminator)->getCond();
1981 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
Ted Kremenek411cdee2008-04-16 21:10:48 +00001983 case Stmt::ChooseExprClass:
1984 E = cast<ChooseExpr>(Terminator)->getCond();
1985 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001986
Ted Kremenek411cdee2008-04-16 21:10:48 +00001987 case Stmt::IndirectGotoStmtClass:
1988 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
1989 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001990
Ted Kremenek411cdee2008-04-16 21:10:48 +00001991 case Stmt::SwitchStmtClass:
1992 E = cast<SwitchStmt>(Terminator)->getCond();
1993 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001994
Ted Kremenek411cdee2008-04-16 21:10:48 +00001995 case Stmt::ConditionalOperatorClass:
1996 E = cast<ConditionalOperator>(Terminator)->getCond();
1997 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00001998
Ted Kremenek411cdee2008-04-16 21:10:48 +00001999 case Stmt::BinaryOperatorClass: // '&&' and '||'
2000 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002001 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002002
Ted Kremenek390e48b2008-11-12 21:11:49 +00002003 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002004 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002005 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002006
Ted Kremenek411cdee2008-04-16 21:10:48 +00002007 return E ? E->IgnoreParens() : NULL;
2008}
2009
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002010bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002011
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002012 if (!Terminator)
2013 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002014
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002015 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002016
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002017 switch (Terminator->getStmtClass()) {
2018 default:
2019 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002020
2021 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002022 case Stmt::WhileStmtClass:
2023 case Stmt::DoStmtClass:
2024 case Stmt::IfStmtClass:
2025 case Stmt::ChooseExprClass:
2026 case Stmt::ConditionalOperatorClass:
2027 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002028 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002029 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002030
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002031 return E ? E->IgnoreParens() : NULL;
2032}
2033
Ted Kremeneka2925852008-01-30 23:02:42 +00002034
Ted Kremenek7dba8602007-08-29 21:56:09 +00002035//===----------------------------------------------------------------------===//
2036// CFG Graphviz Visualization
2037//===----------------------------------------------------------------------===//
2038
Ted Kremenek42a509f2007-08-31 21:30:12 +00002039
2040#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002041static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002042#endif
2043
Chris Lattnere4f21422009-06-30 01:26:17 +00002044void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002045#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002046 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002047 GraphHelper = &H;
2048 llvm::ViewGraph(this,"CFG");
2049 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002050#endif
2051}
2052
Ted Kremenek7dba8602007-08-29 21:56:09 +00002053namespace llvm {
2054template<>
2055struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Owen Anderson02995ce2009-06-24 17:37:55 +00002056 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph,
2057 bool ShortNames) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002058
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002059#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002060 std::string OutSStr;
2061 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002062 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002063 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002064
2065 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2066
2067 // Process string output to make it nicer...
2068 for (unsigned i = 0; i != OutStr.length(); ++i)
2069 if (OutStr[i] == '\n') { // Left justify
2070 OutStr[i] = '\\';
2071 OutStr.insert(OutStr.begin()+i+1, 'l');
2072 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002073
Ted Kremenek7dba8602007-08-29 21:56:09 +00002074 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002075#else
2076 return "";
2077#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002078 }
2079};
2080} // end namespace llvm