blob: 61d521520eba66cba20d019870acbaa14ec0a689 [file] [log] [blame]
Ted Kremenek3d2eed82010-02-23 02:39:16 +00001//=- ReachableCodePathInsensitive.cpp ---------------------------*- C++ --*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a flow-sensitive, path-insensitive analysis of
11// determining reachable blocks within a CFG.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/Analysis/Analyses/ReachableCode.h"
Ted Kremenek72919a32010-02-23 05:59:20 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/ExprCXX.h"
John McCallf85e1932011-06-15 23:02:42 +000018#include "clang/AST/ExprObjC.h"
Ted Kremenek72919a32010-02-23 05:59:20 +000019#include "clang/AST/StmtCXX.h"
Ted Kremenek72919a32010-02-23 05:59:20 +000020#include "clang/Analysis/AnalysisContext.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Analysis/CFG.h"
Ted Kremenek72919a32010-02-23 05:59:20 +000022#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/ADT/BitVector.h"
24#include "llvm/ADT/SmallVector.h"
Ted Kremenek3d2eed82010-02-23 02:39:16 +000025
26using namespace clang;
27
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +000028namespace {
29class DeadCodeScan {
30 llvm::BitVector Visited;
31 llvm::BitVector &Reachable;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000032 SmallVector<const CFGBlock *, 10> WorkList;
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +000033
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000034 typedef SmallVector<std::pair<const CFGBlock *, const Stmt *>, 12>
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +000035 DeferredLocsTy;
36
37 DeferredLocsTy DeferredLocs;
38
39public:
40 DeadCodeScan(llvm::BitVector &reachable)
41 : Visited(reachable.size()),
42 Reachable(reachable) {}
43
44 void enqueue(const CFGBlock *block);
45 unsigned scanBackwards(const CFGBlock *Start,
46 clang::reachable_code::Callback &CB);
47
48 bool isDeadCodeRoot(const CFGBlock *Block);
49
50 const Stmt *findDeadCode(const CFGBlock *Block);
51
52 void reportDeadCode(const Stmt *S,
53 clang::reachable_code::Callback &CB);
54};
55}
56
57void DeadCodeScan::enqueue(const CFGBlock *block) {
58 unsigned blockID = block->getBlockID();
59 if (Reachable[blockID] || Visited[blockID])
60 return;
61 Visited[blockID] = true;
62 WorkList.push_back(block);
63}
64
65bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) {
66 bool isDeadRoot = true;
67
68 for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
69 E = Block->pred_end(); I != E; ++I) {
70 if (const CFGBlock *PredBlock = *I) {
71 unsigned blockID = PredBlock->getBlockID();
72 if (Visited[blockID]) {
73 isDeadRoot = false;
74 continue;
75 }
76 if (!Reachable[blockID]) {
77 isDeadRoot = false;
78 Visited[blockID] = true;
79 WorkList.push_back(PredBlock);
80 continue;
81 }
82 }
83 }
84
85 return isDeadRoot;
86}
87
88static bool isValidDeadStmt(const Stmt *S) {
Ted Kremeneke7a27642011-08-25 19:28:55 +000089 if (S->getLocStart().isInvalid())
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +000090 return false;
Ted Kremeneke7a27642011-08-25 19:28:55 +000091 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S))
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +000092 return BO->getOpcode() != BO_Comma;
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +000093 return true;
94}
95
96const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) {
97 for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I!=E; ++I)
David Blaikieb0780542013-02-23 00:29:34 +000098 if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
99 const Stmt *S = CS->getStmt();
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000100 if (isValidDeadStmt(S))
101 return S;
102 }
103
104 if (CFGTerminator T = Block->getTerminator()) {
105 const Stmt *S = T.getStmt();
106 if (isValidDeadStmt(S))
107 return S;
108 }
109
110 return 0;
111}
112
Benjamin Kramer767b3d22013-09-22 14:10:29 +0000113static int SrcCmp(const std::pair<const CFGBlock *, const Stmt *> *p1,
114 const std::pair<const CFGBlock *, const Stmt *> *p2) {
115 return p2->second->getLocStart() < p1->second->getLocStart();
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000116}
117
118unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start,
119 clang::reachable_code::Callback &CB) {
120
121 unsigned count = 0;
122 enqueue(Start);
123
124 while (!WorkList.empty()) {
125 const CFGBlock *Block = WorkList.pop_back_val();
126
127 // It is possible that this block has been marked reachable after
128 // it was enqueued.
129 if (Reachable[Block->getBlockID()])
130 continue;
131
132 // Look for any dead code within the block.
133 const Stmt *S = findDeadCode(Block);
134
135 if (!S) {
136 // No dead code. Possibly an empty block. Look at dead predecessors.
137 for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
138 E = Block->pred_end(); I != E; ++I) {
139 if (const CFGBlock *predBlock = *I)
140 enqueue(predBlock);
141 }
142 continue;
143 }
Ted Kremeneke7a27642011-08-25 19:28:55 +0000144
145 // Specially handle macro-expanded code.
146 if (S->getLocStart().isMacroID()) {
147 count += clang::reachable_code::ScanReachableFromBlock(Block, Reachable);
148 continue;
149 }
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000150
151 if (isDeadCodeRoot(Block)) {
152 reportDeadCode(S, CB);
153 count += clang::reachable_code::ScanReachableFromBlock(Block, Reachable);
154 }
155 else {
156 // Record this statement as the possibly best location in a
157 // strongly-connected component of dead code for emitting a
158 // warning.
159 DeferredLocs.push_back(std::make_pair(Block, S));
160 }
161 }
162
163 // If we didn't find a dead root, then report the dead code with the
164 // earliest location.
165 if (!DeferredLocs.empty()) {
166 llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp);
167 for (DeferredLocsTy::iterator I = DeferredLocs.begin(),
168 E = DeferredLocs.end(); I != E; ++I) {
169 const CFGBlock *block = I->first;
170 if (Reachable[block->getBlockID()])
171 continue;
172 reportDeadCode(I->second, CB);
173 count += clang::reachable_code::ScanReachableFromBlock(block, Reachable);
174 }
175 }
176
177 return count;
178}
179
180static SourceLocation GetUnreachableLoc(const Stmt *S,
181 SourceRange &R1,
Ted Kremenek72919a32010-02-23 05:59:20 +0000182 SourceRange &R2) {
Ted Kremenek72919a32010-02-23 05:59:20 +0000183 R1 = R2 = SourceRange();
184
Ted Kremenek892697d2010-12-16 07:46:53 +0000185 if (const Expr *Ex = dyn_cast<Expr>(S))
186 S = Ex->IgnoreParenImpCasts();
187
Ted Kremenek72919a32010-02-23 05:59:20 +0000188 switch (S->getStmtClass()) {
189 case Expr::BinaryOperatorClass: {
190 const BinaryOperator *BO = cast<BinaryOperator>(S);
Ted Kremenek72919a32010-02-23 05:59:20 +0000191 return BO->getOperatorLoc();
192 }
193 case Expr::UnaryOperatorClass: {
194 const UnaryOperator *UO = cast<UnaryOperator>(S);
195 R1 = UO->getSubExpr()->getSourceRange();
196 return UO->getOperatorLoc();
197 }
198 case Expr::CompoundAssignOperatorClass: {
199 const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(S);
200 R1 = CAO->getLHS()->getSourceRange();
201 R2 = CAO->getRHS()->getSourceRange();
202 return CAO->getOperatorLoc();
203 }
John McCall56ca35d2011-02-17 10:25:35 +0000204 case Expr::BinaryConditionalOperatorClass:
Ted Kremenek72919a32010-02-23 05:59:20 +0000205 case Expr::ConditionalOperatorClass: {
John McCall56ca35d2011-02-17 10:25:35 +0000206 const AbstractConditionalOperator *CO =
207 cast<AbstractConditionalOperator>(S);
Ted Kremenek72919a32010-02-23 05:59:20 +0000208 return CO->getQuestionLoc();
209 }
210 case Expr::MemberExprClass: {
211 const MemberExpr *ME = cast<MemberExpr>(S);
212 R1 = ME->getSourceRange();
213 return ME->getMemberLoc();
214 }
215 case Expr::ArraySubscriptExprClass: {
216 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(S);
217 R1 = ASE->getLHS()->getSourceRange();
218 R2 = ASE->getRHS()->getSourceRange();
219 return ASE->getRBracketLoc();
220 }
221 case Expr::CStyleCastExprClass: {
222 const CStyleCastExpr *CSC = cast<CStyleCastExpr>(S);
223 R1 = CSC->getSubExpr()->getSourceRange();
224 return CSC->getLParenLoc();
225 }
226 case Expr::CXXFunctionalCastExprClass: {
227 const CXXFunctionalCastExpr *CE = cast <CXXFunctionalCastExpr>(S);
228 R1 = CE->getSubExpr()->getSourceRange();
Eli Friedmancdd4b782013-08-15 22:02:56 +0000229 return CE->getLocStart();
Ted Kremenek72919a32010-02-23 05:59:20 +0000230 }
Ted Kremenek72919a32010-02-23 05:59:20 +0000231 case Stmt::CXXTryStmtClass: {
232 return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
233 }
John McCallf85e1932011-06-15 23:02:42 +0000234 case Expr::ObjCBridgedCastExprClass: {
235 const ObjCBridgedCastExpr *CSC = cast<ObjCBridgedCastExpr>(S);
236 R1 = CSC->getSubExpr()->getSourceRange();
237 return CSC->getLParenLoc();
238 }
Ted Kremenek72919a32010-02-23 05:59:20 +0000239 default: ;
240 }
241 R1 = S->getSourceRange();
242 return S->getLocStart();
243}
244
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000245void DeadCodeScan::reportDeadCode(const Stmt *S,
246 clang::reachable_code::Callback &CB) {
Ted Kremenek72919a32010-02-23 05:59:20 +0000247 SourceRange R1, R2;
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000248 SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
249 CB.HandleUnreachable(Loc, R1, R2);
Ted Kremenek72919a32010-02-23 05:59:20 +0000250}
251
Ted Kremenek72919a32010-02-23 05:59:20 +0000252namespace clang { namespace reachable_code {
David Blaikie99ba9e32011-12-20 02:48:34 +0000253
254void Callback::anchor() { }
255
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000256unsigned ScanReachableFromBlock(const CFGBlock *Start,
Ted Kremenek72919a32010-02-23 05:59:20 +0000257 llvm::BitVector &Reachable) {
258 unsigned count = 0;
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000259
Nico Weber21669482011-04-02 19:45:15 +0000260 // Prep work queue
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000261 SmallVector<const CFGBlock*, 32> WL;
262
263 // The entry block may have already been marked reachable
264 // by the caller.
265 if (!Reachable[Start->getBlockID()]) {
266 ++count;
267 Reachable[Start->getBlockID()] = true;
268 }
269
270 WL.push_back(Start);
271
Ted Kremenek8caec842010-09-09 00:06:10 +0000272 // Find the reachable blocks from 'Start'.
Ted Kremenek72919a32010-02-23 05:59:20 +0000273 while (!WL.empty()) {
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000274 const CFGBlock *item = WL.pop_back_val();
275
276 // Look at the successors and mark then reachable.
277 for (CFGBlock::const_succ_iterator I = item->succ_begin(),
278 E = item->succ_end(); I != E; ++I)
Ted Kremenek3d2eed82010-02-23 02:39:16 +0000279 if (const CFGBlock *B = *I) {
280 unsigned blockID = B->getBlockID();
281 if (!Reachable[blockID]) {
282 Reachable.set(blockID);
Ted Kremenek3d2eed82010-02-23 02:39:16 +0000283 WL.push_back(B);
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000284 ++count;
Ted Kremenek3d2eed82010-02-23 02:39:16 +0000285 }
286 }
287 }
288 return count;
289}
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000290
Ted Kremenek1d26f482011-10-24 01:32:45 +0000291void FindUnreachableCode(AnalysisDeclContext &AC, Callback &CB) {
Ted Kremenek72919a32010-02-23 05:59:20 +0000292 CFG *cfg = AC.getCFG();
293 if (!cfg)
294 return;
295
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000296 // Scan for reachable blocks from the entrance of the CFG.
297 // If there are no unreachable blocks, we're done.
Ted Kremenek72919a32010-02-23 05:59:20 +0000298 llvm::BitVector reachable(cfg->getNumBlockIDs());
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000299 unsigned numReachable = ScanReachableFromBlock(&cfg->getEntry(), reachable);
Ted Kremenek72919a32010-02-23 05:59:20 +0000300 if (numReachable == cfg->getNumBlockIDs())
301 return;
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000302
303 // If there aren't explicit EH edges, we should include the 'try' dispatch
304 // blocks as roots.
305 if (!AC.getCFGBuildOptions().AddEHEdges) {
306 for (CFG::try_block_iterator I = cfg->try_blocks_begin(),
307 E = cfg->try_blocks_end() ; I != E; ++I) {
308 numReachable += ScanReachableFromBlock(*I, reachable);
309 }
310 if (numReachable == cfg->getNumBlockIDs())
311 return;
312 }
Ted Kremenek72919a32010-02-23 05:59:20 +0000313
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000314 // There are some unreachable blocks. We need to find the root blocks that
315 // contain code that should be considered unreachable.
Ted Kremenek72919a32010-02-23 05:59:20 +0000316 for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) {
Ted Kremenek0f3b4ca2011-08-23 23:05:11 +0000317 const CFGBlock *block = *I;
318 // A block may have been marked reachable during this loop.
319 if (reachable[block->getBlockID()])
320 continue;
321
322 DeadCodeScan DS(reachable);
323 numReachable += DS.scanBackwards(block, CB);
324
325 if (numReachable == cfg->getNumBlockIDs())
326 return;
Ted Kremenek72919a32010-02-23 05:59:20 +0000327 }
Ted Kremenek72919a32010-02-23 05:59:20 +0000328}
329
330}} // end namespace clang::reachable_code