blob: 15699dbca42220f4e54cabdda9b72bb4a2b50e92 [file] [log] [blame]
Ted Kremenekfddd5182007-08-21 21:42:03 +00001//===--- CFG.cpp - Classes for representing and building CFGs----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenekfddd5182007-08-21 21:42:03 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the CFG and CFGBuilder classes for representing and
11// building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekbd048782009-07-22 21:45:16 +000015#include "clang/Analysis/Support/SaveAndRestore.h"
Ted Kremeneke41611a2009-07-16 18:13:04 +000016#include "clang/Analysis/CFG.h"
Mike Stumpb978a442010-01-21 02:21:40 +000017#include "clang/AST/DeclCXX.h"
Ted Kremenekc310e932007-08-21 22:06:14 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek42a509f2007-08-31 21:30:12 +000019#include "clang/AST/PrettyPrinter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000020#include "llvm/Support/GraphWriter.h"
Benjamin Kramer6cb7c1a2009-08-23 12:08:50 +000021#include "llvm/Support/Allocator.h"
22#include "llvm/Support/Format.h"
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000023#include "llvm/ADT/DenseMap.h"
Ted Kremenek19bb3562007-08-28 19:26:49 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek0ba497b2009-10-20 23:46:25 +000025#include "llvm/ADT/OwningPtr.h"
Ted Kremenek83c01da2008-01-11 00:40:29 +000026
Ted Kremenekfddd5182007-08-21 21:42:03 +000027using namespace clang;
28
29namespace {
30
Douglas Gregor4afa39d2009-01-20 01:17:11 +000031static SourceLocation GetEndLoc(Decl* D) {
Ted Kremenekc7eb9032008-08-06 23:20:50 +000032 if (VarDecl* VD = dyn_cast<VarDecl>(D))
33 if (Expr* Ex = VD->getInit())
34 return Ex->getSourceRange().getEnd();
Mike Stump6d9828c2009-07-17 01:31:16 +000035
36 return D->getLocation();
Ted Kremenekc7eb9032008-08-06 23:20:50 +000037}
Ted Kremenekad5a8942010-08-02 23:46:59 +000038
Ted Kremenek852274d2009-12-16 03:18:58 +000039class AddStmtChoice {
40public:
Ted Kremenek5ba290a2010-03-02 21:43:54 +000041 enum Kind { NotAlwaysAdd = 0,
42 AlwaysAdd = 1,
43 AsLValueNotAlwaysAdd = 2,
44 AlwaysAddAsLValue = 3 };
45
Benjamin Kramer792bea92010-03-03 16:28:47 +000046 AddStmtChoice(Kind kind) : k(kind) {}
Ted Kremenek5ba290a2010-03-02 21:43:54 +000047
Benjamin Kramer792bea92010-03-03 16:28:47 +000048 bool alwaysAdd() const { return (unsigned)k & 0x1; }
Ted Kremenek431ac2d2010-04-11 17:02:04 +000049 bool asLValue() const { return k >= AsLValueNotAlwaysAdd; }
Ted Kremenek5ba290a2010-03-02 21:43:54 +000050
Ted Kremenek852274d2009-12-16 03:18:58 +000051private:
Benjamin Kramer792bea92010-03-03 16:28:47 +000052 Kind k;
Ted Kremenek852274d2009-12-16 03:18:58 +000053};
Mike Stump6d9828c2009-07-17 01:31:16 +000054
Ted Kremeneka34ea072008-08-04 22:51:42 +000055/// CFGBuilder - This class implements CFG construction from an AST.
Ted Kremenekfddd5182007-08-21 21:42:03 +000056/// The builder is stateful: an instance of the builder should be used to only
57/// construct a single CFG.
58///
59/// Example usage:
60///
61/// CFGBuilder builder;
62/// CFG* cfg = builder.BuildAST(stmt1);
63///
Mike Stump6d9828c2009-07-17 01:31:16 +000064/// CFG construction is done via a recursive walk of an AST. We actually parse
65/// the AST in reverse order so that the successor of a basic block is
66/// constructed prior to its predecessor. This allows us to nicely capture
67/// implicit fall-throughs without extra basic blocks.
Ted Kremenekc310e932007-08-21 22:06:14 +000068///
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000069class CFGBuilder {
Mike Stumpe5af3ce2009-07-20 23:24:15 +000070 ASTContext *Context;
Ted Kremenek0ba497b2009-10-20 23:46:25 +000071 llvm::OwningPtr<CFG> cfg;
Ted Kremenekee82d9b2009-10-12 20:55:07 +000072
Ted Kremenekfddd5182007-08-21 21:42:03 +000073 CFGBlock* Block;
Ted Kremenekfddd5182007-08-21 21:42:03 +000074 CFGBlock* Succ;
Ted Kremenekbf15b272007-08-22 21:36:54 +000075 CFGBlock* ContinueTargetBlock;
Ted Kremenek8a294712007-08-22 21:51:58 +000076 CFGBlock* BreakTargetBlock;
Ted Kremenekb5c13b02007-08-23 18:43:24 +000077 CFGBlock* SwitchTerminatedBlock;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +000078 CFGBlock* DefaultCaseBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +000079 CFGBlock* TryTerminatedBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +000080
Ted Kremenek19bb3562007-08-28 19:26:49 +000081 // LabelMap records the mapping from Label expressions to their blocks.
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000082 typedef llvm::DenseMap<LabelStmt*,CFGBlock*> LabelMapTy;
83 LabelMapTy LabelMap;
Mike Stump6d9828c2009-07-17 01:31:16 +000084
85 // A list of blocks that end with a "goto" that must be backpatched to their
86 // resolved targets upon completion of CFG construction.
Ted Kremenek4a2b8a12007-08-22 15:40:58 +000087 typedef std::vector<CFGBlock*> BackpatchBlocksTy;
Ted Kremenek0cebe3e2007-08-21 23:26:17 +000088 BackpatchBlocksTy BackpatchBlocks;
Mike Stump6d9828c2009-07-17 01:31:16 +000089
Ted Kremenek19bb3562007-08-28 19:26:49 +000090 // A list of labels whose address has been taken (for indirect gotos).
91 typedef llvm::SmallPtrSet<LabelStmt*,5> LabelSetTy;
92 LabelSetTy AddressTakenLabels;
Mike Stump6d9828c2009-07-17 01:31:16 +000093
94public:
Ted Kremenekee82d9b2009-10-12 20:55:07 +000095 explicit CFGBuilder() : cfg(new CFG()), // crew a new CFG
96 Block(NULL), Succ(NULL),
Ted Kremenek8a294712007-08-22 21:51:58 +000097 ContinueTargetBlock(NULL), BreakTargetBlock(NULL),
Mike Stump5d1d2022010-01-19 02:20:09 +000098 SwitchTerminatedBlock(NULL), DefaultCaseBlock(NULL),
99 TryTerminatedBlock(NULL) {}
Mike Stump6d9828c2009-07-17 01:31:16 +0000100
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000101 // buildCFG - Used by external clients to construct the CFG.
Ted Kremenekad5a8942010-08-02 23:46:59 +0000102 CFG* buildCFG(const Decl *D, Stmt *Statement, ASTContext *C,
Ted Kremenekfe255bc2010-09-13 22:25:54 +0000103 bool pruneTriviallyFalseEdges, bool AddEHEdges);
Mike Stump6d9828c2009-07-17 01:31:16 +0000104
Ted Kremenek4f880632009-07-17 22:18:43 +0000105private:
106 // Visitors to walk an AST and construct the CFG.
Ted Kremenek852274d2009-12-16 03:18:58 +0000107 CFGBlock *VisitAddrLabelExpr(AddrLabelExpr *A, AddStmtChoice asc);
108 CFGBlock *VisitBinaryOperator(BinaryOperator *B, AddStmtChoice asc);
109 CFGBlock *VisitBlockExpr(BlockExpr* E, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000110 CFGBlock *VisitBreakStmt(BreakStmt *B);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000111 CFGBlock *VisitCXXCatchStmt(CXXCatchStmt *S);
112 CFGBlock *VisitCXXThrowExpr(CXXThrowExpr *T);
113 CFGBlock *VisitCXXTryStmt(CXXTryStmt *S);
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000114 CFGBlock *VisitCXXMemberCallExpr(CXXMemberCallExpr *C, AddStmtChoice asc);
Ted Kremenek852274d2009-12-16 03:18:58 +0000115 CFGBlock *VisitCallExpr(CallExpr *C, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000116 CFGBlock *VisitCaseStmt(CaseStmt *C);
Ted Kremenek852274d2009-12-16 03:18:58 +0000117 CFGBlock *VisitChooseExpr(ChooseExpr *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000118 CFGBlock *VisitCompoundStmt(CompoundStmt *C);
Ted Kremenek7ea21362010-04-11 17:01:59 +0000119 CFGBlock *VisitConditionalOperator(ConditionalOperator *C, AddStmtChoice asc);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000120 CFGBlock *VisitContinueStmt(ContinueStmt *C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000121 CFGBlock *VisitDeclStmt(DeclStmt *DS);
122 CFGBlock *VisitDeclSubExpr(Decl* D);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000123 CFGBlock *VisitDefaultStmt(DefaultStmt *D);
124 CFGBlock *VisitDoStmt(DoStmt *D);
125 CFGBlock *VisitForStmt(ForStmt *F);
Ted Kremenek4f880632009-07-17 22:18:43 +0000126 CFGBlock *VisitGotoStmt(GotoStmt* G);
127 CFGBlock *VisitIfStmt(IfStmt *I);
128 CFGBlock *VisitIndirectGotoStmt(IndirectGotoStmt *I);
129 CFGBlock *VisitLabelStmt(LabelStmt *L);
Ted Kremenek115c1b92010-04-11 17:02:10 +0000130 CFGBlock *VisitMemberExpr(MemberExpr *M, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000131 CFGBlock *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
132 CFGBlock *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
133 CFGBlock *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
134 CFGBlock *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
135 CFGBlock *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
136 CFGBlock *VisitReturnStmt(ReturnStmt* R);
Ted Kremenek852274d2009-12-16 03:18:58 +0000137 CFGBlock *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E, AddStmtChoice asc);
138 CFGBlock *VisitStmtExpr(StmtExpr *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000139 CFGBlock *VisitSwitchStmt(SwitchStmt *S);
140 CFGBlock *VisitWhileStmt(WhileStmt *W);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000141
Ted Kremenek852274d2009-12-16 03:18:58 +0000142 CFGBlock *Visit(Stmt *S, AddStmtChoice asc = AddStmtChoice::NotAlwaysAdd);
143 CFGBlock *VisitStmt(Stmt *S, AddStmtChoice asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000144 CFGBlock *VisitChildren(Stmt* S);
Mike Stumpcd7bf232009-07-17 01:04:31 +0000145
Ted Kremenek274f4332008-04-28 18:00:46 +0000146 // NYS == Not Yet Supported
147 CFGBlock* NYS() {
Ted Kremenek4102af92008-03-13 03:04:22 +0000148 badCFG = true;
149 return Block;
150 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000151
Ted Kremenek4f880632009-07-17 22:18:43 +0000152 void autoCreateBlock() { if (!Block) Block = createBlock(); }
153 CFGBlock *createBlock(bool add_successor = true);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000154
Zhongxing Xudf119892010-06-03 06:43:23 +0000155 CFGBlock *addStmt(Stmt *S) {
156 return Visit(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek852274d2009-12-16 03:18:58 +0000157 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000158
Ted Kremenek852274d2009-12-16 03:18:58 +0000159 void AppendStmt(CFGBlock *B, Stmt *S,
160 AddStmtChoice asc = AddStmtChoice::AlwaysAdd) {
161 B->appendStmt(S, cfg->getBumpVectorContext(), asc.asLValue());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000162 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000163
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000164 void AddSuccessor(CFGBlock *B, CFGBlock *S) {
165 B->addSuccessor(S, cfg->getBumpVectorContext());
166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000168 /// TryResult - a class representing a variant over the values
169 /// 'true', 'false', or 'unknown'. This is returned by TryEvaluateBool,
170 /// and is used by the CFGBuilder to decide if a branch condition
171 /// can be decided up front during CFG construction.
Ted Kremenek941fde82009-07-24 04:47:11 +0000172 class TryResult {
173 int X;
174 public:
175 TryResult(bool b) : X(b ? 1 : 0) {}
176 TryResult() : X(-1) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek941fde82009-07-24 04:47:11 +0000178 bool isTrue() const { return X == 1; }
179 bool isFalse() const { return X == 0; }
180 bool isKnown() const { return X >= 0; }
181 void negate() {
182 assert(isKnown());
183 X ^= 0x1;
184 }
185 };
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Mike Stump00998a02009-07-23 23:25:26 +0000187 /// TryEvaluateBool - Try and evaluate the Stmt and return 0 or 1
188 /// if we can evaluate to a known value, otherwise return -1.
Ted Kremenek941fde82009-07-24 04:47:11 +0000189 TryResult TryEvaluateBool(Expr *S) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000190 if (!PruneTriviallyFalseEdges)
191 return TryResult();
192
Mike Stump00998a02009-07-23 23:25:26 +0000193 Expr::EvalResult Result;
Douglas Gregor9983cc12009-08-24 21:39:56 +0000194 if (!S->isTypeDependent() && !S->isValueDependent() &&
195 S->Evaluate(Result, *Context) && Result.Val.isInt())
Ted Kremenekfadc9ea2009-07-24 06:55:42 +0000196 return Result.Val.getInt().getBoolValue();
Ted Kremenek941fde82009-07-24 04:47:11 +0000197
198 return TryResult();
Mike Stump00998a02009-07-23 23:25:26 +0000199 }
200
Ted Kremenek4102af92008-03-13 03:04:22 +0000201 bool badCFG;
Mike Stump4c45aa12010-01-21 15:20:48 +0000202
Ted Kremenekad5a8942010-08-02 23:46:59 +0000203 // True iff trivially false edges should be pruned from the CFG.
204 bool PruneTriviallyFalseEdges;
205
Mike Stump4c45aa12010-01-21 15:20:48 +0000206 // True iff EH edges on CallExprs should be added to the CFG.
207 bool AddEHEdges;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000208};
Mike Stump6d9828c2009-07-17 01:31:16 +0000209
Douglas Gregor898574e2008-12-05 23:32:09 +0000210// FIXME: Add support for dependent-sized array types in C++?
211// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000212static VariableArrayType* FindVA(Type* t) {
213 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
214 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
215 if (vat->getSizeExpr())
216 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000217
Ted Kremenek610a09e2008-09-26 22:58:57 +0000218 t = vt->getElementType().getTypePtr();
219 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000220
Ted Kremenek610a09e2008-09-26 22:58:57 +0000221 return 0;
222}
Mike Stump6d9828c2009-07-17 01:31:16 +0000223
224/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
225/// arbitrary statement. Examples include a single expression or a function
226/// body (compound statement). The ownership of the returned CFG is
227/// transferred to the caller. If CFG construction fails, this method returns
228/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000229CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenekad5a8942010-08-02 23:46:59 +0000230 bool pruneTriviallyFalseEdges,
Ted Kremenekfe255bc2010-09-13 22:25:54 +0000231 bool addehedges) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000232
Mike Stump55f988e2010-01-21 17:21:23 +0000233 AddEHEdges = addehedges;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000234 PruneTriviallyFalseEdges = pruneTriviallyFalseEdges;
235
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000236 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000237 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000238 if (!Statement)
239 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000240
Ted Kremenek4102af92008-03-13 03:04:22 +0000241 badCFG = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000242
243 // Create an empty block that will serve as the exit block for the CFG. Since
244 // this is the first block added to the CFG, it will be implicitly registered
245 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000246 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000247 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000248 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000249
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000250 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000251 CFGBlock *B = addStmt(Statement);
252
253 if (badCFG)
254 return NULL;
255
256 if (B)
257 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000258
259 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
260 // FIXME: Add code for base initializers and member initializers.
261 (void)CD;
262 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000263
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000264 // Backpatch the gotos whose label -> block mappings we didn't know when we
265 // encountered them.
266 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
267 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000268
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000269 CFGBlock* B = *I;
270 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
271 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000272
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000273 // If there is no target for the goto, then we are looking at an
274 // incomplete AST. Handle this by not registering a successor.
275 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000276
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000277 AddSuccessor(B, LI->second);
278 }
279
280 // Add successors to the Indirect Goto Dispatch block (if we have one).
281 if (CFGBlock* B = cfg->getIndirectGotoBlock())
282 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
283 E = AddressTakenLabels.end(); I != E; ++I ) {
284
285 // Lookup the target block.
286 LabelMapTy::iterator LI = LabelMap.find(*I);
287
288 // If there is no target block that contains label, then we are looking
289 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000290 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000291
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000292 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000293 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000294
Mike Stump6d9828c2009-07-17 01:31:16 +0000295 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000296 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000297
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000298 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000299}
Mike Stump6d9828c2009-07-17 01:31:16 +0000300
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000301/// createBlock - Used to lazily create blocks that are connected
302/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000303CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000304 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000305 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000306 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000307 return B;
308}
Mike Stump6d9828c2009-07-17 01:31:16 +0000309
Ted Kremenek4f880632009-07-17 22:18:43 +0000310/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000311/// blocks for ternary operators, &&, and ||. We also process "," and
312/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000313CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000314tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000315 if (!S) {
316 badCFG = true;
317 return 0;
318 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000319 switch (S->getStmtClass()) {
320 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000321 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000322
323 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000324 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek4f880632009-07-17 22:18:43 +0000326 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000327 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek4f880632009-07-17 22:18:43 +0000329 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000330 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000331
Ted Kremenek4f880632009-07-17 22:18:43 +0000332 case Stmt::BreakStmtClass:
333 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek4f880632009-07-17 22:18:43 +0000335 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000336 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000337 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek4f880632009-07-17 22:18:43 +0000339 case Stmt::CaseStmtClass:
340 return VisitCaseStmt(cast<CaseStmt>(S));
341
342 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000343 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek4f880632009-07-17 22:18:43 +0000345 case Stmt::CompoundStmtClass:
346 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek4f880632009-07-17 22:18:43 +0000348 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000349 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek4f880632009-07-17 22:18:43 +0000351 case Stmt::ContinueStmtClass:
352 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Ted Kremenek021c8af2010-01-19 20:40:33 +0000354 case Stmt::CXXCatchStmtClass:
355 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
356
Ted Kremenek47e331e2010-08-28 00:19:02 +0000357 case Stmt::CXXExprWithTemporariesClass: {
358 // FIXME: Handle temporaries. For now, just visit the subexpression
359 // so we don't artificially create extra blocks.
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000360 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000361 }
362
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000363 case Stmt::CXXMemberCallExprClass:
364 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
365
Ted Kremenek021c8af2010-01-19 20:40:33 +0000366 case Stmt::CXXThrowExprClass:
367 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000368
Ted Kremenek021c8af2010-01-19 20:40:33 +0000369 case Stmt::CXXTryStmtClass:
370 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000371
Ted Kremenek4f880632009-07-17 22:18:43 +0000372 case Stmt::DeclStmtClass:
373 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek4f880632009-07-17 22:18:43 +0000375 case Stmt::DefaultStmtClass:
376 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek4f880632009-07-17 22:18:43 +0000378 case Stmt::DoStmtClass:
379 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381 case Stmt::ForStmtClass:
382 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek4f880632009-07-17 22:18:43 +0000384 case Stmt::GotoStmtClass:
385 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek4f880632009-07-17 22:18:43 +0000387 case Stmt::IfStmtClass:
388 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek4f880632009-07-17 22:18:43 +0000390 case Stmt::IndirectGotoStmtClass:
391 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek4f880632009-07-17 22:18:43 +0000393 case Stmt::LabelStmtClass:
394 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Ted Kremenek115c1b92010-04-11 17:02:10 +0000396 case Stmt::MemberExprClass:
397 return VisitMemberExpr(cast<MemberExpr>(S), asc);
398
Ted Kremenek4f880632009-07-17 22:18:43 +0000399 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000400 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
401
Ted Kremenek4f880632009-07-17 22:18:43 +0000402 case Stmt::ObjCAtSynchronizedStmtClass:
403 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek4f880632009-07-17 22:18:43 +0000405 case Stmt::ObjCAtThrowStmtClass:
406 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek4f880632009-07-17 22:18:43 +0000408 case Stmt::ObjCAtTryStmtClass:
409 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Ted Kremenek4f880632009-07-17 22:18:43 +0000411 case Stmt::ObjCForCollectionStmtClass:
412 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Ted Kremenek4f880632009-07-17 22:18:43 +0000414 case Stmt::ParenExprClass:
415 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000416 goto tryAgain;
417
Ted Kremenek4f880632009-07-17 22:18:43 +0000418 case Stmt::NullStmtClass:
419 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek4f880632009-07-17 22:18:43 +0000421 case Stmt::ReturnStmtClass:
422 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000425 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek4f880632009-07-17 22:18:43 +0000427 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000428 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek4f880632009-07-17 22:18:43 +0000430 case Stmt::SwitchStmtClass:
431 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Ted Kremenek4f880632009-07-17 22:18:43 +0000433 case Stmt::WhileStmtClass:
434 return VisitWhileStmt(cast<WhileStmt>(S));
435 }
436}
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek852274d2009-12-16 03:18:58 +0000438CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
439 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000440 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000441 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Ted Kremenek4f880632009-07-17 22:18:43 +0000444 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000445}
Mike Stump6d9828c2009-07-17 01:31:16 +0000446
Ted Kremenek4f880632009-07-17 22:18:43 +0000447/// VisitChildren - Visit the children of a Stmt.
448CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
449 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000450 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000451 E = Terminator->child_end(); I != E; ++I) {
452 if (*I) B = Visit(*I);
453 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000454 return B;
455}
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Ted Kremenek852274d2009-12-16 03:18:58 +0000457CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
458 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000459 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000460
Ted Kremenek852274d2009-12-16 03:18:58 +0000461 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000462 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000463 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000464 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000465
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000466 return Block;
467}
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Ted Kremenek852274d2009-12-16 03:18:58 +0000469CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
470 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000471 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000472 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000473 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000475 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000476 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek4f880632009-07-17 22:18:43 +0000478 // create the block evaluating the LHS
479 CFGBlock* LHSBlock = createBlock(false);
480 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek4f880632009-07-17 22:18:43 +0000482 // create the block evaluating the RHS
483 Succ = ConfluenceBlock;
484 Block = NULL;
485 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000486
487 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000488 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000489 return 0;
490 }
491 else {
492 // Create an empty block for cases where the RHS doesn't require
493 // any explicit statements in the CFG.
494 RHSBlock = createBlock();
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Mike Stump00998a02009-07-23 23:25:26 +0000497 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000498 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000499 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000500 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000501
Ted Kremenek4f880632009-07-17 22:18:43 +0000502 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000503 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000504 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
505 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000506 } else {
John McCall2de56d12010-08-25 11:45:40 +0000507 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000508 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
509 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Ted Kremenek4f880632009-07-17 22:18:43 +0000512 // Generate the blocks for evaluating the LHS.
513 Block = LHSBlock;
514 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000515 }
John McCall2de56d12010-08-25 11:45:40 +0000516 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000517 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000518 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000519 addStmt(B->getRHS());
520 return addStmt(B->getLHS());
521 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000522 else if (B->isAssignmentOp()) {
523 if (asc.alwaysAdd()) {
524 autoCreateBlock();
525 AppendStmt(Block, B, asc);
526 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000527
Ted Kremenek44f8ef12010-09-14 01:13:32 +0000528 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
529 // create a new block, then we should return RBlock. Otherwise
530 // we'll incorrectly return NULL.
531 CFGBlock *RBlock = Visit(B->getRHS());
532 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
533 return LBlock ? LBlock : RBlock;
Zhongxing Xufc61d942010-06-03 06:23:18 +0000534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Ted Kremenek852274d2009-12-16 03:18:58 +0000536 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000537}
538
Ted Kremenek852274d2009-12-16 03:18:58 +0000539CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
540 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000541 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000542 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000543 }
544 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000545}
546
Ted Kremenek4f880632009-07-17 22:18:43 +0000547CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
548 // "break" is a control-flow statement. Thus we stop processing the current
549 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000550 if (badCFG)
551 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek4f880632009-07-17 22:18:43 +0000553 // Now create a new block that ends with the break statement.
554 Block = createBlock(false);
555 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Ted Kremenek4f880632009-07-17 22:18:43 +0000557 // If there is no target for the break, then we are looking at an incomplete
558 // AST. This means that the CFG cannot be constructed.
559 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000560 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000561 else
562 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
564
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000565 return Block;
566}
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Mike Stump4c45aa12010-01-21 15:20:48 +0000568static bool CanThrow(Expr *E) {
569 QualType Ty = E->getType();
570 if (Ty->isFunctionPointerType())
571 Ty = Ty->getAs<PointerType>()->getPointeeType();
572 else if (Ty->isBlockPointerType())
573 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +0000574
Mike Stump4c45aa12010-01-21 15:20:48 +0000575 const FunctionType *FT = Ty->getAs<FunctionType>();
576 if (FT) {
577 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
578 if (Proto->hasEmptyExceptionSpec())
579 return false;
580 }
581 return true;
582}
583
Ted Kremenek852274d2009-12-16 03:18:58 +0000584CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000585 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000586 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000587 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000588 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000589 }
Mike Stump24556362009-07-25 21:26:53 +0000590
Mike Stump4c45aa12010-01-21 15:20:48 +0000591 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000592
593 // Languages without exceptions are assumed to not throw.
594 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000595 if (AddEHEdges)
596 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000597 }
598
599 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000600 if (FD->hasAttr<NoReturnAttr>())
601 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000602 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000603 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000604 }
Mike Stump24556362009-07-25 21:26:53 +0000605
Mike Stump4c45aa12010-01-21 15:20:48 +0000606 if (!CanThrow(C->getCallee()))
607 AddEHEdge = false;
608
Zhongxing Xufc61d942010-06-03 06:23:18 +0000609 if (!NoReturn && !AddEHEdge) {
610 if (asc.asLValue())
611 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
612 else
613 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Mike Stump079bd722010-01-19 22:00:14 +0000616 if (Block) {
617 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000618 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +0000619 return 0;
620 }
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Mike Stump079bd722010-01-19 22:00:14 +0000622 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000623 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000624
Mike Stump079bd722010-01-19 22:00:14 +0000625 if (NoReturn) {
626 // Wire this to the exit block directly.
627 AddSuccessor(Block, &cfg->getExit());
628 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000629 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000630 // Add exceptional edges.
631 if (TryTerminatedBlock)
632 AddSuccessor(Block, TryTerminatedBlock);
633 else
634 AddSuccessor(Block, &cfg->getExit());
635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Mike Stump24556362009-07-25 21:26:53 +0000637 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000638}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000639
Ted Kremenek852274d2009-12-16 03:18:58 +0000640CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
641 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000642 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000643 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000644 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000645 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Ted Kremenek115c1b92010-04-11 17:02:10 +0000647 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
648 : AddStmtChoice::AlwaysAdd;
649
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000650 Succ = ConfluenceBlock;
651 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000652 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000653 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000654 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000656 Succ = ConfluenceBlock;
657 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000658 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000659 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000660 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000662 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000663 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000664 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000665 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
666 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000667 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000668 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000669}
Mike Stump1eb44332009-09-09 15:08:12 +0000670
671
672CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
673 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000674
675 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
676 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +0000677 // If we hit a segment of code just containing ';' (NullStmts), we can
678 // get a null block back. In such cases, just use the LastBlock
679 if (CFGBlock *newBlock = addStmt(*I))
680 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000682 if (badCFG)
683 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000684 }
Mike Stump079bd722010-01-19 22:00:14 +0000685
Ted Kremenek4f880632009-07-17 22:18:43 +0000686 return LastBlock;
687}
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Ted Kremenek852274d2009-12-16 03:18:58 +0000689CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
690 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000691 // Create the confluence block that will "merge" the results of the ternary
692 // expression.
693 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000694 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000695 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000696 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Ted Kremenek115c1b92010-04-11 17:02:10 +0000698 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
699 : AddStmtChoice::AlwaysAdd;
700
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000701 // Create a block for the LHS expression if there is an LHS expression. A
702 // GCC extension allows LHS to be NULL, causing the condition to be the
703 // value that is returned instead.
704 // e.g: x ?: y is shorthand for: x ? x : y;
705 Succ = ConfluenceBlock;
706 Block = NULL;
707 CFGBlock* LHSBlock = NULL;
708 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +0000709 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000710 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000711 return 0;
712 Block = NULL;
713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000715 // Create the block for the RHS expression.
716 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +0000717 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000718 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000719 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000721 // Create the block that will contain the condition.
722 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Mike Stump00998a02009-07-23 23:25:26 +0000724 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000725 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000726 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000727 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000728 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000729 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000730 // If we know the condition is false, add NULL as the successor for
731 // the block containing the condition. In this case, the confluence
732 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000733 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000734 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000735 } else {
736 // If we have no LHS expression, add the ConfluenceBlock as a direct
737 // successor for the block containing the condition. Moreover, we need to
738 // reverse the order of the predecessors in the ConfluenceBlock because
739 // the RHSBlock will have been added to the succcessors already, and we
740 // want the first predecessor to the the block containing the expression
741 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000742 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000743 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000744 std::reverse(ConfluenceBlock->pred_begin(),
745 ConfluenceBlock->pred_end());
746 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000749 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000750 Block->setTerminator(C);
751 return addStmt(C->getCond());
752}
753
Ted Kremenek4f880632009-07-17 22:18:43 +0000754CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
755 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000756
Ted Kremenek4f880632009-07-17 22:18:43 +0000757 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000758 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000759 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek4f880632009-07-17 22:18:43 +0000762 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek4f880632009-07-17 22:18:43 +0000764 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
765 typedef llvm::SmallVector<Decl*,10> BufTy;
766 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek4f880632009-07-17 22:18:43 +0000768 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
769 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
770 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
771 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Ted Kremenek4f880632009-07-17 22:18:43 +0000773 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
774 // automatically freed with the CFG.
775 DeclGroupRef DG(*I);
776 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000777 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000778 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenek4f880632009-07-17 22:18:43 +0000780 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000781 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000782 B = VisitDeclSubExpr(D);
783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
785 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000786}
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenek4f880632009-07-17 22:18:43 +0000788/// VisitDeclSubExpr - Utility method to add block-level expressions for
789/// initializers in Decls.
790CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
791 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000792
Ted Kremenek4f880632009-07-17 22:18:43 +0000793 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek4f880632009-07-17 22:18:43 +0000795 if (!VD)
796 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek4f880632009-07-17 22:18:43 +0000798 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek4f880632009-07-17 22:18:43 +0000800 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000801 AddStmtChoice::Kind k =
802 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
803 : AddStmtChoice::NotAlwaysAdd;
804 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek4f880632009-07-17 22:18:43 +0000807 // If the type of VD is a VLA, then we must process its size expressions.
808 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
809 VA = FindVA(VA->getElementType().getTypePtr()))
810 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek4f880632009-07-17 22:18:43 +0000812 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000813}
814
815CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000816 // We may see an if statement in the middle of a basic block, or it may be the
817 // first statement we are processing. In either case, we create a new basic
818 // block. First, we create the blocks for the then...else statements, and
819 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000820 // middle of a block, we stop processing that block. That block is then the
821 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000822
823 // The block we were proccessing is now finished. Make it the successor
824 // block.
825 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000827 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000828 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000829 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000830
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000831 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000832 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000833
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000834 if (Stmt* Else = I->getElse()) {
835 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000836
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000837 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000838 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000839 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000840 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000841
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000842 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
843 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000844 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000845 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000846 return 0;
847 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000848 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000849
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000850 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000851 CFGBlock* ThenBlock;
852 {
853 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000854 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000855 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000856 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000857 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000858
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000859 if (!ThenBlock) {
860 // We can reach here if the "then" body has all NullStmts.
861 // Create an empty block so we can distinguish between true and false
862 // branches in path-sensitive analyses.
863 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000864 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000865 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000866 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000867 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000868 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000869 }
870
Mike Stump6d9828c2009-07-17 01:31:16 +0000871 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000872 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000873
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000874 // Set the terminator of the new block to the If statement.
875 Block->setTerminator(I);
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 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000879
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000880 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000881 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
882 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000883
884 // Add the condition as the last statement in the new block. This may create
885 // new blocks as the condition may contain control-flow. Any newly created
886 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000887 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +0000888
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000889 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
890 // and the condition variable initialization to the CFG.
891 if (VarDecl *VD = I->getConditionVariable()) {
892 if (Expr *Init = VD->getInit()) {
893 autoCreateBlock();
894 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
895 addStmt(Init);
896 }
897 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000898
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000899 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000900}
Mike Stump6d9828c2009-07-17 01:31:16 +0000901
902
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000903CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000904 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000905 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000906 // NOTE: If a "return" appears in the middle of a block, this means that the
907 // code afterwards is DEAD (unreachable). We still keep a basic block
908 // for that code; a simple "mark-and-sweep" from the entry block will be
909 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000910
911 // Create the new block.
912 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000913
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000914 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000915 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000916
917 // Add the return statement to the block. This may create new blocks if R
918 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000919 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000920}
921
922CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
923 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000924 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000925 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000926
Ted Kremenek4f880632009-07-17 22:18:43 +0000927 if (!LabelBlock) // This can happen when the body is empty, i.e.
928 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000929
Ted Kremenek4f880632009-07-17 22:18:43 +0000930 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000931 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000932
933 // Labels partition blocks, so this is the end of the basic block we were
934 // processing (L is the block's label). Because this is label (and we have
935 // already processed the substatement) there is no extra control-flow to worry
936 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000937 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000938 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000939 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000940
941 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000942 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000943
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000944 // This block is now the implicit successor of other blocks.
945 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000946
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000947 return LabelBlock;
948}
949
950CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000951 // Goto is a control-flow statement. Thus we stop processing the current
952 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000953
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000954 Block = createBlock(false);
955 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000956
957 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000958 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000959
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000960 if (I == LabelMap.end())
961 // We will need to backpatch this block later.
962 BackpatchBlocks.push_back(Block);
963 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000964 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000965
Mike Stump6d9828c2009-07-17 01:31:16 +0000966 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000967}
968
969CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000970 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000971
Mike Stumpfefb9f72009-07-21 01:12:51 +0000972 // "for" is a control-flow statement. Thus we stop processing the current
973 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000974 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000975 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000976 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000977 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000978 } else
979 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000980
Ted Kremenek3f64a0e2010-05-21 20:30:15 +0000981 // Save the current value for the break targets.
982 // All breaks should go to the code following the loop.
983 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
984 BreakTargetBlock = LoopSuccessor;
985
Mike Stump6d9828c2009-07-17 01:31:16 +0000986 // Because of short-circuit evaluation, the condition of the loop can span
987 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
988 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000989 CFGBlock* ExitConditionBlock = createBlock(false);
990 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000991
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000992 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000993 ExitConditionBlock->setTerminator(F);
994
995 // Now add the actual condition to the condition block. Because the condition
996 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000997 if (Stmt* C = F->getCond()) {
998 Block = ExitConditionBlock;
999 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +00001000 assert(Block == EntryConditionBlock);
1001
1002 // If this block contains a condition variable, add both the condition
1003 // variable and initializer to the CFG.
1004 if (VarDecl *VD = F->getConditionVariable()) {
1005 if (Expr *Init = VD->getInit()) {
1006 autoCreateBlock();
1007 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1008 EntryConditionBlock = addStmt(Init);
1009 assert(Block == EntryConditionBlock);
1010 }
1011 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001012
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001013 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001014 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001015 return 0;
1016 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001017 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001018
Mike Stump6d9828c2009-07-17 01:31:16 +00001019 // The condition block is the implicit successor for the loop body as well as
1020 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001021 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001022
Mike Stump00998a02009-07-23 23:25:26 +00001023 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001024 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Mike Stump00998a02009-07-23 23:25:26 +00001026 if (F->getCond())
1027 KnownVal = TryEvaluateBool(F->getCond());
1028
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001029 // Now create the loop body.
1030 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001031 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001032
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001033 // Save the current values for Block, Succ, and continue targets.
1034 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1035 save_continue(ContinueTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001036
Ted Kremenekaf603f72007-08-30 18:39:40 +00001037 // Create a new block to contain the (bottom) of the loop body.
1038 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001039
Ted Kremeneke9334502008-09-04 21:48:47 +00001040 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001041 // Generate increment code in its own basic block. This is the target of
1042 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001043 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001044 } else {
1045 // No increment code. Create a special, empty, block that is used as the
1046 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001047 assert(Succ == EntryConditionBlock);
1048 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001049 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001050
Ted Kremenek3575f842009-04-28 00:51:56 +00001051 // Finish up the increment (or empty) block if it hasn't been already.
1052 if (Block) {
1053 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001054 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001055 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001056 Block = 0;
1057 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001058
Ted Kremenek3575f842009-04-28 00:51:56 +00001059 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001060
Ted Kremenek3575f842009-04-28 00:51:56 +00001061 // The starting block for the loop increment is the block that should
1062 // represent the 'loop target' for looping back to the start of the loop.
1063 ContinueTargetBlock->setLoopTarget(F);
1064
Mike Stump6d9828c2009-07-17 01:31:16 +00001065 // Now populate the body block, and in the process create new blocks as we
1066 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001067 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001068
1069 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001070 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001071 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001072 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001073
Ted Kremenek941fde82009-07-24 04:47:11 +00001074 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001075 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001076 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001077
Ted Kremenek941fde82009-07-24 04:47:11 +00001078 // Link up the condition block with the code that follows the loop. (the
1079 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001080 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001081
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001082 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001083 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001084 if (Stmt* I = F->getInit()) {
1085 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001086 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001087 } else {
1088 // There is no loop initialization. We are thus basically a while loop.
1089 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001090 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001091 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001092 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001093 }
1094}
1095
Ted Kremenek115c1b92010-04-11 17:02:10 +00001096CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1097 if (asc.alwaysAdd()) {
1098 autoCreateBlock();
1099 AppendStmt(Block, M, asc);
1100 }
1101 return Visit(M->getBase(),
1102 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1103 : AddStmtChoice::AsLValueNotAlwaysAdd);
1104}
1105
Ted Kremenek514de5a2008-11-11 17:10:00 +00001106CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1107 // Objective-C fast enumeration 'for' statements:
1108 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1109 //
1110 // for ( Type newVariable in collection_expression ) { statements }
1111 //
1112 // becomes:
1113 //
1114 // prologue:
1115 // 1. collection_expression
1116 // T. jump to loop_entry
1117 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001118 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001119 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1120 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1121 // TB:
1122 // statements
1123 // T. jump to loop_entry
1124 // FB:
1125 // what comes after
1126 //
1127 // and
1128 //
1129 // Type existingItem;
1130 // for ( existingItem in expression ) { statements }
1131 //
1132 // becomes:
1133 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001134 // the same with newVariable replaced with existingItem; the binding works
1135 // the same except that for one ObjCForCollectionStmt::getElement() returns
1136 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001137 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001138
Ted Kremenek514de5a2008-11-11 17:10:00 +00001139 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001140
Ted Kremenek514de5a2008-11-11 17:10:00 +00001141 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001142 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001143 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001144 LoopSuccessor = Block;
1145 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001146 } else
1147 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001148
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001149 // Build the condition blocks.
1150 CFGBlock* ExitConditionBlock = createBlock(false);
1151 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001152
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001153 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001154 ExitConditionBlock->setTerminator(S);
1155
1156 // The last statement in the block should be the ObjCForCollectionStmt, which
1157 // performs the actual binding to 'element' and determines if there are any
1158 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001159 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001160 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001161
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001162 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001163 // generate new blocks as necesary. We DON'T add the statement by default to
1164 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001165 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001166 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001167 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001168 return 0;
1169 Block = 0;
1170 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001171
1172 // The condition block is the implicit successor for the loop body as well as
1173 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001174 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001175
Ted Kremenek514de5a2008-11-11 17:10:00 +00001176 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001177 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001178 // Save the current values for Succ, continue and break targets.
1179 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001180 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1181
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001182 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001183 ContinueTargetBlock = EntryConditionBlock;
1184
Ted Kremenek4f880632009-07-17 22:18:43 +00001185 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001186
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001187 if (!BodyBlock)
1188 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001189 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001190 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001191 return 0;
1192 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001193
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001194 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001195 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001196 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001197
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001198 // Link up the condition block with the code that follows the loop.
1199 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001200 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001201
Ted Kremenek514de5a2008-11-11 17:10:00 +00001202 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001203 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001204 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001205}
1206
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001207CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1208 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001209
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001210 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001211 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001212
Ted Kremenekda5348e2009-05-05 23:11:51 +00001213 // The sync body starts its own basic block. This makes it a little easier
1214 // for diagnostic clients.
1215 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001216 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001217 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001218
Ted Kremenekda5348e2009-05-05 23:11:51 +00001219 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001220 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001221 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001222
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001223 // Add the @synchronized to the CFG.
1224 autoCreateBlock();
1225 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1226
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001227 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001228 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001229}
Mike Stump6d9828c2009-07-17 01:31:16 +00001230
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001231CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001232 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001233 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001234}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001235
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001236CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001237 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001238
Mike Stumpfefb9f72009-07-21 01:12:51 +00001239 // "while" is a control-flow statement. Thus we stop processing the current
1240 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001241 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001242 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001243 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001244 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001245 } else
1246 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001247
1248 // Because of short-circuit evaluation, the condition of the loop can span
1249 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1250 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001251 CFGBlock* ExitConditionBlock = createBlock(false);
1252 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001253
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001254 // Set the terminator for the "exit" condition block.
1255 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001256
1257 // Now add the actual condition to the condition block. Because the condition
1258 // itself may contain control-flow, new blocks may be created. Thus we update
1259 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001260 if (Stmt* C = W->getCond()) {
1261 Block = ExitConditionBlock;
1262 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001263 assert(Block == EntryConditionBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001264
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001265 // If this block contains a condition variable, add both the condition
1266 // variable and initializer to the CFG.
1267 if (VarDecl *VD = W->getConditionVariable()) {
1268 if (Expr *Init = VD->getInit()) {
1269 autoCreateBlock();
1270 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1271 EntryConditionBlock = addStmt(Init);
1272 assert(Block == EntryConditionBlock);
1273 }
1274 }
1275
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001276 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001277 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001278 return 0;
1279 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001280 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001281
1282 // The condition block is the implicit successor for the loop body as well as
1283 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001284 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001285
Mike Stump00998a02009-07-23 23:25:26 +00001286 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001287 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001288
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001289 // Process the loop body.
1290 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001291 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001292
1293 // Save the current values for Block, Succ, and continue and break targets
1294 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1295 save_continue(ContinueTargetBlock),
1296 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001297
Mike Stump6d9828c2009-07-17 01:31:16 +00001298 // Create an empty block to represent the transition block for looping back
1299 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001300 Block = 0;
1301 assert(Succ == EntryConditionBlock);
1302 Succ = createBlock();
1303 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001304 ContinueTargetBlock = Succ;
1305
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001306 // All breaks should go to the code following the loop.
1307 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001308
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001309 // NULL out Block to force lazy instantiation of blocks for the body.
1310 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001311
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001312 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001313 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001314
Ted Kremenekaf603f72007-08-30 18:39:40 +00001315 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001316 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001317 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001318 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001319 return 0;
1320 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001321
Ted Kremenek941fde82009-07-24 04:47:11 +00001322 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001323 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001324 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001325
Ted Kremenek941fde82009-07-24 04:47:11 +00001326 // Link up the condition block with the code that follows the loop. (the
1327 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001328 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001329
1330 // There can be no more statements in the condition block since we loop back
1331 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001332 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001333
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001334 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001335 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001336 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001337}
Mike Stump1eb44332009-09-09 15:08:12 +00001338
1339
Ted Kremenek4f880632009-07-17 22:18:43 +00001340CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1341 // FIXME: For now we pretend that @catch and the code it contains does not
1342 // exit.
1343 return Block;
1344}
Mike Stump6d9828c2009-07-17 01:31:16 +00001345
Ted Kremenek2fda5042008-12-09 20:20:09 +00001346CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1347 // FIXME: This isn't complete. We basically treat @throw like a return
1348 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001349
Ted Kremenek6c249722009-09-24 18:45:41 +00001350 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001351 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001352 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001353
Ted Kremenek2fda5042008-12-09 20:20:09 +00001354 // Create the new block.
1355 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001356
Ted Kremenek2fda5042008-12-09 20:20:09 +00001357 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001358 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001359
1360 // Add the statement to the block. This may create new blocks if S contains
1361 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001362 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001363}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001364
Mike Stump0979d802009-07-22 22:56:04 +00001365CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001366 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001367 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001368 return 0;
1369
1370 // Create the new block.
1371 Block = createBlock(false);
1372
Mike Stump5d1d2022010-01-19 02:20:09 +00001373 if (TryTerminatedBlock)
1374 // The current try statement is the only successor.
1375 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001376 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001377 // otherwise the Exit block is the only successor.
1378 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001379
1380 // Add the statement to the block. This may create new blocks if S contains
1381 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001382 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001383}
1384
Ted Kremenek4f880632009-07-17 22:18:43 +00001385CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001386 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001387
Mike Stump8f9893a2009-07-21 01:27:50 +00001388 // "do...while" is a control-flow statement. Thus we stop processing the
1389 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001390 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001391 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001392 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001393 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001394 } else
1395 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001396
1397 // Because of short-circuit evaluation, the condition of the loop can span
1398 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1399 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001400 CFGBlock* ExitConditionBlock = createBlock(false);
1401 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001402
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001403 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001404 ExitConditionBlock->setTerminator(D);
1405
1406 // Now add the actual condition to the condition block. Because the condition
1407 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001408 if (Stmt* C = D->getCond()) {
1409 Block = ExitConditionBlock;
1410 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001411 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001412 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001413 return 0;
1414 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001415 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001416
Ted Kremenek54827132008-02-27 07:20:00 +00001417 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001418 Succ = EntryConditionBlock;
1419
Mike Stump00998a02009-07-23 23:25:26 +00001420 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001421 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001422
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001423 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001424 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001425 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001426 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001427
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001428 // Save the current values for Block, Succ, and continue and break targets
1429 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001430 save_continue(ContinueTargetBlock),
1431 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001432
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001433 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001434 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001435
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001436 // All breaks should go to the code following the loop.
1437 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001438
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001439 // NULL out Block to force lazy instantiation of blocks for the body.
1440 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001441
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001442 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001443 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001444
Ted Kremenekaf603f72007-08-30 18:39:40 +00001445 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001446 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001447 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001448 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001449 return 0;
1450 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001451
Ted Kremenekd173dc72010-08-17 20:59:56 +00001452 if (!KnownVal.isFalse()) {
1453 // Add an intermediate block between the BodyBlock and the
1454 // ExitConditionBlock to represent the "loop back" transition. Create an
1455 // empty block to represent the transition block for looping back to the
1456 // head of the loop.
1457 // FIXME: Can we do this more efficiently without adding another block?
1458 Block = NULL;
1459 Succ = BodyBlock;
1460 CFGBlock *LoopBackBlock = createBlock();
1461 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001462
Ted Kremenekd173dc72010-08-17 20:59:56 +00001463 // Add the loop body entry as a successor to the condition.
1464 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1465 }
1466 else
1467 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001468 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001469
Ted Kremenek941fde82009-07-24 04:47:11 +00001470 // Link up the condition block with the code that follows the loop.
1471 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001472 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001473
1474 // There can be no more statements in the body block(s) since we loop back to
1475 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001476 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001477
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001478 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001479 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001480 return BodyBlock;
1481}
1482
1483CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1484 // "continue" is a control-flow statement. Thus we stop processing the
1485 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001486 if (badCFG)
1487 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001488
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001489 // Now create a new block that ends with the continue statement.
1490 Block = createBlock(false);
1491 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001492
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001493 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001494 // incomplete AST. This means the CFG cannot be constructed.
1495 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001496 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001497 else
1498 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001499
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001500 return Block;
1501}
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001503CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001504 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001505
Ted Kremenek852274d2009-12-16 03:18:58 +00001506 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001507 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001508 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Ted Kremenek4f880632009-07-17 22:18:43 +00001511 // VLA types have expressions that must be evaluated.
1512 if (E->isArgumentType()) {
1513 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1514 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1515 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001516 }
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Mike Stump6d9828c2009-07-17 01:31:16 +00001518 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001519}
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Ted Kremenek4f880632009-07-17 22:18:43 +00001521/// VisitStmtExpr - Utility method to handle (nested) statement
1522/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001523CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1524 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001525 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001526 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001527 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001528 return VisitCompoundStmt(SE->getSubStmt());
1529}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001530
Ted Kremenek411cdee2008-04-16 21:10:48 +00001531CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001532 // "switch" is a control-flow statement. Thus we stop processing the current
1533 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001534 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001535
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001536 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001537 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001538 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001539 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001540 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001541
1542 // Save the current "switch" context.
1543 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001544 save_break(BreakTargetBlock),
1545 save_default(DefaultCaseBlock);
1546
Mike Stump6d9828c2009-07-17 01:31:16 +00001547 // Set the "default" case to be the block after the switch statement. If the
1548 // switch statement contains a "default:", this value will be overwritten with
1549 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001550 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001551
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001552 // Create a new block that will contain the switch statement.
1553 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001554
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001555 // Now process the switch body. The code after the switch is the implicit
1556 // successor.
1557 Succ = SwitchSuccessor;
1558 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001559
1560 // When visiting the body, the case statements should automatically get linked
1561 // up to the switch. We also don't keep a pointer to the body, since all
1562 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001563 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001564 Block = NULL;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001565 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001566 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001567 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001568 return 0;
1569 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001570
Mike Stump6d9828c2009-07-17 01:31:16 +00001571 // If we have no "default:" case, the default transition is to the code
1572 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001573 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001574
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001575 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001576 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001577 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001578 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001579 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001580
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001581 // Finally, if the SwitchStmt contains a condition variable, add both the
1582 // SwitchStmt and the condition variable initialization to the CFG.
1583 if (VarDecl *VD = Terminator->getConditionVariable()) {
1584 if (Expr *Init = VD->getInit()) {
1585 autoCreateBlock();
1586 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1587 addStmt(Init);
1588 }
1589 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001590
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001591 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001592}
1593
Ted Kremenek4f880632009-07-17 22:18:43 +00001594CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001595 // CaseStmts are essentially labels, so they are the first statement in a
1596 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001597 CFGBlock *TopBlock = 0, *LastBlock = 0;
1598
1599 if (Stmt *Sub = CS->getSubStmt()) {
1600 // For deeply nested chains of CaseStmts, instead of doing a recursion
1601 // (which can blow out the stack), manually unroll and create blocks
1602 // along the way.
1603 while (isa<CaseStmt>(Sub)) {
1604 CFGBlock *CurrentBlock = createBlock(false);
1605 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001606
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001607 if (TopBlock)
1608 AddSuccessor(LastBlock, CurrentBlock);
1609 else
1610 TopBlock = CurrentBlock;
1611
1612 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1613 LastBlock = CurrentBlock;
1614
1615 CS = cast<CaseStmt>(Sub);
1616 Sub = CS->getSubStmt();
1617 }
1618
1619 addStmt(Sub);
1620 }
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001622 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001623 if (!CaseBlock)
1624 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001625
1626 // Cases statements partition blocks, so this is the top of the basic block we
1627 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001628 CaseBlock->setLabel(CS);
1629
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001630 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001631 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001632
1633 // Add this block to the list of successors for the block with the switch
1634 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001635 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001636 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001637
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001638 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1639 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001640
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001641 if (TopBlock) {
1642 AddSuccessor(LastBlock, CaseBlock);
1643 Succ = TopBlock;
1644 }
1645 else {
1646 // This block is now the implicit successor of other blocks.
1647 Succ = CaseBlock;
1648 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001649
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001650 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001651}
Mike Stump6d9828c2009-07-17 01:31:16 +00001652
Ted Kremenek411cdee2008-04-16 21:10:48 +00001653CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001654 if (Terminator->getSubStmt())
1655 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001657 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001658
1659 if (!DefaultCaseBlock)
1660 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001661
1662 // Default statements partition blocks, so this is the top of the basic block
1663 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001664 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001666 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001667 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001668
Mike Stump6d9828c2009-07-17 01:31:16 +00001669 // Unlike case statements, we don't add the default block to the successors
1670 // for the switch statement immediately. This is done when we finish
1671 // processing the switch statement. This allows for the default case
1672 // (including a fall-through to the code after the switch statement) to always
1673 // be the last successor of a switch-terminated block.
1674
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001675 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1676 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001677
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001678 // This block is now the implicit successor of other blocks.
1679 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001680
1681 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001682}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001683
Mike Stump5d1d2022010-01-19 02:20:09 +00001684CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1685 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1686 // current block.
1687 CFGBlock* TrySuccessor = NULL;
1688
1689 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001690 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00001691 return 0;
1692 TrySuccessor = Block;
1693 } else TrySuccessor = Succ;
1694
Mike Stumpa1f93632010-01-20 01:15:34 +00001695 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001696
1697 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001698 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001699 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001700 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001701
Mike Stumpa1f93632010-01-20 01:15:34 +00001702 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001703 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1704 // The code after the try is the implicit successor.
1705 Succ = TrySuccessor;
1706 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001707 if (CS->getExceptionDecl() == 0) {
1708 HasCatchAll = true;
1709 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001710 Block = NULL;
1711 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1712 if (CatchBlock == 0)
1713 return 0;
1714 // Add this block to the list of successors for the block with the try
1715 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001716 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001717 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001718 if (!HasCatchAll) {
1719 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001720 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001721 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001722 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001723 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001724
1725 // The code after the try is the implicit successor.
1726 Succ = TrySuccessor;
1727
Mike Stumpf00cca52010-01-20 01:30:58 +00001728 // Save the current "try" context.
1729 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1730 TryTerminatedBlock = NewTryTerminatedBlock;
1731
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001732 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001733 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001734 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001735 return Block;
1736}
1737
1738CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1739 // CXXCatchStmt are treated like labels, so they are the first statement in a
1740 // block.
1741
1742 if (CS->getHandlerBlock())
1743 addStmt(CS->getHandlerBlock());
1744
1745 CFGBlock* CatchBlock = Block;
1746 if (!CatchBlock)
1747 CatchBlock = createBlock();
1748
1749 CatchBlock->setLabel(CS);
1750
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001751 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00001752 return 0;
1753
1754 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1755 Block = NULL;
1756
1757 return CatchBlock;
1758}
1759
Ted Kremenekad5a8942010-08-02 23:46:59 +00001760CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001761 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001762 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001763 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001764 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001765 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001766 return VisitChildren(C);
1767}
1768
Ted Kremenek19bb3562007-08-28 19:26:49 +00001769CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001770 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001771 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001772
Ted Kremenek19bb3562007-08-28 19:26:49 +00001773 if (!IBlock) {
1774 IBlock = createBlock(false);
1775 cfg->setIndirectGotoBlock(IBlock);
1776 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001777
Ted Kremenek19bb3562007-08-28 19:26:49 +00001778 // IndirectGoto is a control-flow statement. Thus we stop processing the
1779 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001780 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001781 return 0;
1782
Ted Kremenek19bb3562007-08-28 19:26:49 +00001783 Block = createBlock(false);
1784 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001785 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001786 return addStmt(I->getTarget());
1787}
1788
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001789} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001790
Mike Stump6d9828c2009-07-17 01:31:16 +00001791/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1792/// no successors or predecessors. If this is the first block created in the
1793/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001794CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001795 bool first_block = begin() == end();
1796
1797 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001798 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1799 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1800 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001801
1802 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001803 if (first_block)
1804 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001805
1806 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001807 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001808}
1809
Ted Kremenek026473c2007-08-23 16:51:22 +00001810/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1811/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001812CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekad5a8942010-08-02 23:46:59 +00001813 bool PruneTriviallyFalse,
Ted Kremenekfe255bc2010-09-13 22:25:54 +00001814 bool AddEHEdges) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001815 CFGBuilder Builder;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001816 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
Ted Kremenekfe255bc2010-09-13 22:25:54 +00001817 AddEHEdges);
Ted Kremenek026473c2007-08-23 16:51:22 +00001818}
1819
Ted Kremenek63f58872007-10-01 19:33:33 +00001820//===----------------------------------------------------------------------===//
1821// CFG: Queries for BlkExprs.
1822//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001823
Ted Kremenek63f58872007-10-01 19:33:33 +00001824namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001825 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001826}
1827
Ted Kremenek8a693662009-12-23 23:37:10 +00001828static void FindSubExprAssignments(Stmt *S,
1829 llvm::SmallPtrSet<Expr*,50>& Set) {
1830 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001831 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001832
Ted Kremenek8a693662009-12-23 23:37:10 +00001833 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001834 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00001835 if (!child)
1836 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001837
Ted Kremenek8a693662009-12-23 23:37:10 +00001838 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001839 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001840
Ted Kremenek8a693662009-12-23 23:37:10 +00001841 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001842 }
1843}
1844
Ted Kremenek63f58872007-10-01 19:33:33 +00001845static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1846 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001847
1848 // Look for assignments that are used as subexpressions. These are the only
1849 // assignments that we want to *possibly* register as a block-level
1850 // expression. Basically, if an assignment occurs both in a subexpression and
1851 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001852 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001853
Ted Kremenek63f58872007-10-01 19:33:33 +00001854 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001855 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001856 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001857
Ted Kremenek411cdee2008-04-16 21:10:48 +00001858 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001859
1860 // Iterate over the statements again on identify the Expr* and Stmt* at the
1861 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001862
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001863 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001864 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001865
Ted Kremenek411cdee2008-04-16 21:10:48 +00001866 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001867 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001868 // expression are really "statements" whose value is never used by
1869 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001870 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001871 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001872 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1873 // Special handling for statement expressions. The last statement in
1874 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001875 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001876 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001877 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001878 (*M)[C->body_back()] = x;
1879 }
1880 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001881
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001882 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001883 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001884 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001885
Ted Kremenek411cdee2008-04-16 21:10:48 +00001886 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001887
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001888 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001889
Ted Kremenek390e48b2008-11-12 21:11:49 +00001890 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001891 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001892 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001893 }
1894 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001895
Ted Kremenek63f58872007-10-01 19:33:33 +00001896 return M;
1897}
1898
Ted Kremenek86946742008-01-17 20:48:37 +00001899CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1900 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001901 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001902
Ted Kremenek63f58872007-10-01 19:33:33 +00001903 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001904 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001905 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001906}
1907
1908unsigned CFG::getNumBlkExprs() {
1909 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1910 return M->size();
1911 else {
1912 // We assume callers interested in the number of BlkExprs will want
1913 // the map constructed if it doesn't already exist.
1914 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1915 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1916 }
1917}
1918
Ted Kremenek274f4332008-04-28 18:00:46 +00001919//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001920// Filtered walking of the CFG.
1921//===----------------------------------------------------------------------===//
1922
1923bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00001924 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001925
1926 if (F.IgnoreDefaultsWithCoveredEnums) {
1927 // If the 'To' has no label or is labeled but the label isn't a
1928 // CaseStmt then filter this edge.
1929 if (const SwitchStmt *S =
Ted Kremenekbe39a562010-09-09 02:57:48 +00001930 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001931 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00001932 const Stmt *L = To->getLabel();
1933 if (!L || !isa<CaseStmt>(L))
1934 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001935 }
1936 }
1937 }
1938
1939 return false;
1940}
1941
1942//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001943// Cleanup: CFG dstor.
1944//===----------------------------------------------------------------------===//
1945
Ted Kremenek63f58872007-10-01 19:33:33 +00001946CFG::~CFG() {
1947 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1948}
Mike Stump6d9828c2009-07-17 01:31:16 +00001949
Ted Kremenek7dba8602007-08-29 21:56:09 +00001950//===----------------------------------------------------------------------===//
1951// CFG pretty printing
1952//===----------------------------------------------------------------------===//
1953
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001954namespace {
1955
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001956class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001957 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1958 StmtMapTy StmtMap;
1959 signed CurrentBlock;
1960 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001961 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001962public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001963
Chris Lattnere4f21422009-06-30 01:26:17 +00001964 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1965 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001966 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1967 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001968 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001969 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001970 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001971 }
1972 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001973
Ted Kremenek42a509f2007-08-31 21:30:12 +00001974 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001975
Chris Lattnere4f21422009-06-30 01:26:17 +00001976 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001977 void setBlockID(signed i) { CurrentBlock = i; }
1978 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001979
Ted Kremeneka95d3752008-09-13 05:16:45 +00001980 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001981
Ted Kremenek411cdee2008-04-16 21:10:48 +00001982 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001983
1984 if (I == StmtMap.end())
1985 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001986
1987 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001988 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001989 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001990 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001991
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001992 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001993 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001994 }
1995};
Chris Lattnere4f21422009-06-30 01:26:17 +00001996} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001997
Chris Lattnere4f21422009-06-30 01:26:17 +00001998
1999namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00002000class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00002001 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00002002
Ted Kremeneka95d3752008-09-13 05:16:45 +00002003 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002004 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002005 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002006public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002007 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002008 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002009 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002010
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002011 void VisitIfStmt(IfStmt* I) {
2012 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002013 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002014 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002015
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002016 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002017 void VisitStmt(Stmt* Terminator) {
2018 Terminator->printPretty(OS, Helper, Policy);
2019 }
2020
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002021 void VisitForStmt(ForStmt* F) {
2022 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002023 if (F->getInit())
2024 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002025 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002026 if (Stmt* C = F->getCond())
2027 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002028 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002029 if (F->getInc())
2030 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002031 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002032 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002033
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002034 void VisitWhileStmt(WhileStmt* W) {
2035 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002036 if (Stmt* C = W->getCond())
2037 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002038 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002039
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002040 void VisitDoStmt(DoStmt* D) {
2041 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002042 if (Stmt* C = D->getCond())
2043 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002044 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002045
Ted Kremenek411cdee2008-04-16 21:10:48 +00002046 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002047 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002048 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002049 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002050
Mike Stump5d1d2022010-01-19 02:20:09 +00002051 void VisitCXXTryStmt(CXXTryStmt* CS) {
2052 OS << "try ...";
2053 }
2054
Ted Kremenek805e9a82007-08-31 21:49:40 +00002055 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002056 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002057 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002058 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002059
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002060 void VisitChooseExpr(ChooseExpr* C) {
2061 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002062 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002063 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002064 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002065
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002066 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2067 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002068 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002069 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002070
Ted Kremenek805e9a82007-08-31 21:49:40 +00002071 void VisitBinaryOperator(BinaryOperator* B) {
2072 if (!B->isLogicalOp()) {
2073 VisitExpr(B);
2074 return;
2075 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002076
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002077 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002078
Ted Kremenek805e9a82007-08-31 21:49:40 +00002079 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002080 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002081 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002082 return;
John McCall2de56d12010-08-25 11:45:40 +00002083 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002084 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002085 return;
2086 default:
2087 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002088 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002089 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002090
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002091 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002092 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002093 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002094};
Chris Lattnere4f21422009-06-30 01:26:17 +00002095} // end anonymous namespace
2096
Mike Stump6d9828c2009-07-17 01:31:16 +00002097
Chris Lattnere4f21422009-06-30 01:26:17 +00002098static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002099 const CFGElement &E) {
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002100 Stmt *S = E;
2101
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002102 if (Helper) {
2103 // special printing for statement-expressions.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002104 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002105 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002106
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002107 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002108 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002109 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002110 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002111 return;
2112 }
2113 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002114
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002115 // special printing for comma expressions.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002116 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +00002117 if (B->getOpcode() == BO_Comma) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002118 OS << "... , ";
2119 Helper->handledStmt(B->getRHS(),OS);
2120 OS << '\n';
2121 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002122 }
2123 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002124 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002125
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002126 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2127
2128 if (isa<CXXOperatorCallExpr>(S)) {
2129 OS << " (OperatorCall)";
2130 }
2131 else if (isa<CXXBindTemporaryExpr>(S)) {
2132 OS << " (BindTemporary)";
2133 }
2134
Mike Stump6d9828c2009-07-17 01:31:16 +00002135
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002136 // Expressions need a newline.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002137 if (isa<Expr>(S))
2138 OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002139}
Mike Stump6d9828c2009-07-17 01:31:16 +00002140
Chris Lattnere4f21422009-06-30 01:26:17 +00002141static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2142 const CFGBlock& B,
2143 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002144
Ted Kremenek42a509f2007-08-31 21:30:12 +00002145 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002146
Ted Kremenek7dba8602007-08-29 21:56:09 +00002147 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002148 OS << "\n [ B" << B.getBlockID();
2149
Ted Kremenek42a509f2007-08-31 21:30:12 +00002150 if (&B == &cfg->getEntry())
2151 OS << " (ENTRY) ]\n";
2152 else if (&B == &cfg->getExit())
2153 OS << " (EXIT) ]\n";
2154 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002155 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002156 else
2157 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002158
Ted Kremenek9cffe732007-08-29 23:20:49 +00002159 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002160 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002161
2162 if (print_edges)
2163 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002164
Mike Stump079bd722010-01-19 22:00:14 +00002165 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002166 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002167 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002168 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002169 C->getLHS()->printPretty(OS, Helper,
2170 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002171 if (C->getRHS()) {
2172 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002173 C->getRHS()->printPretty(OS, Helper,
2174 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002175 }
Mike Stump079bd722010-01-19 22:00:14 +00002176 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002177 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002178 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002179 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002180 if (CS->getExceptionDecl())
2181 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2182 0);
2183 else
2184 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002185 OS << ")";
2186
2187 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002188 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002189
Ted Kremenek9cffe732007-08-29 23:20:49 +00002190 OS << ":\n";
2191 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002192
Ted Kremenekfddd5182007-08-21 21:42:03 +00002193 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002194 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002195
Ted Kremenek42a509f2007-08-31 21:30:12 +00002196 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2197 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002198
Ted Kremenek9cffe732007-08-29 23:20:49 +00002199 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002200 if (print_edges)
2201 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002202
Ted Kremeneka95d3752008-09-13 05:16:45 +00002203 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002204
Ted Kremenek42a509f2007-08-31 21:30:12 +00002205 if (Helper)
2206 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002207
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002208 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002209 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002210
Ted Kremenek9cffe732007-08-29 23:20:49 +00002211 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002212 if (B.getTerminator()) {
2213 if (print_edges)
2214 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002215
Ted Kremenek9cffe732007-08-29 23:20:49 +00002216 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002217
Ted Kremenek42a509f2007-08-31 21:30:12 +00002218 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002219
Chris Lattnere4f21422009-06-30 01:26:17 +00002220 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2221 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002222 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002223 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002224 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002225
Ted Kremenek9cffe732007-08-29 23:20:49 +00002226 if (print_edges) {
2227 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002228 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002229 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002230
Ted Kremenek42a509f2007-08-31 21:30:12 +00002231 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2232 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002233
Ted Kremenek42a509f2007-08-31 21:30:12 +00002234 if (i == 8 || (i-8) == 0)
2235 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002236
Ted Kremenek9cffe732007-08-29 23:20:49 +00002237 OS << " B" << (*I)->getBlockID();
2238 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002239
Ted Kremenek42a509f2007-08-31 21:30:12 +00002240 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002241
Ted Kremenek42a509f2007-08-31 21:30:12 +00002242 // Print the successors of this block.
2243 OS << " Successors (" << B.succ_size() << "):";
2244 i = 0;
2245
2246 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2247 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002248
Ted Kremenek42a509f2007-08-31 21:30:12 +00002249 if (i == 8 || (i-8) % 10 == 0)
2250 OS << "\n ";
2251
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002252 if (*I)
2253 OS << " B" << (*I)->getBlockID();
2254 else
2255 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002256 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002257
Ted Kremenek9cffe732007-08-29 23:20:49 +00002258 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002259 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002260}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002261
Ted Kremenek42a509f2007-08-31 21:30:12 +00002262
2263/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002264void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002265
2266/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002267void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2268 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002269
Ted Kremenek42a509f2007-08-31 21:30:12 +00002270 // Print the entry block.
2271 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002272
Ted Kremenek42a509f2007-08-31 21:30:12 +00002273 // Iterate through the CFGBlocks and print them one by one.
2274 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2275 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002276 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002277 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002278
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002279 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002280 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002281
Ted Kremenek42a509f2007-08-31 21:30:12 +00002282 // Print the exit block.
2283 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002284 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002285}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002286
2287/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002288void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2289 print(llvm::errs(), cfg, LO);
2290}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002291
2292/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2293/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002294void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2295 const LangOptions &LO) const {
2296 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002297 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002298}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002299
Ted Kremeneka2925852008-01-30 23:02:42 +00002300/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002301void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002302 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002303 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002304 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2305}
2306
Ted Kremenek390e48b2008-11-12 21:11:49 +00002307Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002308
Ted Kremenek411cdee2008-04-16 21:10:48 +00002309 if (!Terminator)
2310 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002311
Ted Kremenek411cdee2008-04-16 21:10:48 +00002312 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002313
Ted Kremenek411cdee2008-04-16 21:10:48 +00002314 switch (Terminator->getStmtClass()) {
2315 default:
2316 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002317
Ted Kremenek411cdee2008-04-16 21:10:48 +00002318 case Stmt::ForStmtClass:
2319 E = cast<ForStmt>(Terminator)->getCond();
2320 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002321
Ted Kremenek411cdee2008-04-16 21:10:48 +00002322 case Stmt::WhileStmtClass:
2323 E = cast<WhileStmt>(Terminator)->getCond();
2324 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002325
Ted Kremenek411cdee2008-04-16 21:10:48 +00002326 case Stmt::DoStmtClass:
2327 E = cast<DoStmt>(Terminator)->getCond();
2328 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002329
Ted Kremenek411cdee2008-04-16 21:10:48 +00002330 case Stmt::IfStmtClass:
2331 E = cast<IfStmt>(Terminator)->getCond();
2332 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002333
Ted Kremenek411cdee2008-04-16 21:10:48 +00002334 case Stmt::ChooseExprClass:
2335 E = cast<ChooseExpr>(Terminator)->getCond();
2336 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002337
Ted Kremenek411cdee2008-04-16 21:10:48 +00002338 case Stmt::IndirectGotoStmtClass:
2339 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2340 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002341
Ted Kremenek411cdee2008-04-16 21:10:48 +00002342 case Stmt::SwitchStmtClass:
2343 E = cast<SwitchStmt>(Terminator)->getCond();
2344 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002345
Ted Kremenek411cdee2008-04-16 21:10:48 +00002346 case Stmt::ConditionalOperatorClass:
2347 E = cast<ConditionalOperator>(Terminator)->getCond();
2348 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002349
Ted Kremenek411cdee2008-04-16 21:10:48 +00002350 case Stmt::BinaryOperatorClass: // '&&' and '||'
2351 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002352 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002353
Ted Kremenek390e48b2008-11-12 21:11:49 +00002354 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002355 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002356 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002357
Ted Kremenek411cdee2008-04-16 21:10:48 +00002358 return E ? E->IgnoreParens() : NULL;
2359}
2360
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002361bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002362
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002363 if (!Terminator)
2364 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002365
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002366 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002367
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002368 switch (Terminator->getStmtClass()) {
2369 default:
2370 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002371
2372 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002373 case Stmt::WhileStmtClass:
2374 case Stmt::DoStmtClass:
2375 case Stmt::IfStmtClass:
2376 case Stmt::ChooseExprClass:
2377 case Stmt::ConditionalOperatorClass:
2378 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002379 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002380 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002381
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002382 return E ? E->IgnoreParens() : NULL;
2383}
2384
Ted Kremeneka2925852008-01-30 23:02:42 +00002385
Ted Kremenek7dba8602007-08-29 21:56:09 +00002386//===----------------------------------------------------------------------===//
2387// CFG Graphviz Visualization
2388//===----------------------------------------------------------------------===//
2389
Ted Kremenek42a509f2007-08-31 21:30:12 +00002390
2391#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002392static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002393#endif
2394
Chris Lattnere4f21422009-06-30 01:26:17 +00002395void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002396#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002397 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002398 GraphHelper = &H;
2399 llvm::ViewGraph(this,"CFG");
2400 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002401#endif
2402}
2403
Ted Kremenek7dba8602007-08-29 21:56:09 +00002404namespace llvm {
2405template<>
2406struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002407
2408 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2409
2410 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002411
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002412#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002413 std::string OutSStr;
2414 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002415 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002416 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002417
2418 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2419
2420 // Process string output to make it nicer...
2421 for (unsigned i = 0; i != OutStr.length(); ++i)
2422 if (OutStr[i] == '\n') { // Left justify
2423 OutStr[i] = '\\';
2424 OutStr.insert(OutStr.begin()+i+1, 'l');
2425 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002426
Ted Kremenek7dba8602007-08-29 21:56:09 +00002427 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002428#else
2429 return "";
2430#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002431 }
2432};
2433} // end namespace llvm