Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 1 | //=- 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 Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/Analyses/ReachableCode.h" |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/ExprCXX.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/ParentMap.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtCXX.h" |
George Karpenkov | 50657f6 | 2017-09-06 21:45:03 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/AnalysisDeclContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/BitVector.h" |
| 26 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Core Reachability Analysis routines. |
| 32 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 34 | static bool isEnumConstant(const Expr *Ex) { |
| 35 | const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex); |
| 36 | if (!DR) |
| 37 | return false; |
| 38 | return isa<EnumConstantDecl>(DR->getDecl()); |
| 39 | } |
| 40 | |
| 41 | static bool isTrivialExpression(const Expr *Ex) { |
Ted Kremenek | 0a69cab | 2014-03-05 23:46:07 +0000 | [diff] [blame] | 42 | Ex = Ex->IgnoreParenCasts(); |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 43 | return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) || |
Ted Kremenek | c10830b | 2014-03-07 02:25:50 +0000 | [diff] [blame] | 44 | isa<CXXBoolLiteralExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) || |
| 45 | isa<CharacterLiteral>(Ex) || |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 46 | isEnumConstant(Ex); |
| 47 | } |
| 48 | |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 49 | static bool isTrivialDoWhile(const CFGBlock *B, const Stmt *S) { |
Ted Kremenek | 1de2e14 | 2014-03-06 00:17:44 +0000 | [diff] [blame] | 50 | // Check if the block ends with a do...while() and see if 'S' is the |
| 51 | // condition. |
| 52 | if (const Stmt *Term = B->getTerminator()) { |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 53 | if (const DoStmt *DS = dyn_cast<DoStmt>(Term)) { |
Ted Kremenek | d457631 | 2014-03-20 18:47:53 +0000 | [diff] [blame] | 54 | const Expr *Cond = DS->getCond()->IgnoreParenCasts(); |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 55 | return Cond == S && isTrivialExpression(Cond); |
| 56 | } |
Ted Kremenek | 1de2e14 | 2014-03-06 00:17:44 +0000 | [diff] [blame] | 57 | } |
Ted Kremenek | ad8753c | 2014-03-15 05:47:06 +0000 | [diff] [blame] | 58 | return false; |
| 59 | } |
Ted Kremenek | 1de2e14 | 2014-03-06 00:17:44 +0000 | [diff] [blame] | 60 | |
Alex Lorenz | 46103e0 | 2017-04-11 15:36:06 +0000 | [diff] [blame] | 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; |
| 66 | return false; |
| 67 | } |
| 68 | |
Nico Weber | 758fbac | 2018-02-13 21:31:47 +0000 | [diff] [blame] | 69 | static bool isBuiltinAssumeFalse(const CFGBlock *B, const Stmt *S, |
| 70 | ASTContext &C) { |
| 71 | if (B->empty()) { |
| 72 | // Happens if S is B's terminator and B contains nothing else |
| 73 | // (e.g. a CFGBlock containing only a goto). |
| 74 | return false; |
| 75 | } |
| 76 | if (Optional<CFGStmt> CS = B->back().getAs<CFGStmt>()) { |
| 77 | if (const auto *CE = dyn_cast<CallExpr>(CS->getStmt())) { |
| 78 | return CE->getCallee()->IgnoreCasts() == S && CE->isBuiltinAssumeFalse(C); |
| 79 | } |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 84 | static bool isDeadReturn(const CFGBlock *B, const Stmt *S) { |
Manuel Klimek | 5fbdc93 | 2014-05-23 17:09:56 +0000 | [diff] [blame] | 85 | // Look to see if the current control flow ends with a 'return', and see if |
| 86 | // 'S' is a substatement. The 'return' may not be the last element in the |
| 87 | // block, or may be in a subsequent block because of destructors. |
| 88 | const CFGBlock *Current = B; |
| 89 | while (true) { |
| 90 | for (CFGBlock::const_reverse_iterator I = Current->rbegin(), |
| 91 | E = Current->rend(); |
| 92 | I != E; ++I) { |
| 93 | if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) { |
| 94 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) { |
| 95 | if (RS == S) |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 96 | return true; |
Manuel Klimek | 5fbdc93 | 2014-05-23 17:09:56 +0000 | [diff] [blame] | 97 | if (const Expr *RE = RS->getRetValue()) { |
| 98 | RE = RE->IgnoreParenCasts(); |
| 99 | if (RE == S) |
| 100 | return true; |
| 101 | ParentMap PM(const_cast<Expr *>(RE)); |
| 102 | // If 'S' is in the ParentMap, it is a subexpression of |
| 103 | // the return statement. |
| 104 | return PM.getParent(S); |
| 105 | } |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 106 | } |
Manuel Klimek | 5fbdc93 | 2014-05-23 17:09:56 +0000 | [diff] [blame] | 107 | break; |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 108 | } |
Manuel Klimek | 5fbdc93 | 2014-05-23 17:09:56 +0000 | [diff] [blame] | 109 | } |
| 110 | // Note also that we are restricting the search for the return statement |
| 111 | // to stop at control-flow; only part of a return statement may be dead, |
| 112 | // without the whole return statement being dead. |
| 113 | if (Current->getTerminator().isTemporaryDtorsBranch()) { |
| 114 | // Temporary destructors have a predictable control flow, thus we want to |
| 115 | // look into the next block for the return statement. |
| 116 | // We look into the false branch, as we know the true branch only contains |
| 117 | // the call to the destructor. |
| 118 | assert(Current->succ_size() == 2); |
| 119 | Current = *(Current->succ_begin() + 1); |
| 120 | } else if (!Current->getTerminator() && Current->succ_size() == 1) { |
| 121 | // If there is only one successor, we're not dealing with outgoing control |
| 122 | // flow. Thus, look into the next block. |
| 123 | Current = *Current->succ_begin(); |
| 124 | if (Current->pred_size() > 1) { |
| 125 | // If there is more than one predecessor, we're dealing with incoming |
| 126 | // control flow - if the return statement is in that block, it might |
| 127 | // well be reachable via a different control flow, thus it's not dead. |
| 128 | return false; |
| 129 | } |
| 130 | } else { |
| 131 | // We hit control flow or a dead end. Stop searching. |
| 132 | return false; |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
Manuel Klimek | 5fbdc93 | 2014-05-23 17:09:56 +0000 | [diff] [blame] | 135 | llvm_unreachable("Broke out of infinite loop."); |
Ted Kremenek | cc89338 | 2014-02-27 00:24:08 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 138 | static SourceLocation getTopMostMacro(SourceLocation Loc, SourceManager &SM) { |
| 139 | assert(Loc.isMacroID()); |
| 140 | SourceLocation Last; |
| 141 | while (Loc.isMacroID()) { |
| 142 | Last = Loc; |
| 143 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
| 144 | } |
| 145 | return Last; |
| 146 | } |
| 147 | |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 148 | /// Returns true if the statement is expanded from a configuration macro. |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 149 | static bool isExpandedFromConfigurationMacro(const Stmt *S, |
| 150 | Preprocessor &PP, |
| 151 | bool IgnoreYES_NO = false) { |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 152 | // FIXME: This is not very precise. Here we just check to see if the |
| 153 | // value comes from a macro, but we can do much better. This is likely |
| 154 | // to be over conservative. This logic is factored into a separate function |
| 155 | // so that we can refine it later. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 156 | SourceLocation L = S->getBeginLoc(); |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 157 | if (L.isMacroID()) { |
Alex Lorenz | 6615f2b | 2017-04-05 14:07:21 +0000 | [diff] [blame] | 158 | SourceManager &SM = PP.getSourceManager(); |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 159 | if (IgnoreYES_NO) { |
| 160 | // The Objective-C constant 'YES' and 'NO' |
| 161 | // are defined as macros. Do not treat them |
| 162 | // as configuration values. |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 163 | SourceLocation TopL = getTopMostMacro(L, SM); |
| 164 | StringRef MacroName = PP.getImmediateMacroName(TopL); |
| 165 | if (MacroName == "YES" || MacroName == "NO") |
| 166 | return false; |
Alex Lorenz | 6615f2b | 2017-04-05 14:07:21 +0000 | [diff] [blame] | 167 | } else if (!PP.getLangOpts().CPlusPlus) { |
| 168 | // Do not treat C 'false' and 'true' macros as configuration values. |
| 169 | SourceLocation TopL = getTopMostMacro(L, SM); |
| 170 | StringRef MacroName = PP.getImmediateMacroName(TopL); |
| 171 | if (MacroName == "false" || MacroName == "true") |
| 172 | return false; |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 173 | } |
| 174 | return true; |
| 175 | } |
| 176 | return false; |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Ted Kremenek | f5ae0bc | 2014-03-20 06:44:35 +0000 | [diff] [blame] | 179 | static bool isConfigurationValue(const ValueDecl *D, Preprocessor &PP); |
| 180 | |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 181 | /// Returns true if the statement represents a configuration value. |
| 182 | /// |
| 183 | /// A configuration value is something usually determined at compile-time |
| 184 | /// to conditionally always execute some branch. Such guards are for |
| 185 | /// "sometimes unreachable" code. Such code is usually not interesting |
| 186 | /// to report as unreachable, and may mask truly unreachable code within |
| 187 | /// those blocks. |
Ted Kremenek | c980afc | 2014-03-08 23:20:11 +0000 | [diff] [blame] | 188 | static bool isConfigurationValue(const Stmt *S, |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 189 | Preprocessor &PP, |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 190 | SourceRange *SilenceableCondVal = nullptr, |
| 191 | bool IncludeIntegers = true, |
| 192 | bool WrappedInParens = false) { |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 193 | if (!S) |
| 194 | return false; |
| 195 | |
Tim Shen | 43ee05e8 | 2016-11-01 00:19:04 +0000 | [diff] [blame] | 196 | S = S->IgnoreImplicit(); |
| 197 | |
Ted Kremenek | 6f375e5 | 2014-04-16 07:26:09 +0000 | [diff] [blame] | 198 | if (const Expr *Ex = dyn_cast<Expr>(S)) |
| 199 | S = Ex->IgnoreCasts(); |
| 200 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 201 | // Special case looking for the sigil '()' around an integer literal. |
| 202 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(S)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 203 | if (!PE->getBeginLoc().isMacroID()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 204 | return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal, |
Ted Kremenek | ab57a15 | 2014-03-29 04:49:20 +0000 | [diff] [blame] | 205 | IncludeIntegers, true); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 207 | if (const Expr *Ex = dyn_cast<Expr>(S)) |
Ted Kremenek | 6f375e5 | 2014-04-16 07:26:09 +0000 | [diff] [blame] | 208 | S = Ex->IgnoreCasts(); |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | ab57a15 | 2014-03-29 04:49:20 +0000 | [diff] [blame] | 210 | bool IgnoreYES_NO = false; |
| 211 | |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 212 | switch (S->getStmtClass()) { |
Ted Kremenek | 2766ad2 | 2014-03-20 06:07:35 +0000 | [diff] [blame] | 213 | case Stmt::CallExprClass: { |
| 214 | const FunctionDecl *Callee = |
| 215 | dyn_cast_or_null<FunctionDecl>(cast<CallExpr>(S)->getCalleeDecl()); |
| 216 | return Callee ? Callee->isConstexpr() : false; |
| 217 | } |
Ted Kremenek | f5ae0bc | 2014-03-20 06:44:35 +0000 | [diff] [blame] | 218 | case Stmt::DeclRefExprClass: |
| 219 | return isConfigurationValue(cast<DeclRefExpr>(S)->getDecl(), PP); |
Ted Kremenek | ab57a15 | 2014-03-29 04:49:20 +0000 | [diff] [blame] | 220 | case Stmt::ObjCBoolLiteralExprClass: |
| 221 | IgnoreYES_NO = true; |
Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 222 | LLVM_FALLTHROUGH; |
Ted Kremenek | ab57a15 | 2014-03-29 04:49:20 +0000 | [diff] [blame] | 223 | case Stmt::CXXBoolLiteralExprClass: |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 224 | case Stmt::IntegerLiteralClass: { |
Ted Kremenek | ab57a15 | 2014-03-29 04:49:20 +0000 | [diff] [blame] | 225 | const Expr *E = cast<Expr>(S); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 226 | if (IncludeIntegers) { |
| 227 | if (SilenceableCondVal && !SilenceableCondVal->getBegin().isValid()) |
| 228 | *SilenceableCondVal = E->getSourceRange(); |
Ted Kremenek | ab57a15 | 2014-03-29 04:49:20 +0000 | [diff] [blame] | 229 | return WrappedInParens || isExpandedFromConfigurationMacro(E, PP, IgnoreYES_NO); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 230 | } |
| 231 | return false; |
| 232 | } |
Ted Kremenek | f5ae0bc | 2014-03-20 06:44:35 +0000 | [diff] [blame] | 233 | case Stmt::MemberExprClass: |
| 234 | return isConfigurationValue(cast<MemberExpr>(S)->getMemberDecl(), PP); |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 235 | case Stmt::UnaryExprOrTypeTraitExprClass: |
| 236 | return true; |
| 237 | case Stmt::BinaryOperatorClass: { |
| 238 | const BinaryOperator *B = cast<BinaryOperator>(S); |
Ted Kremenek | c980afc | 2014-03-08 23:20:11 +0000 | [diff] [blame] | 239 | // Only include raw integers (not enums) as configuration |
| 240 | // values if they are used in a logical or comparison operator |
| 241 | // (not arithmetic). |
| 242 | IncludeIntegers &= (B->isLogicalOp() || B->isComparisonOp()); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 243 | return isConfigurationValue(B->getLHS(), PP, SilenceableCondVal, |
| 244 | IncludeIntegers) || |
| 245 | isConfigurationValue(B->getRHS(), PP, SilenceableCondVal, |
| 246 | IncludeIntegers); |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 247 | } |
| 248 | case Stmt::UnaryOperatorClass: { |
| 249 | const UnaryOperator *UO = cast<UnaryOperator>(S); |
Alex Lorenz | 569ad73 | 2017-01-12 10:48:03 +0000 | [diff] [blame] | 250 | if (UO->getOpcode() != UO_LNot) |
| 251 | return false; |
| 252 | bool SilenceableCondValNotSet = |
| 253 | SilenceableCondVal && SilenceableCondVal->getBegin().isInvalid(); |
| 254 | bool IsSubExprConfigValue = |
| 255 | isConfigurationValue(UO->getSubExpr(), PP, SilenceableCondVal, |
| 256 | IncludeIntegers, WrappedInParens); |
| 257 | // Update the silenceable condition value source range only if the range |
| 258 | // was set directly by the child expression. |
| 259 | if (SilenceableCondValNotSet && |
| 260 | SilenceableCondVal->getBegin().isValid() && |
| 261 | *SilenceableCondVal == |
| 262 | UO->getSubExpr()->IgnoreCasts()->getSourceRange()) |
| 263 | *SilenceableCondVal = UO->getSourceRange(); |
| 264 | return IsSubExprConfigValue; |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 265 | } |
| 266 | default: |
| 267 | return false; |
| 268 | } |
| 269 | } |
| 270 | |
Ted Kremenek | f5ae0bc | 2014-03-20 06:44:35 +0000 | [diff] [blame] | 271 | static bool isConfigurationValue(const ValueDecl *D, Preprocessor &PP) { |
| 272 | if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) |
| 273 | return isConfigurationValue(ED->getInitExpr(), PP); |
| 274 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 275 | // As a heuristic, treat globals as configuration values. Note |
| 276 | // that we only will get here if Sema evaluated this |
| 277 | // condition to a constant expression, which means the global |
| 278 | // had to be declared in a way to be a truly constant value. |
| 279 | // We could generalize this to local variables, but it isn't |
| 280 | // clear if those truly represent configuration values that |
| 281 | // gate unreachable code. |
| 282 | if (!VD->hasLocalStorage()) |
| 283 | return true; |
| 284 | |
| 285 | // As a heuristic, locals that have been marked 'const' explicitly |
| 286 | // can be treated as configuration values as well. |
| 287 | return VD->getType().isLocalConstQualified(); |
| 288 | } |
| 289 | return false; |
| 290 | } |
| 291 | |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 292 | /// Returns true if we should always explore all successors of a block. |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 293 | static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B, |
| 294 | Preprocessor &PP) { |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 295 | if (const Stmt *Term = B->getTerminator()) { |
Ted Kremenek | 6999d02 | 2014-03-06 08:09:00 +0000 | [diff] [blame] | 296 | if (isa<SwitchStmt>(Term)) |
| 297 | return true; |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 298 | // Specially handle '||' and '&&'. |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 299 | if (isa<BinaryOperator>(Term)) { |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 300 | return isConfigurationValue(Term, PP); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 301 | } |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 302 | } |
Ted Kremenek | 6999d02 | 2014-03-06 08:09:00 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 304 | const Stmt *Cond = B->getTerminatorCondition(/* stripParens */ false); |
| 305 | return isConfigurationValue(Cond, PP); |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 308 | static unsigned scanFromBlock(const CFGBlock *Start, |
| 309 | llvm::BitVector &Reachable, |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 310 | Preprocessor *PP, |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 311 | bool IncludeSometimesUnreachableEdges) { |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 312 | unsigned count = 0; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 313 | |
Nico Weber | cc2b871 | 2011-04-02 19:45:15 +0000 | [diff] [blame] | 314 | // Prep work queue |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 315 | SmallVector<const CFGBlock*, 32> WL; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 316 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 317 | // The entry block may have already been marked reachable |
| 318 | // by the caller. |
| 319 | if (!Reachable[Start->getBlockID()]) { |
| 320 | ++count; |
| 321 | Reachable[Start->getBlockID()] = true; |
| 322 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 324 | WL.push_back(Start); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | f2b0a1b | 2010-09-09 00:06:10 +0000 | [diff] [blame] | 326 | // Find the reachable blocks from 'Start'. |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 327 | while (!WL.empty()) { |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 328 | const CFGBlock *item = WL.pop_back_val(); |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 329 | |
| 330 | // There are cases where we want to treat all successors as reachable. |
| 331 | // The idea is that some "sometimes unreachable" code is not interesting, |
| 332 | // and that we should forge ahead and explore those branches anyway. |
| 333 | // This allows us to potentially uncover some "always unreachable" code |
| 334 | // within the "sometimes unreachable" code. |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 335 | // Look at the successors and mark then reachable. |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 336 | Optional<bool> TreatAllSuccessorsAsReachable; |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 337 | if (!IncludeSometimesUnreachableEdges) |
| 338 | TreatAllSuccessorsAsReachable = false; |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 339 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 340 | for (CFGBlock::const_succ_iterator I = item->succ_begin(), |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 341 | E = item->succ_end(); I != E; ++I) { |
| 342 | const CFGBlock *B = *I; |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 343 | if (!B) do { |
| 344 | const CFGBlock *UB = I->getPossiblyUnreachableBlock(); |
| 345 | if (!UB) |
| 346 | break; |
| 347 | |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 348 | if (!TreatAllSuccessorsAsReachable.hasValue()) { |
| 349 | assert(PP); |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 350 | TreatAllSuccessorsAsReachable = |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 351 | shouldTreatSuccessorsAsReachable(item, *PP); |
| 352 | } |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 353 | |
| 354 | if (TreatAllSuccessorsAsReachable.getValue()) { |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 355 | B = UB; |
| 356 | break; |
| 357 | } |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 358 | } |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 359 | while (false); |
| 360 | |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 361 | if (B) { |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 362 | unsigned blockID = B->getBlockID(); |
| 363 | if (!Reachable[blockID]) { |
| 364 | Reachable.set(blockID); |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 365 | WL.push_back(B); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 366 | ++count; |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 369 | } |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 370 | } |
| 371 | return count; |
| 372 | } |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 373 | |
| 374 | static unsigned scanMaybeReachableFromBlock(const CFGBlock *Start, |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 375 | Preprocessor &PP, |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 376 | llvm::BitVector &Reachable) { |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 377 | return scanFromBlock(Start, Reachable, &PP, true); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | //===----------------------------------------------------------------------===// |
| 381 | // Dead Code Scanner. |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | namespace { |
| 385 | class DeadCodeScan { |
| 386 | llvm::BitVector Visited; |
| 387 | llvm::BitVector &Reachable; |
| 388 | SmallVector<const CFGBlock *, 10> WorkList; |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 389 | Preprocessor &PP; |
Nico Weber | 758fbac | 2018-02-13 21:31:47 +0000 | [diff] [blame] | 390 | ASTContext &C; |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 391 | |
| 392 | typedef SmallVector<std::pair<const CFGBlock *, const Stmt *>, 12> |
| 393 | DeferredLocsTy; |
| 394 | |
| 395 | DeferredLocsTy DeferredLocs; |
| 396 | |
| 397 | public: |
Nico Weber | 758fbac | 2018-02-13 21:31:47 +0000 | [diff] [blame] | 398 | DeadCodeScan(llvm::BitVector &reachable, Preprocessor &PP, ASTContext &C) |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 399 | : Visited(reachable.size()), |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 400 | Reachable(reachable), |
Nico Weber | 758fbac | 2018-02-13 21:31:47 +0000 | [diff] [blame] | 401 | PP(PP), C(C) {} |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 402 | |
| 403 | void enqueue(const CFGBlock *block); |
| 404 | unsigned scanBackwards(const CFGBlock *Start, |
| 405 | clang::reachable_code::Callback &CB); |
| 406 | |
| 407 | bool isDeadCodeRoot(const CFGBlock *Block); |
| 408 | |
| 409 | const Stmt *findDeadCode(const CFGBlock *Block); |
| 410 | |
| 411 | void reportDeadCode(const CFGBlock *B, |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 412 | const Stmt *S, |
| 413 | clang::reachable_code::Callback &CB); |
| 414 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 415 | } |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 416 | |
| 417 | void DeadCodeScan::enqueue(const CFGBlock *block) { |
| 418 | unsigned blockID = block->getBlockID(); |
| 419 | if (Reachable[blockID] || Visited[blockID]) |
| 420 | return; |
| 421 | Visited[blockID] = true; |
| 422 | WorkList.push_back(block); |
| 423 | } |
| 424 | |
| 425 | bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) { |
| 426 | bool isDeadRoot = true; |
| 427 | |
| 428 | for (CFGBlock::const_pred_iterator I = Block->pred_begin(), |
| 429 | E = Block->pred_end(); I != E; ++I) { |
| 430 | if (const CFGBlock *PredBlock = *I) { |
| 431 | unsigned blockID = PredBlock->getBlockID(); |
| 432 | if (Visited[blockID]) { |
| 433 | isDeadRoot = false; |
| 434 | continue; |
| 435 | } |
| 436 | if (!Reachable[blockID]) { |
| 437 | isDeadRoot = false; |
| 438 | Visited[blockID] = true; |
| 439 | WorkList.push_back(PredBlock); |
| 440 | continue; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return isDeadRoot; |
| 446 | } |
| 447 | |
| 448 | static bool isValidDeadStmt(const Stmt *S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 449 | if (S->getBeginLoc().isInvalid()) |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 450 | return false; |
| 451 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) |
| 452 | return BO->getOpcode() != BO_Comma; |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) { |
| 457 | for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I!=E; ++I) |
| 458 | if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) { |
| 459 | const Stmt *S = CS->getStmt(); |
| 460 | if (isValidDeadStmt(S)) |
| 461 | return S; |
| 462 | } |
| 463 | |
| 464 | if (CFGTerminator T = Block->getTerminator()) { |
Ted Kremenek | efea634 | 2014-03-08 02:22:32 +0000 | [diff] [blame] | 465 | if (!T.isTemporaryDtorsBranch()) { |
| 466 | const Stmt *S = T.getStmt(); |
| 467 | if (isValidDeadStmt(S)) |
| 468 | return S; |
| 469 | } |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 472 | return nullptr; |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 475 | static int SrcCmp(const std::pair<const CFGBlock *, const Stmt *> *p1, |
| 476 | const std::pair<const CFGBlock *, const Stmt *> *p2) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 477 | if (p1->second->getBeginLoc() < p2->second->getBeginLoc()) |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 478 | return -1; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 479 | if (p2->second->getBeginLoc() < p1->second->getBeginLoc()) |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 480 | return 1; |
| 481 | return 0; |
| 482 | } |
| 483 | |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 484 | unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start, |
| 485 | clang::reachable_code::Callback &CB) { |
| 486 | |
| 487 | unsigned count = 0; |
| 488 | enqueue(Start); |
| 489 | |
| 490 | while (!WorkList.empty()) { |
| 491 | const CFGBlock *Block = WorkList.pop_back_val(); |
| 492 | |
| 493 | // It is possible that this block has been marked reachable after |
| 494 | // it was enqueued. |
| 495 | if (Reachable[Block->getBlockID()]) |
| 496 | continue; |
| 497 | |
| 498 | // Look for any dead code within the block. |
| 499 | const Stmt *S = findDeadCode(Block); |
| 500 | |
| 501 | if (!S) { |
| 502 | // No dead code. Possibly an empty block. Look at dead predecessors. |
| 503 | for (CFGBlock::const_pred_iterator I = Block->pred_begin(), |
| 504 | E = Block->pred_end(); I != E; ++I) { |
| 505 | if (const CFGBlock *predBlock = *I) |
| 506 | enqueue(predBlock); |
| 507 | } |
| 508 | continue; |
| 509 | } |
| 510 | |
| 511 | // Specially handle macro-expanded code. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 512 | if (S->getBeginLoc().isMacroID()) { |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 513 | count += scanMaybeReachableFromBlock(Block, PP, Reachable); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 514 | continue; |
| 515 | } |
| 516 | |
| 517 | if (isDeadCodeRoot(Block)) { |
| 518 | reportDeadCode(Block, S, CB); |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 519 | count += scanMaybeReachableFromBlock(Block, PP, Reachable); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 520 | } |
| 521 | else { |
| 522 | // Record this statement as the possibly best location in a |
| 523 | // strongly-connected component of dead code for emitting a |
| 524 | // warning. |
| 525 | DeferredLocs.push_back(std::make_pair(Block, S)); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // If we didn't find a dead root, then report the dead code with the |
| 530 | // earliest location. |
| 531 | if (!DeferredLocs.empty()) { |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 532 | llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 533 | for (DeferredLocsTy::iterator I = DeferredLocs.begin(), |
| 534 | E = DeferredLocs.end(); I != E; ++I) { |
| 535 | const CFGBlock *Block = I->first; |
| 536 | if (Reachable[Block->getBlockID()]) |
| 537 | continue; |
| 538 | reportDeadCode(Block, I->second, CB); |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 539 | count += scanMaybeReachableFromBlock(Block, PP, Reachable); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | return count; |
| 544 | } |
| 545 | |
| 546 | static SourceLocation GetUnreachableLoc(const Stmt *S, |
| 547 | SourceRange &R1, |
| 548 | SourceRange &R2) { |
| 549 | R1 = R2 = SourceRange(); |
| 550 | |
| 551 | if (const Expr *Ex = dyn_cast<Expr>(S)) |
| 552 | S = Ex->IgnoreParenImpCasts(); |
| 553 | |
| 554 | switch (S->getStmtClass()) { |
| 555 | case Expr::BinaryOperatorClass: { |
| 556 | const BinaryOperator *BO = cast<BinaryOperator>(S); |
| 557 | return BO->getOperatorLoc(); |
| 558 | } |
| 559 | case Expr::UnaryOperatorClass: { |
| 560 | const UnaryOperator *UO = cast<UnaryOperator>(S); |
| 561 | R1 = UO->getSubExpr()->getSourceRange(); |
| 562 | return UO->getOperatorLoc(); |
| 563 | } |
| 564 | case Expr::CompoundAssignOperatorClass: { |
| 565 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(S); |
| 566 | R1 = CAO->getLHS()->getSourceRange(); |
| 567 | R2 = CAO->getRHS()->getSourceRange(); |
| 568 | return CAO->getOperatorLoc(); |
| 569 | } |
| 570 | case Expr::BinaryConditionalOperatorClass: |
| 571 | case Expr::ConditionalOperatorClass: { |
| 572 | const AbstractConditionalOperator *CO = |
| 573 | cast<AbstractConditionalOperator>(S); |
| 574 | return CO->getQuestionLoc(); |
| 575 | } |
| 576 | case Expr::MemberExprClass: { |
| 577 | const MemberExpr *ME = cast<MemberExpr>(S); |
| 578 | R1 = ME->getSourceRange(); |
| 579 | return ME->getMemberLoc(); |
| 580 | } |
| 581 | case Expr::ArraySubscriptExprClass: { |
| 582 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(S); |
| 583 | R1 = ASE->getLHS()->getSourceRange(); |
| 584 | R2 = ASE->getRHS()->getSourceRange(); |
| 585 | return ASE->getRBracketLoc(); |
| 586 | } |
| 587 | case Expr::CStyleCastExprClass: { |
| 588 | const CStyleCastExpr *CSC = cast<CStyleCastExpr>(S); |
| 589 | R1 = CSC->getSubExpr()->getSourceRange(); |
| 590 | return CSC->getLParenLoc(); |
| 591 | } |
| 592 | case Expr::CXXFunctionalCastExprClass: { |
| 593 | const CXXFunctionalCastExpr *CE = cast <CXXFunctionalCastExpr>(S); |
| 594 | R1 = CE->getSubExpr()->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 595 | return CE->getBeginLoc(); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 596 | } |
| 597 | case Stmt::CXXTryStmtClass: { |
| 598 | return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc(); |
| 599 | } |
| 600 | case Expr::ObjCBridgedCastExprClass: { |
| 601 | const ObjCBridgedCastExpr *CSC = cast<ObjCBridgedCastExpr>(S); |
| 602 | R1 = CSC->getSubExpr()->getSourceRange(); |
| 603 | return CSC->getLParenLoc(); |
| 604 | } |
| 605 | default: ; |
| 606 | } |
| 607 | R1 = S->getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 608 | return S->getBeginLoc(); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | void DeadCodeScan::reportDeadCode(const CFGBlock *B, |
| 612 | const Stmt *S, |
| 613 | clang::reachable_code::Callback &CB) { |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 614 | // Classify the unreachable code found, or suppress it in some cases. |
Ted Kremenek | 1a8641c | 2014-03-15 01:26:32 +0000 | [diff] [blame] | 615 | reachable_code::UnreachableKind UK = reachable_code::UK_Other; |
| 616 | |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 617 | if (isa<BreakStmt>(S)) { |
| 618 | UK = reachable_code::UK_Break; |
Nico Weber | 758fbac | 2018-02-13 21:31:47 +0000 | [diff] [blame] | 619 | } else if (isTrivialDoWhile(B, S) || isBuiltinUnreachable(S) || |
| 620 | isBuiltinAssumeFalse(B, S, C)) { |
Ted Kremenek | f3c93bb | 2014-03-20 06:07:30 +0000 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | else if (isDeadReturn(B, S)) { |
| 624 | UK = reachable_code::UK_Return; |
| 625 | } |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 626 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 627 | SourceRange SilenceableCondVal; |
| 628 | |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 629 | if (UK == reachable_code::UK_Other) { |
| 630 | // Check if the dead code is part of the "loop target" of |
| 631 | // a for/for-range loop. This is the block that contains |
| 632 | // the increment code. |
| 633 | if (const Stmt *LoopTarget = B->getLoopTarget()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 634 | SourceLocation Loc = LoopTarget->getBeginLoc(); |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 635 | SourceRange R1(Loc, Loc), R2; |
| 636 | |
| 637 | if (const ForStmt *FS = dyn_cast<ForStmt>(LoopTarget)) { |
| 638 | const Expr *Inc = FS->getInc(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 639 | Loc = Inc->getBeginLoc(); |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 640 | R2 = Inc->getSourceRange(); |
| 641 | } |
| 642 | |
| 643 | CB.HandleUnreachable(reachable_code::UK_Loop_Increment, |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 644 | Loc, SourceRange(), SourceRange(Loc, Loc), R2); |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 645 | return; |
| 646 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 647 | |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 648 | // Check if the dead block has a predecessor whose branch has |
| 649 | // a configuration value that *could* be modified to |
| 650 | // silence the warning. |
| 651 | CFGBlock::const_pred_iterator PI = B->pred_begin(); |
| 652 | if (PI != B->pred_end()) { |
| 653 | if (const CFGBlock *PredBlock = PI->getPossiblyUnreachableBlock()) { |
| 654 | const Stmt *TermCond = |
| 655 | PredBlock->getTerminatorCondition(/* strip parens */ false); |
| 656 | isConfigurationValue(TermCond, PP, &SilenceableCondVal); |
| 657 | } |
| 658 | } |
Ted Kremenek | 1421037 | 2014-03-21 06:02:36 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 661 | SourceRange R1, R2; |
| 662 | SourceLocation Loc = GetUnreachableLoc(S, R1, R2); |
Ted Kremenek | ec3bbf4 | 2014-03-29 00:35:20 +0000 | [diff] [blame] | 663 | CB.HandleUnreachable(UK, Loc, SilenceableCondVal, R1, R2); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | // Reachability APIs. |
| 668 | //===----------------------------------------------------------------------===// |
| 669 | |
| 670 | namespace clang { namespace reachable_code { |
| 671 | |
| 672 | void Callback::anchor() { } |
| 673 | |
| 674 | unsigned ScanReachableFromBlock(const CFGBlock *Start, |
| 675 | llvm::BitVector &Reachable) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 676 | return scanFromBlock(Start, Reachable, /* SourceManager* */ nullptr, false); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 679 | void FindUnreachableCode(AnalysisDeclContext &AC, Preprocessor &PP, |
| 680 | Callback &CB) { |
| 681 | |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 682 | CFG *cfg = AC.getCFG(); |
| 683 | if (!cfg) |
| 684 | return; |
| 685 | |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 686 | // Scan for reachable blocks from the entrance of the CFG. |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 687 | // If there are no unreachable blocks, we're done. |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 688 | llvm::BitVector reachable(cfg->getNumBlockIDs()); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 689 | unsigned numReachable = |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 690 | scanMaybeReachableFromBlock(&cfg->getEntry(), PP, reachable); |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 691 | if (numReachable == cfg->getNumBlockIDs()) |
| 692 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 693 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 694 | // If there aren't explicit EH edges, we should include the 'try' dispatch |
| 695 | // blocks as roots. |
| 696 | if (!AC.getCFGBuildOptions().AddEHEdges) { |
| 697 | for (CFG::try_block_iterator I = cfg->try_blocks_begin(), |
| 698 | E = cfg->try_blocks_end() ; I != E; ++I) { |
Ted Kremenek | 2dd810a | 2014-03-09 08:13:49 +0000 | [diff] [blame] | 699 | numReachable += scanMaybeReachableFromBlock(*I, PP, reachable); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 700 | } |
| 701 | if (numReachable == cfg->getNumBlockIDs()) |
| 702 | return; |
| 703 | } |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 704 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 705 | // There are some unreachable blocks. We need to find the root blocks that |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 706 | // contain code that should be considered unreachable. |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 707 | for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) { |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 708 | const CFGBlock *block = *I; |
| 709 | // A block may have been marked reachable during this loop. |
| 710 | if (reachable[block->getBlockID()]) |
| 711 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 712 | |
Nico Weber | 758fbac | 2018-02-13 21:31:47 +0000 | [diff] [blame] | 713 | DeadCodeScan DS(reachable, PP, AC.getASTContext()); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 714 | numReachable += DS.scanBackwards(block, CB); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 715 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 716 | if (numReachable == cfg->getNumBlockIDs()) |
| 717 | return; |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 718 | } |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 721 | }} // end namespace clang::reachable_code |