[clang] [Sema] Fix -Wunreachable-code false negative when operands differ only by implicit casts (PR #149972)
M. Zeeshan Siddiqui via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 22 00:57:09 PDT 2025
https://github.com/codemzs created https://github.com/llvm/llvm-project/pull/149972
## Motivation
This is a follow-up fix for bug exposed by the `warn-unreachable_crash.cpp` test introduced in #142897 , which caused downstream test failures.
## Technical description
Teach `CheckIncorrectLogicOperator` to become cast‑agnostic so that `-Wunreachable-code` warns correctly when a target provides *native* scalar half‑precision. The diagnostic previously relied on an implicit `FloatingCast` that exists on x86‑64 (`__fp16` / `_Float16` → `float`) but is absent on AArch64 `+fullfp16`, RISC‑V `+zfh`, GPUs, etc.
@ziqingluo-90 @yronglin Could you please review promptly? (feel free to also merge it on my behalf) Thanks!
Fixes #149967
>From 0a97139995d6bfe6a7fb98ee95d8f2d35fec2e9f Mon Sep 17 00:00:00 2001
From: Zeeshan Siddiqui <mzs at ntdev.microsoft.com>
Date: Tue, 22 Jul 2025 06:37:54 +0000
Subject: [PATCH] [Sema] Make incorrect-logic operator check cast-agnostic
The check for tautological comparisons (e.g. `x != 0 || x != 1`)
failed on targets that support native scalar half precision because the
operands differed only by an implicit `FloatingCast`.
Teach CheckIncorrectLogicOperator to retry the operand-equality test
after stripping implicit casts and parentheses. This preserves the
original, cast-sensitive fast-path and fixes Issue#149967.
Updates existing test exercising both `__fp16` (promoted) and `_Float16`
(native with +fullfp16) on x86-64 and AArch64.
---
clang/lib/AST/Expr.cpp | 11 ++++--
clang/lib/Analysis/CFG.cpp | 7 ++--
clang/test/Sema/warn-unreachable_crash.cpp | 41 +++++++++++++++-------
3 files changed, 43 insertions(+), 16 deletions(-)
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 2e1a9a3d9ad63..d9aa1ba4f10ba 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -4234,8 +4234,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.
diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp
index d960d5130332b..3ddb1370a3145 100644
--- a/clang/lib/Analysis/CFG.cpp
+++ b/clang/lib/Analysis/CFG.cpp
@@ -1160,8 +1160,11 @@ class CFGBuilder {
return {};
// Check that it is the same variable on both sides.
- if (!Expr::isSameComparisonOperand(DeclExpr1, DeclExpr2))
- return {};
+ if (!Expr::isSameComparisonOperand(DeclExpr1, DeclExpr2)) {
+ if (!Expr::isSameComparisonOperand(DeclExpr1->IgnoreParenImpCasts(),
+ DeclExpr2->IgnoreParenImpCasts()))
+ return {};
+ }
// Make sure the user's intent is clear (e.g. they're comparing against two
// int literals, or two things from the same enum)
diff --git a/clang/test/Sema/warn-unreachable_crash.cpp b/clang/test/Sema/warn-unreachable_crash.cpp
index 628abcc83f810..dd3995ad4d912 100644
--- a/clang/test/Sema/warn-unreachable_crash.cpp
+++ b/clang/test/Sema/warn-unreachable_crash.cpp
@@ -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;
+}
\ No newline at end of file
More information about the cfe-commits
mailing list