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