Skip to content

Commit

Permalink
-Wunreachable-code: refine recognition of unreachable "sigil" to cope…
Browse files Browse the repository at this point in the history
… with implicit casts in C++.

Fixes <rdar://problem/16631033>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@206360 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
tkremenek committed Apr 16, 2014
1 parent e6ff2fc commit 11ab38b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,9 @@ class Expr : public Stmt {
/// or CastExprs, returning their operand.
Expr *IgnoreParenCasts() LLVM_READONLY;

/// Ignore casts. Strip off any CastExprs, returning their operand.
Expr *IgnoreCasts() LLVM_READONLY;

/// IgnoreParenImpCasts - Ignore parentheses and implicit casts. Strip off
/// any ParenExpr or ImplicitCastExprs, returning their operand.
Expr *IgnoreParenImpCasts() LLVM_READONLY;
Expand Down Expand Up @@ -760,6 +763,11 @@ class Expr : public Stmt {
const Expr *IgnoreParenCasts() const LLVM_READONLY {
return const_cast<Expr*>(this)->IgnoreParenCasts();
}
/// Strip off casts, but keep parentheses.
const Expr *IgnoreCasts() const LLVM_READONLY {
return const_cast<Expr*>(this)->IgnoreCasts();
}

const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const LLVM_READONLY {
return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx);
}
Expand Down
21 changes: 21 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2400,6 +2400,27 @@ Expr *Expr::IgnoreParenCasts() {
}
}

Expr *Expr::IgnoreCasts() {
Expr *E = this;
while (true) {
if (CastExpr *P = dyn_cast<CastExpr>(E)) {
E = P->getSubExpr();
continue;
}
if (MaterializeTemporaryExpr *Materialize
= dyn_cast<MaterializeTemporaryExpr>(E)) {
E = Materialize->GetTemporaryExpr();
continue;
}
if (SubstNonTypeTemplateParmExpr *NTTP
= dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
E = NTTP->getReplacement();
continue;
}
return E;
}
}

/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
/// casts. This is intended purely as a temporary workaround for code
/// that hasn't yet been rewritten to do the right thing about those
Expand Down
5 changes: 4 additions & 1 deletion lib/Analysis/ReachableCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@ static bool isConfigurationValue(const Stmt *S,
if (!S)
return false;

if (const Expr *Ex = dyn_cast<Expr>(S))
S = Ex->IgnoreCasts();

// Special case looking for the sigil '()' around an integer literal.
if (const ParenExpr *PE = dyn_cast<ParenExpr>(S))
if (!PE->getLocStart().isMacroID())
return isConfigurationValue(PE->getSubExpr(), PP, SilenceableCondVal,
IncludeIntegers, true);

if (const Expr *Ex = dyn_cast<Expr>(S))
S = Ex->IgnoreParenCasts();
S = Ex->IgnoreCasts();

bool IgnoreYES_NO = false;

Expand Down
30 changes: 30 additions & 0 deletions test/SemaCXX/warn-unreachable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,36 @@ void test_with_paren_silencing(int x) {
calledFun();
}

void test_with_paren_silencing_impcast(int x) {
if (0) calledFun(); // expected-warning {{will never be executed}} expected-note {{silence by adding parentheses to mark code as explicitly dead}}
if ((0)) calledFun(); // no-warning

if (1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
calledFun();
else
calledFun(); // expected-warning {{will never be executed}}

if ((1))
calledFun();
else
calledFun(); // no-warning

if (!1) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
calledFun(); // expected-warning {{code will never be executed}}
else
calledFun();

if ((!1))
calledFun(); // no-warning
else
calledFun();

if (!(1))
calledFun(); // no-warning
else
calledFun();
}

void tautological_compare(bool x, int y) {
if (x > 10) // expected-note {{silence}}
calledFun(); // expected-warning {{will never be executed}}
Expand Down

0 comments on commit 11ab38b

Please sign in to comment.