blob: cc3a38886267ff7f93790eda81ff4977d3315911 [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 Kremenek6c52c782010-09-14 23:41:16 +0000103 CFG::BuildOptions BO);
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 Kremenek6c52c782010-09-14 23:41:16 +0000190 if (!BuildOpts.PruneTriviallyFalseEdges)
Ted Kremenekad5a8942010-08-02 23:46:59 +0000191 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;
Ted Kremenek6c52c782010-09-14 23:41:16 +0000202 CFG::BuildOptions BuildOpts;
Ted Kremenekfddd5182007-08-21 21:42:03 +0000203};
Mike Stump6d9828c2009-07-17 01:31:16 +0000204
Douglas Gregor898574e2008-12-05 23:32:09 +0000205// FIXME: Add support for dependent-sized array types in C++?
206// Does it even make sense to build a CFG for an uninstantiated template?
Ted Kremenek610a09e2008-09-26 22:58:57 +0000207static VariableArrayType* FindVA(Type* t) {
208 while (ArrayType* vt = dyn_cast<ArrayType>(t)) {
209 if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt))
210 if (vat->getSizeExpr())
211 return vat;
Mike Stump6d9828c2009-07-17 01:31:16 +0000212
Ted Kremenek610a09e2008-09-26 22:58:57 +0000213 t = vt->getElementType().getTypePtr();
214 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000215
Ted Kremenek610a09e2008-09-26 22:58:57 +0000216 return 0;
217}
Mike Stump6d9828c2009-07-17 01:31:16 +0000218
219/// BuildCFG - Constructs a CFG from an AST (a Stmt*). The AST can represent an
220/// arbitrary statement. Examples include a single expression or a function
221/// body (compound statement). The ownership of the returned CFG is
222/// transferred to the caller. If CFG construction fails, this method returns
223/// NULL.
Mike Stumpb978a442010-01-21 02:21:40 +0000224CFG* CFGBuilder::buildCFG(const Decl *D, Stmt* Statement, ASTContext* C,
Ted Kremenek6c52c782010-09-14 23:41:16 +0000225 CFG::BuildOptions BO) {
Ted Kremenekad5a8942010-08-02 23:46:59 +0000226
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000227 Context = C;
Ted Kremenek0ba497b2009-10-20 23:46:25 +0000228 assert(cfg.get());
Ted Kremenek4f880632009-07-17 22:18:43 +0000229 if (!Statement)
230 return NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000231
Ted Kremenek4102af92008-03-13 03:04:22 +0000232 badCFG = false;
Ted Kremenek6c52c782010-09-14 23:41:16 +0000233 BuildOpts = BO;
234 if (!C->getLangOptions().CPlusPlus)
235 BuildOpts.AddImplicitDtors = false;
Mike Stump6d9828c2009-07-17 01:31:16 +0000236
237 // Create an empty block that will serve as the exit block for the CFG. Since
238 // this is the first block added to the CFG, it will be implicitly registered
239 // as the exit block.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000240 Succ = createBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000241 assert(Succ == &cfg->getExit());
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000242 Block = NULL; // the EXIT block is empty. Create all other blocks lazily.
Mike Stump6d9828c2009-07-17 01:31:16 +0000243
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000244 // Visit the statements and create the CFG.
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000245 CFGBlock *B = addStmt(Statement);
246
247 if (badCFG)
248 return NULL;
249
250 if (B)
251 Succ = B;
Mike Stumpb978a442010-01-21 02:21:40 +0000252
253 if (const CXXConstructorDecl *CD = dyn_cast_or_null<CXXConstructorDecl>(D)) {
254 // FIXME: Add code for base initializers and member initializers.
255 (void)CD;
256 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000257
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000258 // Backpatch the gotos whose label -> block mappings we didn't know when we
259 // encountered them.
260 for (BackpatchBlocksTy::iterator I = BackpatchBlocks.begin(),
261 E = BackpatchBlocks.end(); I != E; ++I ) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000262
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000263 CFGBlock* B = *I;
264 GotoStmt* G = cast<GotoStmt>(B->getTerminator());
265 LabelMapTy::iterator LI = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000266
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000267 // If there is no target for the goto, then we are looking at an
268 // incomplete AST. Handle this by not registering a successor.
269 if (LI == LabelMap.end()) continue;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000270
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000271 AddSuccessor(B, LI->second);
272 }
273
274 // Add successors to the Indirect Goto Dispatch block (if we have one).
275 if (CFGBlock* B = cfg->getIndirectGotoBlock())
276 for (LabelSetTy::iterator I = AddressTakenLabels.begin(),
277 E = AddressTakenLabels.end(); I != E; ++I ) {
278
279 // Lookup the target block.
280 LabelMapTy::iterator LI = LabelMap.find(*I);
281
282 // If there is no target block that contains label, then we are looking
283 // at an incomplete AST. Handle this by not registering a successor.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000284 if (LI == LabelMap.end()) continue;
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000285
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000286 AddSuccessor(B, LI->second);
Ted Kremenek19bb3562007-08-28 19:26:49 +0000287 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000288
Mike Stump6d9828c2009-07-17 01:31:16 +0000289 // Create an empty entry block that has no predecessors.
Ted Kremenek322f58d2007-09-26 21:23:31 +0000290 cfg->setEntry(createBlock());
Mike Stump6d9828c2009-07-17 01:31:16 +0000291
Zhongxing Xu1b3b7cb2010-09-06 07:04:06 +0000292 return cfg.take();
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000293}
Mike Stump6d9828c2009-07-17 01:31:16 +0000294
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000295/// createBlock - Used to lazily create blocks that are connected
296/// to the current (global) succcessor.
Mike Stump6d9828c2009-07-17 01:31:16 +0000297CFGBlock* CFGBuilder::createBlock(bool add_successor) {
Ted Kremenek94382522007-09-05 20:02:05 +0000298 CFGBlock* B = cfg->createBlock();
Ted Kremenek4f880632009-07-17 22:18:43 +0000299 if (add_successor && Succ)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000300 AddSuccessor(B, Succ);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000301 return B;
302}
Mike Stump6d9828c2009-07-17 01:31:16 +0000303
Ted Kremenek4f880632009-07-17 22:18:43 +0000304/// Visit - Walk the subtree of a statement and add extra
Mike Stump6d9828c2009-07-17 01:31:16 +0000305/// blocks for ternary operators, &&, and ||. We also process "," and
306/// DeclStmts (which may contain nested control-flow).
Ted Kremenek852274d2009-12-16 03:18:58 +0000307CFGBlock* CFGBuilder::Visit(Stmt * S, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000308tryAgain:
Ted Kremenekf42e3372010-04-30 22:25:53 +0000309 if (!S) {
310 badCFG = true;
311 return 0;
312 }
Ted Kremenek4f880632009-07-17 22:18:43 +0000313 switch (S->getStmtClass()) {
314 default:
Ted Kremenek852274d2009-12-16 03:18:58 +0000315 return VisitStmt(S, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000316
317 case Stmt::AddrLabelExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000318 return VisitAddrLabelExpr(cast<AddrLabelExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek4f880632009-07-17 22:18:43 +0000320 case Stmt::BinaryOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000321 return VisitBinaryOperator(cast<BinaryOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek4f880632009-07-17 22:18:43 +0000323 case Stmt::BlockExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000324 return VisitBlockExpr(cast<BlockExpr>(S), asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000325
Ted Kremenek4f880632009-07-17 22:18:43 +0000326 case Stmt::BreakStmtClass:
327 return VisitBreakStmt(cast<BreakStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek4f880632009-07-17 22:18:43 +0000329 case Stmt::CallExprClass:
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000330 case Stmt::CXXOperatorCallExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000331 return VisitCallExpr(cast<CallExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek4f880632009-07-17 22:18:43 +0000333 case Stmt::CaseStmtClass:
334 return VisitCaseStmt(cast<CaseStmt>(S));
335
336 case Stmt::ChooseExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000337 return VisitChooseExpr(cast<ChooseExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek4f880632009-07-17 22:18:43 +0000339 case Stmt::CompoundStmtClass:
340 return VisitCompoundStmt(cast<CompoundStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek4f880632009-07-17 22:18:43 +0000342 case Stmt::ConditionalOperatorClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000343 return VisitConditionalOperator(cast<ConditionalOperator>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremenek4f880632009-07-17 22:18:43 +0000345 case Stmt::ContinueStmtClass:
346 return VisitContinueStmt(cast<ContinueStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek021c8af2010-01-19 20:40:33 +0000348 case Stmt::CXXCatchStmtClass:
349 return VisitCXXCatchStmt(cast<CXXCatchStmt>(S));
350
Ted Kremenek47e331e2010-08-28 00:19:02 +0000351 case Stmt::CXXExprWithTemporariesClass: {
352 // FIXME: Handle temporaries. For now, just visit the subexpression
353 // so we don't artificially create extra blocks.
Ted Kremeneka427f1d2010-08-31 18:47:34 +0000354 return Visit(cast<CXXExprWithTemporaries>(S)->getSubExpr(), asc);
Ted Kremenek47e331e2010-08-28 00:19:02 +0000355 }
356
Zhongxing Xuc5354a22010-04-13 09:38:01 +0000357 case Stmt::CXXMemberCallExprClass:
358 return VisitCXXMemberCallExpr(cast<CXXMemberCallExpr>(S), asc);
359
Ted Kremenek021c8af2010-01-19 20:40:33 +0000360 case Stmt::CXXThrowExprClass:
361 return VisitCXXThrowExpr(cast<CXXThrowExpr>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000362
Ted Kremenek021c8af2010-01-19 20:40:33 +0000363 case Stmt::CXXTryStmtClass:
364 return VisitCXXTryStmt(cast<CXXTryStmt>(S));
Ted Kremenekad5a8942010-08-02 23:46:59 +0000365
Ted Kremenek4f880632009-07-17 22:18:43 +0000366 case Stmt::DeclStmtClass:
367 return VisitDeclStmt(cast<DeclStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek4f880632009-07-17 22:18:43 +0000369 case Stmt::DefaultStmtClass:
370 return VisitDefaultStmt(cast<DefaultStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Ted Kremenek4f880632009-07-17 22:18:43 +0000372 case Stmt::DoStmtClass:
373 return VisitDoStmt(cast<DoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Ted Kremenek4f880632009-07-17 22:18:43 +0000375 case Stmt::ForStmtClass:
376 return VisitForStmt(cast<ForStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek4f880632009-07-17 22:18:43 +0000378 case Stmt::GotoStmtClass:
379 return VisitGotoStmt(cast<GotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek4f880632009-07-17 22:18:43 +0000381 case Stmt::IfStmtClass:
382 return VisitIfStmt(cast<IfStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek4f880632009-07-17 22:18:43 +0000384 case Stmt::IndirectGotoStmtClass:
385 return VisitIndirectGotoStmt(cast<IndirectGotoStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Ted Kremenek4f880632009-07-17 22:18:43 +0000387 case Stmt::LabelStmtClass:
388 return VisitLabelStmt(cast<LabelStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremenek115c1b92010-04-11 17:02:10 +0000390 case Stmt::MemberExprClass:
391 return VisitMemberExpr(cast<MemberExpr>(S), asc);
392
Ted Kremenek4f880632009-07-17 22:18:43 +0000393 case Stmt::ObjCAtCatchStmtClass:
Mike Stump1eb44332009-09-09 15:08:12 +0000394 return VisitObjCAtCatchStmt(cast<ObjCAtCatchStmt>(S));
395
Ted Kremenek4f880632009-07-17 22:18:43 +0000396 case Stmt::ObjCAtSynchronizedStmtClass:
397 return VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek4f880632009-07-17 22:18:43 +0000399 case Stmt::ObjCAtThrowStmtClass:
400 return VisitObjCAtThrowStmt(cast<ObjCAtThrowStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek4f880632009-07-17 22:18:43 +0000402 case Stmt::ObjCAtTryStmtClass:
403 return VisitObjCAtTryStmt(cast<ObjCAtTryStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek4f880632009-07-17 22:18:43 +0000405 case Stmt::ObjCForCollectionStmtClass:
406 return VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Ted Kremenek4f880632009-07-17 22:18:43 +0000408 case Stmt::ParenExprClass:
409 S = cast<ParenExpr>(S)->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000410 goto tryAgain;
411
Ted Kremenek4f880632009-07-17 22:18:43 +0000412 case Stmt::NullStmtClass:
413 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Ted Kremenek4f880632009-07-17 22:18:43 +0000415 case Stmt::ReturnStmtClass:
416 return VisitReturnStmt(cast<ReturnStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek4f880632009-07-17 22:18:43 +0000418 case Stmt::SizeOfAlignOfExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000419 return VisitSizeOfAlignOfExpr(cast<SizeOfAlignOfExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek4f880632009-07-17 22:18:43 +0000421 case Stmt::StmtExprClass:
Ted Kremenek852274d2009-12-16 03:18:58 +0000422 return VisitStmtExpr(cast<StmtExpr>(S), asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek4f880632009-07-17 22:18:43 +0000424 case Stmt::SwitchStmtClass:
425 return VisitSwitchStmt(cast<SwitchStmt>(S));
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek4f880632009-07-17 22:18:43 +0000427 case Stmt::WhileStmtClass:
428 return VisitWhileStmt(cast<WhileStmt>(S));
429 }
430}
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek852274d2009-12-16 03:18:58 +0000432CFGBlock *CFGBuilder::VisitStmt(Stmt *S, AddStmtChoice asc) {
433 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000434 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000435 AppendStmt(Block, S, asc);
Mike Stump6d9828c2009-07-17 01:31:16 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Ted Kremenek4f880632009-07-17 22:18:43 +0000438 return VisitChildren(S);
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000439}
Mike Stump6d9828c2009-07-17 01:31:16 +0000440
Ted Kremenek4f880632009-07-17 22:18:43 +0000441/// VisitChildren - Visit the children of a Stmt.
442CFGBlock *CFGBuilder::VisitChildren(Stmt* Terminator) {
443 CFGBlock *B = Block;
Mike Stump54cc43f2009-02-26 08:00:25 +0000444 for (Stmt::child_iterator I = Terminator->child_begin(),
Ted Kremenek4f880632009-07-17 22:18:43 +0000445 E = Terminator->child_end(); I != E; ++I) {
446 if (*I) B = Visit(*I);
447 }
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000448 return B;
449}
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremenek852274d2009-12-16 03:18:58 +0000451CFGBlock *CFGBuilder::VisitAddrLabelExpr(AddrLabelExpr *A,
452 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000453 AddressTakenLabels.insert(A->getLabel());
Ted Kremenek9da2fb72007-08-27 21:27:44 +0000454
Ted Kremenek852274d2009-12-16 03:18:58 +0000455 if (asc.alwaysAdd()) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000456 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000457 AppendStmt(Block, A, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000458 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000459
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000460 return Block;
461}
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek852274d2009-12-16 03:18:58 +0000463CFGBlock *CFGBuilder::VisitBinaryOperator(BinaryOperator *B,
464 AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000465 if (B->isLogicalOp()) { // && or ||
Ted Kremenek4f880632009-07-17 22:18:43 +0000466 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000467 AppendStmt(ConfluenceBlock, B, asc);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000469 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +0000470 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Ted Kremenek4f880632009-07-17 22:18:43 +0000472 // create the block evaluating the LHS
473 CFGBlock* LHSBlock = createBlock(false);
474 LHSBlock->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenek4f880632009-07-17 22:18:43 +0000476 // create the block evaluating the RHS
477 Succ = ConfluenceBlock;
478 Block = NULL;
479 CFGBlock* RHSBlock = addStmt(B->getRHS());
Ted Kremenek862b24f2010-04-29 01:10:26 +0000480
481 if (RHSBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000482 if (badCFG)
Ted Kremenek862b24f2010-04-29 01:10:26 +0000483 return 0;
484 }
485 else {
486 // Create an empty block for cases where the RHS doesn't require
487 // any explicit statements in the CFG.
488 RHSBlock = createBlock();
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Mike Stump00998a02009-07-23 23:25:26 +0000491 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000492 TryResult KnownVal = TryEvaluateBool(B->getLHS());
John McCall2de56d12010-08-25 11:45:40 +0000493 if (KnownVal.isKnown() && (B->getOpcode() == BO_LOr))
Ted Kremenek941fde82009-07-24 04:47:11 +0000494 KnownVal.negate();
Mike Stump00998a02009-07-23 23:25:26 +0000495
Ted Kremenek4f880632009-07-17 22:18:43 +0000496 // Now link the LHSBlock with RHSBlock.
John McCall2de56d12010-08-25 11:45:40 +0000497 if (B->getOpcode() == BO_LOr) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000498 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
499 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000500 } else {
John McCall2de56d12010-08-25 11:45:40 +0000501 assert(B->getOpcode() == BO_LAnd);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000502 AddSuccessor(LHSBlock, KnownVal.isFalse() ? NULL : RHSBlock);
503 AddSuccessor(LHSBlock, KnownVal.isTrue() ? NULL : ConfluenceBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Ted Kremenek4f880632009-07-17 22:18:43 +0000506 // Generate the blocks for evaluating the LHS.
507 Block = LHSBlock;
508 return addStmt(B->getLHS());
Mike Stump1eb44332009-09-09 15:08:12 +0000509 }
John McCall2de56d12010-08-25 11:45:40 +0000510 else if (B->getOpcode() == BO_Comma) { // ,
Ted Kremenek6dc534e2009-07-17 22:57:50 +0000511 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000512 AppendStmt(Block, B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000513 addStmt(B->getRHS());
514 return addStmt(B->getLHS());
515 }
Zhongxing Xufc61d942010-06-03 06:23:18 +0000516 else if (B->isAssignmentOp()) {
517 if (asc.alwaysAdd()) {
518 autoCreateBlock();
519 AppendStmt(Block, B, asc);
520 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000521
Ted Kremenek44f8ef12010-09-14 01:13:32 +0000522 // If visiting RHS causes us to finish 'Block' and the LHS doesn't
523 // create a new block, then we should return RBlock. Otherwise
524 // we'll incorrectly return NULL.
525 CFGBlock *RBlock = Visit(B->getRHS());
526 CFGBlock *LBlock = Visit(B->getLHS(), AddStmtChoice::AsLValueNotAlwaysAdd);
527 return LBlock ? LBlock : RBlock;
Zhongxing Xufc61d942010-06-03 06:23:18 +0000528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Ted Kremenek852274d2009-12-16 03:18:58 +0000530 return VisitStmt(B, asc);
Ted Kremenek4f880632009-07-17 22:18:43 +0000531}
532
Ted Kremenek852274d2009-12-16 03:18:58 +0000533CFGBlock *CFGBuilder::VisitBlockExpr(BlockExpr *E, AddStmtChoice asc) {
534 if (asc.alwaysAdd()) {
Ted Kremenek721903e2009-11-25 01:34:30 +0000535 autoCreateBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000536 AppendStmt(Block, E, asc);
Ted Kremenek721903e2009-11-25 01:34:30 +0000537 }
538 return Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000539}
540
Ted Kremenek4f880632009-07-17 22:18:43 +0000541CFGBlock *CFGBuilder::VisitBreakStmt(BreakStmt *B) {
542 // "break" is a control-flow statement. Thus we stop processing the current
543 // block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000544 if (badCFG)
545 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Ted Kremenek4f880632009-07-17 22:18:43 +0000547 // Now create a new block that ends with the break statement.
548 Block = createBlock(false);
549 Block->setTerminator(B);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek4f880632009-07-17 22:18:43 +0000551 // If there is no target for the break, then we are looking at an incomplete
552 // AST. This means that the CFG cannot be constructed.
553 if (BreakTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000554 AddSuccessor(Block, BreakTargetBlock);
Ted Kremenek4f880632009-07-17 22:18:43 +0000555 else
556 badCFG = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
558
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000559 return Block;
560}
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Mike Stump4c45aa12010-01-21 15:20:48 +0000562static bool CanThrow(Expr *E) {
563 QualType Ty = E->getType();
564 if (Ty->isFunctionPointerType())
565 Ty = Ty->getAs<PointerType>()->getPointeeType();
566 else if (Ty->isBlockPointerType())
567 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Ted Kremenekad5a8942010-08-02 23:46:59 +0000568
Mike Stump4c45aa12010-01-21 15:20:48 +0000569 const FunctionType *FT = Ty->getAs<FunctionType>();
570 if (FT) {
571 if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
572 if (Proto->hasEmptyExceptionSpec())
573 return false;
574 }
575 return true;
576}
577
Ted Kremenek852274d2009-12-16 03:18:58 +0000578CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) {
Ted Kremenek4f880632009-07-17 22:18:43 +0000579 // If this is a call to a no-return function, this stops the block here.
Mike Stump24556362009-07-25 21:26:53 +0000580 bool NoReturn = false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000581 if (getFunctionExtInfo(*C->getCallee()->getType()).getNoReturn()) {
Mike Stump24556362009-07-25 21:26:53 +0000582 NoReturn = true;
Ted Kremenek4f880632009-07-17 22:18:43 +0000583 }
Mike Stump24556362009-07-25 21:26:53 +0000584
Mike Stump4c45aa12010-01-21 15:20:48 +0000585 bool AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000586
587 // Languages without exceptions are assumed to not throw.
588 if (Context->getLangOptions().Exceptions) {
Ted Kremenek6c52c782010-09-14 23:41:16 +0000589 if (BuildOpts.AddEHEdges)
Mike Stump4c45aa12010-01-21 15:20:48 +0000590 AddEHEdge = true;
Mike Stump079bd722010-01-19 22:00:14 +0000591 }
592
593 if (FunctionDecl *FD = C->getDirectCallee()) {
Mike Stump24556362009-07-25 21:26:53 +0000594 if (FD->hasAttr<NoReturnAttr>())
595 NoReturn = true;
Mike Stump079bd722010-01-19 22:00:14 +0000596 if (FD->hasAttr<NoThrowAttr>())
Mike Stump4c45aa12010-01-21 15:20:48 +0000597 AddEHEdge = false;
Mike Stump079bd722010-01-19 22:00:14 +0000598 }
Mike Stump24556362009-07-25 21:26:53 +0000599
Mike Stump4c45aa12010-01-21 15:20:48 +0000600 if (!CanThrow(C->getCallee()))
601 AddEHEdge = false;
602
Zhongxing Xufc61d942010-06-03 06:23:18 +0000603 if (!NoReturn && !AddEHEdge) {
604 if (asc.asLValue())
605 return VisitStmt(C, AddStmtChoice::AlwaysAddAsLValue);
606 else
607 return VisitStmt(C, AddStmtChoice::AlwaysAdd);
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Mike Stump079bd722010-01-19 22:00:14 +0000610 if (Block) {
611 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000612 if (badCFG)
Mike Stump079bd722010-01-19 22:00:14 +0000613 return 0;
614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Mike Stump079bd722010-01-19 22:00:14 +0000616 Block = createBlock(!NoReturn);
Ted Kremenek852274d2009-12-16 03:18:58 +0000617 AppendStmt(Block, C, asc);
Mike Stump24556362009-07-25 21:26:53 +0000618
Mike Stump079bd722010-01-19 22:00:14 +0000619 if (NoReturn) {
620 // Wire this to the exit block directly.
621 AddSuccessor(Block, &cfg->getExit());
622 }
Mike Stump4c45aa12010-01-21 15:20:48 +0000623 if (AddEHEdge) {
Mike Stump079bd722010-01-19 22:00:14 +0000624 // Add exceptional edges.
625 if (TryTerminatedBlock)
626 AddSuccessor(Block, TryTerminatedBlock);
627 else
628 AddSuccessor(Block, &cfg->getExit());
629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Mike Stump24556362009-07-25 21:26:53 +0000631 return VisitChildren(C);
Ted Kremenek4f880632009-07-17 22:18:43 +0000632}
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000633
Ted Kremenek852274d2009-12-16 03:18:58 +0000634CFGBlock *CFGBuilder::VisitChooseExpr(ChooseExpr *C,
635 AddStmtChoice asc) {
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000636 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000637 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000638 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000639 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek115c1b92010-04-11 17:02:10 +0000641 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
642 : AddStmtChoice::AlwaysAdd;
643
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000644 Succ = ConfluenceBlock;
645 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000646 CFGBlock* LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000647 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000648 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000650 Succ = ConfluenceBlock;
651 Block = NULL;
Zhongxing Xudf119892010-06-03 06:43:23 +0000652 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000653 if (badCFG)
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000654 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000656 Block = createBlock(false);
Mike Stump00998a02009-07-23 23:25:26 +0000657 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000658 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000659 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
660 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000661 Block->setTerminator(C);
Mike Stump1eb44332009-09-09 15:08:12 +0000662 return addStmt(C->getCond());
Ted Kremenek3fc8ef52009-07-17 18:20:32 +0000663}
Mike Stump1eb44332009-09-09 15:08:12 +0000664
665
666CFGBlock* CFGBuilder::VisitCompoundStmt(CompoundStmt* C) {
667 CFGBlock* LastBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000668
669 for (CompoundStmt::reverse_body_iterator I=C->body_rbegin(), E=C->body_rend();
670 I != E; ++I ) {
Ted Kremenek334c1952010-08-17 21:00:06 +0000671 // If we hit a segment of code just containing ';' (NullStmts), we can
672 // get a null block back. In such cases, just use the LastBlock
673 if (CFGBlock *newBlock = addStmt(*I))
674 LastBlock = newBlock;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremeneke8d6d2b2009-08-27 23:16:26 +0000676 if (badCFG)
677 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000678 }
Mike Stump079bd722010-01-19 22:00:14 +0000679
Ted Kremenek4f880632009-07-17 22:18:43 +0000680 return LastBlock;
681}
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremenek852274d2009-12-16 03:18:58 +0000683CFGBlock *CFGBuilder::VisitConditionalOperator(ConditionalOperator *C,
684 AddStmtChoice asc) {
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000685 // Create the confluence block that will "merge" the results of the ternary
686 // expression.
687 CFGBlock* ConfluenceBlock = Block ? Block : createBlock();
Ted Kremenek852274d2009-12-16 03:18:58 +0000688 AppendStmt(ConfluenceBlock, C, asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000689 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000690 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenek115c1b92010-04-11 17:02:10 +0000692 asc = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
693 : AddStmtChoice::AlwaysAdd;
694
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000695 // Create a block for the LHS expression if there is an LHS expression. A
696 // GCC extension allows LHS to be NULL, causing the condition to be the
697 // value that is returned instead.
698 // e.g: x ?: y is shorthand for: x ? x : y;
699 Succ = ConfluenceBlock;
700 Block = NULL;
701 CFGBlock* LHSBlock = NULL;
702 if (C->getLHS()) {
Zhongxing Xudf119892010-06-03 06:43:23 +0000703 LHSBlock = Visit(C->getLHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000704 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000705 return 0;
706 Block = NULL;
707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000709 // Create the block for the RHS expression.
710 Succ = ConfluenceBlock;
Zhongxing Xudf119892010-06-03 06:43:23 +0000711 CFGBlock* RHSBlock = Visit(C->getRHS(), asc);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000712 if (badCFG)
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000713 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000715 // Create the block that will contain the condition.
716 Block = createBlock(false);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Mike Stump00998a02009-07-23 23:25:26 +0000718 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000719 const TryResult& KnownVal = TryEvaluateBool(C->getCond());
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000720 if (LHSBlock) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000721 AddSuccessor(Block, KnownVal.isFalse() ? NULL : LHSBlock);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000722 } else {
Ted Kremenek941fde82009-07-24 04:47:11 +0000723 if (KnownVal.isFalse()) {
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000724 // If we know the condition is false, add NULL as the successor for
725 // the block containing the condition. In this case, the confluence
726 // block will have just one predecessor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000727 AddSuccessor(Block, 0);
Ted Kremenek941fde82009-07-24 04:47:11 +0000728 assert(ConfluenceBlock->pred_size() == 1);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000729 } else {
730 // If we have no LHS expression, add the ConfluenceBlock as a direct
731 // successor for the block containing the condition. Moreover, we need to
732 // reverse the order of the predecessors in the ConfluenceBlock because
733 // the RHSBlock will have been added to the succcessors already, and we
734 // want the first predecessor to the the block containing the expression
735 // for the case when the ternary expression evaluates to true.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000736 AddSuccessor(Block, ConfluenceBlock);
Ted Kremenek941fde82009-07-24 04:47:11 +0000737 assert(ConfluenceBlock->pred_size() == 2);
Mike Stumpe5af3ce2009-07-20 23:24:15 +0000738 std::reverse(ConfluenceBlock->pred_begin(),
739 ConfluenceBlock->pred_end());
740 }
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000743 AddSuccessor(Block, KnownVal.isTrue() ? NULL : RHSBlock);
Ted Kremenekf34bb2e2009-07-17 18:15:54 +0000744 Block->setTerminator(C);
745 return addStmt(C->getCond());
746}
747
Ted Kremenek4f880632009-07-17 22:18:43 +0000748CFGBlock *CFGBuilder::VisitDeclStmt(DeclStmt *DS) {
749 autoCreateBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +0000750
Ted Kremenek4f880632009-07-17 22:18:43 +0000751 if (DS->isSingleDecl()) {
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000752 AppendStmt(Block, DS);
Ted Kremenek4f880632009-07-17 22:18:43 +0000753 return VisitDeclSubExpr(DS->getSingleDecl());
Ted Kremenekd34066c2008-02-26 00:22:58 +0000754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek4f880632009-07-17 22:18:43 +0000756 CFGBlock *B = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Ted Kremenek4f880632009-07-17 22:18:43 +0000758 // FIXME: Add a reverse iterator for DeclStmt to avoid this extra copy.
759 typedef llvm::SmallVector<Decl*,10> BufTy;
760 BufTy Buf(DS->decl_begin(), DS->decl_end());
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek4f880632009-07-17 22:18:43 +0000762 for (BufTy::reverse_iterator I = Buf.rbegin(), E = Buf.rend(); I != E; ++I) {
763 // Get the alignment of the new DeclStmt, padding out to >=8 bytes.
764 unsigned A = llvm::AlignOf<DeclStmt>::Alignment < 8
765 ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenek4f880632009-07-17 22:18:43 +0000767 // Allocate the DeclStmt using the BumpPtrAllocator. It will get
768 // automatically freed with the CFG.
769 DeclGroupRef DG(*I);
770 Decl *D = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000771 void *Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
Ted Kremenek4f880632009-07-17 22:18:43 +0000772 DeclStmt *DSNew = new (Mem) DeclStmt(DG, D->getLocation(), GetEndLoc(D));
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Ted Kremenek4f880632009-07-17 22:18:43 +0000774 // Append the fake DeclStmt to block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000775 AppendStmt(Block, DSNew);
Ted Kremenek4f880632009-07-17 22:18:43 +0000776 B = VisitDeclSubExpr(D);
777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
779 return B;
Ted Kremenek4f880632009-07-17 22:18:43 +0000780}
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Ted Kremenek4f880632009-07-17 22:18:43 +0000782/// VisitDeclSubExpr - Utility method to add block-level expressions for
783/// initializers in Decls.
784CFGBlock *CFGBuilder::VisitDeclSubExpr(Decl* D) {
785 assert(Block);
Ted Kremenekd34066c2008-02-26 00:22:58 +0000786
Ted Kremenek4f880632009-07-17 22:18:43 +0000787 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Ted Kremenek4f880632009-07-17 22:18:43 +0000789 if (!VD)
790 return Block;
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenek4f880632009-07-17 22:18:43 +0000792 Expr *Init = VD->getInit();
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek4f880632009-07-17 22:18:43 +0000794 if (Init) {
Ted Kremenek5ba290a2010-03-02 21:43:54 +0000795 AddStmtChoice::Kind k =
796 VD->getType()->isReferenceType() ? AddStmtChoice::AsLValueNotAlwaysAdd
797 : AddStmtChoice::NotAlwaysAdd;
798 Visit(Init, AddStmtChoice(k));
Ted Kremenek4f880632009-07-17 22:18:43 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Ted Kremenek4f880632009-07-17 22:18:43 +0000801 // If the type of VD is a VLA, then we must process its size expressions.
802 for (VariableArrayType* VA = FindVA(VD->getType().getTypePtr()); VA != 0;
803 VA = FindVA(VA->getElementType().getTypePtr()))
804 Block = addStmt(VA->getSizeExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenek4f880632009-07-17 22:18:43 +0000806 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000807}
808
809CFGBlock* CFGBuilder::VisitIfStmt(IfStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000810 // We may see an if statement in the middle of a basic block, or it may be the
811 // first statement we are processing. In either case, we create a new basic
812 // block. First, we create the blocks for the then...else statements, and
813 // then we create the block containing the if statement. If we were in the
Ted Kremenek6c249722009-09-24 18:45:41 +0000814 // middle of a block, we stop processing that block. That block is then the
815 // implicit successor for the "then" and "else" clauses.
Mike Stump6d9828c2009-07-17 01:31:16 +0000816
817 // The block we were proccessing is now finished. Make it the successor
818 // block.
819 if (Block) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000820 Succ = Block;
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000821 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000822 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000823 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000824
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000825 // Process the false branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000826 CFGBlock* ElseBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000827
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000828 if (Stmt* Else = I->getElse()) {
829 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000830
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000831 // NULL out Block so that the recursive call to Visit will
Mike Stump6d9828c2009-07-17 01:31:16 +0000832 // create a new basic block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000833 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000834 ElseBlock = addStmt(Else);
Mike Stump6d9828c2009-07-17 01:31:16 +0000835
Ted Kremenekb6f7b722007-08-30 18:13:31 +0000836 if (!ElseBlock) // Can occur when the Else body has all NullStmts.
837 ElseBlock = sv.get();
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000838 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000839 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000840 return 0;
841 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000842 }
Mike Stump6d9828c2009-07-17 01:31:16 +0000843
Ted Kremenekb6f1d782009-07-17 18:04:55 +0000844 // Process the true branch.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000845 CFGBlock* ThenBlock;
846 {
847 Stmt* Then = I->getThen();
Ted Kremenek6db0ad32010-01-19 20:46:35 +0000848 assert(Then);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000849 SaveAndRestore<CFGBlock*> sv(Succ);
Mike Stump6d9828c2009-07-17 01:31:16 +0000850 Block = NULL;
Ted Kremenek4f880632009-07-17 22:18:43 +0000851 ThenBlock = addStmt(Then);
Mike Stump6d9828c2009-07-17 01:31:16 +0000852
Ted Kremenekdbdf7942009-04-01 03:52:47 +0000853 if (!ThenBlock) {
854 // We can reach here if the "then" body has all NullStmts.
855 // Create an empty block so we can distinguish between true and false
856 // branches in path-sensitive analyses.
857 ThenBlock = createBlock(false);
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000858 AddSuccessor(ThenBlock, sv.get());
Mike Stump6d9828c2009-07-17 01:31:16 +0000859 } else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000860 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000861 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000862 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000863 }
864
Mike Stump6d9828c2009-07-17 01:31:16 +0000865 // Now create a new block containing the if statement.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000866 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000867
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000868 // Set the terminator of the new block to the If statement.
869 Block->setTerminator(I);
Mike Stump6d9828c2009-07-17 01:31:16 +0000870
Mike Stump00998a02009-07-23 23:25:26 +0000871 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +0000872 const TryResult &KnownVal = TryEvaluateBool(I->getCond());
Mike Stump00998a02009-07-23 23:25:26 +0000873
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000874 // Now add the successors.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000875 AddSuccessor(Block, KnownVal.isFalse() ? NULL : ThenBlock);
876 AddSuccessor(Block, KnownVal.isTrue()? NULL : ElseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +0000877
878 // Add the condition as the last statement in the new block. This may create
879 // new blocks as the condition may contain control-flow. Any newly created
880 // blocks will be pointed to be "Block".
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000881 Block = addStmt(I->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +0000882
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000883 // Finally, if the IfStmt contains a condition variable, add both the IfStmt
884 // and the condition variable initialization to the CFG.
885 if (VarDecl *VD = I->getConditionVariable()) {
886 if (Expr *Init = VD->getInit()) {
887 autoCreateBlock();
888 AppendStmt(Block, I, AddStmtChoice::AlwaysAdd);
889 addStmt(Init);
890 }
891 }
Ted Kremenekad5a8942010-08-02 23:46:59 +0000892
Ted Kremenek61dfbec2009-12-23 04:49:01 +0000893 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000894}
Mike Stump6d9828c2009-07-17 01:31:16 +0000895
896
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000897CFGBlock* CFGBuilder::VisitReturnStmt(ReturnStmt* R) {
Ted Kremenek6c249722009-09-24 18:45:41 +0000898 // If we were in the middle of a block we stop processing that block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000899 //
Mike Stump6d9828c2009-07-17 01:31:16 +0000900 // NOTE: If a "return" appears in the middle of a block, this means that the
901 // code afterwards is DEAD (unreachable). We still keep a basic block
902 // for that code; a simple "mark-and-sweep" from the entry block will be
903 // able to report such dead blocks.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000904
905 // Create the new block.
906 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +0000907
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000908 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000909 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +0000910
911 // Add the return statement to the block. This may create new blocks if R
912 // contains control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +0000913 return VisitStmt(R, AddStmtChoice::AlwaysAdd);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000914}
915
916CFGBlock* CFGBuilder::VisitLabelStmt(LabelStmt* L) {
917 // Get the block of the labeled statement. Add it to our map.
Ted Kremenek4f880632009-07-17 22:18:43 +0000918 addStmt(L->getSubStmt());
Ted Kremenek2677ea82008-03-15 07:45:02 +0000919 CFGBlock* LabelBlock = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +0000920
Ted Kremenek4f880632009-07-17 22:18:43 +0000921 if (!LabelBlock) // This can happen when the body is empty, i.e.
922 LabelBlock = createBlock(); // scopes that only contains NullStmts.
Mike Stump6d9828c2009-07-17 01:31:16 +0000923
Ted Kremenek4f880632009-07-17 22:18:43 +0000924 assert(LabelMap.find(L) == LabelMap.end() && "label already in map");
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000925 LabelMap[ L ] = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000926
927 // Labels partition blocks, so this is the end of the basic block we were
928 // processing (L is the block's label). Because this is label (and we have
929 // already processed the substatement) there is no extra control-flow to worry
930 // about.
Ted Kremenek9cffe732007-08-29 23:20:49 +0000931 LabelBlock->setLabel(L);
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000932 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000933 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +0000934
935 // We set Block to NULL to allow lazy creation of a new block (if necessary);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000936 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000937
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000938 // This block is now the implicit successor of other blocks.
939 Succ = LabelBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000940
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000941 return LabelBlock;
942}
943
944CFGBlock* CFGBuilder::VisitGotoStmt(GotoStmt* G) {
Mike Stump6d9828c2009-07-17 01:31:16 +0000945 // Goto is a control-flow statement. Thus we stop processing the current
946 // block and create a new one.
Ted Kremenek4f880632009-07-17 22:18:43 +0000947
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000948 Block = createBlock(false);
949 Block->setTerminator(G);
Mike Stump6d9828c2009-07-17 01:31:16 +0000950
951 // If we already know the mapping to the label block add the successor now.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000952 LabelMapTy::iterator I = LabelMap.find(G->getLabel());
Mike Stump6d9828c2009-07-17 01:31:16 +0000953
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000954 if (I == LabelMap.end())
955 // We will need to backpatch this block later.
956 BackpatchBlocks.push_back(Block);
957 else
Ted Kremenekee82d9b2009-10-12 20:55:07 +0000958 AddSuccessor(Block, I->second);
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000959
Mike Stump6d9828c2009-07-17 01:31:16 +0000960 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000961}
962
963CFGBlock* CFGBuilder::VisitForStmt(ForStmt* F) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000964 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +0000965
Mike Stumpfefb9f72009-07-21 01:12:51 +0000966 // "for" is a control-flow statement. Thus we stop processing the current
967 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000968 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +0000969 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +0000970 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +0000971 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +0000972 } else
973 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +0000974
Ted Kremenek3f64a0e2010-05-21 20:30:15 +0000975 // Save the current value for the break targets.
976 // All breaks should go to the code following the loop.
977 SaveAndRestore<CFGBlock*> save_break(BreakTargetBlock);
978 BreakTargetBlock = LoopSuccessor;
979
Mike Stump6d9828c2009-07-17 01:31:16 +0000980 // Because of short-circuit evaluation, the condition of the loop can span
981 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
982 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000983 CFGBlock* ExitConditionBlock = createBlock(false);
984 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +0000985
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000986 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +0000987 ExitConditionBlock->setTerminator(F);
988
989 // Now add the actual condition to the condition block. Because the condition
990 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +0000991 if (Stmt* C = F->getCond()) {
992 Block = ExitConditionBlock;
993 EntryConditionBlock = addStmt(C);
Ted Kremenek8f3b8342010-09-15 07:01:20 +0000994 assert(Block == EntryConditionBlock ||
995 (Block == 0 && EntryConditionBlock == Succ));
Ted Kremenek58b87fe2009-12-24 01:49:06 +0000996
997 // If this block contains a condition variable, add both the condition
998 // variable and initializer to the CFG.
999 if (VarDecl *VD = F->getConditionVariable()) {
1000 if (Expr *Init = VD->getInit()) {
1001 autoCreateBlock();
1002 AppendStmt(Block, F, AddStmtChoice::AlwaysAdd);
1003 EntryConditionBlock = addStmt(Init);
1004 assert(Block == EntryConditionBlock);
1005 }
1006 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001007
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001008 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001009 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001010 return 0;
1011 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001012 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001013
Mike Stump6d9828c2009-07-17 01:31:16 +00001014 // The condition block is the implicit successor for the loop body as well as
1015 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001016 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001017
Mike Stump00998a02009-07-23 23:25:26 +00001018 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001019 TryResult KnownVal(true);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Mike Stump00998a02009-07-23 23:25:26 +00001021 if (F->getCond())
1022 KnownVal = TryEvaluateBool(F->getCond());
1023
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001024 // Now create the loop body.
1025 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001026 assert(F->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001027
Ted Kremenek3f64a0e2010-05-21 20:30:15 +00001028 // Save the current values for Block, Succ, and continue targets.
1029 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1030 save_continue(ContinueTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001031
Ted Kremenekaf603f72007-08-30 18:39:40 +00001032 // Create a new block to contain the (bottom) of the loop body.
1033 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001034
Ted Kremeneke9334502008-09-04 21:48:47 +00001035 if (Stmt* I = F->getInc()) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001036 // Generate increment code in its own basic block. This is the target of
1037 // continue statements.
Ted Kremenek4f880632009-07-17 22:18:43 +00001038 Succ = addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001039 } else {
1040 // No increment code. Create a special, empty, block that is used as the
1041 // target block for "looping back" to the start of the loop.
Ted Kremenek3575f842009-04-28 00:51:56 +00001042 assert(Succ == EntryConditionBlock);
1043 Succ = createBlock();
Ted Kremeneke9334502008-09-04 21:48:47 +00001044 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001045
Ted Kremenek3575f842009-04-28 00:51:56 +00001046 // Finish up the increment (or empty) block if it hasn't been already.
1047 if (Block) {
1048 assert(Block == Succ);
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001049 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001050 return 0;
Ted Kremenek3575f842009-04-28 00:51:56 +00001051 Block = 0;
1052 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001053
Ted Kremenek3575f842009-04-28 00:51:56 +00001054 ContinueTargetBlock = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001055
Ted Kremenek3575f842009-04-28 00:51:56 +00001056 // The starting block for the loop increment is the block that should
1057 // represent the 'loop target' for looping back to the start of the loop.
1058 ContinueTargetBlock->setLoopTarget(F);
1059
Mike Stump6d9828c2009-07-17 01:31:16 +00001060 // Now populate the body block, and in the process create new blocks as we
1061 // walk the body of the loop.
Ted Kremenek4f880632009-07-17 22:18:43 +00001062 CFGBlock* BodyBlock = addStmt(F->getBody());
Ted Kremenekaf603f72007-08-30 18:39:40 +00001063
1064 if (!BodyBlock)
Zhongxing Xu1d4b2182009-08-20 02:56:48 +00001065 BodyBlock = ContinueTargetBlock; // can happen for "for (...;...;...) ;"
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001066 else if (badCFG)
Ted Kremenek941fde82009-07-24 04:47:11 +00001067 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001068
Ted Kremenek941fde82009-07-24 04:47:11 +00001069 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001070 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001071 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001072
Ted Kremenek941fde82009-07-24 04:47:11 +00001073 // Link up the condition block with the code that follows the loop. (the
1074 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001075 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001076
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001077 // If the loop contains initialization, create a new block for those
Mike Stump6d9828c2009-07-17 01:31:16 +00001078 // statements. This block can also contain statements that precede the loop.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001079 if (Stmt* I = F->getInit()) {
1080 Block = createBlock();
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001081 return addStmt(I);
Mike Stump6d9828c2009-07-17 01:31:16 +00001082 } else {
1083 // There is no loop initialization. We are thus basically a while loop.
1084 // NULL out Block to force lazy block construction.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001085 Block = NULL;
Ted Kremenek54827132008-02-27 07:20:00 +00001086 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001087 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001088 }
1089}
1090
Ted Kremenek115c1b92010-04-11 17:02:10 +00001091CFGBlock *CFGBuilder::VisitMemberExpr(MemberExpr *M, AddStmtChoice asc) {
1092 if (asc.alwaysAdd()) {
1093 autoCreateBlock();
1094 AppendStmt(Block, M, asc);
1095 }
1096 return Visit(M->getBase(),
1097 M->isArrow() ? AddStmtChoice::NotAlwaysAdd
1098 : AddStmtChoice::AsLValueNotAlwaysAdd);
1099}
1100
Ted Kremenek514de5a2008-11-11 17:10:00 +00001101CFGBlock* CFGBuilder::VisitObjCForCollectionStmt(ObjCForCollectionStmt* S) {
1102 // Objective-C fast enumeration 'for' statements:
1103 // http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC
1104 //
1105 // for ( Type newVariable in collection_expression ) { statements }
1106 //
1107 // becomes:
1108 //
1109 // prologue:
1110 // 1. collection_expression
1111 // T. jump to loop_entry
1112 // loop_entry:
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001113 // 1. side-effects of element expression
Ted Kremenek514de5a2008-11-11 17:10:00 +00001114 // 1. ObjCForCollectionStmt [performs binding to newVariable]
1115 // T. ObjCForCollectionStmt TB, FB [jumps to TB if newVariable != nil]
1116 // TB:
1117 // statements
1118 // T. jump to loop_entry
1119 // FB:
1120 // what comes after
1121 //
1122 // and
1123 //
1124 // Type existingItem;
1125 // for ( existingItem in expression ) { statements }
1126 //
1127 // becomes:
1128 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001129 // the same with newVariable replaced with existingItem; the binding works
1130 // the same except that for one ObjCForCollectionStmt::getElement() returns
1131 // a DeclStmt and the other returns a DeclRefExpr.
Ted Kremenek514de5a2008-11-11 17:10:00 +00001132 //
Mike Stump6d9828c2009-07-17 01:31:16 +00001133
Ted Kremenek514de5a2008-11-11 17:10:00 +00001134 CFGBlock* LoopSuccessor = 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001135
Ted Kremenek514de5a2008-11-11 17:10:00 +00001136 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001137 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001138 return 0;
Ted Kremenek514de5a2008-11-11 17:10:00 +00001139 LoopSuccessor = Block;
1140 Block = 0;
Ted Kremenek4f880632009-07-17 22:18:43 +00001141 } else
1142 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001143
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001144 // Build the condition blocks.
1145 CFGBlock* ExitConditionBlock = createBlock(false);
1146 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001147
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001148 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001149 ExitConditionBlock->setTerminator(S);
1150
1151 // The last statement in the block should be the ObjCForCollectionStmt, which
1152 // performs the actual binding to 'element' and determines if there are any
1153 // more items in the collection.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001154 AppendStmt(ExitConditionBlock, S);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001155 Block = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001156
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001157 // Walk the 'element' expression to see if there are any side-effects. We
Mike Stump6d9828c2009-07-17 01:31:16 +00001158 // generate new blocks as necesary. We DON'T add the statement by default to
1159 // the CFG unless it contains control-flow.
Ted Kremenek852274d2009-12-16 03:18:58 +00001160 EntryConditionBlock = Visit(S->getElement(), AddStmtChoice::NotAlwaysAdd);
Mike Stump6d9828c2009-07-17 01:31:16 +00001161 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001162 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001163 return 0;
1164 Block = 0;
1165 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001166
1167 // The condition block is the implicit successor for the loop body as well as
1168 // any code above the loop.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001169 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001170
Ted Kremenek514de5a2008-11-11 17:10:00 +00001171 // Now create the true branch.
Mike Stump6d9828c2009-07-17 01:31:16 +00001172 {
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001173 // Save the current values for Succ, continue and break targets.
1174 SaveAndRestore<CFGBlock*> save_Succ(Succ),
Mike Stump6d9828c2009-07-17 01:31:16 +00001175 save_continue(ContinueTargetBlock), save_break(BreakTargetBlock);
1176
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001177 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001178 ContinueTargetBlock = EntryConditionBlock;
1179
Ted Kremenek4f880632009-07-17 22:18:43 +00001180 CFGBlock* BodyBlock = addStmt(S->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001181
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001182 if (!BodyBlock)
1183 BodyBlock = EntryConditionBlock; // can happen for "for (X in Y) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001184 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001185 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001186 return 0;
1187 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001188
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001189 // This new body block is a successor to our "exit" condition block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001190 AddSuccessor(ExitConditionBlock, BodyBlock);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001191 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001192
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001193 // Link up the condition block with the code that follows the loop.
1194 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001195 AddSuccessor(ExitConditionBlock, LoopSuccessor);
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001196
Ted Kremenek514de5a2008-11-11 17:10:00 +00001197 // Now create a prologue block to contain the collection expression.
Ted Kremenek4cb3a852008-11-14 01:57:41 +00001198 Block = createBlock();
Ted Kremenek514de5a2008-11-11 17:10:00 +00001199 return addStmt(S->getCollection());
Mike Stump6d9828c2009-07-17 01:31:16 +00001200}
1201
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001202CFGBlock* CFGBuilder::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt* S) {
1203 // FIXME: Add locking 'primitives' to CFG for @synchronized.
Mike Stump6d9828c2009-07-17 01:31:16 +00001204
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001205 // Inline the body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001206 CFGBlock *SyncBlock = addStmt(S->getSynchBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001207
Ted Kremenekda5348e2009-05-05 23:11:51 +00001208 // The sync body starts its own basic block. This makes it a little easier
1209 // for diagnostic clients.
1210 if (SyncBlock) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001211 if (badCFG)
Ted Kremenekda5348e2009-05-05 23:11:51 +00001212 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001213
Ted Kremenekda5348e2009-05-05 23:11:51 +00001214 Block = 0;
Ted Kremenekfadebba2010-05-13 16:38:08 +00001215 Succ = SyncBlock;
Ted Kremenekda5348e2009-05-05 23:11:51 +00001216 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001217
Ted Kremenek4beaa9f2010-09-10 03:05:33 +00001218 // Add the @synchronized to the CFG.
1219 autoCreateBlock();
1220 AppendStmt(Block, S, AddStmtChoice::AlwaysAdd);
1221
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001222 // Inline the sync expression.
Ted Kremenek4f880632009-07-17 22:18:43 +00001223 return addStmt(S->getSynchExpr());
Ted Kremenekb3b0b362009-05-02 01:49:13 +00001224}
Mike Stump6d9828c2009-07-17 01:31:16 +00001225
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001226CFGBlock* CFGBuilder::VisitObjCAtTryStmt(ObjCAtTryStmt* S) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001227 // FIXME
Ted Kremenek90658ec2009-04-07 04:26:02 +00001228 return NYS();
Ted Kremeneke31c0d22009-03-30 22:29:21 +00001229}
Ted Kremenek514de5a2008-11-11 17:10:00 +00001230
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001231CFGBlock* CFGBuilder::VisitWhileStmt(WhileStmt* W) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001232 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001233
Mike Stumpfefb9f72009-07-21 01:12:51 +00001234 // "while" is a control-flow statement. Thus we stop processing the current
1235 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001236 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001237 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001238 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001239 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001240 } else
1241 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001242
1243 // Because of short-circuit evaluation, the condition of the loop can span
1244 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1245 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001246 CFGBlock* ExitConditionBlock = createBlock(false);
1247 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001248
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001249 // Set the terminator for the "exit" condition block.
1250 ExitConditionBlock->setTerminator(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001251
1252 // Now add the actual condition to the condition block. Because the condition
1253 // itself may contain control-flow, new blocks may be created. Thus we update
1254 // "Succ" after adding the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001255 if (Stmt* C = W->getCond()) {
1256 Block = ExitConditionBlock;
1257 EntryConditionBlock = addStmt(C);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001258 assert(Block == EntryConditionBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001259
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001260 // If this block contains a condition variable, add both the condition
1261 // variable and initializer to the CFG.
1262 if (VarDecl *VD = W->getConditionVariable()) {
1263 if (Expr *Init = VD->getInit()) {
1264 autoCreateBlock();
1265 AppendStmt(Block, W, AddStmtChoice::AlwaysAdd);
1266 EntryConditionBlock = addStmt(Init);
1267 assert(Block == EntryConditionBlock);
1268 }
1269 }
1270
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001271 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001272 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001273 return 0;
1274 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001275 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001276
1277 // The condition block is the implicit successor for the loop body as well as
1278 // any code above the loop.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001279 Succ = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001280
Mike Stump00998a02009-07-23 23:25:26 +00001281 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001282 const TryResult& KnownVal = TryEvaluateBool(W->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001283
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001284 // Process the loop body.
1285 {
Ted Kremenekf6e85412009-04-28 03:09:44 +00001286 assert(W->getBody());
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001287
1288 // Save the current values for Block, Succ, and continue and break targets
1289 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
1290 save_continue(ContinueTargetBlock),
1291 save_break(BreakTargetBlock);
Ted Kremenekf6e85412009-04-28 03:09:44 +00001292
Mike Stump6d9828c2009-07-17 01:31:16 +00001293 // Create an empty block to represent the transition block for looping back
1294 // to the head of the loop.
Ted Kremenekf6e85412009-04-28 03:09:44 +00001295 Block = 0;
1296 assert(Succ == EntryConditionBlock);
1297 Succ = createBlock();
1298 Succ->setLoopTarget(W);
Mike Stump6d9828c2009-07-17 01:31:16 +00001299 ContinueTargetBlock = Succ;
1300
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001301 // All breaks should go to the code following the loop.
1302 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001303
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001304 // NULL out Block to force lazy instantiation of blocks for the body.
1305 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001306
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001307 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001308 CFGBlock* BodyBlock = addStmt(W->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001309
Ted Kremenekaf603f72007-08-30 18:39:40 +00001310 if (!BodyBlock)
Zhongxing Xud5c3b132009-08-20 03:21:49 +00001311 BodyBlock = ContinueTargetBlock; // can happen for "while(...) ;"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001312 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001313 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001314 return 0;
1315 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001316
Ted Kremenek941fde82009-07-24 04:47:11 +00001317 // Add the loop body entry as a successor to the condition.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001318 AddSuccessor(ExitConditionBlock, KnownVal.isFalse() ? NULL : BodyBlock);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001319 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001320
Ted Kremenek941fde82009-07-24 04:47:11 +00001321 // Link up the condition block with the code that follows the loop. (the
1322 // false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001323 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001324
1325 // There can be no more statements in the condition block since we loop back
1326 // to this block. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001327 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001328
Ted Kremenek4ec010a2009-12-24 01:34:10 +00001329 // Return the condition block, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001330 Succ = EntryConditionBlock;
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001331 return EntryConditionBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001332}
Mike Stump1eb44332009-09-09 15:08:12 +00001333
1334
Ted Kremenek4f880632009-07-17 22:18:43 +00001335CFGBlock *CFGBuilder::VisitObjCAtCatchStmt(ObjCAtCatchStmt* S) {
1336 // FIXME: For now we pretend that @catch and the code it contains does not
1337 // exit.
1338 return Block;
1339}
Mike Stump6d9828c2009-07-17 01:31:16 +00001340
Ted Kremenek2fda5042008-12-09 20:20:09 +00001341CFGBlock* CFGBuilder::VisitObjCAtThrowStmt(ObjCAtThrowStmt* S) {
1342 // FIXME: This isn't complete. We basically treat @throw like a return
1343 // statement.
Mike Stump6d9828c2009-07-17 01:31:16 +00001344
Ted Kremenek6c249722009-09-24 18:45:41 +00001345 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001346 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001347 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001348
Ted Kremenek2fda5042008-12-09 20:20:09 +00001349 // Create the new block.
1350 Block = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001351
Ted Kremenek2fda5042008-12-09 20:20:09 +00001352 // The Exit block is the only successor.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001353 AddSuccessor(Block, &cfg->getExit());
Mike Stump6d9828c2009-07-17 01:31:16 +00001354
1355 // Add the statement to the block. This may create new blocks if S contains
1356 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001357 return VisitStmt(S, AddStmtChoice::AlwaysAdd);
Ted Kremenek2fda5042008-12-09 20:20:09 +00001358}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001359
Mike Stump0979d802009-07-22 22:56:04 +00001360CFGBlock* CFGBuilder::VisitCXXThrowExpr(CXXThrowExpr* T) {
Ted Kremenek6c249722009-09-24 18:45:41 +00001361 // If we were in the middle of a block we stop processing that block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001362 if (badCFG)
Mike Stump0979d802009-07-22 22:56:04 +00001363 return 0;
1364
1365 // Create the new block.
1366 Block = createBlock(false);
1367
Mike Stump5d1d2022010-01-19 02:20:09 +00001368 if (TryTerminatedBlock)
1369 // The current try statement is the only successor.
1370 AddSuccessor(Block, TryTerminatedBlock);
Ted Kremenekad5a8942010-08-02 23:46:59 +00001371 else
Mike Stump5d1d2022010-01-19 02:20:09 +00001372 // otherwise the Exit block is the only successor.
1373 AddSuccessor(Block, &cfg->getExit());
Mike Stump0979d802009-07-22 22:56:04 +00001374
1375 // Add the statement to the block. This may create new blocks if S contains
1376 // control-flow (short-circuit operations).
Ted Kremenek852274d2009-12-16 03:18:58 +00001377 return VisitStmt(T, AddStmtChoice::AlwaysAdd);
Mike Stump0979d802009-07-22 22:56:04 +00001378}
1379
Ted Kremenek4f880632009-07-17 22:18:43 +00001380CFGBlock *CFGBuilder::VisitDoStmt(DoStmt* D) {
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001381 CFGBlock* LoopSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001382
Mike Stump8f9893a2009-07-21 01:27:50 +00001383 // "do...while" is a control-flow statement. Thus we stop processing the
1384 // current block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001385 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001386 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001387 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001388 LoopSuccessor = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001389 } else
1390 LoopSuccessor = Succ;
Mike Stump6d9828c2009-07-17 01:31:16 +00001391
1392 // Because of short-circuit evaluation, the condition of the loop can span
1393 // multiple basic blocks. Thus we need the "Entry" and "Exit" blocks that
1394 // evaluate the condition.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001395 CFGBlock* ExitConditionBlock = createBlock(false);
1396 CFGBlock* EntryConditionBlock = ExitConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001397
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001398 // Set the terminator for the "exit" condition block.
Mike Stump6d9828c2009-07-17 01:31:16 +00001399 ExitConditionBlock->setTerminator(D);
1400
1401 // Now add the actual condition to the condition block. Because the condition
1402 // itself may contain control-flow, new blocks may be created.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001403 if (Stmt* C = D->getCond()) {
1404 Block = ExitConditionBlock;
1405 EntryConditionBlock = addStmt(C);
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001406 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001407 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001408 return 0;
1409 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001410 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001411
Ted Kremenek54827132008-02-27 07:20:00 +00001412 // The condition block is the implicit successor for the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001413 Succ = EntryConditionBlock;
1414
Mike Stump00998a02009-07-23 23:25:26 +00001415 // See if this is a known constant.
Ted Kremenek941fde82009-07-24 04:47:11 +00001416 const TryResult &KnownVal = TryEvaluateBool(D->getCond());
Mike Stump00998a02009-07-23 23:25:26 +00001417
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001418 // Process the loop body.
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001419 CFGBlock* BodyBlock = NULL;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001420 {
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001421 assert(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001422
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001423 // Save the current values for Block, Succ, and continue and break targets
1424 SaveAndRestore<CFGBlock*> save_Block(Block), save_Succ(Succ),
Mike Stump5d1d2022010-01-19 02:20:09 +00001425 save_continue(ContinueTargetBlock),
1426 save_break(BreakTargetBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001427
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001428 // All continues within this loop should go to the condition block
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001429 ContinueTargetBlock = EntryConditionBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001430
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001431 // All breaks should go to the code following the loop.
1432 BreakTargetBlock = LoopSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001433
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001434 // NULL out Block to force lazy instantiation of blocks for the body.
1435 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001436
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001437 // Create the body. The returned block is the entry to the loop body.
Ted Kremenek4f880632009-07-17 22:18:43 +00001438 BodyBlock = addStmt(D->getBody());
Mike Stump6d9828c2009-07-17 01:31:16 +00001439
Ted Kremenekaf603f72007-08-30 18:39:40 +00001440 if (!BodyBlock)
Ted Kremeneka9d996d2008-02-27 00:28:17 +00001441 BodyBlock = EntryConditionBlock; // can happen for "do ; while(...)"
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001442 else if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001443 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001444 return 0;
1445 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001446
Ted Kremenekd173dc72010-08-17 20:59:56 +00001447 if (!KnownVal.isFalse()) {
1448 // Add an intermediate block between the BodyBlock and the
1449 // ExitConditionBlock to represent the "loop back" transition. Create an
1450 // empty block to represent the transition block for looping back to the
1451 // head of the loop.
1452 // FIXME: Can we do this more efficiently without adding another block?
1453 Block = NULL;
1454 Succ = BodyBlock;
1455 CFGBlock *LoopBackBlock = createBlock();
1456 LoopBackBlock->setLoopTarget(D);
Mike Stump6d9828c2009-07-17 01:31:16 +00001457
Ted Kremenekd173dc72010-08-17 20:59:56 +00001458 // Add the loop body entry as a successor to the condition.
1459 AddSuccessor(ExitConditionBlock, LoopBackBlock);
1460 }
1461 else
1462 AddSuccessor(ExitConditionBlock, NULL);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001463 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001464
Ted Kremenek941fde82009-07-24 04:47:11 +00001465 // Link up the condition block with the code that follows the loop.
1466 // (the false branch).
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001467 AddSuccessor(ExitConditionBlock, KnownVal.isTrue() ? NULL : LoopSuccessor);
Mike Stump6d9828c2009-07-17 01:31:16 +00001468
1469 // There can be no more statements in the body block(s) since we loop back to
1470 // the body. NULL out Block to force lazy creation of another block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001471 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001472
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001473 // Return the loop body, which is the dominating block for the loop.
Ted Kremenek54827132008-02-27 07:20:00 +00001474 Succ = BodyBlock;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001475 return BodyBlock;
1476}
1477
1478CFGBlock* CFGBuilder::VisitContinueStmt(ContinueStmt* C) {
1479 // "continue" is a control-flow statement. Thus we stop processing the
1480 // current block.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001481 if (badCFG)
1482 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001483
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001484 // Now create a new block that ends with the continue statement.
1485 Block = createBlock(false);
1486 Block->setTerminator(C);
Mike Stump6d9828c2009-07-17 01:31:16 +00001487
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001488 // If there is no target for the continue, then we are looking at an
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001489 // incomplete AST. This means the CFG cannot be constructed.
1490 if (ContinueTargetBlock)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001491 AddSuccessor(Block, ContinueTargetBlock);
Ted Kremenek235c5ed2009-04-07 18:53:24 +00001492 else
1493 badCFG = true;
Mike Stump6d9828c2009-07-17 01:31:16 +00001494
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001495 return Block;
1496}
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001498CFGBlock *CFGBuilder::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E,
Ted Kremenek852274d2009-12-16 03:18:58 +00001499 AddStmtChoice asc) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001500
Ted Kremenek852274d2009-12-16 03:18:58 +00001501 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001502 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001503 AppendStmt(Block, E);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Ted Kremenek4f880632009-07-17 22:18:43 +00001506 // VLA types have expressions that must be evaluated.
1507 if (E->isArgumentType()) {
1508 for (VariableArrayType* VA = FindVA(E->getArgumentType().getTypePtr());
1509 VA != 0; VA = FindVA(VA->getElementType().getTypePtr()))
1510 addStmt(VA->getSizeExpr());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001511 }
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Mike Stump6d9828c2009-07-17 01:31:16 +00001513 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001514}
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Ted Kremenek4f880632009-07-17 22:18:43 +00001516/// VisitStmtExpr - Utility method to handle (nested) statement
1517/// expressions (a GCC extension).
Ted Kremenek852274d2009-12-16 03:18:58 +00001518CFGBlock* CFGBuilder::VisitStmtExpr(StmtExpr *SE, AddStmtChoice asc) {
1519 if (asc.alwaysAdd()) {
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001520 autoCreateBlock();
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001521 AppendStmt(Block, SE);
Ted Kremenek13fc08a2009-07-18 00:47:21 +00001522 }
Ted Kremenek4f880632009-07-17 22:18:43 +00001523 return VisitCompoundStmt(SE->getSubStmt());
1524}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001525
Ted Kremenek411cdee2008-04-16 21:10:48 +00001526CFGBlock* CFGBuilder::VisitSwitchStmt(SwitchStmt* Terminator) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001527 // "switch" is a control-flow statement. Thus we stop processing the current
1528 // block.
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001529 CFGBlock* SwitchSuccessor = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001530
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001531 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001532 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001533 return 0;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001534 SwitchSuccessor = Block;
Mike Stump6d9828c2009-07-17 01:31:16 +00001535 } else SwitchSuccessor = Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001536
1537 // Save the current "switch" context.
1538 SaveAndRestore<CFGBlock*> save_switch(SwitchTerminatedBlock),
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001539 save_break(BreakTargetBlock),
1540 save_default(DefaultCaseBlock);
1541
Mike Stump6d9828c2009-07-17 01:31:16 +00001542 // Set the "default" case to be the block after the switch statement. If the
1543 // switch statement contains a "default:", this value will be overwritten with
1544 // the block for that code.
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001545 DefaultCaseBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001546
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001547 // Create a new block that will contain the switch statement.
1548 SwitchTerminatedBlock = createBlock(false);
Mike Stump6d9828c2009-07-17 01:31:16 +00001549
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001550 // Now process the switch body. The code after the switch is the implicit
1551 // successor.
1552 Succ = SwitchSuccessor;
1553 BreakTargetBlock = SwitchSuccessor;
Mike Stump6d9828c2009-07-17 01:31:16 +00001554
1555 // When visiting the body, the case statements should automatically get linked
1556 // up to the switch. We also don't keep a pointer to the body, since all
1557 // control-flow from the switch goes to case/default statements.
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001558 assert(Terminator->getBody() && "switch must contain a non-NULL body");
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001559 Block = NULL;
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001560 addStmt(Terminator->getBody());
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001561 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001562 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001563 return 0;
1564 }
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001565
Mike Stump6d9828c2009-07-17 01:31:16 +00001566 // If we have no "default:" case, the default transition is to the code
1567 // following the switch body.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001568 AddSuccessor(SwitchTerminatedBlock, DefaultCaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001569
Ted Kremenek49af7cb2007-08-27 19:46:09 +00001570 // Add the terminator and condition in the switch block.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001571 SwitchTerminatedBlock->setTerminator(Terminator);
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001572 assert(Terminator->getCond() && "switch condition must be non-NULL");
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001573 Block = SwitchTerminatedBlock;
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001574 Block = addStmt(Terminator->getCond());
Ted Kremenekad5a8942010-08-02 23:46:59 +00001575
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001576 // Finally, if the SwitchStmt contains a condition variable, add both the
1577 // SwitchStmt and the condition variable initialization to the CFG.
1578 if (VarDecl *VD = Terminator->getConditionVariable()) {
1579 if (Expr *Init = VD->getInit()) {
1580 autoCreateBlock();
1581 AppendStmt(Block, Terminator, AddStmtChoice::AlwaysAdd);
1582 addStmt(Init);
1583 }
1584 }
Ted Kremenekad5a8942010-08-02 23:46:59 +00001585
Ted Kremenek6b501eb2009-12-24 00:39:26 +00001586 return Block;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001587}
1588
Ted Kremenek4f880632009-07-17 22:18:43 +00001589CFGBlock* CFGBuilder::VisitCaseStmt(CaseStmt* CS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001590 // CaseStmts are essentially labels, so they are the first statement in a
1591 // block.
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001592 CFGBlock *TopBlock = 0, *LastBlock = 0;
1593
1594 if (Stmt *Sub = CS->getSubStmt()) {
1595 // For deeply nested chains of CaseStmts, instead of doing a recursion
1596 // (which can blow out the stack), manually unroll and create blocks
1597 // along the way.
1598 while (isa<CaseStmt>(Sub)) {
1599 CFGBlock *CurrentBlock = createBlock(false);
1600 CurrentBlock->setLabel(CS);
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001601
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001602 if (TopBlock)
1603 AddSuccessor(LastBlock, CurrentBlock);
1604 else
1605 TopBlock = CurrentBlock;
1606
1607 AddSuccessor(SwitchTerminatedBlock, CurrentBlock);
1608 LastBlock = CurrentBlock;
1609
1610 CS = cast<CaseStmt>(Sub);
1611 Sub = CS->getSubStmt();
1612 }
1613
1614 addStmt(Sub);
1615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Ted Kremenek29ccaa12007-08-30 18:48:11 +00001617 CFGBlock* CaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001618 if (!CaseBlock)
1619 CaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001620
1621 // Cases statements partition blocks, so this is the top of the basic block we
1622 // were processing (the "case XXX:" is the label).
Ted Kremenek4f880632009-07-17 22:18:43 +00001623 CaseBlock->setLabel(CS);
1624
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001625 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001626 return 0;
Mike Stump6d9828c2009-07-17 01:31:16 +00001627
1628 // Add this block to the list of successors for the block with the switch
1629 // statement.
Ted Kremenek4f880632009-07-17 22:18:43 +00001630 assert(SwitchTerminatedBlock);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001631 AddSuccessor(SwitchTerminatedBlock, CaseBlock);
Mike Stump6d9828c2009-07-17 01:31:16 +00001632
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001633 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1634 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001635
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001636 if (TopBlock) {
1637 AddSuccessor(LastBlock, CaseBlock);
1638 Succ = TopBlock;
1639 }
1640 else {
1641 // This block is now the implicit successor of other blocks.
1642 Succ = CaseBlock;
1643 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001644
Ted Kremenek0fc67e22010-08-04 23:54:30 +00001645 return Succ;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001646}
Mike Stump6d9828c2009-07-17 01:31:16 +00001647
Ted Kremenek411cdee2008-04-16 21:10:48 +00001648CFGBlock* CFGBuilder::VisitDefaultStmt(DefaultStmt* Terminator) {
Ted Kremenek4f880632009-07-17 22:18:43 +00001649 if (Terminator->getSubStmt())
1650 addStmt(Terminator->getSubStmt());
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001652 DefaultCaseBlock = Block;
Ted Kremenek4f880632009-07-17 22:18:43 +00001653
1654 if (!DefaultCaseBlock)
1655 DefaultCaseBlock = createBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001656
1657 // Default statements partition blocks, so this is the top of the basic block
1658 // we were processing (the "default:" is the label).
Ted Kremenek411cdee2008-04-16 21:10:48 +00001659 DefaultCaseBlock->setLabel(Terminator);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001661 if (badCFG)
Ted Kremenek4e8df2e2009-05-02 00:13:27 +00001662 return 0;
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001663
Mike Stump6d9828c2009-07-17 01:31:16 +00001664 // Unlike case statements, we don't add the default block to the successors
1665 // for the switch statement immediately. This is done when we finish
1666 // processing the switch statement. This allows for the default case
1667 // (including a fall-through to the code after the switch statement) to always
1668 // be the last successor of a switch-terminated block.
1669
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001670 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1671 Block = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00001672
Ted Kremenekeef5a9a2008-02-13 22:05:39 +00001673 // This block is now the implicit successor of other blocks.
1674 Succ = DefaultCaseBlock;
Mike Stump6d9828c2009-07-17 01:31:16 +00001675
1676 return DefaultCaseBlock;
Ted Kremenek295222c2008-02-13 21:46:34 +00001677}
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001678
Mike Stump5d1d2022010-01-19 02:20:09 +00001679CFGBlock *CFGBuilder::VisitCXXTryStmt(CXXTryStmt *Terminator) {
1680 // "try"/"catch" is a control-flow statement. Thus we stop processing the
1681 // current block.
1682 CFGBlock* TrySuccessor = NULL;
1683
1684 if (Block) {
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001685 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00001686 return 0;
1687 TrySuccessor = Block;
1688 } else TrySuccessor = Succ;
1689
Mike Stumpa1f93632010-01-20 01:15:34 +00001690 CFGBlock *PrevTryTerminatedBlock = TryTerminatedBlock;
Mike Stump5d1d2022010-01-19 02:20:09 +00001691
1692 // Create a new block that will contain the try statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001693 CFGBlock *NewTryTerminatedBlock = createBlock(false);
Mike Stump5d1d2022010-01-19 02:20:09 +00001694 // Add the terminator in the try block.
Mike Stumpf00cca52010-01-20 01:30:58 +00001695 NewTryTerminatedBlock->setTerminator(Terminator);
Mike Stump5d1d2022010-01-19 02:20:09 +00001696
Mike Stumpa1f93632010-01-20 01:15:34 +00001697 bool HasCatchAll = false;
Mike Stump5d1d2022010-01-19 02:20:09 +00001698 for (unsigned h = 0; h <Terminator->getNumHandlers(); ++h) {
1699 // The code after the try is the implicit successor.
1700 Succ = TrySuccessor;
1701 CXXCatchStmt *CS = Terminator->getHandler(h);
Mike Stumpa1f93632010-01-20 01:15:34 +00001702 if (CS->getExceptionDecl() == 0) {
1703 HasCatchAll = true;
1704 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001705 Block = NULL;
1706 CFGBlock *CatchBlock = VisitCXXCatchStmt(CS);
1707 if (CatchBlock == 0)
1708 return 0;
1709 // Add this block to the list of successors for the block with the try
1710 // statement.
Mike Stumpf00cca52010-01-20 01:30:58 +00001711 AddSuccessor(NewTryTerminatedBlock, CatchBlock);
Mike Stump5d1d2022010-01-19 02:20:09 +00001712 }
Mike Stumpa1f93632010-01-20 01:15:34 +00001713 if (!HasCatchAll) {
1714 if (PrevTryTerminatedBlock)
Mike Stumpf00cca52010-01-20 01:30:58 +00001715 AddSuccessor(NewTryTerminatedBlock, PrevTryTerminatedBlock);
Mike Stumpa1f93632010-01-20 01:15:34 +00001716 else
Mike Stumpf00cca52010-01-20 01:30:58 +00001717 AddSuccessor(NewTryTerminatedBlock, &cfg->getExit());
Mike Stumpa1f93632010-01-20 01:15:34 +00001718 }
Mike Stump5d1d2022010-01-19 02:20:09 +00001719
1720 // The code after the try is the implicit successor.
1721 Succ = TrySuccessor;
1722
Mike Stumpf00cca52010-01-20 01:30:58 +00001723 // Save the current "try" context.
1724 SaveAndRestore<CFGBlock*> save_try(TryTerminatedBlock);
1725 TryTerminatedBlock = NewTryTerminatedBlock;
1726
Ted Kremenek6db0ad32010-01-19 20:46:35 +00001727 assert(Terminator->getTryBlock() && "try must contain a non-NULL body");
Mike Stump5d1d2022010-01-19 02:20:09 +00001728 Block = NULL;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001729 Block = addStmt(Terminator->getTryBlock());
Mike Stump5d1d2022010-01-19 02:20:09 +00001730 return Block;
1731}
1732
1733CFGBlock* CFGBuilder::VisitCXXCatchStmt(CXXCatchStmt* CS) {
1734 // CXXCatchStmt are treated like labels, so they are the first statement in a
1735 // block.
1736
1737 if (CS->getHandlerBlock())
1738 addStmt(CS->getHandlerBlock());
1739
1740 CFGBlock* CatchBlock = Block;
1741 if (!CatchBlock)
1742 CatchBlock = createBlock();
1743
1744 CatchBlock->setLabel(CS);
1745
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001746 if (badCFG)
Mike Stump5d1d2022010-01-19 02:20:09 +00001747 return 0;
1748
1749 // We set Block to NULL to allow lazy creation of a new block (if necessary)
1750 Block = NULL;
1751
1752 return CatchBlock;
1753}
1754
Ted Kremenekad5a8942010-08-02 23:46:59 +00001755CFGBlock *CFGBuilder::VisitCXXMemberCallExpr(CXXMemberCallExpr *C,
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001756 AddStmtChoice asc) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001757 AddStmtChoice::Kind K = asc.asLValue() ? AddStmtChoice::AlwaysAddAsLValue
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001758 : AddStmtChoice::AlwaysAdd;
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001759 autoCreateBlock();
Zhongxing Xu21f6d6e2010-04-14 05:50:04 +00001760 AppendStmt(Block, C, AddStmtChoice(K));
Zhongxing Xuc5354a22010-04-13 09:38:01 +00001761 return VisitChildren(C);
1762}
1763
Ted Kremenek19bb3562007-08-28 19:26:49 +00001764CFGBlock* CFGBuilder::VisitIndirectGotoStmt(IndirectGotoStmt* I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001765 // Lazily create the indirect-goto dispatch block if there isn't one already.
Ted Kremenek19bb3562007-08-28 19:26:49 +00001766 CFGBlock* IBlock = cfg->getIndirectGotoBlock();
Mike Stump6d9828c2009-07-17 01:31:16 +00001767
Ted Kremenek19bb3562007-08-28 19:26:49 +00001768 if (!IBlock) {
1769 IBlock = createBlock(false);
1770 cfg->setIndirectGotoBlock(IBlock);
1771 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001772
Ted Kremenek19bb3562007-08-28 19:26:49 +00001773 // IndirectGoto is a control-flow statement. Thus we stop processing the
1774 // current block and create a new one.
Zhongxing Xud438b3d2010-09-06 07:32:31 +00001775 if (badCFG)
Ted Kremenek4f880632009-07-17 22:18:43 +00001776 return 0;
1777
Ted Kremenek19bb3562007-08-28 19:26:49 +00001778 Block = createBlock(false);
1779 Block->setTerminator(I);
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001780 AddSuccessor(Block, IBlock);
Ted Kremenek19bb3562007-08-28 19:26:49 +00001781 return addStmt(I->getTarget());
1782}
1783
Ted Kremenekbefef2f2007-08-23 21:26:19 +00001784} // end anonymous namespace
Ted Kremenek026473c2007-08-23 16:51:22 +00001785
Mike Stump6d9828c2009-07-17 01:31:16 +00001786/// createBlock - Constructs and adds a new CFGBlock to the CFG. The block has
1787/// no successors or predecessors. If this is the first block created in the
1788/// CFG, it is automatically set to be the Entry and Exit of the CFG.
Ted Kremenek94382522007-09-05 20:02:05 +00001789CFGBlock* CFG::createBlock() {
Ted Kremenek026473c2007-08-23 16:51:22 +00001790 bool first_block = begin() == end();
1791
1792 // Create the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001793 CFGBlock *Mem = getAllocator().Allocate<CFGBlock>();
1794 new (Mem) CFGBlock(NumBlockIDs++, BlkBVC);
1795 Blocks.push_back(Mem, BlkBVC);
Ted Kremenek026473c2007-08-23 16:51:22 +00001796
1797 // If this is the first block, set it as the Entry and Exit.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001798 if (first_block)
1799 Entry = Exit = &back();
Ted Kremenek026473c2007-08-23 16:51:22 +00001800
1801 // Return the block.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001802 return &back();
Ted Kremenekfddd5182007-08-21 21:42:03 +00001803}
1804
Ted Kremenek026473c2007-08-23 16:51:22 +00001805/// buildCFG - Constructs a CFG from an AST. Ownership of the returned
1806/// CFG is returned to the caller.
Mike Stumpb978a442010-01-21 02:21:40 +00001807CFG* CFG::buildCFG(const Decl *D, Stmt* Statement, ASTContext *C,
Ted Kremenek6c52c782010-09-14 23:41:16 +00001808 BuildOptions BO) {
Ted Kremenek026473c2007-08-23 16:51:22 +00001809 CFGBuilder Builder;
Ted Kremenek6c52c782010-09-14 23:41:16 +00001810 return Builder.buildCFG(D, Statement, C, BO);
Ted Kremenek026473c2007-08-23 16:51:22 +00001811}
1812
Ted Kremenek63f58872007-10-01 19:33:33 +00001813//===----------------------------------------------------------------------===//
1814// CFG: Queries for BlkExprs.
1815//===----------------------------------------------------------------------===//
Ted Kremenek7dba8602007-08-29 21:56:09 +00001816
Ted Kremenek63f58872007-10-01 19:33:33 +00001817namespace {
Ted Kremenek86946742008-01-17 20:48:37 +00001818 typedef llvm::DenseMap<const Stmt*,unsigned> BlkExprMapTy;
Ted Kremenek63f58872007-10-01 19:33:33 +00001819}
1820
Ted Kremenek8a693662009-12-23 23:37:10 +00001821static void FindSubExprAssignments(Stmt *S,
1822 llvm::SmallPtrSet<Expr*,50>& Set) {
1823 if (!S)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001824 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00001825
Ted Kremenek8a693662009-12-23 23:37:10 +00001826 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I!=E; ++I) {
Ted Kremenekad5a8942010-08-02 23:46:59 +00001827 Stmt *child = *I;
Ted Kremenek8a693662009-12-23 23:37:10 +00001828 if (!child)
1829 continue;
Ted Kremenekad5a8942010-08-02 23:46:59 +00001830
Ted Kremenek8a693662009-12-23 23:37:10 +00001831 if (BinaryOperator* B = dyn_cast<BinaryOperator>(child))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001832 if (B->isAssignmentOp()) Set.insert(B);
Mike Stump6d9828c2009-07-17 01:31:16 +00001833
Ted Kremenek8a693662009-12-23 23:37:10 +00001834 FindSubExprAssignments(child, Set);
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001835 }
1836}
1837
Ted Kremenek63f58872007-10-01 19:33:33 +00001838static BlkExprMapTy* PopulateBlkExprMap(CFG& cfg) {
1839 BlkExprMapTy* M = new BlkExprMapTy();
Mike Stump6d9828c2009-07-17 01:31:16 +00001840
1841 // Look for assignments that are used as subexpressions. These are the only
1842 // assignments that we want to *possibly* register as a block-level
1843 // expression. Basically, if an assignment occurs both in a subexpression and
1844 // at the block-level, it is a block-level expression.
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001845 llvm::SmallPtrSet<Expr*,50> SubExprAssignments;
Mike Stump6d9828c2009-07-17 01:31:16 +00001846
Ted Kremenek63f58872007-10-01 19:33:33 +00001847 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I)
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001848 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001849 FindSubExprAssignments(*BI, SubExprAssignments);
Ted Kremenek86946742008-01-17 20:48:37 +00001850
Ted Kremenek411cdee2008-04-16 21:10:48 +00001851 for (CFG::iterator I=cfg.begin(), E=cfg.end(); I != E; ++I) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001852
1853 // Iterate over the statements again on identify the Expr* and Stmt* at the
1854 // block-level that are block-level expressions.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001855
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001856 for (CFGBlock::iterator BI=(*I)->begin(), EI=(*I)->end(); BI != EI; ++BI)
Ted Kremenek411cdee2008-04-16 21:10:48 +00001857 if (Expr* Exp = dyn_cast<Expr>(*BI)) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001858
Ted Kremenek411cdee2008-04-16 21:10:48 +00001859 if (BinaryOperator* B = dyn_cast<BinaryOperator>(Exp)) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001860 // Assignment expressions that are not nested within another
Mike Stump6d9828c2009-07-17 01:31:16 +00001861 // expression are really "statements" whose value is never used by
1862 // another expression.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001863 if (B->isAssignmentOp() && !SubExprAssignments.count(Exp))
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001864 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00001865 } else if (const StmtExpr* Terminator = dyn_cast<StmtExpr>(Exp)) {
1866 // Special handling for statement expressions. The last statement in
1867 // the statement expression is also a block-level expr.
Ted Kremenek411cdee2008-04-16 21:10:48 +00001868 const CompoundStmt* C = Terminator->getSubStmt();
Ted Kremenek86946742008-01-17 20:48:37 +00001869 if (!C->body_empty()) {
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001870 unsigned x = M->size();
Ted Kremenek86946742008-01-17 20:48:37 +00001871 (*M)[C->body_back()] = x;
1872 }
1873 }
Ted Kremeneke2dcd782008-01-25 23:22:27 +00001874
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001875 unsigned x = M->size();
Ted Kremenek411cdee2008-04-16 21:10:48 +00001876 (*M)[Exp] = x;
Ted Kremenek33d4aab2008-01-26 00:03:27 +00001877 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001878
Ted Kremenek411cdee2008-04-16 21:10:48 +00001879 // Look at terminators. The condition is a block-level expression.
Mike Stump6d9828c2009-07-17 01:31:16 +00001880
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001881 Stmt* S = (*I)->getTerminatorCondition();
Mike Stump6d9828c2009-07-17 01:31:16 +00001882
Ted Kremenek390e48b2008-11-12 21:11:49 +00001883 if (S && M->find(S) == M->end()) {
Ted Kremenek411cdee2008-04-16 21:10:48 +00001884 unsigned x = M->size();
Ted Kremenek390e48b2008-11-12 21:11:49 +00001885 (*M)[S] = x;
Ted Kremenek411cdee2008-04-16 21:10:48 +00001886 }
1887 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001888
Ted Kremenek63f58872007-10-01 19:33:33 +00001889 return M;
1890}
1891
Ted Kremenek86946742008-01-17 20:48:37 +00001892CFG::BlkExprNumTy CFG::getBlkExprNum(const Stmt* S) {
1893 assert(S != NULL);
Ted Kremenek63f58872007-10-01 19:33:33 +00001894 if (!BlkExprMap) { BlkExprMap = (void*) PopulateBlkExprMap(*this); }
Mike Stump6d9828c2009-07-17 01:31:16 +00001895
Ted Kremenek63f58872007-10-01 19:33:33 +00001896 BlkExprMapTy* M = reinterpret_cast<BlkExprMapTy*>(BlkExprMap);
Ted Kremenek86946742008-01-17 20:48:37 +00001897 BlkExprMapTy::iterator I = M->find(S);
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001898 return (I == M->end()) ? CFG::BlkExprNumTy() : CFG::BlkExprNumTy(I->second);
Ted Kremenek63f58872007-10-01 19:33:33 +00001899}
1900
1901unsigned CFG::getNumBlkExprs() {
1902 if (const BlkExprMapTy* M = reinterpret_cast<const BlkExprMapTy*>(BlkExprMap))
1903 return M->size();
1904 else {
1905 // We assume callers interested in the number of BlkExprs will want
1906 // the map constructed if it doesn't already exist.
1907 BlkExprMap = (void*) PopulateBlkExprMap(*this);
1908 return reinterpret_cast<BlkExprMapTy*>(BlkExprMap)->size();
1909 }
1910}
1911
Ted Kremenek274f4332008-04-28 18:00:46 +00001912//===----------------------------------------------------------------------===//
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001913// Filtered walking of the CFG.
1914//===----------------------------------------------------------------------===//
1915
1916bool CFGBlock::FilterEdge(const CFGBlock::FilterOptions &F,
Ted Kremenekbe39a562010-09-09 02:57:48 +00001917 const CFGBlock *From, const CFGBlock *To) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001918
1919 if (F.IgnoreDefaultsWithCoveredEnums) {
1920 // If the 'To' has no label or is labeled but the label isn't a
1921 // CaseStmt then filter this edge.
1922 if (const SwitchStmt *S =
Ted Kremenekbe39a562010-09-09 02:57:48 +00001923 dyn_cast_or_null<SwitchStmt>(From->getTerminator())) {
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001924 if (S->isAllEnumCasesCovered()) {
Ted Kremenekbe39a562010-09-09 02:57:48 +00001925 const Stmt *L = To->getLabel();
1926 if (!L || !isa<CaseStmt>(L))
1927 return true;
Ted Kremenekee7f84d2010-09-09 00:06:04 +00001928 }
1929 }
1930 }
1931
1932 return false;
1933}
1934
1935//===----------------------------------------------------------------------===//
Ted Kremenek274f4332008-04-28 18:00:46 +00001936// Cleanup: CFG dstor.
1937//===----------------------------------------------------------------------===//
1938
Ted Kremenek63f58872007-10-01 19:33:33 +00001939CFG::~CFG() {
1940 delete reinterpret_cast<const BlkExprMapTy*>(BlkExprMap);
1941}
Mike Stump6d9828c2009-07-17 01:31:16 +00001942
Ted Kremenek7dba8602007-08-29 21:56:09 +00001943//===----------------------------------------------------------------------===//
1944// CFG pretty printing
1945//===----------------------------------------------------------------------===//
1946
Ted Kremeneke8ee26b2007-08-22 18:22:34 +00001947namespace {
1948
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001949class StmtPrinterHelper : public PrinterHelper {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001950 typedef llvm::DenseMap<Stmt*,std::pair<unsigned,unsigned> > StmtMapTy;
1951 StmtMapTy StmtMap;
1952 signed CurrentBlock;
1953 unsigned CurrentStmt;
Chris Lattnere4f21422009-06-30 01:26:17 +00001954 const LangOptions &LangOpts;
Ted Kremenekd4fdee32007-08-23 21:42:29 +00001955public:
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001956
Chris Lattnere4f21422009-06-30 01:26:17 +00001957 StmtPrinterHelper(const CFG* cfg, const LangOptions &LO)
1958 : CurrentBlock(0), CurrentStmt(0), LangOpts(LO) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001959 for (CFG::const_iterator I = cfg->begin(), E = cfg->end(); I != E; ++I ) {
1960 unsigned j = 1;
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001961 for (CFGBlock::const_iterator BI = (*I)->begin(), BEnd = (*I)->end() ;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001962 BI != BEnd; ++BI, ++j )
Ted Kremenekee82d9b2009-10-12 20:55:07 +00001963 StmtMap[*BI] = std::make_pair((*I)->getBlockID(),j);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001964 }
1965 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001966
Ted Kremenek42a509f2007-08-31 21:30:12 +00001967 virtual ~StmtPrinterHelper() {}
Mike Stump6d9828c2009-07-17 01:31:16 +00001968
Chris Lattnere4f21422009-06-30 01:26:17 +00001969 const LangOptions &getLangOpts() const { return LangOpts; }
Ted Kremenek42a509f2007-08-31 21:30:12 +00001970 void setBlockID(signed i) { CurrentBlock = i; }
1971 void setStmtID(unsigned i) { CurrentStmt = i; }
Mike Stump6d9828c2009-07-17 01:31:16 +00001972
Ted Kremeneka95d3752008-09-13 05:16:45 +00001973 virtual bool handledStmt(Stmt* Terminator, llvm::raw_ostream& OS) {
Mike Stump6d9828c2009-07-17 01:31:16 +00001974
Ted Kremenek411cdee2008-04-16 21:10:48 +00001975 StmtMapTy::iterator I = StmtMap.find(Terminator);
Ted Kremenek42a509f2007-08-31 21:30:12 +00001976
1977 if (I == StmtMap.end())
1978 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00001979
1980 if (CurrentBlock >= 0 && I->second.first == (unsigned) CurrentBlock
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001981 && I->second.second == CurrentStmt) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00001982 return false;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001983 }
Mike Stump6d9828c2009-07-17 01:31:16 +00001984
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00001985 OS << "[B" << I->second.first << "." << I->second.second << "]";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00001986 return true;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001987 }
1988};
Chris Lattnere4f21422009-06-30 01:26:17 +00001989} // end anonymous namespace
Ted Kremenek42a509f2007-08-31 21:30:12 +00001990
Chris Lattnere4f21422009-06-30 01:26:17 +00001991
1992namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +00001993class CFGBlockTerminatorPrint
Ted Kremenek6fa9b882008-01-08 18:15:10 +00001994 : public StmtVisitor<CFGBlockTerminatorPrint,void> {
Mike Stump6d9828c2009-07-17 01:31:16 +00001995
Ted Kremeneka95d3752008-09-13 05:16:45 +00001996 llvm::raw_ostream& OS;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001997 StmtPrinterHelper* Helper;
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00001998 PrintingPolicy Policy;
Ted Kremenek42a509f2007-08-31 21:30:12 +00001999public:
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002000 CFGBlockTerminatorPrint(llvm::raw_ostream& os, StmtPrinterHelper* helper,
Chris Lattnere4f21422009-06-30 01:26:17 +00002001 const PrintingPolicy &Policy)
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002002 : OS(os), Helper(helper), Policy(Policy) {}
Mike Stump6d9828c2009-07-17 01:31:16 +00002003
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002004 void VisitIfStmt(IfStmt* I) {
2005 OS << "if ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002006 I->getCond()->printPretty(OS,Helper,Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002007 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002008
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002009 // Default case.
Mike Stump6d9828c2009-07-17 01:31:16 +00002010 void VisitStmt(Stmt* Terminator) {
2011 Terminator->printPretty(OS, Helper, Policy);
2012 }
2013
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002014 void VisitForStmt(ForStmt* F) {
2015 OS << "for (" ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002016 if (F->getInit())
2017 OS << "...";
Ted Kremenek535bb202007-08-30 21:28:02 +00002018 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002019 if (Stmt* C = F->getCond())
2020 C->printPretty(OS, Helper, Policy);
Ted Kremenek535bb202007-08-30 21:28:02 +00002021 OS << "; ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002022 if (F->getInc())
2023 OS << "...";
Ted Kremeneka2925852008-01-30 23:02:42 +00002024 OS << ")";
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002025 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002026
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002027 void VisitWhileStmt(WhileStmt* W) {
2028 OS << "while " ;
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002029 if (Stmt* C = W->getCond())
2030 C->printPretty(OS, Helper, Policy);
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002031 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002032
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002033 void VisitDoStmt(DoStmt* D) {
2034 OS << "do ... while ";
Ted Kremenek3fa1e4b2010-01-19 20:52:05 +00002035 if (Stmt* C = D->getCond())
2036 C->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002037 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002038
Ted Kremenek411cdee2008-04-16 21:10:48 +00002039 void VisitSwitchStmt(SwitchStmt* Terminator) {
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002040 OS << "switch ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002041 Terminator->getCond()->printPretty(OS, Helper, Policy);
Ted Kremenek9da2fb72007-08-27 21:27:44 +00002042 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002043
Mike Stump5d1d2022010-01-19 02:20:09 +00002044 void VisitCXXTryStmt(CXXTryStmt* CS) {
2045 OS << "try ...";
2046 }
2047
Ted Kremenek805e9a82007-08-31 21:49:40 +00002048 void VisitConditionalOperator(ConditionalOperator* C) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002049 C->getCond()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002050 OS << " ? ... : ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002051 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002052
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002053 void VisitChooseExpr(ChooseExpr* C) {
2054 OS << "__builtin_choose_expr( ";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002055 C->getCond()->printPretty(OS, Helper, Policy);
Ted Kremeneka2925852008-01-30 23:02:42 +00002056 OS << " )";
Ted Kremenekaeddbf62007-08-31 22:29:13 +00002057 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002058
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002059 void VisitIndirectGotoStmt(IndirectGotoStmt* I) {
2060 OS << "goto *";
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002061 I->getTarget()->printPretty(OS, Helper, Policy);
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002062 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002063
Ted Kremenek805e9a82007-08-31 21:49:40 +00002064 void VisitBinaryOperator(BinaryOperator* B) {
2065 if (!B->isLogicalOp()) {
2066 VisitExpr(B);
2067 return;
2068 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002069
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002070 B->getLHS()->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002071
Ted Kremenek805e9a82007-08-31 21:49:40 +00002072 switch (B->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +00002073 case BO_LOr:
Ted Kremeneka2925852008-01-30 23:02:42 +00002074 OS << " || ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002075 return;
John McCall2de56d12010-08-25 11:45:40 +00002076 case BO_LAnd:
Ted Kremeneka2925852008-01-30 23:02:42 +00002077 OS << " && ...";
Ted Kremenek805e9a82007-08-31 21:49:40 +00002078 return;
2079 default:
2080 assert(false && "Invalid logical operator.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002081 }
Ted Kremenek805e9a82007-08-31 21:49:40 +00002082 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002083
Ted Kremenek0b1d9b72007-08-27 21:54:41 +00002084 void VisitExpr(Expr* E) {
Douglas Gregord249e1d1f2009-05-29 20:38:28 +00002085 E->printPretty(OS, Helper, Policy);
Mike Stump6d9828c2009-07-17 01:31:16 +00002086 }
Ted Kremenekd4fdee32007-08-23 21:42:29 +00002087};
Chris Lattnere4f21422009-06-30 01:26:17 +00002088} // end anonymous namespace
2089
Mike Stump6d9828c2009-07-17 01:31:16 +00002090
Chris Lattnere4f21422009-06-30 01:26:17 +00002091static void print_stmt(llvm::raw_ostream &OS, StmtPrinterHelper* Helper,
Mike Stump079bd722010-01-19 22:00:14 +00002092 const CFGElement &E) {
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002093 Stmt *S = E;
2094
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002095 if (Helper) {
2096 // special printing for statement-expressions.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002097 if (StmtExpr* SE = dyn_cast<StmtExpr>(S)) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002098 CompoundStmt* Sub = SE->getSubStmt();
Mike Stump6d9828c2009-07-17 01:31:16 +00002099
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002100 if (Sub->child_begin() != Sub->child_end()) {
Ted Kremenek60266e82007-08-31 22:47:06 +00002101 OS << "({ ... ; ";
Ted Kremenek7a9d9d72007-10-29 20:41:04 +00002102 Helper->handledStmt(*SE->getSubStmt()->body_rbegin(),OS);
Ted Kremenek60266e82007-08-31 22:47:06 +00002103 OS << " })\n";
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002104 return;
2105 }
2106 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002107
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002108 // special printing for comma expressions.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002109 if (BinaryOperator* B = dyn_cast<BinaryOperator>(S)) {
John McCall2de56d12010-08-25 11:45:40 +00002110 if (B->getOpcode() == BO_Comma) {
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002111 OS << "... , ";
2112 Helper->handledStmt(B->getRHS(),OS);
2113 OS << '\n';
2114 return;
Mike Stump6d9828c2009-07-17 01:31:16 +00002115 }
2116 }
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002117 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002118
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002119 S->printPretty(OS, Helper, PrintingPolicy(Helper->getLangOpts()));
2120
2121 if (isa<CXXOperatorCallExpr>(S)) {
2122 OS << " (OperatorCall)";
2123 }
2124 else if (isa<CXXBindTemporaryExpr>(S)) {
2125 OS << " (BindTemporary)";
2126 }
2127
Mike Stump6d9828c2009-07-17 01:31:16 +00002128
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002129 // Expressions need a newline.
Ted Kremenek4e0cfa82010-08-31 18:47:37 +00002130 if (isa<Expr>(S))
2131 OS << '\n';
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002132}
Mike Stump6d9828c2009-07-17 01:31:16 +00002133
Chris Lattnere4f21422009-06-30 01:26:17 +00002134static void print_block(llvm::raw_ostream& OS, const CFG* cfg,
2135 const CFGBlock& B,
2136 StmtPrinterHelper* Helper, bool print_edges) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002137
Ted Kremenek42a509f2007-08-31 21:30:12 +00002138 if (Helper) Helper->setBlockID(B.getBlockID());
Mike Stump6d9828c2009-07-17 01:31:16 +00002139
Ted Kremenek7dba8602007-08-29 21:56:09 +00002140 // Print the header.
Mike Stump6d9828c2009-07-17 01:31:16 +00002141 OS << "\n [ B" << B.getBlockID();
2142
Ted Kremenek42a509f2007-08-31 21:30:12 +00002143 if (&B == &cfg->getEntry())
2144 OS << " (ENTRY) ]\n";
2145 else if (&B == &cfg->getExit())
2146 OS << " (EXIT) ]\n";
2147 else if (&B == cfg->getIndirectGotoBlock())
Ted Kremenek7dba8602007-08-29 21:56:09 +00002148 OS << " (INDIRECT GOTO DISPATCH) ]\n";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002149 else
2150 OS << " ]\n";
Mike Stump6d9828c2009-07-17 01:31:16 +00002151
Ted Kremenek9cffe732007-08-29 23:20:49 +00002152 // Print the label of this block.
Mike Stump079bd722010-01-19 22:00:14 +00002153 if (Stmt* Label = const_cast<Stmt*>(B.getLabel())) {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002154
2155 if (print_edges)
2156 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002157
Mike Stump079bd722010-01-19 22:00:14 +00002158 if (LabelStmt* L = dyn_cast<LabelStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002159 OS << L->getName();
Mike Stump079bd722010-01-19 22:00:14 +00002160 else if (CaseStmt* C = dyn_cast<CaseStmt>(Label)) {
Ted Kremenek9cffe732007-08-29 23:20:49 +00002161 OS << "case ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002162 C->getLHS()->printPretty(OS, Helper,
2163 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002164 if (C->getRHS()) {
2165 OS << " ... ";
Chris Lattnere4f21422009-06-30 01:26:17 +00002166 C->getRHS()->printPretty(OS, Helper,
2167 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek9cffe732007-08-29 23:20:49 +00002168 }
Mike Stump079bd722010-01-19 22:00:14 +00002169 } else if (isa<DefaultStmt>(Label))
Ted Kremenek9cffe732007-08-29 23:20:49 +00002170 OS << "default";
Mike Stump079bd722010-01-19 22:00:14 +00002171 else if (CXXCatchStmt *CS = dyn_cast<CXXCatchStmt>(Label)) {
Mike Stump5d1d2022010-01-19 02:20:09 +00002172 OS << "catch (";
Mike Stumpa1f93632010-01-20 01:15:34 +00002173 if (CS->getExceptionDecl())
2174 CS->getExceptionDecl()->print(OS, PrintingPolicy(Helper->getLangOpts()),
2175 0);
2176 else
2177 OS << "...";
Mike Stump5d1d2022010-01-19 02:20:09 +00002178 OS << ")";
2179
2180 } else
Ted Kremenek42a509f2007-08-31 21:30:12 +00002181 assert(false && "Invalid label statement in CFGBlock.");
Mike Stump6d9828c2009-07-17 01:31:16 +00002182
Ted Kremenek9cffe732007-08-29 23:20:49 +00002183 OS << ":\n";
2184 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002185
Ted Kremenekfddd5182007-08-21 21:42:03 +00002186 // Iterate through the statements in the block and print them.
Ted Kremenekfddd5182007-08-21 21:42:03 +00002187 unsigned j = 1;
Mike Stump6d9828c2009-07-17 01:31:16 +00002188
Ted Kremenek42a509f2007-08-31 21:30:12 +00002189 for (CFGBlock::const_iterator I = B.begin(), E = B.end() ;
2190 I != E ; ++I, ++j ) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002191
Ted Kremenek9cffe732007-08-29 23:20:49 +00002192 // Print the statement # in the basic block and the statement itself.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002193 if (print_edges)
2194 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002195
Ted Kremeneka95d3752008-09-13 05:16:45 +00002196 OS << llvm::format("%3d", j) << ": ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002197
Ted Kremenek42a509f2007-08-31 21:30:12 +00002198 if (Helper)
2199 Helper->setStmtID(j);
Mike Stump6d9828c2009-07-17 01:31:16 +00002200
Ted Kremenek1c29bba2007-08-31 22:26:13 +00002201 print_stmt(OS,Helper,*I);
Ted Kremenekfddd5182007-08-21 21:42:03 +00002202 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002203
Ted Kremenek9cffe732007-08-29 23:20:49 +00002204 // Print the terminator of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002205 if (B.getTerminator()) {
2206 if (print_edges)
2207 OS << " ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002208
Ted Kremenek9cffe732007-08-29 23:20:49 +00002209 OS << " T: ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002210
Ted Kremenek42a509f2007-08-31 21:30:12 +00002211 if (Helper) Helper->setBlockID(-1);
Mike Stump6d9828c2009-07-17 01:31:16 +00002212
Chris Lattnere4f21422009-06-30 01:26:17 +00002213 CFGBlockTerminatorPrint TPrinter(OS, Helper,
2214 PrintingPolicy(Helper->getLangOpts()));
Ted Kremenek42a509f2007-08-31 21:30:12 +00002215 TPrinter.Visit(const_cast<Stmt*>(B.getTerminator()));
Ted Kremeneka2925852008-01-30 23:02:42 +00002216 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002217 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002218
Ted Kremenek9cffe732007-08-29 23:20:49 +00002219 if (print_edges) {
2220 // Print the predecessors of this block.
Ted Kremenek42a509f2007-08-31 21:30:12 +00002221 OS << " Predecessors (" << B.pred_size() << "):";
Ted Kremenek9cffe732007-08-29 23:20:49 +00002222 unsigned i = 0;
Ted Kremenek9cffe732007-08-29 23:20:49 +00002223
Ted Kremenek42a509f2007-08-31 21:30:12 +00002224 for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end();
2225 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002226
Ted Kremenek42a509f2007-08-31 21:30:12 +00002227 if (i == 8 || (i-8) == 0)
2228 OS << "\n ";
Mike Stump6d9828c2009-07-17 01:31:16 +00002229
Ted Kremenek9cffe732007-08-29 23:20:49 +00002230 OS << " B" << (*I)->getBlockID();
2231 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002232
Ted Kremenek42a509f2007-08-31 21:30:12 +00002233 OS << '\n';
Mike Stump6d9828c2009-07-17 01:31:16 +00002234
Ted Kremenek42a509f2007-08-31 21:30:12 +00002235 // Print the successors of this block.
2236 OS << " Successors (" << B.succ_size() << "):";
2237 i = 0;
2238
2239 for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end();
2240 I != E; ++I, ++i) {
Mike Stump6d9828c2009-07-17 01:31:16 +00002241
Ted Kremenek42a509f2007-08-31 21:30:12 +00002242 if (i == 8 || (i-8) % 10 == 0)
2243 OS << "\n ";
2244
Mike Stumpe5af3ce2009-07-20 23:24:15 +00002245 if (*I)
2246 OS << " B" << (*I)->getBlockID();
2247 else
2248 OS << " NULL";
Ted Kremenek42a509f2007-08-31 21:30:12 +00002249 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002250
Ted Kremenek9cffe732007-08-29 23:20:49 +00002251 OS << '\n';
Ted Kremenekfddd5182007-08-21 21:42:03 +00002252 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002253}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002254
Ted Kremenek42a509f2007-08-31 21:30:12 +00002255
2256/// dump - A simple pretty printer of a CFG that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002257void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); }
Ted Kremenek42a509f2007-08-31 21:30:12 +00002258
2259/// print - A simple pretty printer of a CFG that outputs to an ostream.
Chris Lattnere4f21422009-06-30 01:26:17 +00002260void CFG::print(llvm::raw_ostream &OS, const LangOptions &LO) const {
2261 StmtPrinterHelper Helper(this, LO);
Mike Stump6d9828c2009-07-17 01:31:16 +00002262
Ted Kremenek42a509f2007-08-31 21:30:12 +00002263 // Print the entry block.
2264 print_block(OS, this, getEntry(), &Helper, true);
Mike Stump6d9828c2009-07-17 01:31:16 +00002265
Ted Kremenek42a509f2007-08-31 21:30:12 +00002266 // Iterate through the CFGBlocks and print them one by one.
2267 for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) {
2268 // Skip the entry block, because we already printed it.
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002269 if (&(**I) == &getEntry() || &(**I) == &getExit())
Ted Kremenek42a509f2007-08-31 21:30:12 +00002270 continue;
Mike Stump6d9828c2009-07-17 01:31:16 +00002271
Ted Kremenekee82d9b2009-10-12 20:55:07 +00002272 print_block(OS, this, **I, &Helper, true);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002273 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002274
Ted Kremenek42a509f2007-08-31 21:30:12 +00002275 // Print the exit block.
2276 print_block(OS, this, getExit(), &Helper, true);
Ted Kremenekd0172432008-11-24 20:50:24 +00002277 OS.flush();
Mike Stump6d9828c2009-07-17 01:31:16 +00002278}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002279
2280/// dump - A simply pretty printer of a CFGBlock that outputs to stderr.
Chris Lattnere4f21422009-06-30 01:26:17 +00002281void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const {
2282 print(llvm::errs(), cfg, LO);
2283}
Ted Kremenek42a509f2007-08-31 21:30:12 +00002284
2285/// print - A simple pretty printer of a CFGBlock that outputs to an ostream.
2286/// Generally this will only be called from CFG::print.
Chris Lattnere4f21422009-06-30 01:26:17 +00002287void CFGBlock::print(llvm::raw_ostream& OS, const CFG* cfg,
2288 const LangOptions &LO) const {
2289 StmtPrinterHelper Helper(cfg, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002290 print_block(OS, cfg, *this, &Helper, true);
Ted Kremenek026473c2007-08-23 16:51:22 +00002291}
Ted Kremenek7dba8602007-08-29 21:56:09 +00002292
Ted Kremeneka2925852008-01-30 23:02:42 +00002293/// printTerminator - A simple pretty printer of the terminator of a CFGBlock.
Chris Lattnere4f21422009-06-30 01:26:17 +00002294void CFGBlock::printTerminator(llvm::raw_ostream &OS,
Mike Stump6d9828c2009-07-17 01:31:16 +00002295 const LangOptions &LO) const {
Chris Lattnere4f21422009-06-30 01:26:17 +00002296 CFGBlockTerminatorPrint TPrinter(OS, NULL, PrintingPolicy(LO));
Ted Kremeneka2925852008-01-30 23:02:42 +00002297 TPrinter.Visit(const_cast<Stmt*>(getTerminator()));
2298}
2299
Ted Kremenek390e48b2008-11-12 21:11:49 +00002300Stmt* CFGBlock::getTerminatorCondition() {
Mike Stump6d9828c2009-07-17 01:31:16 +00002301
Ted Kremenek411cdee2008-04-16 21:10:48 +00002302 if (!Terminator)
2303 return NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002304
Ted Kremenek411cdee2008-04-16 21:10:48 +00002305 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002306
Ted Kremenek411cdee2008-04-16 21:10:48 +00002307 switch (Terminator->getStmtClass()) {
2308 default:
2309 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002310
Ted Kremenek411cdee2008-04-16 21:10:48 +00002311 case Stmt::ForStmtClass:
2312 E = cast<ForStmt>(Terminator)->getCond();
2313 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002314
Ted Kremenek411cdee2008-04-16 21:10:48 +00002315 case Stmt::WhileStmtClass:
2316 E = cast<WhileStmt>(Terminator)->getCond();
2317 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002318
Ted Kremenek411cdee2008-04-16 21:10:48 +00002319 case Stmt::DoStmtClass:
2320 E = cast<DoStmt>(Terminator)->getCond();
2321 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002322
Ted Kremenek411cdee2008-04-16 21:10:48 +00002323 case Stmt::IfStmtClass:
2324 E = cast<IfStmt>(Terminator)->getCond();
2325 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002326
Ted Kremenek411cdee2008-04-16 21:10:48 +00002327 case Stmt::ChooseExprClass:
2328 E = cast<ChooseExpr>(Terminator)->getCond();
2329 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002330
Ted Kremenek411cdee2008-04-16 21:10:48 +00002331 case Stmt::IndirectGotoStmtClass:
2332 E = cast<IndirectGotoStmt>(Terminator)->getTarget();
2333 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002334
Ted Kremenek411cdee2008-04-16 21:10:48 +00002335 case Stmt::SwitchStmtClass:
2336 E = cast<SwitchStmt>(Terminator)->getCond();
2337 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002338
Ted Kremenek411cdee2008-04-16 21:10:48 +00002339 case Stmt::ConditionalOperatorClass:
2340 E = cast<ConditionalOperator>(Terminator)->getCond();
2341 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002342
Ted Kremenek411cdee2008-04-16 21:10:48 +00002343 case Stmt::BinaryOperatorClass: // '&&' and '||'
2344 E = cast<BinaryOperator>(Terminator)->getLHS();
Ted Kremenek390e48b2008-11-12 21:11:49 +00002345 break;
Mike Stump6d9828c2009-07-17 01:31:16 +00002346
Ted Kremenek390e48b2008-11-12 21:11:49 +00002347 case Stmt::ObjCForCollectionStmtClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002348 return Terminator;
Ted Kremenek411cdee2008-04-16 21:10:48 +00002349 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002350
Ted Kremenek411cdee2008-04-16 21:10:48 +00002351 return E ? E->IgnoreParens() : NULL;
2352}
2353
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002354bool CFGBlock::hasBinaryBranchTerminator() const {
Mike Stump6d9828c2009-07-17 01:31:16 +00002355
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002356 if (!Terminator)
2357 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002358
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002359 Expr* E = NULL;
Mike Stump6d9828c2009-07-17 01:31:16 +00002360
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002361 switch (Terminator->getStmtClass()) {
2362 default:
2363 return false;
Mike Stump6d9828c2009-07-17 01:31:16 +00002364
2365 case Stmt::ForStmtClass:
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002366 case Stmt::WhileStmtClass:
2367 case Stmt::DoStmtClass:
2368 case Stmt::IfStmtClass:
2369 case Stmt::ChooseExprClass:
2370 case Stmt::ConditionalOperatorClass:
2371 case Stmt::BinaryOperatorClass:
Mike Stump6d9828c2009-07-17 01:31:16 +00002372 return true;
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002373 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002374
Ted Kremenek9c2535a2008-05-16 16:06:00 +00002375 return E ? E->IgnoreParens() : NULL;
2376}
2377
Ted Kremeneka2925852008-01-30 23:02:42 +00002378
Ted Kremenek7dba8602007-08-29 21:56:09 +00002379//===----------------------------------------------------------------------===//
2380// CFG Graphviz Visualization
2381//===----------------------------------------------------------------------===//
2382
Ted Kremenek42a509f2007-08-31 21:30:12 +00002383
2384#ifndef NDEBUG
Mike Stump6d9828c2009-07-17 01:31:16 +00002385static StmtPrinterHelper* GraphHelper;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002386#endif
2387
Chris Lattnere4f21422009-06-30 01:26:17 +00002388void CFG::viewCFG(const LangOptions &LO) const {
Ted Kremenek42a509f2007-08-31 21:30:12 +00002389#ifndef NDEBUG
Chris Lattnere4f21422009-06-30 01:26:17 +00002390 StmtPrinterHelper H(this, LO);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002391 GraphHelper = &H;
2392 llvm::ViewGraph(this,"CFG");
2393 GraphHelper = NULL;
Ted Kremenek42a509f2007-08-31 21:30:12 +00002394#endif
2395}
2396
Ted Kremenek7dba8602007-08-29 21:56:09 +00002397namespace llvm {
2398template<>
2399struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +00002400
2401 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
2402
2403 static std::string getNodeLabel(const CFGBlock* Node, const CFG* Graph) {
Ted Kremenek7dba8602007-08-29 21:56:09 +00002404
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002405#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +00002406 std::string OutSStr;
2407 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek42a509f2007-08-31 21:30:12 +00002408 print_block(Out,Graph, *Node, GraphHelper, false);
Ted Kremeneka95d3752008-09-13 05:16:45 +00002409 std::string& OutStr = Out.str();
Ted Kremenek7dba8602007-08-29 21:56:09 +00002410
2411 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
2412
2413 // Process string output to make it nicer...
2414 for (unsigned i = 0; i != OutStr.length(); ++i)
2415 if (OutStr[i] == '\n') { // Left justify
2416 OutStr[i] = '\\';
2417 OutStr.insert(OutStr.begin()+i+1, 'l');
2418 }
Mike Stump6d9828c2009-07-17 01:31:16 +00002419
Ted Kremenek7dba8602007-08-29 21:56:09 +00002420 return OutStr;
Hartmut Kaiserbd250b42007-09-16 00:28:28 +00002421#else
2422 return "";
2423#endif
Ted Kremenek7dba8602007-08-29 21:56:09 +00002424 }
2425};
2426} // end namespace llvm