blob: 02ff218851989ba8f164f70767800623c923b20d [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
Zhongxing Xufc61d942010-06-03 06:23:18 +0000528 Visit(B->getRHS());
529 return Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Ted Kremenek852274d2009-12-16 03:18:58 +0000532 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000533}
534
Ted Kremenek852274d2009-12-16 03:18:58 +0000535CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
536 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000537 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000538 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000539 }
540 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000541}
542
Ted Kremenek4f880632009-07-17 22:18:43 +0000543CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
544 // "break" is a control-flow statement. Thus we stop processing the current
545 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000546 if (badCFG)
547 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Ted Kremenek4f880632009-07-17 22:18:43 +0000549 // Now create a new block that ends with the break statement.
550 Block = createBlock(false);
551 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Ted Kremenek4f880632009-07-17 22:18:43 +0000553 // If there is no target for the break, then we are looking at an incomplete
554 // AST. This means that the CFG cannot be constructed.
555 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000556 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000557 else
558 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000559
560
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000561 return Block;
562}
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Mike Stump4c45aa12010-01-21 15:20:48 +0000564static bool CanThrow(Expr *E) {
565 QualType Ty = E->getType();
566 if (Ty->isFunctionPointerType())
567 Ty = Ty->getAs<PointerType>()->getPointeeType();
568 else if (Ty->isBlockPointerType())
569 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +0000570
Mike Stump4c45aa12010-01-21 15:20:48 +0000571 const FunctionType *FT = Ty->getAs<FunctionType>();
572 if (FT) {
573 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
574 if (Proto->hasEmptyExceptionSpec())
575 return false;
576 }
577 return true;
578}
579
Ted Kremenek852274d2009-12-16 03:18:58 +0000580CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000581 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000582 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000583 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000584 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000585 }
Mike Stump24556362009-07-25 21:26:53 +0000586
Mike Stump4c45aa12010-01-21 15:20:48 +0000587 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000588
589 // Languages without exceptions are assumed to not throw.
590 if (Context->getLangOptions().Exceptions) {
Mike Stump4c45aa12010-01-21 15:20:48 +0000591 if (AddEHEdges)
592 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000593 }
594
595 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000596 if (FD->hasAttr<NoReturnAttr>())
597 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000598 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000599 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000600 }
Mike Stump24556362009-07-25 21:26:53 +0000601
Mike Stump4c45aa12010-01-21 15:20:48 +0000602 if (!CanThrow(C->getCallee()))
603 AddEHEdge = false;
604
Zhongxing Xufc61d942010-06-03 06:23:18 +0000605 if (!NoReturn && !AddEHEdge) {
606 if (asc.asLValue())
607 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
608 else
609 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Mike Stump079bd722010-01-19 22:00:14 +0000612 if (Block) {
613 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000614 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +0000615 return 0;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Mike Stump079bd722010-01-19 22:00:14 +0000618 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000619 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000620
Mike Stump079bd722010-01-19 22:00:14 +0000621 if (NoReturn) {
622 // Wire this to the exit block directly.
623 AddSuccessor(Block, &cfg->getExit());
624 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000625 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000626 // Add exceptional edges.
627 if (TryTerminatedBlock)
628 AddSuccessor(Block, TryTerminatedBlock);
629 else
630 AddSuccessor(Block, &cfg->getExit());
631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Mike Stump24556362009-07-25 21:26:53 +0000633 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000634}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000635
Ted Kremenek852274d2009-12-16 03:18:58 +0000636CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
637 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000638 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000639 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000640 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000641 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Ted Kremenek115c1b92010-04-11 17:02:10 +0000643 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
644 : AddStmtChoice::AlwaysAdd;
645
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000646 Succ = ConfluenceBlock;
647 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000648 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000649 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000650 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000652 Succ = ConfluenceBlock;
653 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000654 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000655 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000656 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000658 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000659 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000660 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000661 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
662 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000663 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000664 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000665}
Mike Stump1eb44332009-09-09 15:08:12 +0000666
667
668CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
669 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000670
671 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
672 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +0000673 // If we hit a segment of code just containing ';' (NullStmts), we can
674 // get a null block back. In such cases, just use the LastBlock
675 if (CFGBlock *newBlock = addStmt(*I))
676 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000678 if (badCFG)
679 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000680 }
Mike Stump079bd722010-01-19 22:00:14 +0000681
Ted Kremenek4f880632009-07-17 22:18:43 +0000682 return LastBlock;
683}
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremenek852274d2009-12-16 03:18:58 +0000685CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
686 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000687 // Create the confluence block that will "merge" the results of the ternary
688 // expression.
689 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000690 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000691 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000692 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek115c1b92010-04-11 17:02:10 +0000694 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
695 : AddStmtChoice::AlwaysAdd;
696
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000697 // Create a block for the LHS expression if there is an LHS expression. A
698 // GCC extension allows LHS to be NULL, causing the condition to be the
699 // value that is returned instead.
700 // e.g: x ?: y is shorthand for: x ? x : y;
701 Succ = ConfluenceBlock;
702 Block = NULL;
703 CFGBlock* LHSBlock = NULL;
704 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +0000705 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000706 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000707 return 0;
708 Block = NULL;
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000711 // Create the block for the RHS expression.
712 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +0000713 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000714 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000715 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000717 // Create the block that will contain the condition.
718 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Mike Stump00998a02009-07-23 23:25:26 +0000720 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000721 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000722 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000723 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000724 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000725 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000726 // If we know the condition is false, add NULL as the successor for
727 // the block containing the condition. In this case, the confluence
728 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000729 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000730 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000731 } else {
732 // If we have no LHS expression, add the ConfluenceBlock as a direct
733 // successor for the block containing the condition. Moreover, we need to
734 // reverse the order of the predecessors in the ConfluenceBlock because
735 // the RHSBlock will have been added to the succcessors already, and we
736 // want the first predecessor to the the block containing the expression
737 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000738 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000739 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000740 std::reverse(ConfluenceBlock->pred_begin(),
741 ConfluenceBlock->pred_end());
742 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000745 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000746 Block->setTerminator(C);
747 return addStmt(C->getCond());
748}
749
Ted Kremenek4f880632009-07-17 22:18:43 +0000750CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
751 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000752
Ted Kremenek4f880632009-07-17 22:18:43 +0000753 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000754 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000755 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek4f880632009-07-17 22:18:43 +0000758 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek4f880632009-07-17 22:18:43 +0000760 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
761 typedef llvm::SmallVector<Decl*,10> BufTy;
762 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek4f880632009-07-17 22:18:43 +0000764 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
765 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
766 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
767 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenek4f880632009-07-17 22:18:43 +0000769 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
770 // automatically freed with the CFG.
771 DeclGroupRef DG(*I);
772 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000773 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000774 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek4f880632009-07-17 22:18:43 +0000776 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000777 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000778 B = VisitDeclSubExpr(D);
779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
781 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000782}
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Ted Kremenek4f880632009-07-17 22:18:43 +0000784/// VisitDeclSubExpr - Utility method to add block-level expressions for
785/// initializers in Decls.
786CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
787 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000788
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek4f880632009-07-17 22:18:43 +0000791 if (!VD)
792 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek4f880632009-07-17 22:18:43 +0000794 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Ted Kremenek4f880632009-07-17 22:18:43 +0000796 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000797 AddStmtChoice::Kind k =
798 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
799 : AddStmtChoice::NotAlwaysAdd;
800 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenek4f880632009-07-17 22:18:43 +0000803 // If the type of VD is a VLA, then we must process its size expressions.
804 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
805 VA = FindVA(VA->getElementType().getTypePtr()))
806 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek4f880632009-07-17 22:18:43 +0000808 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000809}
810
811CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000812 // We may see an if statement in the middle of a basic block, or it may be the
813 // first statement we are processing. In either case, we create a new basic
814 // block. First, we create the blocks for the then...else statements, and
815 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000816 // middle of a block, we stop processing that block. That block is then the
817 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000818
819 // The block we were proccessing is now finished. Make it the successor
820 // block.
821 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000822 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000823 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000824 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000825 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000826
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000827 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000828 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000829
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000830 if (Stmt* Else = I->getElse()) {
831 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000832
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000833 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000834 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000835 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000836 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000837
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000838 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
839 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000840 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000841 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000842 return 0;
843 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000844 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000845
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000846 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000847 CFGBlock* ThenBlock;
848 {
849 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000850 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000851 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000852 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000853 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000854
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000855 if (!ThenBlock) {
856 // We can reach here if the "then" body has all NullStmts.
857 // Create an empty block so we can distinguish between true and false
858 // branches in path-sensitive analyses.
859 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000860 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000861 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000862 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000863 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000864 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000865 }
866
Mike Stump6d9828c2009-07-17 01:31:16 +0000867 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000868 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000869
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000870 // Set the terminator of the new block to the If statement.
871 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000872
Mike Stump00998a02009-07-23 23:25:26 +0000873 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000874 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000875
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000876 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000877 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
878 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000879
880 // Add the condition as the last statement in the new block. This may create
881 // new blocks as the condition may contain control-flow. Any newly created
882 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000883 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +0000884
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000885 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
886 // and the condition variable initialization to the CFG.
887 if (VarDecl *VD = I->getConditionVariable()) {
888 if (Expr *Init = VD->getInit()) {
889 autoCreateBlock();
890 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
891 addStmt(Init);
892 }
893 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000894
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000895 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000896}
Mike Stump6d9828c2009-07-17 01:31:16 +0000897
898
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000899CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000900 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000901 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000902 // NOTE: If a "return" appears in the middle of a block, this means that the
903 // code afterwards is DEAD (unreachable). We still keep a basic block
904 // for that code; a simple "mark-and-sweep" from the entry block will be
905 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000906
907 // Create the new block.
908 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000909
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000910 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000911 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000912
913 // Add the return statement to the block. This may create new blocks if R
914 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000915 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000916}
917
918CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
919 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000920 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000921 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000922
Ted Kremenek4f880632009-07-17 22:18:43 +0000923 if (!LabelBlock) // This can happen when the body is empty, i.e.
924 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000925
Ted Kremenek4f880632009-07-17 22:18:43 +0000926 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000927 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000928
929 // Labels partition blocks, so this is the end of the basic block we were
930 // processing (L is the block's label). Because this is label (and we have
931 // already processed the substatement) there is no extra control-flow to worry
932 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000933 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000934 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000935 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000936
937 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000938 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000939
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000940 // This block is now the implicit successor of other blocks.
941 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000942
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000943 return LabelBlock;
944}
945
946CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000947 // Goto is a control-flow statement. Thus we stop processing the current
948 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000949
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000950 Block = createBlock(false);
951 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000952
953 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000954 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000955
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000956 if (I == LabelMap.end())
957 // We will need to backpatch this block later.
958 BackpatchBlocks.push_back(Block);
959 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000960 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000961
Mike Stump6d9828c2009-07-17 01:31:16 +0000962 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000963}
964
965CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000966 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000967
Mike Stumpfefb9f72009-07-21 01:12:51 +0000968 // "for" is a control-flow statement. Thus we stop processing the current
969 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000970 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000971 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000972 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000973 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000974 } else
975 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000976
Ted Kremenek3f64a0e2010-05-21 20:30:15 +0000977 // Save the current value for the break targets.
978 // All breaks should go to the code following the loop.
979 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
980 BreakTargetBlock = LoopSuccessor;
981
Mike Stump6d9828c2009-07-17 01:31:16 +0000982 // Because of short-circuit evaluation, the condition of the loop can span
983 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
984 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000985 CFGBlock* ExitConditionBlock = createBlock(false);
986 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000987
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000988 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000989 ExitConditionBlock->setTerminator(F);
990
991 // Now add the actual condition to the condition block. Because the condition
992 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000993 if (Stmt* C = F->getCond()) {
994 Block = ExitConditionBlock;
995 EntryConditionBlock = addStmt(C);
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000996 assert(Block == EntryConditionBlock);
997
998 // If this block contains a condition variable, add both the condition
999 // variable and initializer to the CFG.
1000 if (VarDecl *VD = F->getConditionVariable()) {
1001 if (Expr *Init = VD->getInit()) {
1002 autoCreateBlock();
1003 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1004 EntryConditionBlock = addStmt(Init);
1005 assert(Block == EntryConditionBlock);
1006 }
1007 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001008
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001009 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001010 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001011 return 0;
1012 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001013 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001014
Mike Stump6d9828c2009-07-17 01:31:16 +00001015 // The condition block is the implicit successor for the loop body as well as
1016 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001017 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001018
Mike Stump00998a02009-07-23 23:25:26 +00001019 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001020 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Mike Stump00998a02009-07-23 23:25:26 +00001022 if (F->getCond())
1023 KnownVal = TryEvaluateBool(F->getCond());
1024
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001025 // Now create the loop body.
1026 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001027 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001028
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001029 // Save the current values for Block, Succ, and continue targets.
1030 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1031 save_continue(ContinueTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001032
Ted Kremenekaf603f72007-08-30 18:39:40 +00001033 // Create a new block to contain the (bottom) of the loop body.
1034 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001035
Ted Kremeneke9334502008-09-04 21:48:47 +00001036 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001037 // Generate increment code in its own basic block. This is the target of
1038 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001039 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001040 } else {
1041 // No increment code. Create a special, empty, block that is used as the
1042 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001043 assert(Succ == EntryConditionBlock);
1044 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001045 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001046
Ted Kremenek3575f842009-04-28 00:51:56 +00001047 // Finish up the increment (or empty) block if it hasn't been already.
1048 if (Block) {
1049 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001050 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001051 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001052 Block = 0;
1053 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001054
Ted Kremenek3575f842009-04-28 00:51:56 +00001055 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001056
Ted Kremenek3575f842009-04-28 00:51:56 +00001057 // The starting block for the loop increment is the block that should
1058 // represent the 'loop target' for looping back to the start of the loop.
1059 ContinueTargetBlock->setLoopTarget(F);
1060
Mike Stump6d9828c2009-07-17 01:31:16 +00001061 // Now populate the body block, and in the process create new blocks as we
1062 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001063 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001064
1065 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001066 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001067 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001068 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001069
Ted Kremenek941fde82009-07-24 04:47:11 +00001070 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001071 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001072 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001073
Ted Kremenek941fde82009-07-24 04:47:11 +00001074 // Link up the condition block with the code that follows the loop. (the
1075 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001076 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001077
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001078 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001079 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001080 if (Stmt* I = F->getInit()) {
1081 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001082 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001083 } else {
1084 // There is no loop initialization. We are thus basically a while loop.
1085 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001086 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001087 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001088 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001089 }
1090}
1091
Ted Kremenek115c1b92010-04-11 17:02:10 +00001092CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1093 if (asc.alwaysAdd()) {
1094 autoCreateBlock();
1095 AppendStmt(Block, M, asc);
1096 }
1097 return Visit(M->getBase(),
1098 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1099 : AddStmtChoice::AsLValueNotAlwaysAdd);
1100}
1101
Ted Kremenek514de5a2008-11-11 17:10:00 +00001102CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1103 // Objective-C fast enumeration 'for' statements:
1104 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1105 //
1106 // for ( Type newVariable in collection_expression ) { statements }
1107 //
1108 // becomes:
1109 //
1110 // prologue:
1111 // 1. collection_expression
1112 // T. jump to loop_entry
1113 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001114 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001115 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1116 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1117 // TB:
1118 // statements
1119 // T. jump to loop_entry
1120 // FB:
1121 // what comes after
1122 //
1123 // and
1124 //
1125 // Type existingItem;
1126 // for ( existingItem in expression ) { statements }
1127 //
1128 // becomes:
1129 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001130 // the same with newVariable replaced with existingItem; the binding works
1131 // the same except that for one ObjCForCollectionStmt::getElement() returns
1132 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001133 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001134
Ted Kremenek514de5a2008-11-11 17:10:00 +00001135 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001136
Ted Kremenek514de5a2008-11-11 17:10:00 +00001137 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001138 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001139 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001140 LoopSuccessor = Block;
1141 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001142 } else
1143 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001144
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001145 // Build the condition blocks.
1146 CFGBlock* ExitConditionBlock = createBlock(false);
1147 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001148
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001149 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001150 ExitConditionBlock->setTerminator(S);
1151
1152 // The last statement in the block should be the ObjCForCollectionStmt, which
1153 // performs the actual binding to 'element' and determines if there are any
1154 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001155 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001156 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001157
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001158 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001159 // generate new blocks as necesary. We DON'T add the statement by default to
1160 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001161 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001162 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001163 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001164 return 0;
1165 Block = 0;
1166 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001167
1168 // The condition block is the implicit successor for the loop body as well as
1169 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001170 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001171
Ted Kremenek514de5a2008-11-11 17:10:00 +00001172 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001173 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001174 // Save the current values for Succ, continue and break targets.
1175 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001176 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1177
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001178 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001179 ContinueTargetBlock = EntryConditionBlock;
1180
Ted Kremenek4f880632009-07-17 22:18:43 +00001181 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001182
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001183 if (!BodyBlock)
1184 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001185 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001186 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001187 return 0;
1188 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001189
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001190 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001191 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001192 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001193
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001194 // Link up the condition block with the code that follows the loop.
1195 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001196 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001197
Ted Kremenek514de5a2008-11-11 17:10:00 +00001198 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001199 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001200 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001201}
1202
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001203CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1204 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001205
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001206 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001207 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001208
Ted Kremenekda5348e2009-05-05 23:11:51 +00001209 // The sync body starts its own basic block. This makes it a little easier
1210 // for diagnostic clients.
1211 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001212 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001213 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001214
Ted Kremenekda5348e2009-05-05 23:11:51 +00001215 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001216 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001217 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001218
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001219 // Add the @synchronized to the CFG.
1220 autoCreateBlock();
1221 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1222
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001223 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001224 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001225}
Mike Stump6d9828c2009-07-17 01:31:16 +00001226
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001227CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001228 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001229 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001230}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001231
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001232CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001233 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001234
Mike Stumpfefb9f72009-07-21 01:12:51 +00001235 // "while" is a control-flow statement. Thus we stop processing the current
1236 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001237 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001238 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001239 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001240 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001241 } else
1242 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001243
1244 // Because of short-circuit evaluation, the condition of the loop can span
1245 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1246 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001247 CFGBlock* ExitConditionBlock = createBlock(false);
1248 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001249
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001250 // Set the terminator for the "exit" condition block.
1251 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001252
1253 // Now add the actual condition to the condition block. Because the condition
1254 // itself may contain control-flow, new blocks may be created. Thus we update
1255 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001256 if (Stmt* C = W->getCond()) {
1257 Block = ExitConditionBlock;
1258 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001259 assert(Block == EntryConditionBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001260
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001261 // If this block contains a condition variable, add both the condition
1262 // variable and initializer to the CFG.
1263 if (VarDecl *VD = W->getConditionVariable()) {
1264 if (Expr *Init = VD->getInit()) {
1265 autoCreateBlock();
1266 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1267 EntryConditionBlock = addStmt(Init);
1268 assert(Block == EntryConditionBlock);
1269 }
1270 }
1271
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001272 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001273 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001274 return 0;
1275 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001276 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001277
1278 // The condition block is the implicit successor for the loop body as well as
1279 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001280 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001281
Mike Stump00998a02009-07-23 23:25:26 +00001282 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001283 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001284
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001285 // Process the loop body.
1286 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001287 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001288
1289 // Save the current values for Block, Succ, and continue and break targets
1290 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1291 save_continue(ContinueTargetBlock),
1292 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001293
Mike Stump6d9828c2009-07-17 01:31:16 +00001294 // Create an empty block to represent the transition block for looping back
1295 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001296 Block = 0;
1297 assert(Succ == EntryConditionBlock);
1298 Succ = createBlock();
1299 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001300 ContinueTargetBlock = Succ;
1301
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001302 // All breaks should go to the code following the loop.
1303 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001304
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001305 // NULL out Block to force lazy instantiation of blocks for the body.
1306 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001307
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001308 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001309 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001310
Ted Kremenekaf603f72007-08-30 18:39:40 +00001311 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001312 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001313 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001314 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001315 return 0;
1316 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001317
Ted Kremenek941fde82009-07-24 04:47:11 +00001318 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001319 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001320 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001321
Ted Kremenek941fde82009-07-24 04:47:11 +00001322 // Link up the condition block with the code that follows the loop. (the
1323 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001324 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001325
1326 // There can be no more statements in the condition block since we loop back
1327 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001328 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001329
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001330 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001331 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001332 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001333}
Mike Stump1eb44332009-09-09 15:08:12 +00001334
1335
Ted Kremenek4f880632009-07-17 22:18:43 +00001336CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1337 // FIXME: For now we pretend that @catch and the code it contains does not
1338 // exit.
1339 return Block;
1340}
Mike Stump6d9828c2009-07-17 01:31:16 +00001341
Ted Kremenek2fda5042008-12-09 20:20:09 +00001342CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1343 // FIXME: This isn't complete. We basically treat @throw like a return
1344 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001345
Ted Kremenek6c249722009-09-24 18:45:41 +00001346 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001347 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001348 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001349
Ted Kremenek2fda5042008-12-09 20:20:09 +00001350 // Create the new block.
1351 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001352
Ted Kremenek2fda5042008-12-09 20:20:09 +00001353 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001354 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001355
1356 // Add the statement to the block. This may create new blocks if S contains
1357 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001358 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001359}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001360
Mike Stump0979d802009-07-22 22:56:04 +00001361CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001362 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001363 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001364 return 0;
1365
1366 // Create the new block.
1367 Block = createBlock(false);
1368
Mike Stump5d1d2022010-01-19 02:20:09 +00001369 if (TryTerminatedBlock)
1370 // The current try statement is the only successor.
1371 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001372 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001373 // otherwise the Exit block is the only successor.
1374 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001375
1376 // Add the statement to the block. This may create new blocks if S contains
1377 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001378 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001379}
1380
Ted Kremenek4f880632009-07-17 22:18:43 +00001381CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001382 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001383
Mike Stump8f9893a2009-07-21 01:27:50 +00001384 // "do...while" is a control-flow statement. Thus we stop processing the
1385 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001386 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001387 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001388 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001389 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001390 } else
1391 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001392
1393 // Because of short-circuit evaluation, the condition of the loop can span
1394 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1395 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001396 CFGBlock* ExitConditionBlock = createBlock(false);
1397 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001398
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001399 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001400 ExitConditionBlock->setTerminator(D);
1401
1402 // Now add the actual condition to the condition block. Because the condition
1403 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001404 if (Stmt* C = D->getCond()) {
1405 Block = ExitConditionBlock;
1406 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001407 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001408 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001409 return 0;
1410 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001411 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001412
Ted Kremenek54827132008-02-27 07:20:00 +00001413 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001414 Succ = EntryConditionBlock;
1415
Mike Stump00998a02009-07-23 23:25:26 +00001416 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001417 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001418
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001419 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001420 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001421 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001422 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001423
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001424 // Save the current values for Block, Succ, and continue and break targets
1425 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001426 save_continue(ContinueTargetBlock),
1427 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001428
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001429 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001430 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001431
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001432 // All breaks should go to the code following the loop.
1433 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001434
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001435 // NULL out Block to force lazy instantiation of blocks for the body.
1436 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001437
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001438 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001439 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001440
Ted Kremenekaf603f72007-08-30 18:39:40 +00001441 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001442 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001443 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001444 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001445 return 0;
1446 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001447
Ted Kremenekd173dc72010-08-17 20:59:56 +00001448 if (!KnownVal.isFalse()) {
1449 // Add an intermediate block between the BodyBlock and the
1450 // ExitConditionBlock to represent the "loop back" transition. Create an
1451 // empty block to represent the transition block for looping back to the
1452 // head of the loop.
1453 // FIXME: Can we do this more efficiently without adding another block?
1454 Block = NULL;
1455 Succ = BodyBlock;
1456 CFGBlock *LoopBackBlock = createBlock();
1457 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001458
Ted Kremenekd173dc72010-08-17 20:59:56 +00001459 // Add the loop body entry as a successor to the condition.
1460 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1461 }
1462 else
1463 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001464 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001465
Ted Kremenek941fde82009-07-24 04:47:11 +00001466 // Link up the condition block with the code that follows the loop.
1467 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001468 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001469
1470 // There can be no more statements in the body block(s) since we loop back to
1471 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001472 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001473
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001474 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001475 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001476 return BodyBlock;
1477}
1478
1479CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1480 // "continue" is a control-flow statement. Thus we stop processing the
1481 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001482 if (badCFG)
1483 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001484
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001485 // Now create a new block that ends with the continue statement.
1486 Block = createBlock(false);
1487 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001488
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001489 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001490 // incomplete AST. This means the CFG cannot be constructed.
1491 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001492 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001493 else
1494 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001495
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001496 return Block;
1497}
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001499CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001500 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001501
Ted Kremenek852274d2009-12-16 03:18:58 +00001502 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001503 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001504 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001505 }
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Ted Kremenek4f880632009-07-17 22:18:43 +00001507 // VLA types have expressions that must be evaluated.
1508 if (E->isArgumentType()) {
1509 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1510 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1511 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001512 }
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Mike Stump6d9828c2009-07-17 01:31:16 +00001514 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001515}
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Ted Kremenek4f880632009-07-17 22:18:43 +00001517/// VisitStmtExpr - Utility method to handle (nested) statement
1518/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001519CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1520 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001521 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001522 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001523 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001524 return VisitCompoundStmt(SE->getSubStmt());
1525}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001526
Ted Kremenek411cdee2008-04-16 21:10:48 +00001527CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001528 // "switch" is a control-flow statement. Thus we stop processing the current
1529 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001530 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001531
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001532 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001533 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001534 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001535 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001536 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001537
1538 // Save the current "switch" context.
1539 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001540 save_break(BreakTargetBlock),
1541 save_default(DefaultCaseBlock);
1542
Mike Stump6d9828c2009-07-17 01:31:16 +00001543 // Set the "default" case to be the block after the switch statement. If the
1544 // switch statement contains a "default:", this value will be overwritten with
1545 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001546 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001547
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001548 // Create a new block that will contain the switch statement.
1549 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001550
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001551 // Now process the switch body. The code after the switch is the implicit
1552 // successor.
1553 Succ = SwitchSuccessor;
1554 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001555
1556 // When visiting the body, the case statements should automatically get linked
1557 // up to the switch. We also don't keep a pointer to the body, since all
1558 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001559 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001560 Block = NULL;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001561 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001562 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001563 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001564 return 0;
1565 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001566
Mike Stump6d9828c2009-07-17 01:31:16 +00001567 // If we have no "default:" case, the default transition is to the code
1568 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001569 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001570
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001571 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001572 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001573 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001574 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001575 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001576
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001577 // Finally, if the SwitchStmt contains a condition variable, add both the
1578 // SwitchStmt and the condition variable initialization to the CFG.
1579 if (VarDecl *VD = Terminator->getConditionVariable()) {
1580 if (Expr *Init = VD->getInit()) {
1581 autoCreateBlock();
1582 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1583 addStmt(Init);
1584 }
1585 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001586
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001587 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001588}
1589
Ted Kremenek4f880632009-07-17 22:18:43 +00001590CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001591 // CaseStmts are essentially labels, so they are the first statement in a
1592 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001593 CFGBlock *TopBlock = 0, *LastBlock = 0;
1594
1595 if (Stmt *Sub = CS->getSubStmt()) {
1596 // For deeply nested chains of CaseStmts, instead of doing a recursion
1597 // (which can blow out the stack), manually unroll and create blocks
1598 // along the way.
1599 while (isa<CaseStmt>(Sub)) {
1600 CFGBlock *CurrentBlock = createBlock(false);
1601 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001602
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001603 if (TopBlock)
1604 AddSuccessor(LastBlock, CurrentBlock);
1605 else
1606 TopBlock = CurrentBlock;
1607
1608 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1609 LastBlock = CurrentBlock;
1610
1611 CS = cast<CaseStmt>(Sub);
1612 Sub = CS->getSubStmt();
1613 }
1614
1615 addStmt(Sub);
1616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001618 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001619 if (!CaseBlock)
1620 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001621
1622 // Cases statements partition blocks, so this is the top of the basic block we
1623 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001624 CaseBlock->setLabel(CS);
1625
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001626 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001627 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001628
1629 // Add this block to the list of successors for the block with the switch
1630 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001631 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001632 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001633
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001634 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1635 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001636
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001637 if (TopBlock) {
1638 AddSuccessor(LastBlock, CaseBlock);
1639 Succ = TopBlock;
1640 }
1641 else {
1642 // This block is now the implicit successor of other blocks.
1643 Succ = CaseBlock;
1644 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001645
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001646 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001647}
Mike Stump6d9828c2009-07-17 01:31:16 +00001648
Ted Kremenek411cdee2008-04-16 21:10:48 +00001649CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001650 if (Terminator->getSubStmt())
1651 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001653 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001654
1655 if (!DefaultCaseBlock)
1656 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001657
1658 // Default statements partition blocks, so this is the top of the basic block
1659 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001660 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001662 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001663 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001664
Mike Stump6d9828c2009-07-17 01:31:16 +00001665 // Unlike case statements, we don't add the default block to the successors
1666 // for the switch statement immediately. This is done when we finish
1667 // processing the switch statement. This allows for the default case
1668 // (including a fall-through to the code after the switch statement) to always
1669 // be the last successor of a switch-terminated block.
1670
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001671 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1672 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001673
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001674 // This block is now the implicit successor of other blocks.
1675 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001676
1677 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001678}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001679
Mike Stump5d1d2022010-01-19 02:20:09 +00001680CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1681 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1682 // current block.
1683 CFGBlock* TrySuccessor = NULL;
1684
1685 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001686 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00001687 return 0;
1688 TrySuccessor = Block;
1689 } else TrySuccessor = Succ;
1690
Mike Stumpa1f93632010-01-20 01:15:34 +00001691 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001692
1693 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001694 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001695 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001696 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001697
Mike Stumpa1f93632010-01-20 01:15:34 +00001698 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001699 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1700 // The code after the try is the implicit successor.
1701 Succ = TrySuccessor;
1702 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001703 if (CS->getExceptionDecl() == 0) {
1704 HasCatchAll = true;
1705 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001706 Block = NULL;
1707 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1708 if (CatchBlock == 0)
1709 return 0;
1710 // Add this block to the list of successors for the block with the try
1711 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001712 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001713 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001714 if (!HasCatchAll) {
1715 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001716 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001717 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001718 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001719 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001720
1721 // The code after the try is the implicit successor.
1722 Succ = TrySuccessor;
1723
Mike Stumpf00cca52010-01-20 01:30:58 +00001724 // Save the current "try" context.
1725 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1726 TryTerminatedBlock = NewTryTerminatedBlock;
1727
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001728 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001729 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001730 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001731 return Block;
1732}
1733
1734CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1735 // CXXCatchStmt are treated like labels, so they are the first statement in a
1736 // block.
1737
1738 if (CS->getHandlerBlock())
1739 addStmt(CS->getHandlerBlock());
1740
1741 CFGBlock* CatchBlock = Block;
1742 if (!CatchBlock)
1743 CatchBlock = createBlock();
1744
1745 CatchBlock->setLabel(CS);
1746
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001747 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00001748 return 0;
1749
1750 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1751 Block = NULL;
1752
1753 return CatchBlock;
1754}
1755
Ted Kremenekad5a8942010-08-02 23:46:59 +00001756CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001757 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001758 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001759 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001760 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001761 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001762 return VisitChildren(C);
1763}
1764
Ted Kremenek19bb3562007-08-28 19:26:49 +00001765CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001766 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001767 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001768
Ted Kremenek19bb3562007-08-28 19:26:49 +00001769 if (!IBlock) {
1770 IBlock = createBlock(false);
1771 cfg->setIndirectGotoBlock(IBlock);
1772 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001773
Ted Kremenek19bb3562007-08-28 19:26:49 +00001774 // IndirectGoto is a control-flow statement. Thus we stop processing the
1775 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001776 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001777 return 0;
1778
Ted Kremenek19bb3562007-08-28 19:26:49 +00001779 Block = createBlock(false);
1780 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001781 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001782 return addStmt(I->getTarget());
1783}
1784
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001785} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001786
Mike Stump6d9828c2009-07-17 01:31:16 +00001787/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1788/// no successors or predecessors. If this is the first block created in the
1789/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001790CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001791 bool first_block = begin() == end();
1792
1793 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001794 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1795 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1796 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001797
1798 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001799 if (first_block)
1800 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001801
1802 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001803 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001804}
1805
Ted Kremenek026473c2007-08-23 16:51:22 +00001806/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1807/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001808CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenekad5a8942010-08-02 23:46:59 +00001809 bool PruneTriviallyFalse,
Ted Kremenekfe255bc2010-09-13 22:25:54 +00001810 bool AddEHEdges) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001811 CFGBuilder Builder;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001812 return Builder.buildCFG(D, Statement, C, PruneTriviallyFalse,
Ted Kremenekfe255bc2010-09-13 22:25:54 +00001813 AddEHEdges);
Ted Kremenek026473c2007-08-23 16:51:22 +00001814}
1815
Ted Kremenek63f58872007-10-01 19:33:33 +00001816//===----------------------------------------------------------------------===//
1817// CFG: Queries for BlkExprs.
1818//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001819
Ted Kremenek63f58872007-10-01 19:33:33 +00001820namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001821 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001822}
1823
Ted Kremenek8a693662009-12-23 23:37:10 +00001824static void FindSubExprAssignments(Stmt *S,
1825 llvm::SmallPtrSet<Expr*,50>& Set) {
1826 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001827 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001828
Ted Kremenek8a693662009-12-23 23:37:10 +00001829 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001830 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00001831 if (!child)
1832 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001833
Ted Kremenek8a693662009-12-23 23:37:10 +00001834 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001835 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001836
Ted Kremenek8a693662009-12-23 23:37:10 +00001837 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001838 }
1839}
1840
Ted Kremenek63f58872007-10-01 19:33:33 +00001841static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1842 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001843
1844 // Look for assignments that are used as subexpressions. These are the only
1845 // assignments that we want to *possibly* register as a block-level
1846 // expression. Basically, if an assignment occurs both in a subexpression and
1847 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001848 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001849
Ted Kremenek63f58872007-10-01 19:33:33 +00001850 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001851 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001852 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001853
Ted Kremenek411cdee2008-04-16 21:10:48 +00001854 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001855
1856 // Iterate over the statements again on identify the Expr* and Stmt* at the
1857 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001858
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001859 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001860 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001861
Ted Kremenek411cdee2008-04-16 21:10:48 +00001862 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001863 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001864 // expression are really "statements" whose value is never used by
1865 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001866 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001867 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001868 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1869 // Special handling for statement expressions. The last statement in
1870 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001871 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001872 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001873 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001874 (*M)[C->body_back()] = x;
1875 }
1876 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001877
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001878 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001879 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001880 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001881
Ted Kremenek411cdee2008-04-16 21:10:48 +00001882 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001883
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001884 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001885
Ted Kremenek390e48b2008-11-12 21:11:49 +00001886 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001887 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001888 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001889 }
1890 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001891
Ted Kremenek63f58872007-10-01 19:33:33 +00001892 return M;
1893}
1894
Ted Kremenek86946742008-01-17 20:48:37 +00001895CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1896 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001897 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001898
Ted Kremenek63f58872007-10-01 19:33:33 +00001899 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001900 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001901 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001902}
1903
1904unsigned CFG::getNumBlkExprs() {
1905 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1906 return M->size();
1907 else {
1908 // We assume callers interested in the number of BlkExprs will want
1909 // the map constructed if it doesn't already exist.
1910 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1911 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1912 }
1913}
1914
Ted Kremenek274f4332008-04-28 18:00:46 +00001915//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001916// Filtered walking of the CFG.
1917//===----------------------------------------------------------------------===//
1918
1919bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00001920 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001921
1922 if (F.IgnoreDefaultsWithCoveredEnums) {
1923 // If the 'To' has no label or is labeled but the label isn't a
1924 // CaseStmt then filter this edge.
1925 if (const SwitchStmt *S =
Ted Kremenekbe39a562010-09-09 02:57:48 +00001926 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001927 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00001928 const Stmt *L = To->getLabel();
1929 if (!L || !isa<CaseStmt>(L))
1930 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001931 }
1932 }
1933 }
1934
1935 return false;
1936}
1937
1938//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001939// Cleanup: CFG dstor.
1940//===----------------------------------------------------------------------===//
1941
Ted Kremenek63f58872007-10-01 19:33:33 +00001942CFG::~CFG() {
1943 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1944}
Mike Stump6d9828c2009-07-17 01:31:16 +00001945
Ted Kremenek7dba8602007-08-29 21:56:09 +00001946//===----------------------------------------------------------------------===//
1947// CFG pretty printing
1948//===----------------------------------------------------------------------===//
1949
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001950namespace {
1951
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001952class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001953 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1954 StmtMapTy StmtMap;
1955 signed CurrentBlock;
1956 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001957 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001958public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001959
Chris Lattnere4f21422009-06-30 01:26:17 +00001960 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1961 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001962 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1963 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001964 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001965 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001966 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001967 }
1968 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001969
Ted Kremenek42a509f2007-08-31 21:30:12 +00001970 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001971
Chris Lattnere4f21422009-06-30 01:26:17 +00001972 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001973 void setBlockID(signed i) { CurrentBlock = i; }
1974 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001975
Ted Kremeneka95d3752008-09-13 05:16:45 +00001976 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001977
Ted Kremenek411cdee2008-04-16 21:10:48 +00001978 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001979
1980 if (I == StmtMap.end())
1981 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001982
1983 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001984 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001985 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001986 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001987
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001988 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001989 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001990 }
1991};
Chris Lattnere4f21422009-06-30 01:26:17 +00001992} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001993
Chris Lattnere4f21422009-06-30 01:26:17 +00001994
1995namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001996class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001997 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001998
Ted Kremeneka95d3752008-09-13 05:16:45 +00001999 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002000 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002001 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002002public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002003 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002004 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002005 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002006
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002007 void VisitIfStmt(IfStmt* I) {
2008 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002009 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002010 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002011
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002012 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002013 void VisitStmt(Stmt* Terminator) {
2014 Terminator->printPretty(OS, Helper, Policy);
2015 }
2016
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002017 void VisitForStmt(ForStmt* F) {
2018 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002019 if (F->getInit())
2020 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002021 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002022 if (Stmt* C = F->getCond())
2023 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002024 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002025 if (F->getInc())
2026 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002027 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002028 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002029
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002030 void VisitWhileStmt(WhileStmt* W) {
2031 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002032 if (Stmt* C = W->getCond())
2033 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002034 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002035
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002036 void VisitDoStmt(DoStmt* D) {
2037 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002038 if (Stmt* C = D->getCond())
2039 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002040 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002041
Ted Kremenek411cdee2008-04-16 21:10:48 +00002042 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002043 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002044 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002045 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002046
Mike Stump5d1d2022010-01-19 02:20:09 +00002047 void VisitCXXTryStmt(CXXTryStmt* CS) {
2048 OS << "try ...";
2049 }
2050
Ted Kremenek805e9a82007-08-31 21:49:40 +00002051 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002052 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002053 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002054 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002055
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002056 void VisitChooseExpr(ChooseExpr* C) {
2057 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002058 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002059 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002060 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002061
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002062 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2063 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002064 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002065 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002066
Ted Kremenek805e9a82007-08-31 21:49:40 +00002067 void VisitBinaryOperator(BinaryOperator* B) {
2068 if (!B->isLogicalOp()) {
2069 VisitExpr(B);
2070 return;
2071 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002072
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002073 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002074
Ted Kremenek805e9a82007-08-31 21:49:40 +00002075 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002076 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002077 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002078 return;
John McCall2de56d12010-08-25 11:45:40 +00002079 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002080 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002081 return;
2082 default:
2083 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002084 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002085 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002086
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002087 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002088 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002089 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002090};
Chris Lattnere4f21422009-06-30 01:26:17 +00002091} // end anonymous namespace
2092
Mike Stump6d9828c2009-07-17 01:31:16 +00002093
Chris Lattnere4f21422009-06-30 01:26:17 +00002094static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002095 const CFGElement &E) {
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002096 Stmt *S = E;
2097
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002098 if (Helper) {
2099 // special printing for statement-expressions.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002100 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002101 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002102
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002103 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002104 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002105 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002106 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002107 return;
2108 }
2109 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002110
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002111 // special printing for comma expressions.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002112 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +00002113 if (B->getOpcode() == BO_Comma) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002114 OS << "... , ";
2115 Helper->handledStmt(B->getRHS(),OS);
2116 OS << '\n';
2117 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002118 }
2119 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002120 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002121
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002122 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2123
2124 if (isa<CXXOperatorCallExpr>(S)) {
2125 OS << " (OperatorCall)";
2126 }
2127 else if (isa<CXXBindTemporaryExpr>(S)) {
2128 OS << " (BindTemporary)";
2129 }
2130
Mike Stump6d9828c2009-07-17 01:31:16 +00002131
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002132 // Expressions need a newline.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002133 if (isa<Expr>(S))
2134 OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002135}
Mike Stump6d9828c2009-07-17 01:31:16 +00002136
Chris Lattnere4f21422009-06-30 01:26:17 +00002137static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2138 const CFGBlock& B,
2139 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002140
Ted Kremenek42a509f2007-08-31 21:30:12 +00002141 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002142
Ted Kremenek7dba8602007-08-29 21:56:09 +00002143 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002144 OS << "\n [ B" << B.getBlockID();
2145
Ted Kremenek42a509f2007-08-31 21:30:12 +00002146 if (&B == &cfg->getEntry())
2147 OS << " (ENTRY) ]\n";
2148 else if (&B == &cfg->getExit())
2149 OS << " (EXIT) ]\n";
2150 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002151 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002152 else
2153 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002154
Ted Kremenek9cffe732007-08-29 23:20:49 +00002155 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002156 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002157
2158 if (print_edges)
2159 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002160
Mike Stump079bd722010-01-19 22:00:14 +00002161 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002162 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002163 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002164 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002165 C->getLHS()->printPretty(OS, Helper,
2166 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002167 if (C->getRHS()) {
2168 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002169 C->getRHS()->printPretty(OS, Helper,
2170 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002171 }
Mike Stump079bd722010-01-19 22:00:14 +00002172 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002173 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002174 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002175 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002176 if (CS->getExceptionDecl())
2177 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2178 0);
2179 else
2180 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002181 OS << ")";
2182
2183 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002184 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002185
Ted Kremenek9cffe732007-08-29 23:20:49 +00002186 OS << ":\n";
2187 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002188
Ted Kremenekfddd5182007-08-21 21:42:03 +00002189 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002190 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002191
Ted Kremenek42a509f2007-08-31 21:30:12 +00002192 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2193 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002194
Ted Kremenek9cffe732007-08-29 23:20:49 +00002195 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002196 if (print_edges)
2197 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002198
Ted Kremeneka95d3752008-09-13 05:16:45 +00002199 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002200
Ted Kremenek42a509f2007-08-31 21:30:12 +00002201 if (Helper)
2202 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002203
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002204 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002205 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002206
Ted Kremenek9cffe732007-08-29 23:20:49 +00002207 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002208 if (B.getTerminator()) {
2209 if (print_edges)
2210 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002211
Ted Kremenek9cffe732007-08-29 23:20:49 +00002212 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002213
Ted Kremenek42a509f2007-08-31 21:30:12 +00002214 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002215
Chris Lattnere4f21422009-06-30 01:26:17 +00002216 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2217 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002218 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002219 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002220 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002221
Ted Kremenek9cffe732007-08-29 23:20:49 +00002222 if (print_edges) {
2223 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002224 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002225 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002226
Ted Kremenek42a509f2007-08-31 21:30:12 +00002227 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2228 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002229
Ted Kremenek42a509f2007-08-31 21:30:12 +00002230 if (i == 8 || (i-8) == 0)
2231 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002232
Ted Kremenek9cffe732007-08-29 23:20:49 +00002233 OS << " B" << (*I)->getBlockID();
2234 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002235
Ted Kremenek42a509f2007-08-31 21:30:12 +00002236 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002237
Ted Kremenek42a509f2007-08-31 21:30:12 +00002238 // Print the successors of this block.
2239 OS << " Successors (" << B.succ_size() << "):";
2240 i = 0;
2241
2242 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2243 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002244
Ted Kremenek42a509f2007-08-31 21:30:12 +00002245 if (i == 8 || (i-8) % 10 == 0)
2246 OS << "\n ";
2247
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002248 if (*I)
2249 OS << " B" << (*I)->getBlockID();
2250 else
2251 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002252 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002253
Ted Kremenek9cffe732007-08-29 23:20:49 +00002254 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002255 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002256}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002257
Ted Kremenek42a509f2007-08-31 21:30:12 +00002258
2259/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002260void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002261
2262/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002263void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2264 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002265
Ted Kremenek42a509f2007-08-31 21:30:12 +00002266 // Print the entry block.
2267 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002268
Ted Kremenek42a509f2007-08-31 21:30:12 +00002269 // Iterate through the CFGBlocks and print them one by one.
2270 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2271 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002272 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002273 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002274
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002275 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002276 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002277
Ted Kremenek42a509f2007-08-31 21:30:12 +00002278 // Print the exit block.
2279 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002280 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002281}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002282
2283/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002284void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2285 print(llvm::errs(), cfg, LO);
2286}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002287
2288/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2289/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002290void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2291 const LangOptions &LO) const {
2292 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002293 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002294}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002295
Ted Kremeneka2925852008-01-30 23:02:42 +00002296/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002297void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002298 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002299 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002300 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2301}
2302
Ted Kremenek390e48b2008-11-12 21:11:49 +00002303Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002304
Ted Kremenek411cdee2008-04-16 21:10:48 +00002305 if (!Terminator)
2306 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002307
Ted Kremenek411cdee2008-04-16 21:10:48 +00002308 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002309
Ted Kremenek411cdee2008-04-16 21:10:48 +00002310 switch (Terminator->getStmtClass()) {
2311 default:
2312 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002313
Ted Kremenek411cdee2008-04-16 21:10:48 +00002314 case Stmt::ForStmtClass:
2315 E = cast<ForStmt>(Terminator)->getCond();
2316 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002317
Ted Kremenek411cdee2008-04-16 21:10:48 +00002318 case Stmt::WhileStmtClass:
2319 E = cast<WhileStmt>(Terminator)->getCond();
2320 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002321
Ted Kremenek411cdee2008-04-16 21:10:48 +00002322 case Stmt::DoStmtClass:
2323 E = cast<DoStmt>(Terminator)->getCond();
2324 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002325
Ted Kremenek411cdee2008-04-16 21:10:48 +00002326 case Stmt::IfStmtClass:
2327 E = cast<IfStmt>(Terminator)->getCond();
2328 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002329
Ted Kremenek411cdee2008-04-16 21:10:48 +00002330 case Stmt::ChooseExprClass:
2331 E = cast<ChooseExpr>(Terminator)->getCond();
2332 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002333
Ted Kremenek411cdee2008-04-16 21:10:48 +00002334 case Stmt::IndirectGotoStmtClass:
2335 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2336 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002337
Ted Kremenek411cdee2008-04-16 21:10:48 +00002338 case Stmt::SwitchStmtClass:
2339 E = cast<SwitchStmt>(Terminator)->getCond();
2340 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002341
Ted Kremenek411cdee2008-04-16 21:10:48 +00002342 case Stmt::ConditionalOperatorClass:
2343 E = cast<ConditionalOperator>(Terminator)->getCond();
2344 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002345
Ted Kremenek411cdee2008-04-16 21:10:48 +00002346 case Stmt::BinaryOperatorClass: // '&&' and '||'
2347 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002348 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002349
Ted Kremenek390e48b2008-11-12 21:11:49 +00002350 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002351 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002352 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002353
Ted Kremenek411cdee2008-04-16 21:10:48 +00002354 return E ? E->IgnoreParens() : NULL;
2355}
2356
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002357bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002358
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002359 if (!Terminator)
2360 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002361
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002362 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002363
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002364 switch (Terminator->getStmtClass()) {
2365 default:
2366 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002367
2368 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002369 case Stmt::WhileStmtClass:
2370 case Stmt::DoStmtClass:
2371 case Stmt::IfStmtClass:
2372 case Stmt::ChooseExprClass:
2373 case Stmt::ConditionalOperatorClass:
2374 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002375 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002376 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002377
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002378 return E ? E->IgnoreParens() : NULL;
2379}
2380
Ted Kremeneka2925852008-01-30 23:02:42 +00002381
Ted Kremenek7dba8602007-08-29 21:56:09 +00002382//===----------------------------------------------------------------------===//
2383// CFG Graphviz Visualization
2384//===----------------------------------------------------------------------===//
2385
Ted Kremenek42a509f2007-08-31 21:30:12 +00002386
2387#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002388static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002389#endif
2390
Chris Lattnere4f21422009-06-30 01:26:17 +00002391void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002392#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002393 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002394 GraphHelper = &H;
2395 llvm::ViewGraph(this,"CFG");
2396 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002397#endif
2398}
2399
Ted Kremenek7dba8602007-08-29 21:56:09 +00002400namespace llvm {
2401template<>
2402struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002403
2404 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2405
2406 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002407
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002408#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002409 std::string OutSStr;
2410 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002411 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002412 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002413
2414 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2415
2416 // Process string output to make it nicer...
2417 for (unsigned i = 0; i != OutStr.length(); ++i)
2418 if (OutStr[i] == '\n') { // Left justify
2419 OutStr[i] = '\\';
2420 OutStr.insert(OutStr.begin()+i+1, 'l');
2421 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002422
Ted Kremenek7dba8602007-08-29 21:56:09 +00002423 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002424#else
2425 return "";
2426#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002427 }
2428};
2429} // end namespace llvm