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 | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtCXX.h" |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/AnalysisContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/BitVector.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | // Core Reachability Analysis routines. |
| 30 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | cc89338 | 2014-02-27 00:24:08 +0000 | [diff] [blame] | 32 | static 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 Kremenek | ec2dc73 | 2014-03-06 06:50:46 +0000 | [diff] [blame] | 36 | 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 Kremenek | cc89338 | 2014-02-27 00:24:08 +0000 | [diff] [blame] | 40 | QualType CalleeType = CE->getCallee()->getType(); |
| 41 | if (getFunctionExtInfo(*CalleeType).getNoReturn()) |
| 42 | return true; |
| 43 | } |
| 44 | break; |
| 45 | } |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 50 | static bool bodyEndsWithNoReturn(const CFGBlock::AdjacentBlock &AB) { |
Ted Kremenek | eb86284 | 2014-03-04 21:41:38 +0000 | [diff] [blame] | 51 | // 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 Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 56 | const CFGBlock *Pred = AB.getPossiblyUnreachableBlock(); |
| 57 | assert(!AB.isReachable() && Pred); |
| 58 | return bodyEndsWithNoReturn(Pred); |
| 59 | } |
| 60 | |
Ted Kremenek | cc89338 | 2014-02-27 00:24:08 +0000 | [diff] [blame] | 61 | static 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 Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 68 | return bodyEndsWithNoReturn(*B->pred_begin()); |
| 69 | } |
| 70 | |
| 71 | static 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 Kremenek | ec2dc73 | 2014-03-06 06:50:46 +0000 | [diff] [blame] | 78 | static 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. |
| 114 | static 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 Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 125 | static bool isTrivialExpression(const Expr *Ex) { |
Ted Kremenek | 0a69cab | 2014-03-05 23:46:07 +0000 | [diff] [blame] | 126 | Ex = Ex->IgnoreParenCasts(); |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 127 | return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) || |
Ted Kremenek | c10830b | 2014-03-07 02:25:50 +0000 | [diff] [blame] | 128 | isa<CXXBoolLiteralExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) || |
| 129 | isa<CharacterLiteral>(Ex) || |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 130 | isEnumConstant(Ex); |
| 131 | } |
| 132 | |
Ted Kremenek | 1de2e14 | 2014-03-06 00:17:44 +0000 | [diff] [blame] | 133 | static bool isTrivialReturnOrDoWhile(const CFGBlock *B, const Stmt *S) { |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 134 | const Expr *Ex = dyn_cast<Expr>(S); |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 04bfbee | 2014-03-08 02:22:23 +0000 | [diff] [blame] | 136 | if (Ex && !isTrivialExpression(Ex)) |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 137 | return false; |
| 138 | |
Ted Kremenek | 1de2e14 | 2014-03-06 00:17:44 +0000 | [diff] [blame] | 139 | // Check if the block ends with a do...while() and see if 'S' is the |
| 140 | // condition. |
| 141 | if (const Stmt *Term = B->getTerminator()) { |
| 142 | if (const DoStmt *DS = dyn_cast<DoStmt>(Term)) |
| 143 | if (DS->getCond() == S) |
| 144 | return true; |
| 145 | } |
| 146 | |
Ted Kremenek | 7549f0f | 2014-03-06 01:09:45 +0000 | [diff] [blame] | 147 | if (B->pred_size() != 1) |
| 148 | return false; |
Ted Kremenek | 1de2e14 | 2014-03-06 00:17:44 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 150 | // Look to see if the block ends with a 'return', and see if 'S' |
| 151 | // is a substatement. The 'return' may not be the last element in |
| 152 | // the block because of destructors. |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 153 | for (CFGBlock::const_reverse_iterator I = B->rbegin(), E = B->rend(); |
| 154 | I != E; ++I) { |
| 155 | if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) { |
| 156 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) { |
Ted Kremenek | 04bfbee | 2014-03-08 02:22:23 +0000 | [diff] [blame] | 157 | bool LookAtBody = false; |
| 158 | if (RS == S) |
| 159 | LookAtBody = true; |
| 160 | else { |
| 161 | const Expr *RE = RS->getRetValue(); |
| 162 | if (RE && stripExprSugar(RE->IgnoreParenCasts()) == Ex) |
| 163 | LookAtBody = true; |
| 164 | } |
| 165 | |
| 166 | if (LookAtBody) |
Ted Kremenek | 7549f0f | 2014-03-06 01:09:45 +0000 | [diff] [blame] | 167 | return bodyEndsWithNoReturn(*B->pred_begin()); |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 168 | } |
Ted Kremenek | 7549f0f | 2014-03-06 01:09:45 +0000 | [diff] [blame] | 169 | break; |
Ted Kremenek | 5441c18 | 2014-02-27 06:32:32 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
Ted Kremenek | 0a69cab | 2014-03-05 23:46:07 +0000 | [diff] [blame] | 172 | return false; |
Ted Kremenek | cc89338 | 2014-02-27 00:24:08 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 175 | /// Returns true if the statement is expanded from a configuration macro. |
| 176 | static bool isExpandedFromConfigurationMacro(const Stmt *S) { |
| 177 | // FIXME: This is not very precise. Here we just check to see if the |
| 178 | // value comes from a macro, but we can do much better. This is likely |
| 179 | // to be over conservative. This logic is factored into a separate function |
| 180 | // so that we can refine it later. |
| 181 | SourceLocation L = S->getLocStart(); |
| 182 | return L.isMacroID(); |
| 183 | } |
| 184 | |
| 185 | /// Returns true if the statement represents a configuration value. |
| 186 | /// |
| 187 | /// A configuration value is something usually determined at compile-time |
| 188 | /// to conditionally always execute some branch. Such guards are for |
| 189 | /// "sometimes unreachable" code. Such code is usually not interesting |
| 190 | /// to report as unreachable, and may mask truly unreachable code within |
| 191 | /// those blocks. |
| 192 | static bool isConfigurationValue(const Stmt *S) { |
| 193 | if (!S) |
| 194 | return false; |
| 195 | |
| 196 | if (const Expr *Ex = dyn_cast<Expr>(S)) |
| 197 | S = Ex->IgnoreParenCasts(); |
| 198 | |
| 199 | switch (S->getStmtClass()) { |
Ted Kremenek | 01a39b6 | 2014-03-05 23:38:41 +0000 | [diff] [blame] | 200 | case Stmt::DeclRefExprClass: { |
| 201 | const DeclRefExpr *DR = cast<DeclRefExpr>(S); |
Ted Kremenek | 94d1617 | 2014-03-07 20:51:13 +0000 | [diff] [blame] | 202 | const ValueDecl *D = DR->getDecl(); |
| 203 | if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) |
| 204 | return ED ? isConfigurationValue(ED->getInitExpr()) : false; |
| 205 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 206 | // As a heuristic, treat globals as configuration values. Note |
| 207 | // that we only will get here if Sema evaluated this |
| 208 | // condition to a constant expression, which means the global |
| 209 | // had to be declared in a way to be a truly constant value. |
| 210 | // We could generalize this to local variables, but it isn't |
| 211 | // clear if those truly represent configuration values that |
| 212 | // gate unreachable code. |
| 213 | return !VD->hasLocalStorage(); |
| 214 | } |
| 215 | return false; |
Ted Kremenek | 01a39b6 | 2014-03-05 23:38:41 +0000 | [diff] [blame] | 216 | } |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 217 | case Stmt::IntegerLiteralClass: |
| 218 | return isExpandedFromConfigurationMacro(S); |
| 219 | case Stmt::UnaryExprOrTypeTraitExprClass: |
| 220 | return true; |
| 221 | case Stmt::BinaryOperatorClass: { |
| 222 | const BinaryOperator *B = cast<BinaryOperator>(S); |
Ted Kremenek | 3cdbc39 | 2014-03-05 22:32:39 +0000 | [diff] [blame] | 223 | return (B->isLogicalOp() || B->isComparisonOp()) && |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 224 | (isConfigurationValue(B->getLHS()) || |
| 225 | isConfigurationValue(B->getRHS())); |
| 226 | } |
| 227 | case Stmt::UnaryOperatorClass: { |
| 228 | const UnaryOperator *UO = cast<UnaryOperator>(S); |
| 229 | return UO->getOpcode() == UO_LNot && |
| 230 | isConfigurationValue(UO->getSubExpr()); |
| 231 | } |
| 232 | default: |
| 233 | return false; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /// Returns true if we should always explore all successors of a block. |
| 238 | static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B) { |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 239 | if (const Stmt *Term = B->getTerminator()) { |
Ted Kremenek | 6999d02 | 2014-03-06 08:09:00 +0000 | [diff] [blame] | 240 | if (isa<SwitchStmt>(Term)) |
| 241 | return true; |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 242 | // Specially handle '||' and '&&'. |
| 243 | if (isa<BinaryOperator>(Term)) |
| 244 | return isConfigurationValue(Term); |
| 245 | } |
Ted Kremenek | 6999d02 | 2014-03-06 08:09:00 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 247 | return isConfigurationValue(B->getTerminatorCondition()); |
| 248 | } |
| 249 | |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 250 | static unsigned scanFromBlock(const CFGBlock *Start, |
| 251 | llvm::BitVector &Reachable, |
| 252 | bool IncludeSometimesUnreachableEdges) { |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 253 | unsigned count = 0; |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 254 | |
Nico Weber | cc2b871 | 2011-04-02 19:45:15 +0000 | [diff] [blame] | 255 | // Prep work queue |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 256 | SmallVector<const CFGBlock*, 32> WL; |
| 257 | |
| 258 | // The entry block may have already been marked reachable |
| 259 | // by the caller. |
| 260 | if (!Reachable[Start->getBlockID()]) { |
| 261 | ++count; |
| 262 | Reachable[Start->getBlockID()] = true; |
| 263 | } |
| 264 | |
| 265 | WL.push_back(Start); |
| 266 | |
Ted Kremenek | f2b0a1b | 2010-09-09 00:06:10 +0000 | [diff] [blame] | 267 | // Find the reachable blocks from 'Start'. |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 268 | while (!WL.empty()) { |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 269 | const CFGBlock *item = WL.pop_back_val(); |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 270 | |
| 271 | // There are cases where we want to treat all successors as reachable. |
| 272 | // The idea is that some "sometimes unreachable" code is not interesting, |
| 273 | // and that we should forge ahead and explore those branches anyway. |
| 274 | // This allows us to potentially uncover some "always unreachable" code |
| 275 | // within the "sometimes unreachable" code. |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 276 | // Look at the successors and mark then reachable. |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 277 | Optional<bool> TreatAllSuccessorsAsReachable; |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 278 | if (!IncludeSometimesUnreachableEdges) |
| 279 | TreatAllSuccessorsAsReachable = false; |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 281 | for (CFGBlock::const_succ_iterator I = item->succ_begin(), |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 282 | E = item->succ_end(); I != E; ++I) { |
| 283 | const CFGBlock *B = *I; |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 284 | if (!B) do { |
| 285 | const CFGBlock *UB = I->getPossiblyUnreachableBlock(); |
| 286 | if (!UB) |
| 287 | break; |
| 288 | |
Ted Kremenek | 782f003 | 2014-03-07 02:25:53 +0000 | [diff] [blame] | 289 | if (!TreatAllSuccessorsAsReachable.hasValue()) |
| 290 | TreatAllSuccessorsAsReachable = |
| 291 | shouldTreatSuccessorsAsReachable(item); |
| 292 | |
| 293 | if (TreatAllSuccessorsAsReachable.getValue()) { |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 294 | B = UB; |
| 295 | break; |
| 296 | } |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 297 | } |
Ted Kremenek | 6d9bb56 | 2014-03-05 00:01:17 +0000 | [diff] [blame] | 298 | while (false); |
| 299 | |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 300 | if (B) { |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 301 | unsigned blockID = B->getBlockID(); |
| 302 | if (!Reachable[blockID]) { |
| 303 | Reachable.set(blockID); |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 304 | WL.push_back(B); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 305 | ++count; |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
Ted Kremenek | 08da978 | 2014-02-27 21:56:47 +0000 | [diff] [blame] | 308 | } |
Ted Kremenek | 7296de9 | 2010-02-23 02:39:16 +0000 | [diff] [blame] | 309 | } |
| 310 | return count; |
| 311 | } |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 312 | |
| 313 | static unsigned scanMaybeReachableFromBlock(const CFGBlock *Start, |
| 314 | llvm::BitVector &Reachable) { |
| 315 | return scanFromBlock(Start, Reachable, true); |
| 316 | } |
| 317 | |
| 318 | //===----------------------------------------------------------------------===// |
| 319 | // Dead Code Scanner. |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
| 322 | namespace { |
| 323 | class DeadCodeScan { |
| 324 | llvm::BitVector Visited; |
| 325 | llvm::BitVector &Reachable; |
| 326 | SmallVector<const CFGBlock *, 10> WorkList; |
| 327 | |
| 328 | typedef SmallVector<std::pair<const CFGBlock *, const Stmt *>, 12> |
| 329 | DeferredLocsTy; |
| 330 | |
| 331 | DeferredLocsTy DeferredLocs; |
| 332 | |
| 333 | public: |
| 334 | DeadCodeScan(llvm::BitVector &reachable) |
| 335 | : Visited(reachable.size()), |
| 336 | Reachable(reachable) {} |
| 337 | |
| 338 | void enqueue(const CFGBlock *block); |
| 339 | unsigned scanBackwards(const CFGBlock *Start, |
| 340 | clang::reachable_code::Callback &CB); |
| 341 | |
| 342 | bool isDeadCodeRoot(const CFGBlock *Block); |
| 343 | |
| 344 | const Stmt *findDeadCode(const CFGBlock *Block); |
| 345 | |
| 346 | void reportDeadCode(const CFGBlock *B, |
| 347 | const Stmt *S, |
| 348 | clang::reachable_code::Callback &CB); |
| 349 | }; |
| 350 | } |
| 351 | |
| 352 | void DeadCodeScan::enqueue(const CFGBlock *block) { |
| 353 | unsigned blockID = block->getBlockID(); |
| 354 | if (Reachable[blockID] || Visited[blockID]) |
| 355 | return; |
| 356 | Visited[blockID] = true; |
| 357 | WorkList.push_back(block); |
| 358 | } |
| 359 | |
| 360 | bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) { |
| 361 | bool isDeadRoot = true; |
| 362 | |
| 363 | for (CFGBlock::const_pred_iterator I = Block->pred_begin(), |
| 364 | E = Block->pred_end(); I != E; ++I) { |
| 365 | if (const CFGBlock *PredBlock = *I) { |
| 366 | unsigned blockID = PredBlock->getBlockID(); |
| 367 | if (Visited[blockID]) { |
| 368 | isDeadRoot = false; |
| 369 | continue; |
| 370 | } |
| 371 | if (!Reachable[blockID]) { |
| 372 | isDeadRoot = false; |
| 373 | Visited[blockID] = true; |
| 374 | WorkList.push_back(PredBlock); |
| 375 | continue; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return isDeadRoot; |
| 381 | } |
| 382 | |
| 383 | static bool isValidDeadStmt(const Stmt *S) { |
| 384 | if (S->getLocStart().isInvalid()) |
| 385 | return false; |
| 386 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) |
| 387 | return BO->getOpcode() != BO_Comma; |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) { |
| 392 | for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I!=E; ++I) |
| 393 | if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) { |
| 394 | const Stmt *S = CS->getStmt(); |
| 395 | if (isValidDeadStmt(S)) |
| 396 | return S; |
| 397 | } |
| 398 | |
| 399 | if (CFGTerminator T = Block->getTerminator()) { |
Ted Kremenek | efea634 | 2014-03-08 02:22:32 +0000 | [diff] [blame^] | 400 | if (!T.isTemporaryDtorsBranch()) { |
| 401 | const Stmt *S = T.getStmt(); |
| 402 | if (isValidDeadStmt(S)) |
| 403 | return S; |
| 404 | } |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 410 | static int SrcCmp(const std::pair<const CFGBlock *, const Stmt *> *p1, |
| 411 | const std::pair<const CFGBlock *, const Stmt *> *p2) { |
| 412 | if (p1->second->getLocStart() < p2->second->getLocStart()) |
| 413 | return -1; |
| 414 | if (p2->second->getLocStart() < p1->second->getLocStart()) |
| 415 | return 1; |
| 416 | return 0; |
| 417 | } |
| 418 | |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 419 | unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start, |
| 420 | clang::reachable_code::Callback &CB) { |
| 421 | |
| 422 | unsigned count = 0; |
| 423 | enqueue(Start); |
| 424 | |
| 425 | while (!WorkList.empty()) { |
| 426 | const CFGBlock *Block = WorkList.pop_back_val(); |
| 427 | |
| 428 | // It is possible that this block has been marked reachable after |
| 429 | // it was enqueued. |
| 430 | if (Reachable[Block->getBlockID()]) |
| 431 | continue; |
| 432 | |
| 433 | // Look for any dead code within the block. |
| 434 | const Stmt *S = findDeadCode(Block); |
| 435 | |
| 436 | if (!S) { |
| 437 | // No dead code. Possibly an empty block. Look at dead predecessors. |
| 438 | for (CFGBlock::const_pred_iterator I = Block->pred_begin(), |
| 439 | E = Block->pred_end(); I != E; ++I) { |
| 440 | if (const CFGBlock *predBlock = *I) |
| 441 | enqueue(predBlock); |
| 442 | } |
| 443 | continue; |
| 444 | } |
| 445 | |
| 446 | // Specially handle macro-expanded code. |
| 447 | if (S->getLocStart().isMacroID()) { |
| 448 | count += scanMaybeReachableFromBlock(Block, Reachable); |
| 449 | continue; |
| 450 | } |
| 451 | |
| 452 | if (isDeadCodeRoot(Block)) { |
| 453 | reportDeadCode(Block, S, CB); |
| 454 | count += scanMaybeReachableFromBlock(Block, Reachable); |
| 455 | } |
| 456 | else { |
| 457 | // Record this statement as the possibly best location in a |
| 458 | // strongly-connected component of dead code for emitting a |
| 459 | // warning. |
| 460 | DeferredLocs.push_back(std::make_pair(Block, S)); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // If we didn't find a dead root, then report the dead code with the |
| 465 | // earliest location. |
| 466 | if (!DeferredLocs.empty()) { |
Benjamin Kramer | 4cadf29 | 2014-03-07 21:51:58 +0000 | [diff] [blame] | 467 | llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 468 | for (DeferredLocsTy::iterator I = DeferredLocs.begin(), |
| 469 | E = DeferredLocs.end(); I != E; ++I) { |
| 470 | const CFGBlock *Block = I->first; |
| 471 | if (Reachable[Block->getBlockID()]) |
| 472 | continue; |
| 473 | reportDeadCode(Block, I->second, CB); |
| 474 | count += scanMaybeReachableFromBlock(Block, Reachable); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return count; |
| 479 | } |
| 480 | |
| 481 | static SourceLocation GetUnreachableLoc(const Stmt *S, |
| 482 | SourceRange &R1, |
| 483 | SourceRange &R2) { |
| 484 | R1 = R2 = SourceRange(); |
| 485 | |
| 486 | if (const Expr *Ex = dyn_cast<Expr>(S)) |
| 487 | S = Ex->IgnoreParenImpCasts(); |
| 488 | |
| 489 | switch (S->getStmtClass()) { |
| 490 | case Expr::BinaryOperatorClass: { |
| 491 | const BinaryOperator *BO = cast<BinaryOperator>(S); |
| 492 | return BO->getOperatorLoc(); |
| 493 | } |
| 494 | case Expr::UnaryOperatorClass: { |
| 495 | const UnaryOperator *UO = cast<UnaryOperator>(S); |
| 496 | R1 = UO->getSubExpr()->getSourceRange(); |
| 497 | return UO->getOperatorLoc(); |
| 498 | } |
| 499 | case Expr::CompoundAssignOperatorClass: { |
| 500 | const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(S); |
| 501 | R1 = CAO->getLHS()->getSourceRange(); |
| 502 | R2 = CAO->getRHS()->getSourceRange(); |
| 503 | return CAO->getOperatorLoc(); |
| 504 | } |
| 505 | case Expr::BinaryConditionalOperatorClass: |
| 506 | case Expr::ConditionalOperatorClass: { |
| 507 | const AbstractConditionalOperator *CO = |
| 508 | cast<AbstractConditionalOperator>(S); |
| 509 | return CO->getQuestionLoc(); |
| 510 | } |
| 511 | case Expr::MemberExprClass: { |
| 512 | const MemberExpr *ME = cast<MemberExpr>(S); |
| 513 | R1 = ME->getSourceRange(); |
| 514 | return ME->getMemberLoc(); |
| 515 | } |
| 516 | case Expr::ArraySubscriptExprClass: { |
| 517 | const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(S); |
| 518 | R1 = ASE->getLHS()->getSourceRange(); |
| 519 | R2 = ASE->getRHS()->getSourceRange(); |
| 520 | return ASE->getRBracketLoc(); |
| 521 | } |
| 522 | case Expr::CStyleCastExprClass: { |
| 523 | const CStyleCastExpr *CSC = cast<CStyleCastExpr>(S); |
| 524 | R1 = CSC->getSubExpr()->getSourceRange(); |
| 525 | return CSC->getLParenLoc(); |
| 526 | } |
| 527 | case Expr::CXXFunctionalCastExprClass: { |
| 528 | const CXXFunctionalCastExpr *CE = cast <CXXFunctionalCastExpr>(S); |
| 529 | R1 = CE->getSubExpr()->getSourceRange(); |
| 530 | return CE->getLocStart(); |
| 531 | } |
| 532 | case Stmt::CXXTryStmtClass: { |
| 533 | return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc(); |
| 534 | } |
| 535 | case Expr::ObjCBridgedCastExprClass: { |
| 536 | const ObjCBridgedCastExpr *CSC = cast<ObjCBridgedCastExpr>(S); |
| 537 | R1 = CSC->getSubExpr()->getSourceRange(); |
| 538 | return CSC->getLParenLoc(); |
| 539 | } |
| 540 | default: ; |
| 541 | } |
| 542 | R1 = S->getSourceRange(); |
| 543 | return S->getLocStart(); |
| 544 | } |
| 545 | |
| 546 | void DeadCodeScan::reportDeadCode(const CFGBlock *B, |
| 547 | const Stmt *S, |
| 548 | clang::reachable_code::Callback &CB) { |
| 549 | // Suppress idiomatic cases of calling a noreturn function just |
| 550 | // before executing a 'break'. If there is other code after the 'break' |
| 551 | // in the block then don't suppress the warning. |
| 552 | if (isBreakPrecededByNoReturn(B, S)) |
| 553 | return; |
| 554 | |
| 555 | // Suppress trivial 'return' statements that are dead. |
| 556 | if (isTrivialReturnOrDoWhile(B, S)) |
| 557 | return; |
| 558 | |
| 559 | SourceRange R1, R2; |
| 560 | SourceLocation Loc = GetUnreachableLoc(S, R1, R2); |
| 561 | CB.HandleUnreachable(Loc, R1, R2); |
| 562 | } |
| 563 | |
| 564 | //===----------------------------------------------------------------------===// |
| 565 | // Reachability APIs. |
| 566 | //===----------------------------------------------------------------------===// |
| 567 | |
| 568 | namespace clang { namespace reachable_code { |
| 569 | |
| 570 | void Callback::anchor() { } |
| 571 | |
| 572 | unsigned ScanReachableFromBlock(const CFGBlock *Start, |
| 573 | llvm::BitVector &Reachable) { |
| 574 | return scanFromBlock(Start, Reachable, false); |
| 575 | } |
| 576 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 577 | void FindUnreachableCode(AnalysisDeclContext &AC, Callback &CB) { |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 578 | CFG *cfg = AC.getCFG(); |
| 579 | if (!cfg) |
| 580 | return; |
| 581 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 582 | // Scan for reachable blocks from the entrance of the CFG. |
| 583 | // If there are no unreachable blocks, we're done. |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 584 | llvm::BitVector reachable(cfg->getNumBlockIDs()); |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 585 | unsigned numReachable = |
| 586 | scanMaybeReachableFromBlock(&cfg->getEntry(), reachable); |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 587 | if (numReachable == cfg->getNumBlockIDs()) |
| 588 | return; |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 589 | |
| 590 | // If there aren't explicit EH edges, we should include the 'try' dispatch |
| 591 | // blocks as roots. |
| 592 | if (!AC.getCFGBuildOptions().AddEHEdges) { |
| 593 | for (CFG::try_block_iterator I = cfg->try_blocks_begin(), |
| 594 | E = cfg->try_blocks_end() ; I != E; ++I) { |
Ted Kremenek | 7d47cac | 2014-03-07 07:14:36 +0000 | [diff] [blame] | 595 | numReachable += scanMaybeReachableFromBlock(*I, reachable); |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 596 | } |
| 597 | if (numReachable == cfg->getNumBlockIDs()) |
| 598 | return; |
| 599 | } |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 600 | |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 601 | // There are some unreachable blocks. We need to find the root blocks that |
| 602 | // contain code that should be considered unreachable. |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 603 | for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) { |
Ted Kremenek | bd91371 | 2011-08-23 23:05:11 +0000 | [diff] [blame] | 604 | const CFGBlock *block = *I; |
| 605 | // A block may have been marked reachable during this loop. |
| 606 | if (reachable[block->getBlockID()]) |
| 607 | continue; |
| 608 | |
| 609 | DeadCodeScan DS(reachable); |
| 610 | numReachable += DS.scanBackwards(block, CB); |
| 611 | |
| 612 | if (numReachable == cfg->getNumBlockIDs()) |
| 613 | return; |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 614 | } |
Ted Kremenek | 552eeaa | 2010-02-23 05:59:20 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | }} // end namespace clang::reachable_code |