1 //=- ReachableCodePathInsensitive.cpp ---------------------------*- C++ --*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a flow-sensitive, path-insensitive analysis of
11 // determining reachable blocks within a CFG.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Analysis/Analyses/ReachableCode.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/ExprObjC.h"
19 #include "clang/AST/ParentMap.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/Analysis/AnalysisDeclContext.h"
22 #include "clang/Analysis/CFG.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "llvm/ADT/BitVector.h"
26 #include "llvm/ADT/SmallVector.h"
28 using namespace clang;
30 //===----------------------------------------------------------------------===//
31 // Core Reachability Analysis routines.
32 //===----------------------------------------------------------------------===//
34 static bool isEnumConstant(const Expr *Ex) {
35 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex);
38 return isa<EnumConstantDecl>(DR->getDecl());
41 static bool isTrivialExpression(const Expr *Ex) {
42 Ex = Ex->IgnoreParenCasts();
43 return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) ||
44 isa<CXXBoolLiteralExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) ||
45 isa<CharacterLiteral>(Ex) ||
49 static bool isTrivialDoWhile(const CFGBlock *B, const Stmt *S) {
50 // Check if the block ends with a do...while() and see if 'S' is the
52 if (const Stmt *Term = B->getTerminator()) {
53 if (const DoStmt *DS = dyn_cast<DoStmt>(Term)) {
54 const Expr *Cond = DS->getCond()->IgnoreParenCasts();
55 return Cond == S && isTrivialExpression(Cond);
61 static bool isBuiltinUnreachable(const Stmt *S) {
62 if (const auto *DRE = dyn_cast<DeclRefExpr>(S))
63 if (const auto *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()))
64 return FDecl->getIdentifier() &&
65 FDecl->getBuiltinID() == Builtin::BI__builtin_unreachable;
69 static bool isDeadReturn(const CFGBlock *B, const Stmt *S) {
70 // Look to see if the current control flow ends with a 'return', and see if
71 // 'S' is a substatement. The 'return' may not be the last element in the
72 // block, or may be in a subsequent block because of destructors.
73 const CFGBlock *Current = B;
75 for (CFGBlock::const_reverse_iterator I = Current->rbegin(),
78 if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
79 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
82 if (const Expr *RE = RS->getRetValue()) {
83 RE = RE->IgnoreParenCasts();
86 ParentMap PM(const_cast<Expr *>(RE));
87 // If 'S' is in the ParentMap, it is a subexpression of
88 // the return statement.
89 return PM.getParent(S);
95 // Note also that we are restricting the search for the return statement
96 // to stop at control-flow; only part of a return statement may be dead,
97 // without the whole return statement being dead.
98 if (Current->getTerminator().isTemporaryDtorsBranch()) {
99 // Temporary destructors have a predictable control flow, thus we want to
100 // look into the next block for the return statement.
101 // We look into the false branch, as we know the true branch only contains
102 // the call to the destructor.
103 assert(Current->succ_size() == 2);
104 Current = *(Current->succ_begin() + 1);
105 } else if (!Current->getTerminator() && Current->succ_size() == 1) {
106 // If there is only one successor, we're not dealing with outgoing control
107 // flow. Thus, look into the next block.
108 Current = *Current->succ_begin();
109 if (Current->pred_size() > 1) {
110 // If there is more than one predecessor, we're dealing with incoming
111 // control flow - if the return statement is in that block, it might
112 // well be reachable via a different control flow, thus it's not dead.
116 // We hit control flow or a dead end. Stop searching.
120 llvm_unreachable("Broke out of infinite loop.");
123 static SourceLocation getTopMostMacro(SourceLocation Loc, SourceManager &SM) {
124 assert(Loc.isMacroID());
126 while (Loc.isMacroID()) {
128 Loc = SM.getImmediateMacroCallerLoc(Loc);
133 /// Returns true if the statement is expanded from a configuration macro.
134 static bool isExpandedFromConfigurationMacro(const Stmt *S,
136 bool IgnoreYES_NO = false) {
137 // FIXME: This is not very precise. Here we just check to see if the
138 // value comes from a macro, but we can do much better. This is likely
139 // to be over conservative. This logic is factored into a separate function
140 // so that we can refine it later.
141 SourceLocation L = S->getLocStart();
143 SourceManager &SM = PP.getSourceManager();
145 // The Objective-C constant 'YES' and 'NO'
146 // are defined as macros. Do not treat them
147 // as configuration values.
148 SourceLocation TopL = getTopMostMacro(L, SM);
149 StringRef MacroName = PP.getImmediateMacroName(TopL);
150 if (MacroName == "YES" || MacroName == "NO")
152 } else if (!PP.getLangOpts().CPlusPlus) {
153 // Do not treat C 'false' and 'true' macros as configuration values.
154 SourceLocation TopL = getTopMostMacro(L, SM);
155 StringRef MacroName = PP.getImmediateMacroName(TopL);
156 if (MacroName == "false" || MacroName == "true")
164 static bool isConfigurationValue(const ValueDecl *D, Preprocessor &PP);
166 /// Returns true if the statement represents a configuration value.
168 /// A configuration value is something usually determined at compile-time
169 /// to conditionally always execute some branch. Such guards are for
170 /// "sometimes unreachable" code. Such code is usually not interesting
171 /// to report as unreachable, and may mask truly unreachable code within
173 static bool isConfigurationValue(const Stmt *S,
175 SourceRange *SilenceableCondVal = nullptr,
176 bool IncludeIntegers = true,
177 bool WrappedInParens = false) {
181 S = S->IgnoreImplicit();
183 if (const Expr *Ex = dyn_cast<Expr>(S))
184 S = Ex->IgnoreCasts();
186 // Special case looking for the sigil '()' around an integer literal.
187 if (const ParenExpr *PE = dyn_cast<ParenExpr>(S))
188 if (!PE->getLocStart().isMacroID())
189 return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal,
190 IncludeIntegers, true);
192 if (const Expr *Ex = dyn_cast<Expr>(S))
193 S = Ex->IgnoreCasts();
195 bool IgnoreYES_NO = false;
197 switch (S->getStmtClass()) {
198 case Stmt::CallExprClass: {
199 const FunctionDecl *Callee =
200 dyn_cast_or_null<FunctionDecl>(cast<CallExpr>(S)->getCalleeDecl());
201 return Callee ? Callee->isConstexpr() : false;
203 case Stmt::DeclRefExprClass:
204 return isConfigurationValue(cast<DeclRefExpr>(S)->getDecl(), PP);
205 case Stmt::ObjCBoolLiteralExprClass:
208 case Stmt::CXXBoolLiteralExprClass:
209 case Stmt::IntegerLiteralClass: {
210 const Expr *E = cast<Expr>(S);
211 if (IncludeIntegers) {
212 if (SilenceableCondVal && !SilenceableCondVal->getBegin().isValid())
213 *SilenceableCondVal = E->getSourceRange();
214 return WrappedInParens || isExpandedFromConfigurationMacro(E, PP, IgnoreYES_NO);
218 case Stmt::MemberExprClass:
219 return isConfigurationValue(cast<MemberExpr>(S)->getMemberDecl(), PP);
220 case Stmt::UnaryExprOrTypeTraitExprClass:
222 case Stmt::BinaryOperatorClass: {
223 const BinaryOperator *B = cast<BinaryOperator>(S);
224 // Only include raw integers (not enums) as configuration
225 // values if they are used in a logical or comparison operator
227 IncludeIntegers &= (B->isLogicalOp() || B->isComparisonOp());
228 return isConfigurationValue(B->getLHS(), PP, SilenceableCondVal,
230 isConfigurationValue(B->getRHS(), PP, SilenceableCondVal,
233 case Stmt::UnaryOperatorClass: {
234 const UnaryOperator *UO = cast<UnaryOperator>(S);
235 if (UO->getOpcode() != UO_LNot)
237 bool SilenceableCondValNotSet =
238 SilenceableCondVal && SilenceableCondVal->getBegin().isInvalid();
239 bool IsSubExprConfigValue =
240 isConfigurationValue(UO->getSubExpr(), PP, SilenceableCondVal,
241 IncludeIntegers, WrappedInParens);
242 // Update the silenceable condition value source range only if the range
243 // was set directly by the child expression.
244 if (SilenceableCondValNotSet &&
245 SilenceableCondVal->getBegin().isValid() &&
246 *SilenceableCondVal ==
247 UO->getSubExpr()->IgnoreCasts()->getSourceRange())
248 *SilenceableCondVal = UO->getSourceRange();
249 return IsSubExprConfigValue;
256 static bool isConfigurationValue(const ValueDecl *D, Preprocessor &PP) {
257 if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D))
258 return isConfigurationValue(ED->getInitExpr(), PP);
259 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
260 // As a heuristic, treat globals as configuration values. Note
261 // that we only will get here if Sema evaluated this
262 // condition to a constant expression, which means the global
263 // had to be declared in a way to be a truly constant value.
264 // We could generalize this to local variables, but it isn't
265 // clear if those truly represent configuration values that
266 // gate unreachable code.
267 if (!VD->hasLocalStorage())
270 // As a heuristic, locals that have been marked 'const' explicitly
271 // can be treated as configuration values as well.
272 return VD->getType().isLocalConstQualified();
277 /// Returns true if we should always explore all successors of a block.
278 static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B,
280 if (const Stmt *Term = B->getTerminator()) {
281 if (isa<SwitchStmt>(Term))
283 // Specially handle '||' and '&&'.
284 if (isa<BinaryOperator>(Term)) {
285 return isConfigurationValue(Term, PP);
289 const Stmt *Cond = B->getTerminatorCondition(/* stripParens */ false);
290 return isConfigurationValue(Cond, PP);
293 static unsigned scanFromBlock(const CFGBlock *Start,
294 llvm::BitVector &Reachable,
296 bool IncludeSometimesUnreachableEdges) {
300 SmallVector<const CFGBlock*, 32> WL;
302 // The entry block may have already been marked reachable
304 if (!Reachable[Start->getBlockID()]) {
306 Reachable[Start->getBlockID()] = true;
311 // Find the reachable blocks from 'Start'.
312 while (!WL.empty()) {
313 const CFGBlock *item = WL.pop_back_val();
315 // There are cases where we want to treat all successors as reachable.
316 // The idea is that some "sometimes unreachable" code is not interesting,
317 // and that we should forge ahead and explore those branches anyway.
318 // This allows us to potentially uncover some "always unreachable" code
319 // within the "sometimes unreachable" code.
320 // Look at the successors and mark then reachable.
321 Optional<bool> TreatAllSuccessorsAsReachable;
322 if (!IncludeSometimesUnreachableEdges)
323 TreatAllSuccessorsAsReachable = false;
325 for (CFGBlock::const_succ_iterator I = item->succ_begin(),
326 E = item->succ_end(); I != E; ++I) {
327 const CFGBlock *B = *I;
329 const CFGBlock *UB = I->getPossiblyUnreachableBlock();
333 if (!TreatAllSuccessorsAsReachable.hasValue()) {
335 TreatAllSuccessorsAsReachable =
336 shouldTreatSuccessorsAsReachable(item, *PP);
339 if (TreatAllSuccessorsAsReachable.getValue()) {
347 unsigned blockID = B->getBlockID();
348 if (!Reachable[blockID]) {
349 Reachable.set(blockID);
359 static unsigned scanMaybeReachableFromBlock(const CFGBlock *Start,
361 llvm::BitVector &Reachable) {
362 return scanFromBlock(Start, Reachable, &PP, true);
365 //===----------------------------------------------------------------------===//
366 // Dead Code Scanner.
367 //===----------------------------------------------------------------------===//
371 llvm::BitVector Visited;
372 llvm::BitVector &Reachable;
373 SmallVector<const CFGBlock *, 10> WorkList;
376 typedef SmallVector<std::pair<const CFGBlock *, const Stmt *>, 12>
379 DeferredLocsTy DeferredLocs;
382 DeadCodeScan(llvm::BitVector &reachable, Preprocessor &PP)
383 : Visited(reachable.size()),
384 Reachable(reachable),
387 void enqueue(const CFGBlock *block);
388 unsigned scanBackwards(const CFGBlock *Start,
389 clang::reachable_code::Callback &CB);
391 bool isDeadCodeRoot(const CFGBlock *Block);
393 const Stmt *findDeadCode(const CFGBlock *Block);
395 void reportDeadCode(const CFGBlock *B,
397 clang::reachable_code::Callback &CB);
401 void DeadCodeScan::enqueue(const CFGBlock *block) {
402 unsigned blockID = block->getBlockID();
403 if (Reachable[blockID] || Visited[blockID])
405 Visited[blockID] = true;
406 WorkList.push_back(block);
409 bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) {
410 bool isDeadRoot = true;
412 for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
413 E = Block->pred_end(); I != E; ++I) {
414 if (const CFGBlock *PredBlock = *I) {
415 unsigned blockID = PredBlock->getBlockID();
416 if (Visited[blockID]) {
420 if (!Reachable[blockID]) {
422 Visited[blockID] = true;
423 WorkList.push_back(PredBlock);
432 static bool isValidDeadStmt(const Stmt *S) {
433 if (S->getLocStart().isInvalid())
435 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S))
436 return BO->getOpcode() != BO_Comma;
440 const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) {
441 for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I!=E; ++I)
442 if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
443 const Stmt *S = CS->getStmt();
444 if (isValidDeadStmt(S))
448 if (CFGTerminator T = Block->getTerminator()) {
449 if (!T.isTemporaryDtorsBranch()) {
450 const Stmt *S = T.getStmt();
451 if (isValidDeadStmt(S))
459 static int SrcCmp(const std::pair<const CFGBlock *, const Stmt *> *p1,
460 const std::pair<const CFGBlock *, const Stmt *> *p2) {
461 if (p1->second->getLocStart() < p2->second->getLocStart())
463 if (p2->second->getLocStart() < p1->second->getLocStart())
468 unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start,
469 clang::reachable_code::Callback &CB) {
474 while (!WorkList.empty()) {
475 const CFGBlock *Block = WorkList.pop_back_val();
477 // It is possible that this block has been marked reachable after
479 if (Reachable[Block->getBlockID()])
482 // Look for any dead code within the block.
483 const Stmt *S = findDeadCode(Block);
486 // No dead code. Possibly an empty block. Look at dead predecessors.
487 for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
488 E = Block->pred_end(); I != E; ++I) {
489 if (const CFGBlock *predBlock = *I)
495 // Specially handle macro-expanded code.
496 if (S->getLocStart().isMacroID()) {
497 count += scanMaybeReachableFromBlock(Block, PP, Reachable);
501 if (isDeadCodeRoot(Block)) {
502 reportDeadCode(Block, S, CB);
503 count += scanMaybeReachableFromBlock(Block, PP, Reachable);
506 // Record this statement as the possibly best location in a
507 // strongly-connected component of dead code for emitting a
509 DeferredLocs.push_back(std::make_pair(Block, S));
513 // If we didn't find a dead root, then report the dead code with the
514 // earliest location.
515 if (!DeferredLocs.empty()) {
516 llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp);
517 for (DeferredLocsTy::iterator I = DeferredLocs.begin(),
518 E = DeferredLocs.end(); I != E; ++I) {
519 const CFGBlock *Block = I->first;
520 if (Reachable[Block->getBlockID()])
522 reportDeadCode(Block, I->second, CB);
523 count += scanMaybeReachableFromBlock(Block, PP, Reachable);
530 static SourceLocation GetUnreachableLoc(const Stmt *S,
533 R1 = R2 = SourceRange();
535 if (const Expr *Ex = dyn_cast<Expr>(S))
536 S = Ex->IgnoreParenImpCasts();
538 switch (S->getStmtClass()) {
539 case Expr::BinaryOperatorClass: {
540 const BinaryOperator *BO = cast<BinaryOperator>(S);
541 return BO->getOperatorLoc();
543 case Expr::UnaryOperatorClass: {
544 const UnaryOperator *UO = cast<UnaryOperator>(S);
545 R1 = UO->getSubExpr()->getSourceRange();
546 return UO->getOperatorLoc();
548 case Expr::CompoundAssignOperatorClass: {
549 const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(S);
550 R1 = CAO->getLHS()->getSourceRange();
551 R2 = CAO->getRHS()->getSourceRange();
552 return CAO->getOperatorLoc();
554 case Expr::BinaryConditionalOperatorClass:
555 case Expr::ConditionalOperatorClass: {
556 const AbstractConditionalOperator *CO =
557 cast<AbstractConditionalOperator>(S);
558 return CO->getQuestionLoc();
560 case Expr::MemberExprClass: {
561 const MemberExpr *ME = cast<MemberExpr>(S);
562 R1 = ME->getSourceRange();
563 return ME->getMemberLoc();
565 case Expr::ArraySubscriptExprClass: {
566 const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(S);
567 R1 = ASE->getLHS()->getSourceRange();
568 R2 = ASE->getRHS()->getSourceRange();
569 return ASE->getRBracketLoc();
571 case Expr::CStyleCastExprClass: {
572 const CStyleCastExpr *CSC = cast<CStyleCastExpr>(S);
573 R1 = CSC->getSubExpr()->getSourceRange();
574 return CSC->getLParenLoc();
576 case Expr::CXXFunctionalCastExprClass: {
577 const CXXFunctionalCastExpr *CE = cast <CXXFunctionalCastExpr>(S);
578 R1 = CE->getSubExpr()->getSourceRange();
579 return CE->getLocStart();
581 case Stmt::CXXTryStmtClass: {
582 return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
584 case Expr::ObjCBridgedCastExprClass: {
585 const ObjCBridgedCastExpr *CSC = cast<ObjCBridgedCastExpr>(S);
586 R1 = CSC->getSubExpr()->getSourceRange();
587 return CSC->getLParenLoc();
591 R1 = S->getSourceRange();
592 return S->getLocStart();
595 void DeadCodeScan::reportDeadCode(const CFGBlock *B,
597 clang::reachable_code::Callback &CB) {
598 // Classify the unreachable code found, or suppress it in some cases.
599 reachable_code::UnreachableKind UK = reachable_code::UK_Other;
601 if (isa<BreakStmt>(S)) {
602 UK = reachable_code::UK_Break;
603 } else if (isTrivialDoWhile(B, S) || isBuiltinUnreachable(S)) {
606 else if (isDeadReturn(B, S)) {
607 UK = reachable_code::UK_Return;
610 SourceRange SilenceableCondVal;
612 if (UK == reachable_code::UK_Other) {
613 // Check if the dead code is part of the "loop target" of
614 // a for/for-range loop. This is the block that contains
615 // the increment code.
616 if (const Stmt *LoopTarget = B->getLoopTarget()) {
617 SourceLocation Loc = LoopTarget->getLocStart();
618 SourceRange R1(Loc, Loc), R2;
620 if (const ForStmt *FS = dyn_cast<ForStmt>(LoopTarget)) {
621 const Expr *Inc = FS->getInc();
622 Loc = Inc->getLocStart();
623 R2 = Inc->getSourceRange();
626 CB.HandleUnreachable(reachable_code::UK_Loop_Increment,
627 Loc, SourceRange(), SourceRange(Loc, Loc), R2);
631 // Check if the dead block has a predecessor whose branch has
632 // a configuration value that *could* be modified to
633 // silence the warning.
634 CFGBlock::const_pred_iterator PI = B->pred_begin();
635 if (PI != B->pred_end()) {
636 if (const CFGBlock *PredBlock = PI->getPossiblyUnreachableBlock()) {
637 const Stmt *TermCond =
638 PredBlock->getTerminatorCondition(/* strip parens */ false);
639 isConfigurationValue(TermCond, PP, &SilenceableCondVal);
645 SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
646 CB.HandleUnreachable(UK, Loc, SilenceableCondVal, R1, R2);
649 //===----------------------------------------------------------------------===//
650 // Reachability APIs.
651 //===----------------------------------------------------------------------===//
653 namespace clang { namespace reachable_code {
655 void Callback::anchor() { }
657 unsigned ScanReachableFromBlock(const CFGBlock *Start,
658 llvm::BitVector &Reachable) {
659 return scanFromBlock(Start, Reachable, /* SourceManager* */ nullptr, false);
662 void FindUnreachableCode(AnalysisDeclContext &AC, Preprocessor &PP,
665 CFG *cfg = AC.getCFG();
669 // Scan for reachable blocks from the entrance of the CFG.
670 // If there are no unreachable blocks, we're done.
671 llvm::BitVector reachable(cfg->getNumBlockIDs());
672 unsigned numReachable =
673 scanMaybeReachableFromBlock(&cfg->getEntry(), PP, reachable);
674 if (numReachable == cfg->getNumBlockIDs())
677 // If there aren't explicit EH edges, we should include the 'try' dispatch
679 if (!AC.getCFGBuildOptions().AddEHEdges) {
680 for (CFG::try_block_iterator I = cfg->try_blocks_begin(),
681 E = cfg->try_blocks_end() ; I != E; ++I) {
682 numReachable += scanMaybeReachableFromBlock(*I, PP, reachable);
684 if (numReachable == cfg->getNumBlockIDs())
688 // There are some unreachable blocks. We need to find the root blocks that
689 // contain code that should be considered unreachable.
690 for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) {
691 const CFGBlock *block = *I;
692 // A block may have been marked reachable during this loop.
693 if (reachable[block->getBlockID()])
696 DeadCodeScan DS(reachable, PP);
697 numReachable += DS.scanBackwards(block, CB);
699 if (numReachable == cfg->getNumBlockIDs())
704 }} // end namespace clang::reachable_code