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