[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)
Yutong Zhu via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 23 15:18:32 PST 2025
https://github.com/YutongZhuu updated https://github.com/llvm/llvm-project/pull/126846
>From d7404029e8998c8c8945cfaa34cf99b743ec2b70 Mon Sep 17 00:00:00 2001
From: Yutong Zhu <y25zhu at uwaterloo.ca>
Date: Sun, 23 Feb 2025 18:16:06 -0500
Subject: [PATCH] Fix no warning for comparison of integers of different signs
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/lib/Sema/SemaChecking.cpp | 18 ++++++++++++++++++
clang/test/Sema/compare.c | 8 ++++++++
3 files changed, 29 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6344c4b36e357..e8de334c93a2e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -135,6 +135,9 @@ Improvements to Clang's diagnostics
- Fixed a bug where Clang's Analysis did not correctly model the destructor behavior of ``union`` members (#GH119415).
- A statement attribute applied to a ``case`` label no longer suppresses
'bypassing variable initialization' diagnostics (#84072).
+- The ``-Wsign-compare`` warning now treats expressions with bitwise not(~) and minus(-) as signed integers
+ except for the case where the operand is an unsigned integer
+ and throws warning if they are compared with unsigned integers (##18878).
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 66c233de4ef30..8dd586f8ab2a8 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10069,6 +10069,24 @@ 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:
+ case UO_Not: {
+ 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;
+
+ // The width increments by 1 if the sub-expression cannot be negative
+ // since it now can be.
+ return IntRange(SubRange->Width + (int)SubRange->NonNegative, false);
+ }
+
default:
return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, InConstantContext,
Approximate);
diff --git a/clang/test/Sema/compare.c b/clang/test/Sema/compare.c
index 17cf0351ef4f5..950793631c38c 100644
--- a/clang/test/Sema/compare.c
+++ b/clang/test/Sema/compare.c
@@ -419,3 +419,11 @@ void pr36008(enum PR36008EnumTest lhs) {
if (x == y) x = y; // no warning
if (y == x) y = x; // no warning
}
+
+int test13(unsigned a, int b) {
+ return a > ~(95 != b); // expected-warning {{comparison of integers of different signs}}
+}
+
+int test14(unsigned a, int b) {
+ return a > -(95 != b); // expected-warning {{comparison of integers of different signs}}
+}
More information about the cfe-commits
mailing list