[clang] Make UO_Minus and UO_Not having the same logic in TryGetExprRange (PR #139429)
Yutong Zhu via cfe-commits
cfe-commits at lists.llvm.org
Sat May 10 19:52:21 PDT 2025
https://github.com/YutongZhuu updated https://github.com/llvm/llvm-project/pull/139429
>From b3b1714c067ab830165cef7ee96867ffbe941caa Mon Sep 17 00:00:00 2001
From: Yutong Zhu <y25zhu at uwaterloo.ca>
Date: Sat, 10 May 2025 22:11:03 -0400
Subject: [PATCH] Make UO_Minus and UO_Not having the same logic in
TryGetExprRange
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/lib/Sema/SemaChecking.cpp | 24 +++++-------------------
clang/test/Sema/compare.c | 23 +++--------------------
3 files changed, 11 insertions(+), 39 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 11f62bc881b03..edbcbea7828b2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -352,6 +352,9 @@ Improvements to Clang's diagnostics
- Now correctly diagnose a tentative definition of an array with static
storage duration in pedantic mode in C. (#GH50661)
+- The -Wimplicit-int-conversion warning no longer triggers for direct assignments between integer types narrower than int.
+ However, -Wsign-compare can now incorrectly produce a warning when comparing a value to another with just one more bit of width.
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index bffd0dd461d3d..084c3dbdecb20 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10626,25 +10626,7 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
case UO_AddrOf: // should be impossible
return IntRange::forValueOfType(C, GetExprType(E));
- case UO_Minus: {
- if (E->getType()->isUnsignedIntegerType()) {
- return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
- Approximate);
- }
-
- std::optional<IntRange> SubRange = TryGetExprRange(
- C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
-
- if (!SubRange)
- return std::nullopt;
-
- // If the range was previously non-negative, we need an extra bit for the
- // sign bit. If the range was not non-negative, we need an extra bit
- // because the negation of the most-negative value is one bit wider than
- // that value.
- return IntRange(SubRange->Width + 1, false);
- }
-
+ case UO_Minus:
case UO_Not: {
if (E->getType()->isUnsignedIntegerType()) {
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
@@ -10659,6 +10641,10 @@ static std::optional<IntRange> TryGetExprRange(ASTContext &C, const Expr *E,
// The width increments by 1 if the sub-expression cannot be negative
// since it now can be.
+ // This isn't technically correct for UO_Minus since we need an extra bit
+ // because the negation of the most-negative value is one bit wider than
+ // the original value. However, the correct version triggers many unwanted
+ // warnings.
return IntRange(SubRange->Width + (int)SubRange->NonNegative, false);
}
diff --git a/clang/test/Sema/compare.c b/clang/test/Sema/compare.c
index fdae3bc19841e..f8c4694b8730e 100644
--- a/clang/test/Sema/compare.c
+++ b/clang/test/Sema/compare.c
@@ -454,16 +454,6 @@ int test20(int n) {
}
#endif
-int test21(short n) {
- return -n == 32768; // no-warning
-}
-
-#if TEST == 1
-int test22(short n) {
- return -n == 65536; // expected-warning {{result of comparison of 17-bit signed value == 65536 is always false}}
-}
-#endif
-
int test23(unsigned short n) {
return ~n == 32768; // no-warning
}
@@ -471,13 +461,6 @@ int test23(unsigned short n) {
int test24(short n) {
return ~n == 32767; // no-warning
}
-
-#if TEST == 1
-int test25(unsigned short n) {
- return ~n == 65536; // expected-warning {{result of comparison of 17-bit signed value == 65536 is always false}}
-}
-
-int test26(short n) {
- return ~n == 32768; // expected-warning {{result of comparison of 16-bit signed value == 32768 is always false}}
-}
-#endif
+unsigned char test25(unsigned char n) {
+ return -n; // no-warning
+}
\ No newline at end of file
More information about the cfe-commits
mailing list