2010-02-23 10:39:16 +08:00
|
|
|
//=- ReachableCodePathInsensitive.cpp ---------------------------*- C++ --*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a flow-sensitive, path-insensitive analysis of
|
|
|
|
// determining reachable blocks within a CFG.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/Analyses/ReachableCode.h"
|
2014-03-09 16:13:49 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2010-02-23 13:59:20 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprCXX.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2010-02-23 13:59:20 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
|
|
|
#include "clang/Analysis/AnalysisContext.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Analysis/CFG.h"
|
2010-02-23 13:59:20 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-02-23 10:39:16 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2014-03-07 15:14:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Core Reachability Analysis routines.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-02-23 13:59:20 +08:00
|
|
|
|
2014-02-27 08:24:08 +08:00
|
|
|
static bool bodyEndsWithNoReturn(const CFGBlock *B) {
|
|
|
|
for (CFGBlock::const_reverse_iterator I = B->rbegin(), E = B->rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
|
2014-03-06 14:50:46 +08:00
|
|
|
const Stmt *S = CS->getStmt();
|
|
|
|
if (const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(S))
|
|
|
|
S = EWC->getSubExpr();
|
|
|
|
if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
|
2014-02-27 08:24:08 +08:00
|
|
|
QualType CalleeType = CE->getCallee()->getType();
|
|
|
|
if (getFunctionExtInfo(*CalleeType).getNoReturn())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:32:32 +08:00
|
|
|
static bool bodyEndsWithNoReturn(const CFGBlock::AdjacentBlock &AB) {
|
2014-03-05 05:41:38 +08:00
|
|
|
// If the predecessor is a normal CFG edge, then by definition
|
|
|
|
// the predecessor did not end with a 'noreturn'.
|
|
|
|
if (AB.getReachableBlock())
|
|
|
|
return false;
|
|
|
|
|
2014-02-27 14:32:32 +08:00
|
|
|
const CFGBlock *Pred = AB.getPossiblyUnreachableBlock();
|
|
|
|
assert(!AB.isReachable() && Pred);
|
|
|
|
return bodyEndsWithNoReturn(Pred);
|
|
|
|
}
|
|
|
|
|
2014-03-15 09:26:32 +08:00
|
|
|
static bool isBreakPrecededByNoReturn(const CFGBlock *B, const Stmt *S,
|
|
|
|
reachable_code::UnreachableKind &UK) {
|
|
|
|
if (!isa<BreakStmt>(S))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UK = reachable_code::UK_Break;
|
|
|
|
|
|
|
|
if (B->pred_empty())
|
2014-02-27 08:24:08 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(B->empty());
|
|
|
|
assert(B->pred_size() == 1);
|
2014-02-27 14:32:32 +08:00
|
|
|
return bodyEndsWithNoReturn(*B->pred_begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEnumConstant(const Expr *Ex) {
|
|
|
|
const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex);
|
|
|
|
if (!DR)
|
|
|
|
return false;
|
|
|
|
return isa<EnumConstantDecl>(DR->getDecl());
|
|
|
|
}
|
|
|
|
|
2014-03-06 14:50:46 +08:00
|
|
|
static const Expr *stripStdStringCtor(const Expr *Ex) {
|
|
|
|
// Go crazy pattern matching an implicit construction of std::string("").
|
|
|
|
const ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(Ex);
|
|
|
|
if (!EWC)
|
|
|
|
return 0;
|
|
|
|
const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(EWC->getSubExpr());
|
|
|
|
if (!CCE)
|
|
|
|
return 0;
|
|
|
|
QualType Ty = CCE->getType();
|
|
|
|
if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Ty))
|
|
|
|
Ty = ET->getNamedType();
|
|
|
|
const TypedefType *TT = dyn_cast<TypedefType>(Ty);
|
|
|
|
StringRef Name = TT->getDecl()->getName();
|
|
|
|
if (Name != "string")
|
|
|
|
return 0;
|
|
|
|
if (CCE->getNumArgs() != 1)
|
|
|
|
return 0;
|
|
|
|
const MaterializeTemporaryExpr *MTE =
|
|
|
|
dyn_cast<MaterializeTemporaryExpr>(CCE->getArg(0));
|
|
|
|
if (!MTE)
|
|
|
|
return 0;
|
|
|
|
CXXBindTemporaryExpr *CBT =
|
|
|
|
dyn_cast<CXXBindTemporaryExpr>(MTE->GetTemporaryExpr()->IgnoreParenCasts());
|
|
|
|
if (!CBT)
|
|
|
|
return 0;
|
|
|
|
Ex = CBT->getSubExpr()->IgnoreParenCasts();
|
|
|
|
CCE = dyn_cast<CXXConstructExpr>(Ex);
|
|
|
|
if (!CCE)
|
|
|
|
return 0;
|
|
|
|
if (CCE->getNumArgs() != 1)
|
|
|
|
return 0;
|
|
|
|
return dyn_cast<StringLiteral>(CCE->getArg(0)->IgnoreParenCasts());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Strip away "sugar" around trivial expressions that are for the
|
|
|
|
/// purpose of this analysis considered uninteresting for dead code warnings.
|
|
|
|
static const Expr *stripExprSugar(const Expr *Ex) {
|
|
|
|
Ex = Ex->IgnoreParenCasts();
|
|
|
|
// If 'Ex' is a constructor for a std::string, strip that
|
|
|
|
// away. We can only get here if the trivial expression was
|
|
|
|
// something like a C string literal, with the std::string
|
|
|
|
// just wrapping that value.
|
|
|
|
if (const Expr *StdStringVal = stripStdStringCtor(Ex))
|
|
|
|
return StdStringVal;
|
|
|
|
return Ex;
|
|
|
|
}
|
|
|
|
|
2014-02-27 14:32:32 +08:00
|
|
|
static bool isTrivialExpression(const Expr *Ex) {
|
2014-03-06 07:46:07 +08:00
|
|
|
Ex = Ex->IgnoreParenCasts();
|
2014-02-27 14:32:32 +08:00
|
|
|
return isa<IntegerLiteral>(Ex) || isa<StringLiteral>(Ex) ||
|
2014-03-07 10:25:50 +08:00
|
|
|
isa<CXXBoolLiteralExpr>(Ex) || isa<ObjCBoolLiteralExpr>(Ex) ||
|
|
|
|
isa<CharacterLiteral>(Ex) ||
|
2014-02-27 14:32:32 +08:00
|
|
|
isEnumConstant(Ex);
|
|
|
|
}
|
|
|
|
|
2014-03-15 09:26:32 +08:00
|
|
|
static bool isTrivialReturnOrDoWhile(const CFGBlock *B, const Stmt *S,
|
|
|
|
reachable_code::UnreachableKind &UK) {
|
2014-03-06 08:17:44 +08:00
|
|
|
// Check if the block ends with a do...while() and see if 'S' is the
|
|
|
|
// condition.
|
|
|
|
if (const Stmt *Term = B->getTerminator()) {
|
2014-03-15 09:26:32 +08:00
|
|
|
if (const DoStmt *DS = dyn_cast<DoStmt>(Term)) {
|
|
|
|
const Expr *Cond = DS->getCond();
|
|
|
|
return Cond == S && isTrivialExpression(Cond);
|
|
|
|
}
|
2014-03-06 08:17:44 +08:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:32:32 +08:00
|
|
|
// Look to see if the block ends with a 'return', and see if 'S'
|
|
|
|
// is a substatement. The 'return' may not be the last element in
|
|
|
|
// the block because of destructors.
|
|
|
|
for (CFGBlock::const_reverse_iterator I = B->rbegin(), E = B->rend();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
|
|
|
|
if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(CS->getStmt())) {
|
2014-03-15 09:26:32 +08:00
|
|
|
// Determine if we need to lock at the body of the block
|
|
|
|
// before the dead return.
|
2014-03-08 10:22:23 +08:00
|
|
|
bool LookAtBody = false;
|
2014-03-15 09:26:32 +08:00
|
|
|
if (RS == S) {
|
2014-03-08 10:22:23 +08:00
|
|
|
LookAtBody = true;
|
2014-03-15 09:26:32 +08:00
|
|
|
UK = reachable_code::UK_TrivialReturn;
|
|
|
|
}
|
2014-03-08 10:22:23 +08:00
|
|
|
else {
|
|
|
|
const Expr *RE = RS->getRetValue();
|
2014-03-15 09:26:32 +08:00
|
|
|
if (RE) {
|
|
|
|
RE = stripExprSugar(RE->IgnoreParenCasts());
|
|
|
|
if (RE == S) {
|
|
|
|
UK = reachable_code::UK_TrivialReturn;
|
|
|
|
LookAtBody = isTrivialExpression(RE);
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 10:22:23 +08:00
|
|
|
}
|
|
|
|
|
2014-03-15 09:26:32 +08:00
|
|
|
if (LookAtBody) {
|
|
|
|
// More than one predecessor? Restrict the heuristic
|
|
|
|
// to looking at return statements directly dominated
|
|
|
|
// by a noreturn call.
|
|
|
|
if (B->pred_size() != 1)
|
|
|
|
return false;
|
|
|
|
|
2014-03-06 09:09:45 +08:00
|
|
|
return bodyEndsWithNoReturn(*B->pred_begin());
|
2014-03-15 09:26:32 +08:00
|
|
|
}
|
2014-02-27 14:32:32 +08:00
|
|
|
}
|
2014-03-06 09:09:45 +08:00
|
|
|
break;
|
2014-02-27 14:32:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-06 07:46:07 +08:00
|
|
|
return false;
|
2014-02-27 08:24:08 +08:00
|
|
|
}
|
|
|
|
|
2014-03-09 16:13:49 +08:00
|
|
|
static SourceLocation getTopMostMacro(SourceLocation Loc, SourceManager &SM) {
|
|
|
|
assert(Loc.isMacroID());
|
|
|
|
SourceLocation Last;
|
|
|
|
while (Loc.isMacroID()) {
|
|
|
|
Last = Loc;
|
|
|
|
Loc = SM.getImmediateMacroCallerLoc(Loc);
|
|
|
|
}
|
|
|
|
return Last;
|
|
|
|
}
|
|
|
|
|
2014-03-05 08:01:17 +08:00
|
|
|
/// Returns true if the statement is expanded from a configuration macro.
|
2014-03-09 16:13:49 +08:00
|
|
|
static bool isExpandedFromConfigurationMacro(const Stmt *S,
|
|
|
|
Preprocessor &PP,
|
|
|
|
bool IgnoreYES_NO = false) {
|
2014-03-05 08:01:17 +08:00
|
|
|
// FIXME: This is not very precise. Here we just check to see if the
|
|
|
|
// value comes from a macro, but we can do much better. This is likely
|
|
|
|
// to be over conservative. This logic is factored into a separate function
|
|
|
|
// so that we can refine it later.
|
|
|
|
SourceLocation L = S->getLocStart();
|
2014-03-09 16:13:49 +08:00
|
|
|
if (L.isMacroID()) {
|
|
|
|
if (IgnoreYES_NO) {
|
|
|
|
// The Objective-C constant 'YES' and 'NO'
|
|
|
|
// are defined as macros. Do not treat them
|
|
|
|
// as configuration values.
|
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
SourceLocation TopL = getTopMostMacro(L, SM);
|
|
|
|
StringRef MacroName = PP.getImmediateMacroName(TopL);
|
|
|
|
if (MacroName == "YES" || MacroName == "NO")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-03-05 08:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the statement represents a configuration value.
|
|
|
|
///
|
|
|
|
/// A configuration value is something usually determined at compile-time
|
|
|
|
/// to conditionally always execute some branch. Such guards are for
|
|
|
|
/// "sometimes unreachable" code. Such code is usually not interesting
|
|
|
|
/// to report as unreachable, and may mask truly unreachable code within
|
|
|
|
/// those blocks.
|
2014-03-09 07:20:11 +08:00
|
|
|
static bool isConfigurationValue(const Stmt *S,
|
2014-03-09 16:13:49 +08:00
|
|
|
Preprocessor &PP,
|
2014-03-09 07:20:11 +08:00
|
|
|
bool IncludeIntegers = true) {
|
2014-03-05 08:01:17 +08:00
|
|
|
if (!S)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const Expr *Ex = dyn_cast<Expr>(S))
|
|
|
|
S = Ex->IgnoreParenCasts();
|
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
2014-03-06 07:38:41 +08:00
|
|
|
case Stmt::DeclRefExprClass: {
|
|
|
|
const DeclRefExpr *DR = cast<DeclRefExpr>(S);
|
2014-03-08 04:51:13 +08:00
|
|
|
const ValueDecl *D = DR->getDecl();
|
|
|
|
if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D))
|
2014-03-09 16:13:49 +08:00
|
|
|
return isConfigurationValue(ED->getInitExpr(), PP);
|
2014-03-08 04:51:13 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
|
|
|
// As a heuristic, treat globals as configuration values. Note
|
|
|
|
// that we only will get here if Sema evaluated this
|
|
|
|
// condition to a constant expression, which means the global
|
|
|
|
// had to be declared in a way to be a truly constant value.
|
|
|
|
// We could generalize this to local variables, but it isn't
|
|
|
|
// clear if those truly represent configuration values that
|
|
|
|
// gate unreachable code.
|
|
|
|
return !VD->hasLocalStorage();
|
|
|
|
}
|
|
|
|
return false;
|
2014-03-06 07:38:41 +08:00
|
|
|
}
|
2014-03-05 08:01:17 +08:00
|
|
|
case Stmt::IntegerLiteralClass:
|
2014-03-09 16:13:49 +08:00
|
|
|
return IncludeIntegers ? isExpandedFromConfigurationMacro(S, PP)
|
2014-03-09 07:20:11 +08:00
|
|
|
: false;
|
2014-03-09 16:13:49 +08:00
|
|
|
case Stmt::ObjCBoolLiteralExprClass:
|
|
|
|
return isExpandedFromConfigurationMacro(S, PP, /* IgnoreYES_NO */ true);
|
|
|
|
|
2014-03-05 08:01:17 +08:00
|
|
|
case Stmt::UnaryExprOrTypeTraitExprClass:
|
|
|
|
return true;
|
|
|
|
case Stmt::BinaryOperatorClass: {
|
|
|
|
const BinaryOperator *B = cast<BinaryOperator>(S);
|
2014-03-09 07:20:11 +08:00
|
|
|
// Only include raw integers (not enums) as configuration
|
|
|
|
// values if they are used in a logical or comparison operator
|
|
|
|
// (not arithmetic).
|
|
|
|
IncludeIntegers &= (B->isLogicalOp() || B->isComparisonOp());
|
2014-03-09 16:13:49 +08:00
|
|
|
return isConfigurationValue(B->getLHS(), PP, IncludeIntegers) ||
|
|
|
|
isConfigurationValue(B->getRHS(), PP, IncludeIntegers);
|
2014-03-05 08:01:17 +08:00
|
|
|
}
|
|
|
|
case Stmt::UnaryOperatorClass: {
|
|
|
|
const UnaryOperator *UO = cast<UnaryOperator>(S);
|
|
|
|
return UO->getOpcode() == UO_LNot &&
|
2014-03-09 16:13:49 +08:00
|
|
|
isConfigurationValue(UO->getSubExpr(), PP);
|
2014-03-05 08:01:17 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if we should always explore all successors of a block.
|
2014-03-09 16:13:49 +08:00
|
|
|
static bool shouldTreatSuccessorsAsReachable(const CFGBlock *B,
|
|
|
|
Preprocessor &PP) {
|
2014-03-07 10:25:53 +08:00
|
|
|
if (const Stmt *Term = B->getTerminator()) {
|
2014-03-06 16:09:00 +08:00
|
|
|
if (isa<SwitchStmt>(Term))
|
|
|
|
return true;
|
2014-03-07 10:25:53 +08:00
|
|
|
// Specially handle '||' and '&&'.
|
|
|
|
if (isa<BinaryOperator>(Term))
|
2014-03-09 16:13:49 +08:00
|
|
|
return isConfigurationValue(Term, PP);
|
2014-03-07 10:25:53 +08:00
|
|
|
}
|
2014-03-06 16:09:00 +08:00
|
|
|
|
2014-03-09 16:13:49 +08:00
|
|
|
return isConfigurationValue(B->getTerminatorCondition(), PP);
|
2014-03-05 08:01:17 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 15:14:36 +08:00
|
|
|
static unsigned scanFromBlock(const CFGBlock *Start,
|
|
|
|
llvm::BitVector &Reachable,
|
2014-03-09 16:13:49 +08:00
|
|
|
Preprocessor *PP,
|
2014-03-07 15:14:36 +08:00
|
|
|
bool IncludeSometimesUnreachableEdges) {
|
2010-02-23 10:39:16 +08:00
|
|
|
unsigned count = 0;
|
2011-08-24 07:05:11 +08:00
|
|
|
|
2011-04-03 03:45:15 +08:00
|
|
|
// Prep work queue
|
2011-08-24 07:05:11 +08:00
|
|
|
SmallVector<const CFGBlock*, 32> WL;
|
|
|
|
|
|
|
|
// The entry block may have already been marked reachable
|
|
|
|
// by the caller.
|
|
|
|
if (!Reachable[Start->getBlockID()]) {
|
|
|
|
++count;
|
|
|
|
Reachable[Start->getBlockID()] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WL.push_back(Start);
|
|
|
|
|
2010-09-09 08:06:10 +08:00
|
|
|
// Find the reachable blocks from 'Start'.
|
2010-02-23 10:39:16 +08:00
|
|
|
while (!WL.empty()) {
|
2011-08-24 07:05:11 +08:00
|
|
|
const CFGBlock *item = WL.pop_back_val();
|
2014-03-05 08:01:17 +08:00
|
|
|
|
|
|
|
// There are cases where we want to treat all successors as reachable.
|
|
|
|
// The idea is that some "sometimes unreachable" code is not interesting,
|
|
|
|
// and that we should forge ahead and explore those branches anyway.
|
|
|
|
// This allows us to potentially uncover some "always unreachable" code
|
|
|
|
// within the "sometimes unreachable" code.
|
2011-08-24 07:05:11 +08:00
|
|
|
// Look at the successors and mark then reachable.
|
2014-03-07 10:25:53 +08:00
|
|
|
Optional<bool> TreatAllSuccessorsAsReachable;
|
2014-03-07 15:14:36 +08:00
|
|
|
if (!IncludeSometimesUnreachableEdges)
|
|
|
|
TreatAllSuccessorsAsReachable = false;
|
2014-03-07 10:25:53 +08:00
|
|
|
|
2011-08-24 07:05:11 +08:00
|
|
|
for (CFGBlock::const_succ_iterator I = item->succ_begin(),
|
2014-02-28 05:56:47 +08:00
|
|
|
E = item->succ_end(); I != E; ++I) {
|
|
|
|
const CFGBlock *B = *I;
|
2014-03-05 08:01:17 +08:00
|
|
|
if (!B) do {
|
|
|
|
const CFGBlock *UB = I->getPossiblyUnreachableBlock();
|
|
|
|
if (!UB)
|
|
|
|
break;
|
|
|
|
|
2014-03-09 16:13:49 +08:00
|
|
|
if (!TreatAllSuccessorsAsReachable.hasValue()) {
|
|
|
|
assert(PP);
|
2014-03-07 10:25:53 +08:00
|
|
|
TreatAllSuccessorsAsReachable =
|
2014-03-09 16:13:49 +08:00
|
|
|
shouldTreatSuccessorsAsReachable(item, *PP);
|
|
|
|
}
|
2014-03-07 10:25:53 +08:00
|
|
|
|
|
|
|
if (TreatAllSuccessorsAsReachable.getValue()) {
|
2014-03-05 08:01:17 +08:00
|
|
|
B = UB;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-28 05:56:47 +08:00
|
|
|
}
|
2014-03-05 08:01:17 +08:00
|
|
|
while (false);
|
|
|
|
|
2014-02-28 05:56:47 +08:00
|
|
|
if (B) {
|
2010-02-23 10:39:16 +08:00
|
|
|
unsigned blockID = B->getBlockID();
|
|
|
|
if (!Reachable[blockID]) {
|
|
|
|
Reachable.set(blockID);
|
|
|
|
WL.push_back(B);
|
2011-08-24 07:05:11 +08:00
|
|
|
++count;
|
2010-02-23 10:39:16 +08:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 05:56:47 +08:00
|
|
|
}
|
2010-02-23 10:39:16 +08:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2014-03-07 15:14:36 +08:00
|
|
|
|
|
|
|
static unsigned scanMaybeReachableFromBlock(const CFGBlock *Start,
|
2014-03-09 16:13:49 +08:00
|
|
|
Preprocessor &PP,
|
2014-03-07 15:14:36 +08:00
|
|
|
llvm::BitVector &Reachable) {
|
2014-03-09 16:13:49 +08:00
|
|
|
return scanFromBlock(Start, Reachable, &PP, true);
|
2014-03-07 15:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dead Code Scanner.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DeadCodeScan {
|
|
|
|
llvm::BitVector Visited;
|
|
|
|
llvm::BitVector &Reachable;
|
|
|
|
SmallVector<const CFGBlock *, 10> WorkList;
|
2014-03-09 16:13:49 +08:00
|
|
|
Preprocessor &PP;
|
2014-03-07 15:14:36 +08:00
|
|
|
|
|
|
|
typedef SmallVector<std::pair<const CFGBlock *, const Stmt *>, 12>
|
|
|
|
DeferredLocsTy;
|
|
|
|
|
|
|
|
DeferredLocsTy DeferredLocs;
|
|
|
|
|
|
|
|
public:
|
2014-03-09 16:13:49 +08:00
|
|
|
DeadCodeScan(llvm::BitVector &reachable, Preprocessor &PP)
|
2014-03-07 15:14:36 +08:00
|
|
|
: Visited(reachable.size()),
|
2014-03-09 16:13:49 +08:00
|
|
|
Reachable(reachable),
|
|
|
|
PP(PP) {}
|
2014-03-07 15:14:36 +08:00
|
|
|
|
|
|
|
void enqueue(const CFGBlock *block);
|
|
|
|
unsigned scanBackwards(const CFGBlock *Start,
|
|
|
|
clang::reachable_code::Callback &CB);
|
|
|
|
|
|
|
|
bool isDeadCodeRoot(const CFGBlock *Block);
|
|
|
|
|
|
|
|
const Stmt *findDeadCode(const CFGBlock *Block);
|
|
|
|
|
|
|
|
void reportDeadCode(const CFGBlock *B,
|
|
|
|
const Stmt *S,
|
|
|
|
clang::reachable_code::Callback &CB);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeadCodeScan::enqueue(const CFGBlock *block) {
|
|
|
|
unsigned blockID = block->getBlockID();
|
|
|
|
if (Reachable[blockID] || Visited[blockID])
|
|
|
|
return;
|
|
|
|
Visited[blockID] = true;
|
|
|
|
WorkList.push_back(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeadCodeScan::isDeadCodeRoot(const clang::CFGBlock *Block) {
|
|
|
|
bool isDeadRoot = true;
|
|
|
|
|
|
|
|
for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
|
|
|
|
E = Block->pred_end(); I != E; ++I) {
|
|
|
|
if (const CFGBlock *PredBlock = *I) {
|
|
|
|
unsigned blockID = PredBlock->getBlockID();
|
|
|
|
if (Visited[blockID]) {
|
|
|
|
isDeadRoot = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!Reachable[blockID]) {
|
|
|
|
isDeadRoot = false;
|
|
|
|
Visited[blockID] = true;
|
|
|
|
WorkList.push_back(PredBlock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return isDeadRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isValidDeadStmt(const Stmt *S) {
|
|
|
|
if (S->getLocStart().isInvalid())
|
|
|
|
return false;
|
|
|
|
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S))
|
|
|
|
return BO->getOpcode() != BO_Comma;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Stmt *DeadCodeScan::findDeadCode(const clang::CFGBlock *Block) {
|
|
|
|
for (CFGBlock::const_iterator I = Block->begin(), E = Block->end(); I!=E; ++I)
|
|
|
|
if (Optional<CFGStmt> CS = I->getAs<CFGStmt>()) {
|
|
|
|
const Stmt *S = CS->getStmt();
|
|
|
|
if (isValidDeadStmt(S))
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CFGTerminator T = Block->getTerminator()) {
|
2014-03-08 10:22:32 +08:00
|
|
|
if (!T.isTemporaryDtorsBranch()) {
|
|
|
|
const Stmt *S = T.getStmt();
|
|
|
|
if (isValidDeadStmt(S))
|
|
|
|
return S;
|
|
|
|
}
|
2014-03-07 15:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:51:58 +08:00
|
|
|
static int SrcCmp(const std::pair<const CFGBlock *, const Stmt *> *p1,
|
|
|
|
const std::pair<const CFGBlock *, const Stmt *> *p2) {
|
|
|
|
if (p1->second->getLocStart() < p2->second->getLocStart())
|
|
|
|
return -1;
|
|
|
|
if (p2->second->getLocStart() < p1->second->getLocStart())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-07 15:14:36 +08:00
|
|
|
unsigned DeadCodeScan::scanBackwards(const clang::CFGBlock *Start,
|
|
|
|
clang::reachable_code::Callback &CB) {
|
|
|
|
|
|
|
|
unsigned count = 0;
|
|
|
|
enqueue(Start);
|
|
|
|
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
const CFGBlock *Block = WorkList.pop_back_val();
|
|
|
|
|
|
|
|
// It is possible that this block has been marked reachable after
|
|
|
|
// it was enqueued.
|
|
|
|
if (Reachable[Block->getBlockID()])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Look for any dead code within the block.
|
|
|
|
const Stmt *S = findDeadCode(Block);
|
|
|
|
|
|
|
|
if (!S) {
|
|
|
|
// No dead code. Possibly an empty block. Look at dead predecessors.
|
|
|
|
for (CFGBlock::const_pred_iterator I = Block->pred_begin(),
|
|
|
|
E = Block->pred_end(); I != E; ++I) {
|
|
|
|
if (const CFGBlock *predBlock = *I)
|
|
|
|
enqueue(predBlock);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Specially handle macro-expanded code.
|
|
|
|
if (S->getLocStart().isMacroID()) {
|
2014-03-09 16:13:49 +08:00
|
|
|
count += scanMaybeReachableFromBlock(Block, PP, Reachable);
|
2014-03-07 15:14:36 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDeadCodeRoot(Block)) {
|
|
|
|
reportDeadCode(Block, S, CB);
|
2014-03-09 16:13:49 +08:00
|
|
|
count += scanMaybeReachableFromBlock(Block, PP, Reachable);
|
2014-03-07 15:14:36 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Record this statement as the possibly best location in a
|
|
|
|
// strongly-connected component of dead code for emitting a
|
|
|
|
// warning.
|
|
|
|
DeferredLocs.push_back(std::make_pair(Block, S));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find a dead root, then report the dead code with the
|
|
|
|
// earliest location.
|
|
|
|
if (!DeferredLocs.empty()) {
|
2014-03-08 05:51:58 +08:00
|
|
|
llvm::array_pod_sort(DeferredLocs.begin(), DeferredLocs.end(), SrcCmp);
|
2014-03-07 15:14:36 +08:00
|
|
|
for (DeferredLocsTy::iterator I = DeferredLocs.begin(),
|
|
|
|
E = DeferredLocs.end(); I != E; ++I) {
|
|
|
|
const CFGBlock *Block = I->first;
|
|
|
|
if (Reachable[Block->getBlockID()])
|
|
|
|
continue;
|
|
|
|
reportDeadCode(Block, I->second, CB);
|
2014-03-09 16:13:49 +08:00
|
|
|
count += scanMaybeReachableFromBlock(Block, PP, Reachable);
|
2014-03-07 15:14:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SourceLocation GetUnreachableLoc(const Stmt *S,
|
|
|
|
SourceRange &R1,
|
|
|
|
SourceRange &R2) {
|
|
|
|
R1 = R2 = SourceRange();
|
|
|
|
|
|
|
|
if (const Expr *Ex = dyn_cast<Expr>(S))
|
|
|
|
S = Ex->IgnoreParenImpCasts();
|
|
|
|
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
case Expr::BinaryOperatorClass: {
|
|
|
|
const BinaryOperator *BO = cast<BinaryOperator>(S);
|
|
|
|
return BO->getOperatorLoc();
|
|
|
|
}
|
|
|
|
case Expr::UnaryOperatorClass: {
|
|
|
|
const UnaryOperator *UO = cast<UnaryOperator>(S);
|
|
|
|
R1 = UO->getSubExpr()->getSourceRange();
|
|
|
|
return UO->getOperatorLoc();
|
|
|
|
}
|
|
|
|
case Expr::CompoundAssignOperatorClass: {
|
|
|
|
const CompoundAssignOperator *CAO = cast<CompoundAssignOperator>(S);
|
|
|
|
R1 = CAO->getLHS()->getSourceRange();
|
|
|
|
R2 = CAO->getRHS()->getSourceRange();
|
|
|
|
return CAO->getOperatorLoc();
|
|
|
|
}
|
|
|
|
case Expr::BinaryConditionalOperatorClass:
|
|
|
|
case Expr::ConditionalOperatorClass: {
|
|
|
|
const AbstractConditionalOperator *CO =
|
|
|
|
cast<AbstractConditionalOperator>(S);
|
|
|
|
return CO->getQuestionLoc();
|
|
|
|
}
|
|
|
|
case Expr::MemberExprClass: {
|
|
|
|
const MemberExpr *ME = cast<MemberExpr>(S);
|
|
|
|
R1 = ME->getSourceRange();
|
|
|
|
return ME->getMemberLoc();
|
|
|
|
}
|
|
|
|
case Expr::ArraySubscriptExprClass: {
|
|
|
|
const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(S);
|
|
|
|
R1 = ASE->getLHS()->getSourceRange();
|
|
|
|
R2 = ASE->getRHS()->getSourceRange();
|
|
|
|
return ASE->getRBracketLoc();
|
|
|
|
}
|
|
|
|
case Expr::CStyleCastExprClass: {
|
|
|
|
const CStyleCastExpr *CSC = cast<CStyleCastExpr>(S);
|
|
|
|
R1 = CSC->getSubExpr()->getSourceRange();
|
|
|
|
return CSC->getLParenLoc();
|
|
|
|
}
|
|
|
|
case Expr::CXXFunctionalCastExprClass: {
|
|
|
|
const CXXFunctionalCastExpr *CE = cast <CXXFunctionalCastExpr>(S);
|
|
|
|
R1 = CE->getSubExpr()->getSourceRange();
|
|
|
|
return CE->getLocStart();
|
|
|
|
}
|
|
|
|
case Stmt::CXXTryStmtClass: {
|
|
|
|
return cast<CXXTryStmt>(S)->getHandler(0)->getCatchLoc();
|
|
|
|
}
|
|
|
|
case Expr::ObjCBridgedCastExprClass: {
|
|
|
|
const ObjCBridgedCastExpr *CSC = cast<ObjCBridgedCastExpr>(S);
|
|
|
|
R1 = CSC->getSubExpr()->getSourceRange();
|
|
|
|
return CSC->getLParenLoc();
|
|
|
|
}
|
|
|
|
default: ;
|
|
|
|
}
|
|
|
|
R1 = S->getSourceRange();
|
|
|
|
return S->getLocStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeadCodeScan::reportDeadCode(const CFGBlock *B,
|
|
|
|
const Stmt *S,
|
|
|
|
clang::reachable_code::Callback &CB) {
|
2014-03-15 09:26:32 +08:00
|
|
|
// The kind of unreachable code found.
|
|
|
|
reachable_code::UnreachableKind UK = reachable_code::UK_Other;
|
|
|
|
|
2014-03-07 15:14:36 +08:00
|
|
|
// Suppress idiomatic cases of calling a noreturn function just
|
|
|
|
// before executing a 'break'. If there is other code after the 'break'
|
|
|
|
// in the block then don't suppress the warning.
|
2014-03-15 09:26:32 +08:00
|
|
|
if (isBreakPrecededByNoReturn(B, S, UK))
|
2014-03-07 15:14:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Suppress trivial 'return' statements that are dead.
|
2014-03-15 09:26:32 +08:00
|
|
|
if (UK == reachable_code::UK_Other && isTrivialReturnOrDoWhile(B, S, UK))
|
2014-03-07 15:14:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SourceRange R1, R2;
|
|
|
|
SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
|
2014-03-15 09:26:32 +08:00
|
|
|
CB.HandleUnreachable(UK, Loc, R1, R2);
|
2014-03-07 15:14:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Reachability APIs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace clang { namespace reachable_code {
|
|
|
|
|
|
|
|
void Callback::anchor() { }
|
|
|
|
|
|
|
|
unsigned ScanReachableFromBlock(const CFGBlock *Start,
|
|
|
|
llvm::BitVector &Reachable) {
|
2014-03-09 16:13:49 +08:00
|
|
|
return scanFromBlock(Start, Reachable, /* SourceManager* */ 0, false);
|
2014-03-07 15:14:36 +08:00
|
|
|
}
|
|
|
|
|
2014-03-09 16:13:49 +08:00
|
|
|
void FindUnreachableCode(AnalysisDeclContext &AC, Preprocessor &PP,
|
|
|
|
Callback &CB) {
|
|
|
|
|
2010-02-23 13:59:20 +08:00
|
|
|
CFG *cfg = AC.getCFG();
|
|
|
|
if (!cfg)
|
|
|
|
return;
|
|
|
|
|
2014-03-09 16:13:49 +08:00
|
|
|
// Scan for reachable blocks from the entrance of the CFG.
|
2011-08-24 07:05:11 +08:00
|
|
|
// If there are no unreachable blocks, we're done.
|
2010-02-23 13:59:20 +08:00
|
|
|
llvm::BitVector reachable(cfg->getNumBlockIDs());
|
2014-03-07 15:14:36 +08:00
|
|
|
unsigned numReachable =
|
2014-03-09 16:13:49 +08:00
|
|
|
scanMaybeReachableFromBlock(&cfg->getEntry(), PP, reachable);
|
2010-02-23 13:59:20 +08:00
|
|
|
if (numReachable == cfg->getNumBlockIDs())
|
|
|
|
return;
|
2011-08-24 07:05:11 +08:00
|
|
|
|
|
|
|
// If there aren't explicit EH edges, we should include the 'try' dispatch
|
|
|
|
// blocks as roots.
|
|
|
|
if (!AC.getCFGBuildOptions().AddEHEdges) {
|
|
|
|
for (CFG::try_block_iterator I = cfg->try_blocks_begin(),
|
|
|
|
E = cfg->try_blocks_end() ; I != E; ++I) {
|
2014-03-09 16:13:49 +08:00
|
|
|
numReachable += scanMaybeReachableFromBlock(*I, PP, reachable);
|
2010-02-23 13:59:20 +08:00
|
|
|
}
|
2011-08-24 07:05:11 +08:00
|
|
|
if (numReachable == cfg->getNumBlockIDs())
|
|
|
|
return;
|
2010-02-23 13:59:20 +08:00
|
|
|
}
|
|
|
|
|
2011-08-24 07:05:11 +08:00
|
|
|
// There are some unreachable blocks. We need to find the root blocks that
|
|
|
|
// contain code that should be considered unreachable.
|
|
|
|
for (CFG::iterator I = cfg->begin(), E = cfg->end(); I != E; ++I) {
|
|
|
|
const CFGBlock *block = *I;
|
|
|
|
// A block may have been marked reachable during this loop.
|
|
|
|
if (reachable[block->getBlockID()])
|
|
|
|
continue;
|
|
|
|
|
2014-03-09 16:13:49 +08:00
|
|
|
DeadCodeScan DS(reachable, PP);
|
2011-08-24 07:05:11 +08:00
|
|
|
numReachable += DS.scanBackwards(block, CB);
|
|
|
|
|
|
|
|
if (numReachable == cfg->getNumBlockIDs())
|
|
|
|
return;
|
2010-02-23 13:59:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}} // end namespace clang::reachable_code
|