Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ Bug Fixes in This Version
-------------------------
- Fix a crash when marco name is empty in ``#pragma push_macro("")`` or
``#pragma pop_macro("")``. (#GH149762).
- `-Wunreachable-code`` now diagnoses tautological or contradictory
comparisons such as ``x != 0 || x != 1.0`` and ``x == 0 && x == 1.0`` on
targets that treat ``_Float16``/``__fp16`` as native scalar types. Previously
the warning was silently lost because the operands differed only by an implicit
cast chain. (#GH149967).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
14 changes: 11 additions & 3 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4233,8 +4233,15 @@ bool Expr::isSameComparisonOperand(const Expr* E1, const Expr* E2) {
// template parameters.
const auto *DRE1 = cast<DeclRefExpr>(E1);
const auto *DRE2 = cast<DeclRefExpr>(E2);
return DRE1->isPRValue() && DRE2->isPRValue() &&
DRE1->getDecl() == DRE2->getDecl();

if (DRE1->getDecl() != DRE2->getDecl())
return false;

if ((DRE1->isPRValue() && DRE2->isPRValue()) ||
(DRE1->isLValue() && DRE2->isLValue()))
return true;

return false;
}
case ImplicitCastExprClass: {
// Peel off implicit casts.
Expand All @@ -4244,7 +4251,8 @@ bool Expr::isSameComparisonOperand(const Expr* E1, const Expr* E2) {
if (!ICE1 || !ICE2)
return false;
if (ICE1->getCastKind() != ICE2->getCastKind())
return false;
return isSameComparisonOperand(ICE1->IgnoreParenImpCasts(),
ICE2->IgnoreParenImpCasts());
E1 = ICE1->getSubExpr()->IgnoreParens();
E2 = ICE2->getSubExpr()->IgnoreParens();
// The final cast must be one of these types.
Expand Down
41 changes: 29 additions & 12 deletions clang/test/Sema/warn-unreachable_crash.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
// RUN: %clang_cc1 -verify -Wunreachable-code %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -verify -Wunreachable-code %s
// RUN: %clang_cc1 -triple aarch64-unknown-linux-gnu -target-feature +fullfp16 -verify -Wunreachable-code %s
// REQUIRES: aarch64-registered-target

// Previously this test will crash
static void test(__fp16& x) {
if (x != 0 || x != 1.0) { // expected-note{{}} no-crash
x = 0.9;
} else
x = 0.8; // expected-warning{{code will never be executed}}
// ======= __fp16 version =======
static void test_fp16(__fp16 &x) {
if (x != 0 || x != 1.0) { // expected-note {{}} no-crash
x = 0.9;
} else
x = 0.8; // expected-warning{{code will never be executed}}
}

static void test2(__fp16& x) {
if (x != 1 && x == 1.0) { // expected-note{{}} no-crash
x = 0.9; // expected-warning{{code will never be executed}}
} else
x = 0.8;
static void test_fp16_b(__fp16 &x) {
if (x != 1 && x == 1.0) { // expected-note {{}} no-crash
x = 0.9; // expected-warning{{code will never be executed}}
} else
x = 0.8;
}

// ======= _Float16 version =======
static void test_f16(_Float16 &x) {
if (x != 0 || x != 1.0) { // expected-note {{}} no-crash
x = 0.9;
} else
x = 0.8; // expected-warning{{code will never be executed}}
}

static void test_f16_b(_Float16 &x) {
if (x != 1 && x == 1.0) { // expected-note {{}} no-crash
x = 0.9; // expected-warning{{code will never be executed}}
} else
x = 0.8;
}