blob: f563cbdc593051a3d6624d028f2cba9c96e63207 [file] [log] [blame]
Ted Kremenek7296de92010-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 Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Analysis/Analyses/ReachableCode.h"
Ted Kremenek552eeaa2010-02-23 05:59:20 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/ExprCXX.h"
John McCall31168b02011-06-15 23:02:42 +000018#include "clang/AST/ExprObjC.h"
Ted Kremenek552eeaa2010-02-23 05:59:20 +000019#include "clang/AST/StmtCXX.h"
Ted Kremenek552eeaa2010-02-23 05:59:20 +000020#include "clang/Analysis/AnalysisContext.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Analysis/CFG.h"
Ted Kremenek552eeaa2010-02-23 05:59:20 +000022#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/ADT/BitVector.h"
24#include "llvm/ADT/SmallVector.h"
Ted Kremenek7296de92010-02-23 02:39:16 +000025
26using namespace clang;
27
Ted Kremenek7d47cac2014-03-07 07:14:36 +000028//===----------------------------------------------------------------------===//
29// Core Reachability Analysis routines.
30//===----------------------------------------------------------------------===//
Ted Kremenek552eeaa2010-02-23 05:59:20 +000031
Ted Kremenekcc893382014-02-27 00:24:08 +000032static bool bodyEndsWithNoReturn(const CFGBlock *B) {
33 for (CFGBlock::const_reverse_iterator I = B->rbegin(), E = B->rend();
34 I != E; ++I) {
35 if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
Ted Kremenekec2dc732014-03-06 06:50:46 +000036 const Stmt *S = CS->getStmt();
37 if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S))
38 S = EWC->getSubExpr();
39 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenekcc893382014-02-27 00:24:08 +000040 QualType CalleeType = CE->getCallee()->getType();
41 if (getFunctionExtInfo(*CalleeType).getNoReturn())
42 return true;
43 }
44 break;
45 }
46 }
47 return false;
48}
49
Ted Kremenek5441c182014-02-27 06:32:32 +000050static bool bodyEndsWithNoReturn(const CFGBlock::AdjacentBlock &AB) {
Ted Kremenekeb862842014-03-04 21:41:38 +000051 // If the predecessor is a normal CFG edge, then by definition
52 // the predecessor did not end with a 'noreturn'.
53 if (AB.getReachableBlock())
54 return false;
55
Ted Kremenek5441c182014-02-27 06:32:32 +000056 const CFGBlock *Pred = AB.getPossiblyUnreachableBlock();
57 assert(!AB.isReachable() && Pred);
58 return bodyEndsWithNoReturn(Pred);
59}
60
Ted Kremenekcc893382014-02-27 00:24:08 +000061static bool isBreakPrecededByNoReturn(const CFGBlock *B,
62 const Stmt *S) {
63 if (!isa<BreakStmt>(S) || B->pred_empty())
64 return false;
65
66 assert(B->empty());
67 assert(B->pred_size() == 1);
Ted Kremenek5441c182014-02-27 06:32:32 +000068 return bodyEndsWithNoReturn(*B->pred_begin());
69}
70
71static bool isEnumConstant(const Expr *Ex) {
72 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex);
73 if (!DR)
74 return false;
75 return isa<EnumConstantDecl>(DR->getDecl());
76}
77
Ted Kremenekec2dc732014-03-06 06:50:46 +000078static const Expr *stripStdStringCtor(const Expr *Ex) {
79 // Go crazy pattern matching an implicit construction of std::string("").
80 const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Ex);
81 if (!EWC)
82 return 0;
83 const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(EWC->getSubExpr());
84 if (!CCE)
85 return 0;
86 QualType Ty = CCE->getType();
87 if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty))
88 Ty = ET->getNamedType();
89 const TypedefType *TT = dyn_cast<TypedefType>(Ty);
90 StringRef Name = TT->getDecl()->getName();
91 if (Name != "string")
92 return 0;
93 if (CCE->getNumArgs() != 1)
94 return 0;
95 const MaterializeTemporaryExpr *MTE =
96 dyn_cast<MaterializeTemporaryExpr>(CCE->getArg(0));
97 if (!MTE)
98 return 0;
99 CXXBindTemporaryExpr *CBT =
100 dyn_cast<CXXBindTemporaryExpr>(MTE->GetTemporaryExpr()->IgnoreParenCasts());
101 if (!CBT)
102 return 0;
103 Ex = CBT->getSubExpr()->IgnoreParenCasts();
104 CCE = dyn_cast<CXXConstructExpr>(Ex);
105 if (!CCE)
106 return 0;
107 if (CCE->getNumArgs() != 1)
108 return 0;
109 return dyn_cast<StringLiteral>(CCE->getArg(0)->IgnoreParenCasts());
110}
111
112/// Strip away "sugar" around trivial expressions that are for the
113/// purpose of this analysis considered uninteresting for dead code warnings.
114static const Expr *stripExprSugar(const Expr *Ex) {
115 Ex = Ex->IgnoreParenCasts();
116 // If 'Ex' is a constructor for a std::string, strip that
117 // away. We can only get here if the trivial expression was
118 // something like a C string literal, with the std::string
119 // just wrapping that value.
120 if (const Expr *StdStringVal = stripStdStringCtor(Ex))
121 return StdStringVal;
122 return Ex;
123}
124
Ted Kremenek5441c182014-02-27 06:32:32 +0000125static bool isTrivialExpression(const Expr *Ex) {
Ted Kremenek0a69cab2014-03-05 23:46:07 +0000126 Ex = Ex->IgnoreParenCasts();
Ted Kremenek5441c182014-02-27 06:32:32 +0000127 return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) ||
Ted Kremenekc10830b2014-03-07 02:25:50 +0000128 isa<CXXBoolLiteralExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) ||
129 isa<CharacterLiteral>(Ex) ||
Ted Kremenek5441c182014-02-27 06:32:32 +0000130 isEnumConstant(Ex);
131}
132
Ted Kremenek1de2e142014-03-06 00:17:44 +0000133static bool isTrivialReturnOrDoWhile(const CFGBlock *B, const Stmt *S) {
Ted Kremenek5441c182014-02-27 06:32:32 +0000134 const Expr *Ex = dyn_cast<Expr>(S);
135 if (!Ex)
136 return false;
137
Ted Kremenek5441c182014-02-27 06:32:32 +0000138 if (!isTrivialExpression(Ex))
139 return false;
140
Ted Kremenek1de2e142014-03-06 00:17:44 +0000141 // Check if the block ends with a do...while() and see if 'S' is the
142 // condition.
143 if (const Stmt *Term = B->getTerminator()) {
144 if (const DoStmt *DS = dyn_cast<DoStmt>(Term))
145 if (DS->getCond() == S)
146 return true;
147 }
148
Ted Kremenek7549f0f2014-03-06 01:09:45 +0000149 if (B->pred_size() != 1)
150 return false;
Ted Kremenek1de2e142014-03-06 00:17:44 +0000151
Ted Kremenek5441c182014-02-27 06:32:32 +0000152 // Look to see if the block ends with a 'return', and see if 'S'
153 // is a substatement. The 'return' may not be the last element in
154 // the block because of destructors.
155 assert(!B->empty());
156 for (CFGBlock::const_reverse_iterator I = B->rbegin(), E = B->rend();
157 I != E; ++I) {
158 if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
159 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
160 const Expr *RE = RS->getRetValue();
Ted Kremenekec2dc732014-03-06 06:50:46 +0000161 if (RE && stripExprSugar(RE->IgnoreParenCasts()) == Ex)
Ted Kremenek7549f0f2014-03-06 01:09:45 +0000162 return bodyEndsWithNoReturn(*B->pred_begin());
Ted Kremenek5441c182014-02-27 06:32:32 +0000163 }
Ted Kremenek7549f0f2014-03-06 01:09:45 +0000164 break;
Ted Kremenek5441c182014-02-27 06:32:32 +0000165 }
166 }
Ted Kremenek0a69cab2014-03-05 23:46:07 +0000167 return false;
Ted Kremenekcc893382014-02-27 00:24:08 +0000168}
169
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000170/// Returns true if the statement is expanded from a configuration macro.
171static bool isExpandedFromConfigurationMacro(const Stmt *S) {
172 // FIXME: This is not very precise. Here we just check to see if the
173 // value comes from a macro, but we can do much better. This is likely
174 // to be over conservative. This logic is factored into a separate function
175 // so that we can refine it later.
176 SourceLocation L = S->getLocStart();
177 return L.isMacroID();
178}
179
180/// Returns true if the statement represents a configuration value.
181///
182/// A configuration value is something usually determined at compile-time
183/// to conditionally always execute some branch. Such guards are for
184/// "sometimes unreachable" code. Such code is usually not interesting
185/// to report as unreachable, and may mask truly unreachable code within
186/// those blocks.
187static bool isConfigurationValue(const Stmt *S) {
188 if (!S)
189 return false;
190
191 if (const Expr *Ex = dyn_cast<Expr>(S))
192 S = Ex->IgnoreParenCasts();
193
194 switch (S->getStmtClass()) {
Ted Kremenek01a39b62014-03-05 23:38:41 +0000195 case Stmt::DeclRefExprClass: {
196 const DeclRefExpr *DR = cast<DeclRefExpr>(S);
197 const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(DR->getDecl());
198 return ED ? isConfigurationValue(ED->getInitExpr()) : false;
199 }
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000200 case Stmt::IntegerLiteralClass:
201 return isExpandedFromConfigurationMacro(S);
202 case Stmt::UnaryExprOrTypeTraitExprClass:
203 return true;
204 case Stmt::BinaryOperatorClass: {
205 const BinaryOperator *B = cast<BinaryOperator>(S);
Ted Kremenek3cdbc392014-03-05 22:32:39 +0000206 return (B->isLogicalOp() || B->isComparisonOp()) &&
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000207 (isConfigurationValue(B->getLHS()) ||
208 isConfigurationValue(B->getRHS()));
209 }
210 case Stmt::UnaryOperatorClass: {
211 const UnaryOperator *UO = cast<UnaryOperator>(S);
212 return UO->getOpcode() == UO_LNot &&
213 isConfigurationValue(UO->getSubExpr());
214 }
215 default:
216 return false;
217 }
218}
219
220/// Returns true if we should always explore all successors of a block.
221static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B) {
Ted Kremenek782f0032014-03-07 02:25:53 +0000222 if (const Stmt *Term = B->getTerminator()) {
Ted Kremenek6999d022014-03-06 08:09:00 +0000223 if (isa<SwitchStmt>(Term))
224 return true;
Ted Kremenek782f0032014-03-07 02:25:53 +0000225 // Specially handle '||' and '&&'.
226 if (isa<BinaryOperator>(Term))
227 return isConfigurationValue(Term);
228 }
Ted Kremenek6999d022014-03-06 08:09:00 +0000229
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000230 return isConfigurationValue(B->getTerminatorCondition());
231}
232
Ted Kremenek7d47cac2014-03-07 07:14:36 +0000233static unsigned scanFromBlock(const CFGBlock *Start,
234 llvm::BitVector &Reachable,
235 bool IncludeSometimesUnreachableEdges) {
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000236 unsigned count = 0;
Ted Kremenekbd913712011-08-23 23:05:11 +0000237
Nico Webercc2b8712011-04-02 19:45:15 +0000238 // Prep work queue
Ted Kremenekbd913712011-08-23 23:05:11 +0000239 SmallVector<const CFGBlock*, 32> WL;
240
241 // The entry block may have already been marked reachable
242 // by the caller.
243 if (!Reachable[Start->getBlockID()]) {
244 ++count;
245 Reachable[Start->getBlockID()] = true;
246 }
247
248 WL.push_back(Start);
249
Ted Kremenekf2b0a1b2010-09-09 00:06:10 +0000250 // Find the reachable blocks from 'Start'.
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000251 while (!WL.empty()) {
Ted Kremenekbd913712011-08-23 23:05:11 +0000252 const CFGBlock *item = WL.pop_back_val();
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000253
254 // There are cases where we want to treat all successors as reachable.
255 // The idea is that some "sometimes unreachable" code is not interesting,
256 // and that we should forge ahead and explore those branches anyway.
257 // This allows us to potentially uncover some "always unreachable" code
258 // within the "sometimes unreachable" code.
Ted Kremenekbd913712011-08-23 23:05:11 +0000259 // Look at the successors and mark then reachable.
Ted Kremenek782f0032014-03-07 02:25:53 +0000260 Optional<bool> TreatAllSuccessorsAsReachable;
Ted Kremenek7d47cac2014-03-07 07:14:36 +0000261 if (!IncludeSometimesUnreachableEdges)
262 TreatAllSuccessorsAsReachable = false;
Ted Kremenek782f0032014-03-07 02:25:53 +0000263
Ted Kremenekbd913712011-08-23 23:05:11 +0000264 for (CFGBlock::const_succ_iterator I = item->succ_begin(),
Ted Kremenek08da9782014-02-27 21:56:47 +0000265 E = item->succ_end(); I != E; ++I) {
266 const CFGBlock *B = *I;
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000267 if (!B) do {
268 const CFGBlock *UB = I->getPossiblyUnreachableBlock();
269 if (!UB)
270 break;
271
Ted Kremenek782f0032014-03-07 02:25:53 +0000272 if (!TreatAllSuccessorsAsReachable.hasValue())
273 TreatAllSuccessorsAsReachable =
274 shouldTreatSuccessorsAsReachable(item);
275
276 if (TreatAllSuccessorsAsReachable.getValue()) {
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000277 B = UB;
278 break;
279 }
Ted Kremenek08da9782014-02-27 21:56:47 +0000280 }
Ted Kremenek6d9bb562014-03-05 00:01:17 +0000281 while (false);
282
Ted Kremenek08da9782014-02-27 21:56:47 +0000283 if (B) {
Ted Kremenek7296de92010-02-23 02:39:16 +0000284 unsigned blockID = B->getBlockID();
285 if (!Reachable[blockID]) {
286 Reachable.set(blockID);
Ted Kremenek7296de92010-02-23 02:39:16 +0000287 WL.push_back(B);
Ted Kremenekbd913712011-08-23 23:05:11 +0000288 ++count;
Ted Kremenek7296de92010-02-23 02:39:16 +0000289 }
290 }
Ted Kremenek08da9782014-02-27 21:56:47 +0000291 }
Ted Kremenek7296de92010-02-23 02:39:16 +0000292 }
293 return count;
294}
Ted Kremenek7d47cac2014-03-07 07:14:36 +0000295
296static unsigned scanMaybeReachableFromBlock(const CFGBlock *Start,
297 llvm::BitVector &Reachable) {
298 return scanFromBlock(Start, Reachable, true);
299}
300
301//===----------------------------------------------------------------------===//
302// Dead Code Scanner.
303//===----------------------------------------------------------------------===//
304
305namespace {
306 class DeadCodeScan {
307 llvm::BitVector Visited;
308 llvm::BitVector &Reachable;
309 SmallVector<const CFGBlock *, 10> WorkList;
310
311 typedef SmallVector<std::pair<const CFGBlock *, const Stmt *>, 12>
312 DeferredLocsTy;
313
314 DeferredLocsTy DeferredLocs;
315
316 public:
317 DeadCodeScan(llvm::BitVector &reachable)
318 : Visited(reachable.size()),
319 Reachable(reachable) {}
320
321 void enqueue(const CFGBlock *block);
322 unsigned scanBackwards(const CFGBlock *Start,
323 clang::reachable_code::Callback &CB);
324
325 bool isDeadCodeRoot(const CFGBlock *Block);
326
327 const Stmt *findDeadCode(const CFGBlock *Block);
328
329 void reportDeadCode(const CFGBlock *B,
330 const Stmt *S,
331 clang::reachable_code::Callback &CB);
332 };
333}
334
335void DeadCodeScan::enqueue(const CFGBlock *block) {
336 unsigned blockID = block->getBlockID();
337 if (Reachable[blockID] || Visited[blockID])
338 return;
339 Visited[blockID] = true;
340 WorkList.push_back(block);
341}
342
343bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) {
344 bool isDeadRoot = true;
345
346 for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
347 E = Block->pred_end(); I != E; ++I) {
348 if (const CFGBlock *PredBlock = *I) {
349 unsigned blockID = PredBlock->getBlockID();
350 if (Visited[blockID]) {
351 isDeadRoot = false;
352 continue;
353 }
354 if (!Reachable[blockID]) {
355 isDeadRoot = false;
356 Visited[blockID] = true;
357 WorkList.push_back(PredBlock);
358 continue;
359 }
360 }
361 }
362
363 return isDeadRoot;
364}
365
366static bool isValidDeadStmt(const Stmt *S) {
367 if (S->getLocStart().isInvalid())
368 return false;
369 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S))
370 return BO->getOpcode() != BO_Comma;
371 return true;
372}
373
374const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) {
375 for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I!=E; ++I)
376 if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
377 const Stmt *S = CS->getStmt();
378 if (isValidDeadStmt(S))
379 return S;
380 }
381
382 if (CFGTerminator T = Block->getTerminator()) {
383 const Stmt *S = T.getStmt();
384 if (isValidDeadStmt(S))
385 return S;
386 }
387
388 return 0;
389}
390
391static int SrcCmp(const std::pair<const CFGBlock *, const Stmt *> *p1,
392 const std::pair<const CFGBlock *, const Stmt *> *p2) {
393 if (p1->second->getLocStart() < p2->second->getLocStart())
394 return -1;
395 if (p2->second->getLocStart() < p1->second->getLocStart())
396 return 1;
397 return 0;
398}
399
400unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start,
401 clang::reachable_code::Callback &CB) {
402
403 unsigned count = 0;
404 enqueue(Start);
405
406 while (!WorkList.empty()) {
407 const CFGBlock *Block = WorkList.pop_back_val();
408
409 // It is possible that this block has been marked reachable after
410 // it was enqueued.
411 if (Reachable[Block->getBlockID()])
412 continue;
413
414 // Look for any dead code within the block.
415 const Stmt *S = findDeadCode(Block);
416
417 if (!S) {
418 // No dead code. Possibly an empty block. Look at dead predecessors.
419 for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
420 E = Block->pred_end(); I != E; ++I) {
421 if (const CFGBlock *predBlock = *I)
422 enqueue(predBlock);
423 }
424 continue;
425 }
426
427 // Specially handle macro-expanded code.
428 if (S->getLocStart().isMacroID()) {
429 count += scanMaybeReachableFromBlock(Block, Reachable);
430 continue;
431 }
432
433 if (isDeadCodeRoot(Block)) {
434 reportDeadCode(Block, S, CB);
435 count += scanMaybeReachableFromBlock(Block, Reachable);
436 }
437 else {
438 // Record this statement as the possibly best location in a
439 // strongly-connected component of dead code for emitting a
440 // warning.
441 DeferredLocs.push_back(std::make_pair(Block, S));
442 }
443 }
444
445 // If we didn't find a dead root, then report the dead code with the
446 // earliest location.
447 if (!DeferredLocs.empty()) {
448 llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp);
449 for (DeferredLocsTy::iterator I = DeferredLocs.begin(),
450 E = DeferredLocs.end(); I != E; ++I) {
451 const CFGBlock *Block = I->first;
452 if (Reachable[Block->getBlockID()])
453 continue;
454 reportDeadCode(Block, I->second, CB);
455 count += scanMaybeReachableFromBlock(Block, Reachable);
456 }
457 }
458
459 return count;
460}
461
462static SourceLocation GetUnreachableLoc(const Stmt *S,
463 SourceRange &R1,
464 SourceRange &R2) {
465 R1 = R2 = SourceRange();
466
467 if (const Expr *Ex = dyn_cast<Expr>(S))
468 S = Ex->IgnoreParenImpCasts();
469
470 switch (S->getStmtClass()) {
471 case Expr::BinaryOperatorClass: {
472 const BinaryOperator *BO = cast<BinaryOperator>(S);
473 return BO->getOperatorLoc();
474 }
475 case Expr::UnaryOperatorClass: {
476 const UnaryOperator *UO = cast<UnaryOperator>(S);
477 R1 = UO->getSubExpr()->getSourceRange();
478 return UO->getOperatorLoc();
479 }
480 case Expr::CompoundAssignOperatorClass: {
481 const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(S);
482 R1 = CAO->getLHS()->getSourceRange();
483 R2 = CAO->getRHS()->getSourceRange();
484 return CAO->getOperatorLoc();
485 }
486 case Expr::BinaryConditionalOperatorClass:
487 case Expr::ConditionalOperatorClass: {
488 const AbstractConditionalOperator *CO =
489 cast<AbstractConditionalOperator>(S);
490 return CO->getQuestionLoc();
491 }
492 case Expr::MemberExprClass: {
493 const MemberExpr *ME = cast<MemberExpr>(S);
494 R1 = ME->getSourceRange();
495 return ME->getMemberLoc();
496 }
497 case Expr::ArraySubscriptExprClass: {
498 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(S);
499 R1 = ASE->getLHS()->getSourceRange();
500 R2 = ASE->getRHS()->getSourceRange();
501 return ASE->getRBracketLoc();
502 }
503 case Expr::CStyleCastExprClass: {
504 const CStyleCastExpr *CSC = cast<CStyleCastExpr>(S);
505 R1 = CSC->getSubExpr()->getSourceRange();
506 return CSC->getLParenLoc();
507 }
508 case Expr::CXXFunctionalCastExprClass: {
509 const CXXFunctionalCastExpr *CE = cast <CXXFunctionalCastExpr>(S);
510 R1 = CE->getSubExpr()->getSourceRange();
511 return CE->getLocStart();
512 }
513 case Stmt::CXXTryStmtClass: {
514 return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
515 }
516 case Expr::ObjCBridgedCastExprClass: {
517 const ObjCBridgedCastExpr *CSC = cast<ObjCBridgedCastExpr>(S);
518 R1 = CSC->getSubExpr()->getSourceRange();
519 return CSC->getLParenLoc();
520 }
521 default: ;
522 }
523 R1 = S->getSourceRange();
524 return S->getLocStart();
525}
526
527void DeadCodeScan::reportDeadCode(const CFGBlock *B,
528 const Stmt *S,
529 clang::reachable_code::Callback &CB) {
530 // Suppress idiomatic cases of calling a noreturn function just
531 // before executing a 'break'. If there is other code after the 'break'
532 // in the block then don't suppress the warning.
533 if (isBreakPrecededByNoReturn(B, S))
534 return;
535
536 // Suppress trivial 'return' statements that are dead.
537 if (isTrivialReturnOrDoWhile(B, S))
538 return;
539
540 SourceRange R1, R2;
541 SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
542 CB.HandleUnreachable(Loc, R1, R2);
543}
544
545//===----------------------------------------------------------------------===//
546// Reachability APIs.
547//===----------------------------------------------------------------------===//
548
549namespace clang { namespace reachable_code {
550
551void Callback::anchor() { }
552
553unsigned ScanReachableFromBlock(const CFGBlock *Start,
554 llvm::BitVector &Reachable) {
555 return scanFromBlock(Start, Reachable, false);
556}
557
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000558void FindUnreachableCode(AnalysisDeclContext &AC, Callback &CB) {
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000559 CFG *cfg = AC.getCFG();
560 if (!cfg)
561 return;
562
Ted Kremenekbd913712011-08-23 23:05:11 +0000563 // Scan for reachable blocks from the entrance of the CFG.
564 // If there are no unreachable blocks, we're done.
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000565 llvm::BitVector reachable(cfg->getNumBlockIDs());
Ted Kremenek7d47cac2014-03-07 07:14:36 +0000566 unsigned numReachable =
567 scanMaybeReachableFromBlock(&cfg->getEntry(), reachable);
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000568 if (numReachable == cfg->getNumBlockIDs())
569 return;
Ted Kremenekbd913712011-08-23 23:05:11 +0000570
571 // If there aren't explicit EH edges, we should include the 'try' dispatch
572 // blocks as roots.
573 if (!AC.getCFGBuildOptions().AddEHEdges) {
574 for (CFG::try_block_iterator I = cfg->try_blocks_begin(),
575 E = cfg->try_blocks_end() ; I != E; ++I) {
Ted Kremenek7d47cac2014-03-07 07:14:36 +0000576 numReachable += scanMaybeReachableFromBlock(*I, reachable);
Ted Kremenekbd913712011-08-23 23:05:11 +0000577 }
578 if (numReachable == cfg->getNumBlockIDs())
579 return;
580 }
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000581
Ted Kremenekbd913712011-08-23 23:05:11 +0000582 // There are some unreachable blocks. We need to find the root blocks that
583 // contain code that should be considered unreachable.
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000584 for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) {
Ted Kremenekbd913712011-08-23 23:05:11 +0000585 const CFGBlock *block = *I;
586 // A block may have been marked reachable during this loop.
587 if (reachable[block->getBlockID()])
588 continue;
589
590 DeadCodeScan DS(reachable);
591 numReachable += DS.scanBackwards(block, CB);
592
593 if (numReachable == cfg->getNumBlockIDs())
594 return;
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000595 }
Ted Kremenek552eeaa2010-02-23 05:59:20 +0000596}
597
598}} // end namespace clang::reachable_code