[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-04 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-04 Thread Yutong Zhu via cfe-commits


@@ -658,7 +649,13 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 Next = SC->getSubStmt();
   else if (LabelStmt *LS = dyn_cast(SubStmt))
 Next = LS->getSubStmt();
-  else
+  else if (AttributedStmt *AS = dyn_cast(SubStmt)) {
+if (GetMustTailAttr(AS)) {
+  LabelAndGotoScopes[AS] = ParentScope;
+  MustTailStmts.push_back(AS);
+}
+Next = AS->getSubStmt();
+  } else

YutongZhuu wrote:

Please correct me if my understanding is incorrect. 
This function is basically doing a DFS on the AST and skipping the nodes that 
is not considered to be scope parents(also could you confirm attributes can not 
be scope parents?). If my understanding is correct, the node for attributes 
will be skipped and ``case Stmt::AttributedStmtClass`` never gets executed. 

https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-04 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-04 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> Could you add a test case - check in clang/test to see if other tests for the 
> diagnostic text in the original bug, and add a test case for that nearby 
> (maybe the same file the diagnostic is already tested in)?

Do you mean I should check if there exists a test for the original bug, if not, 
add one?

https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-04 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-04 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-04 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-07 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/125370

>From 380ae2020f71cc5006db2e29b0a69f61297f585c Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sat, 1 Feb 2025 20:09:13 -0500
Subject: [PATCH] Force AttributedStmtClass to not be scope parents

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/lib/Sema/JumpDiagnostics.cpp  | 17 +++--
 .../stmt.stmt/stmt.select/stmt.switch/p4.cpp| 11 +++
 3 files changed, 20 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4410b9f99e802f7..fc11f213b2be10e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -733,6 +733,8 @@ Improvements to Clang's diagnostics
   scope.Unlock();
   require(scope); // Warning!  Requires mu1.
 }
+
+- Clang now forces attributes to not be scope parents (#84072).
 
 Improvements to Clang's time-trace
 --
diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index d465599450e7ffc..c5577e09a86a1c3 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -597,15 +597,6 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 LabelAndGotoScopes[S] = ParentScope;
 break;
 
-  case Stmt::AttributedStmtClass: {
-AttributedStmt *AS = cast(S);
-if (GetMustTailAttr(AS)) {
-  LabelAndGotoScopes[AS] = ParentScope;
-  MustTailStmts.push_back(AS);
-}
-break;
-  }
-
   case Stmt::OpenACCComputeConstructClass: {
 unsigned NewParentScope = Scopes.size();
 OpenACCComputeConstruct *CC = cast(S);
@@ -658,7 +649,13 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 Next = SC->getSubStmt();
   else if (LabelStmt *LS = dyn_cast(SubStmt))
 Next = LS->getSubStmt();
-  else
+  else if (AttributedStmt *AS = dyn_cast(SubStmt)) {
+if (GetMustTailAttr(AS)) {
+  LabelAndGotoScopes[AS] = ParentScope;
+  MustTailStmts.push_back(AS);
+}
+Next = AS->getSubStmt();
+  } else
 break;
 
   LabelAndGotoScopes[SubStmt] = ParentScope;
diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp 
b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
new file mode 100644
index 000..d89dda7215074f7
--- /dev/null
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang++ -fsyntax-only -std=c++20 -Xclang -verify %s
+
+void Func(int x) {
+switch (x) {
+[[likely]] case 0:
+case 1: 
+int i = 3; // expected-note {{jump bypasses variable 
initialization}}
+case 2: // expected-error {{cannot jump from switch statement to this 
case label}}
+break;
+}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-09 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/125370

>From 380ae2020f71cc5006db2e29b0a69f61297f585c Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sat, 1 Feb 2025 20:09:13 -0500
Subject: [PATCH 1/3] Force AttributedStmtClass to not be scope parents

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/lib/Sema/JumpDiagnostics.cpp  | 17 +++--
 .../stmt.stmt/stmt.select/stmt.switch/p4.cpp| 11 +++
 3 files changed, 20 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4410b9f99e802f7..fc11f213b2be10e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -733,6 +733,8 @@ Improvements to Clang's diagnostics
   scope.Unlock();
   require(scope); // Warning!  Requires mu1.
 }
+
+- Clang now forces attributes to not be scope parents (#84072).
 
 Improvements to Clang's time-trace
 --
diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index d465599450e7ffc..c5577e09a86a1c3 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -597,15 +597,6 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 LabelAndGotoScopes[S] = ParentScope;
 break;
 
-  case Stmt::AttributedStmtClass: {
-AttributedStmt *AS = cast(S);
-if (GetMustTailAttr(AS)) {
-  LabelAndGotoScopes[AS] = ParentScope;
-  MustTailStmts.push_back(AS);
-}
-break;
-  }
-
   case Stmt::OpenACCComputeConstructClass: {
 unsigned NewParentScope = Scopes.size();
 OpenACCComputeConstruct *CC = cast(S);
@@ -658,7 +649,13 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 Next = SC->getSubStmt();
   else if (LabelStmt *LS = dyn_cast(SubStmt))
 Next = LS->getSubStmt();
-  else
+  else if (AttributedStmt *AS = dyn_cast(SubStmt)) {
+if (GetMustTailAttr(AS)) {
+  LabelAndGotoScopes[AS] = ParentScope;
+  MustTailStmts.push_back(AS);
+}
+Next = AS->getSubStmt();
+  } else
 break;
 
   LabelAndGotoScopes[SubStmt] = ParentScope;
diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp 
b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
new file mode 100644
index 000..d89dda7215074f7
--- /dev/null
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang++ -fsyntax-only -std=c++20 -Xclang -verify %s
+
+void Func(int x) {
+switch (x) {
+[[likely]] case 0:
+case 1: 
+int i = 3; // expected-note {{jump bypasses variable 
initialization}}
+case 2: // expected-error {{cannot jump from switch statement to this 
case label}}
+break;
+}
+}

>From a47f6c1710600ece997a435c0ddd05c6935eea97 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Fri, 7 Feb 2025 22:13:55 -0500
Subject: [PATCH 2/3] Fix test

---
 clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp 
b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
index d89dda7215074f7..e816da18036943a 100644
--- a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang++ -fsyntax-only -std=c++20 -Xclang -verify %s
+// RUN: %clang -fsyntax-only -std=c++20 -Xclang -verify %s
 
 void Func(int x) {
 switch (x) {

>From f654f53a8062fd89a0ddc4998d5131fde71334c0 Mon Sep 17 00:00:00 2001
From: Yutong Zhu <115899167+yutongz...@users.noreply.github.com>
Date: Sun, 9 Feb 2025 20:48:24 -0500
Subject: [PATCH 3/3] Edit comments

---
 clang/lib/Sema/JumpDiagnostics.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index 4d443ce82379966..bac2cf392bfbc11 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -640,7 +640,7 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
   continue;
 }
 
-// Cases, labels, and defaults aren't "scope parents".  It's also
+// Cases, labels, attributes, and defaults aren't "scope parents".  It's 
also
 // important to handle these iteratively instead of recursively in
 // order to avoid blowing out the stack.
 while (true) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-07 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/125370

>From 380ae2020f71cc5006db2e29b0a69f61297f585c Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sat, 1 Feb 2025 20:09:13 -0500
Subject: [PATCH 1/2] Force AttributedStmtClass to not be scope parents

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/lib/Sema/JumpDiagnostics.cpp  | 17 +++--
 .../stmt.stmt/stmt.select/stmt.switch/p4.cpp| 11 +++
 3 files changed, 20 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4410b9f99e802f7..fc11f213b2be10e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -733,6 +733,8 @@ Improvements to Clang's diagnostics
   scope.Unlock();
   require(scope); // Warning!  Requires mu1.
 }
+
+- Clang now forces attributes to not be scope parents (#84072).
 
 Improvements to Clang's time-trace
 --
diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index d465599450e7ffc..c5577e09a86a1c3 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -597,15 +597,6 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 LabelAndGotoScopes[S] = ParentScope;
 break;
 
-  case Stmt::AttributedStmtClass: {
-AttributedStmt *AS = cast(S);
-if (GetMustTailAttr(AS)) {
-  LabelAndGotoScopes[AS] = ParentScope;
-  MustTailStmts.push_back(AS);
-}
-break;
-  }
-
   case Stmt::OpenACCComputeConstructClass: {
 unsigned NewParentScope = Scopes.size();
 OpenACCComputeConstruct *CC = cast(S);
@@ -658,7 +649,13 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 Next = SC->getSubStmt();
   else if (LabelStmt *LS = dyn_cast(SubStmt))
 Next = LS->getSubStmt();
-  else
+  else if (AttributedStmt *AS = dyn_cast(SubStmt)) {
+if (GetMustTailAttr(AS)) {
+  LabelAndGotoScopes[AS] = ParentScope;
+  MustTailStmts.push_back(AS);
+}
+Next = AS->getSubStmt();
+  } else
 break;
 
   LabelAndGotoScopes[SubStmt] = ParentScope;
diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp 
b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
new file mode 100644
index 000..d89dda7215074f7
--- /dev/null
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang++ -fsyntax-only -std=c++20 -Xclang -verify %s
+
+void Func(int x) {
+switch (x) {
+[[likely]] case 0:
+case 1: 
+int i = 3; // expected-note {{jump bypasses variable 
initialization}}
+case 2: // expected-error {{cannot jump from switch statement to this 
case label}}
+break;
+}
+}

>From a47f6c1710600ece997a435c0ddd05c6935eea97 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Fri, 7 Feb 2025 22:13:55 -0500
Subject: [PATCH 2/2] Fix test

---
 clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp 
b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
index d89dda7215074f7..e816da18036943a 100644
--- a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang++ -fsyntax-only -std=c++20 -Xclang -verify %s
+// RUN: %clang -fsyntax-only -std=c++20 -Xclang -verify %s
 
 void Func(int x) {
 switch (x) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-08 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> Could you add a test case - check in clang/test to see if other tests for the 
> diagnostic text in the original bug, and add a test case for that nearby 
> (maybe the same file the diagnostic is already tested in)?

Done

https://github.com/llvm/llvm-project/pull/125370
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-10 Thread Yutong Zhu via cfe-commits


@@ -121,6 +121,186 @@ Attribute Changes in Clang
 Improvements to Clang's diagnostics
 ---
 
+- Some template related diagnostics have been improved.
+
+  .. code-block:: c++
+
+ void foo() { template  int i; } // error: templates can only be 
declared in namespace or class scope
+
+ struct S {
+  template  int i; // error: non-static data member 'i' cannot 
be declared as a template
+ };
+
+- Clang now has improved diagnostics for functions with explicit 'this' 
parameters. Fixes #GH97878
+
+- Clang now diagnoses dangling references to fields of temporary objects. 
Fixes #GH81589.
+
+- Clang now diagnoses undefined behavior in constant expressions more 
consistently. This includes invalid shifts, and signed overflow in arithmetic.
+
+- -Wdangling-assignment-gsl is enabled by default.
+- Clang now always preserves the template arguments as written used
+  to specialize template type aliases.
+
+- Clang now diagnoses the use of ``main`` in an ``extern`` context as invalid 
according to [basic.start.main] p3. Fixes #GH101512.
+
+- Clang now diagnoses when the result of a [[nodiscard]] function is discarded 
after being cast in C. Fixes #GH104391.
+
+- Don't emit duplicated dangling diagnostics. (#GH93386).
+
+- Improved diagnostic when trying to befriend a concept. (#GH45182).
+
+- Added the ``-Winvalid-gnu-asm-cast`` diagnostic group to control warnings
+  about use of "noop" casts for lvalues (a GNU extension). This diagnostic is
+  a warning which defaults to being an error, is enabled by default, and is
+  also controlled by the now-deprecated ``-fheinous-gnu-extensions`` flag.
+
+- Added the ``-Wdecls-in-multiple-modules`` option to assist users to identify
+  multiple declarations in different modules, which is the major reason of the 
slow
+  compilation speed with modules. This warning is disabled by default and it 
needs
+  to be explicitly enabled or by ``-Weverything``.
+
+- Improved diagnostic when trying to overload a function in an ``extern "C"`` 
context. (#GH80235)
+
+- Clang now respects lifetimebound attribute for the assignment operator 
parameter. (#GH106372).
+
+- The lifetimebound and GSL analysis in clang are coherent, allowing clang to
+  detect more use-after-free bugs. (#GH100549).
+
+- Clang now diagnoses dangling cases where a gsl-pointer is constructed from a 
gsl-owner object inside a container (#GH100384).
+
+- Clang now warns for u8 character literals used in C23 with 
``-Wpre-c23-compat`` instead of ``-Wpre-c++17-compat``.
+
+- Clang now diagnose when importing module implementation partition units in 
module interface units.
+
+- Don't emit bogus dangling diagnostics when ``[[gsl::Owner]]`` and 
`[[clang::lifetimebound]]` are used together (#GH108272).
+
+- The ``-Wreturn-stack-address`` warning now also warns about addresses of
+  local variables passed to function calls using the ``[[clang::musttail]]``
+  attribute.
+
+- Clang now diagnoses cases where a dangling ``GSLOwner`` object 
is constructed, e.g. ``std::vector v = {std::string()};`` 
(#GH100526).
+
+- Clang now diagnoses when a ``requires`` expression has a local parameter of 
void type, aligning with the function parameter (#GH109831).
+
+- Clang now emits a diagnostic note at the class declaration when the method 
definition does not match any declaration (#GH110638).
+
+- Clang now omits warnings for extra parentheses in fold expressions with 
single expansion (#GH101863).
+
+- The warning for an unsupported type for a named register variable is now 
phrased ``unsupported type for named register variable``,
+  instead of ``bad type for named register variable``. This makes it clear 
that the type is not supported at all, rather than being
+  suboptimal in some way the error fails to mention (#GH111550).
+
+- Clang now emits a ``-Wdepredcated-literal-operator`` diagnostic, even if the
+  name was a reserved name, which we improperly allowed to suppress the
+  diagnostic.
+
+- Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables 
(#GH90073).
+
+- Fix false positives when `[[gsl::Owner/Pointer]]` and 
`[[clang::lifetimebound]]` are used together.
+
+- Improved diagnostic message for ``__builtin_bit_cast`` size mismatch 
(#GH115870).
+
+- Clang now omits shadow warnings for enum constants in separate class scopes 
(#GH62588).
+
+- When diagnosing an unused return value of a type declared ``[[nodiscard]]``, 
the type
+  itself is now included in the diagnostic.
+
+- Clang will now prefer the ``[[nodiscard]]`` declaration on function 
declarations over ``[[nodiscard]]``
+  declaration on the return type of a function. Previously, when both have a 
``[[nodiscard]]`` declaration attached,
+  the one on the return type would be preferred. This may affect the generated 
warning message:
+
+  .. code-block:: c++
+
+struct [[nodiscard("Reason 1")]] S {};
+[[nodiscard("Reason 2")]] S getS();
+void use()
+{
+  getS(); /

[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-10 Thread Yutong Zhu via cfe-commits


@@ -121,6 +121,186 @@ Attribute Changes in Clang
 Improvements to Clang's diagnostics
 ---
 
+- Some template related diagnostics have been improved.
+
+  .. code-block:: c++
+
+ void foo() { template  int i; } // error: templates can only be 
declared in namespace or class scope
+
+ struct S {
+  template  int i; // error: non-static data member 'i' cannot 
be declared as a template
+ };
+
+- Clang now has improved diagnostics for functions with explicit 'this' 
parameters. Fixes #GH97878
+
+- Clang now diagnoses dangling references to fields of temporary objects. 
Fixes #GH81589.
+
+- Clang now diagnoses undefined behavior in constant expressions more 
consistently. This includes invalid shifts, and signed overflow in arithmetic.
+
+- -Wdangling-assignment-gsl is enabled by default.
+- Clang now always preserves the template arguments as written used
+  to specialize template type aliases.
+
+- Clang now diagnoses the use of ``main`` in an ``extern`` context as invalid 
according to [basic.start.main] p3. Fixes #GH101512.
+
+- Clang now diagnoses when the result of a [[nodiscard]] function is discarded 
after being cast in C. Fixes #GH104391.
+
+- Don't emit duplicated dangling diagnostics. (#GH93386).
+
+- Improved diagnostic when trying to befriend a concept. (#GH45182).
+
+- Added the ``-Winvalid-gnu-asm-cast`` diagnostic group to control warnings
+  about use of "noop" casts for lvalues (a GNU extension). This diagnostic is
+  a warning which defaults to being an error, is enabled by default, and is
+  also controlled by the now-deprecated ``-fheinous-gnu-extensions`` flag.
+
+- Added the ``-Wdecls-in-multiple-modules`` option to assist users to identify
+  multiple declarations in different modules, which is the major reason of the 
slow
+  compilation speed with modules. This warning is disabled by default and it 
needs
+  to be explicitly enabled or by ``-Weverything``.
+
+- Improved diagnostic when trying to overload a function in an ``extern "C"`` 
context. (#GH80235)
+
+- Clang now respects lifetimebound attribute for the assignment operator 
parameter. (#GH106372).
+
+- The lifetimebound and GSL analysis in clang are coherent, allowing clang to
+  detect more use-after-free bugs. (#GH100549).
+
+- Clang now diagnoses dangling cases where a gsl-pointer is constructed from a 
gsl-owner object inside a container (#GH100384).
+
+- Clang now warns for u8 character literals used in C23 with 
``-Wpre-c23-compat`` instead of ``-Wpre-c++17-compat``.
+
+- Clang now diagnose when importing module implementation partition units in 
module interface units.
+
+- Don't emit bogus dangling diagnostics when ``[[gsl::Owner]]`` and 
`[[clang::lifetimebound]]` are used together (#GH108272).
+
+- The ``-Wreturn-stack-address`` warning now also warns about addresses of
+  local variables passed to function calls using the ``[[clang::musttail]]``
+  attribute.
+
+- Clang now diagnoses cases where a dangling ``GSLOwner`` object 
is constructed, e.g. ``std::vector v = {std::string()};`` 
(#GH100526).
+
+- Clang now diagnoses when a ``requires`` expression has a local parameter of 
void type, aligning with the function parameter (#GH109831).
+
+- Clang now emits a diagnostic note at the class declaration when the method 
definition does not match any declaration (#GH110638).
+
+- Clang now omits warnings for extra parentheses in fold expressions with 
single expansion (#GH101863).
+
+- The warning for an unsupported type for a named register variable is now 
phrased ``unsupported type for named register variable``,
+  instead of ``bad type for named register variable``. This makes it clear 
that the type is not supported at all, rather than being
+  suboptimal in some way the error fails to mention (#GH111550).
+
+- Clang now emits a ``-Wdepredcated-literal-operator`` diagnostic, even if the
+  name was a reserved name, which we improperly allowed to suppress the
+  diagnostic.
+
+- Clang now diagnoses ``[[deprecated]]`` attribute usage on local variables 
(#GH90073).
+
+- Fix false positives when `[[gsl::Owner/Pointer]]` and 
`[[clang::lifetimebound]]` are used together.
+
+- Improved diagnostic message for ``__builtin_bit_cast`` size mismatch 
(#GH115870).
+
+- Clang now omits shadow warnings for enum constants in separate class scopes 
(#GH62588).
+
+- When diagnosing an unused return value of a type declared ``[[nodiscard]]``, 
the type
+  itself is now included in the diagnostic.
+
+- Clang will now prefer the ``[[nodiscard]]`` declaration on function 
declarations over ``[[nodiscard]]``
+  declaration on the return type of a function. Previously, when both have a 
``[[nodiscard]]`` declaration attached,
+  the one on the return type would be preferred. This may affect the generated 
warning message:
+
+  .. code-block:: c++
+
+struct [[nodiscard("Reason 1")]] S {};
+[[nodiscard("Reason 2")]] S getS();
+void use()
+{
+  getS(); /

[clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-02-12 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-01 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu created 
https://github.com/llvm/llvm-project/pull/125370

This PR addresses https://github.com/llvm/llvm-project/issues/84072.

>From 01497e746ae23ea5033b1c6cd6f9f9718d6dc3d6 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sat, 1 Feb 2025 20:09:13 -0500
Subject: [PATCH 1/2] Force AttributedStmtClass to not be scope parents

---
 clang/lib/Sema/JumpDiagnostics.cpp | 22 ++
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index d465599450e7ffc..68a7049185b32ff 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -310,8 +310,8 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
   unsigned &ParentScope = ((isa(S) && !isa(S))
 ? origParentScope : independentParentScope);
 
-  unsigned StmtsToSkip = 0u;
-
+  unsigned StmtsToSkip = 0u;  
+  
   // If we found a label, remember that it is in ParentScope scope.
   switch (S->getStmtClass()) {
   case Stmt::AddrLabelExprClass:
@@ -597,15 +597,6 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 LabelAndGotoScopes[S] = ParentScope;
 break;
 
-  case Stmt::AttributedStmtClass: {
-AttributedStmt *AS = cast(S);
-if (GetMustTailAttr(AS)) {
-  LabelAndGotoScopes[AS] = ParentScope;
-  MustTailStmts.push_back(AS);
-}
-break;
-  }
-
   case Stmt::OpenACCComputeConstructClass: {
 unsigned NewParentScope = Scopes.size();
 OpenACCComputeConstruct *CC = cast(S);
@@ -658,6 +649,13 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 Next = SC->getSubStmt();
   else if (LabelStmt *LS = dyn_cast(SubStmt))
 Next = LS->getSubStmt();
+  else if (AttributedStmt *AS = dyn_cast(SubStmt)){
+if (GetMustTailAttr(AS)) {
+  LabelAndGotoScopes[AS] = ParentScope;
+  MustTailStmts.push_back(AS);
+}
+Next = AS->getSubStmt();
+  }
   else
 break;
 
@@ -945,7 +943,7 @@ void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, 
SourceLocation DiagLoc,
 
   unsigned FromScope = LabelAndGotoScopes[From];
   unsigned ToScope = LabelAndGotoScopes[To];
-
+  
   // Common case: exactly the same scope, which is fine.
   if (FromScope == ToScope) return;
 

>From e71018ffee1be8cd26ee2ac22f226152ea385998 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sat, 1 Feb 2025 20:13:41 -0500
Subject: [PATCH 2/2] Clang-format

---
 clang/lib/Sema/JumpDiagnostics.cpp | 11 +--
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index 68a7049185b32ff..c5577e09a86a1c3 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -310,8 +310,8 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
   unsigned &ParentScope = ((isa(S) && !isa(S))
 ? origParentScope : independentParentScope);
 
-  unsigned StmtsToSkip = 0u;  
-  
+  unsigned StmtsToSkip = 0u;
+
   // If we found a label, remember that it is in ParentScope scope.
   switch (S->getStmtClass()) {
   case Stmt::AddrLabelExprClass:
@@ -649,14 +649,13 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 Next = SC->getSubStmt();
   else if (LabelStmt *LS = dyn_cast(SubStmt))
 Next = LS->getSubStmt();
-  else if (AttributedStmt *AS = dyn_cast(SubStmt)){
+  else if (AttributedStmt *AS = dyn_cast(SubStmt)) {
 if (GetMustTailAttr(AS)) {
   LabelAndGotoScopes[AS] = ParentScope;
   MustTailStmts.push_back(AS);
 }
 Next = AS->getSubStmt();
-  }
-  else
+  } else
 break;
 
   LabelAndGotoScopes[SubStmt] = ParentScope;
@@ -943,7 +942,7 @@ void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, 
SourceLocation DiagLoc,
 
   unsigned FromScope = LabelAndGotoScopes[From];
   unsigned ToScope = LabelAndGotoScopes[To];
-  
+
   // Common case: exactly the same scope, which is fine.
   if (FromScope == ToScope) return;
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Force AttributedStmtClass to not be scope parents (PR #125370)

2025-02-09 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/125370

>From 380ae2020f71cc5006db2e29b0a69f61297f585c Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sat, 1 Feb 2025 20:09:13 -0500
Subject: [PATCH 1/2] Force AttributedStmtClass to not be scope parents

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/lib/Sema/JumpDiagnostics.cpp  | 17 +++--
 .../stmt.stmt/stmt.select/stmt.switch/p4.cpp| 11 +++
 3 files changed, 20 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4410b9f99e802f7..fc11f213b2be10e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -733,6 +733,8 @@ Improvements to Clang's diagnostics
   scope.Unlock();
   require(scope); // Warning!  Requires mu1.
 }
+
+- Clang now forces attributes to not be scope parents (#84072).
 
 Improvements to Clang's time-trace
 --
diff --git a/clang/lib/Sema/JumpDiagnostics.cpp 
b/clang/lib/Sema/JumpDiagnostics.cpp
index d465599450e7ffc..c5577e09a86a1c3 100644
--- a/clang/lib/Sema/JumpDiagnostics.cpp
+++ b/clang/lib/Sema/JumpDiagnostics.cpp
@@ -597,15 +597,6 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 LabelAndGotoScopes[S] = ParentScope;
 break;
 
-  case Stmt::AttributedStmtClass: {
-AttributedStmt *AS = cast(S);
-if (GetMustTailAttr(AS)) {
-  LabelAndGotoScopes[AS] = ParentScope;
-  MustTailStmts.push_back(AS);
-}
-break;
-  }
-
   case Stmt::OpenACCComputeConstructClass: {
 unsigned NewParentScope = Scopes.size();
 OpenACCComputeConstruct *CC = cast(S);
@@ -658,7 +649,13 @@ void JumpScopeChecker::BuildScopeInformation(Stmt *S,
 Next = SC->getSubStmt();
   else if (LabelStmt *LS = dyn_cast(SubStmt))
 Next = LS->getSubStmt();
-  else
+  else if (AttributedStmt *AS = dyn_cast(SubStmt)) {
+if (GetMustTailAttr(AS)) {
+  LabelAndGotoScopes[AS] = ParentScope;
+  MustTailStmts.push_back(AS);
+}
+Next = AS->getSubStmt();
+  } else
 break;
 
   LabelAndGotoScopes[SubStmt] = ParentScope;
diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp 
b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
new file mode 100644
index 000..d89dda7215074f7
--- /dev/null
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang++ -fsyntax-only -std=c++20 -Xclang -verify %s
+
+void Func(int x) {
+switch (x) {
+[[likely]] case 0:
+case 1: 
+int i = 3; // expected-note {{jump bypasses variable 
initialization}}
+case 2: // expected-error {{cannot jump from switch statement to this 
case label}}
+break;
+}
+}

>From a47f6c1710600ece997a435c0ddd05c6935eea97 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Fri, 7 Feb 2025 22:13:55 -0500
Subject: [PATCH 2/2] Fix test

---
 clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp 
b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
index d89dda7215074f7..e816da18036943a 100644
--- a/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
+++ b/clang/test/CXX/stmt.stmt/stmt.select/stmt.switch/p4.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang++ -fsyntax-only -std=c++20 -Xclang -verify %s
+// RUN: %clang -fsyntax-only -std=c++20 -Xclang -verify %s
 
 void Func(int x) {
 switch (x) {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-02-11 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu created 
https://github.com/llvm/llvm-project/pull/126846

Fix to issue #18878

This PR issues the bug of not throwing warning for the following code:
```c++
int test13(unsigned a, int *b) {
return a > ~(95 != *b); // expected-warning {{comparison of integers of 
different signs}}
}
```

However, in the original issue, a comment mentioned that negation, 
pre-increment, and pre-decrement operators are also incorrect in this case. For 
negation, I am not able to implement a trivial fix without failing other tests, 
I will research further and submit another PR for that if necessary. For 
pre-increment, and pre-decrement, I don't think any fixes are required since 
errors will be triggered if that is the case.

>From e660f4070dbc11ffcf8d333f9f79516c4d2f32f9 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Tue, 11 Feb 2025 22:49:40 -0500
Subject: [PATCH] Force expressions with UO_Not to not be non-negative

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/lib/Sema/SemaChecking.cpp | 14 +-
 clang/test/Sema/compare.c   |  5 +
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6344c4b36e357..e8aeaa6e514e4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -135,6 +135,8 @@ 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(~) as 
signed integers 
+  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..37da477637c00 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10069,6 +10069,16 @@ static std::optional 
TryGetExprRange(ASTContext &C, const Expr *E,
 case UO_AddrOf: // should be impossible
   return IntRange::forValueOfType(C, GetExprType(E));
 
+case UO_Not:{
+  std::optional 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);
@@ -10543,6 +10553,8 @@ static void AnalyzeComparison(Sema &S, BinaryOperator 
*E) {
   // Check to see if one of the (unmodified) operands is of different
   // signedness.
   Expr *signedOperand, *unsignedOperand;
+  llvm::outs() << LHS->getType()->hasSignedIntegerRepresentation() << "\n";
+  llvm::outs() << RHS->getType()->hasSignedIntegerRepresentation() << "\n";
   if (LHS->getType()->hasSignedIntegerRepresentation()) {
 assert(!RHS->getType()->hasSignedIntegerRepresentation() &&
"unsigned comparison between two signed integer expressions?");
@@ -10561,7 +10573,7 @@ static void AnalyzeComparison(Sema &S, BinaryOperator 
*E) {
   /*Approximate=*/true);
   if (!signedRange)
 return;
-
+  llvm::outs() << signedRange->NonNegative << "\n";
   // Go ahead and analyze implicit conversions in the operands.  Note
   // that we skip the implicit conversions on both sides.
   AnalyzeImplicitConversions(S, LHS, E->getOperatorLoc());
diff --git a/clang/test/Sema/compare.c b/clang/test/Sema/compare.c
index 17cf0351ef4f5..7abd6fb35976c 100644
--- a/clang/test/Sema/compare.c
+++ b/clang/test/Sema/compare.c
@@ -419,3 +419,8 @@ 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}}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-02-13 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/126846

>From 44673ebf7c3fa773ffc7c52141b889c9ea352a93 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Tue, 11 Feb 2025 22:49:40 -0500
Subject: [PATCH] Force expressions with UO_Not to not be non-negative

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/lib/Sema/SemaChecking.cpp | 11 +++
 clang/test/Sema/compare.c   |  4 
 3 files changed, 17 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6344c4b36e357..e8aeaa6e514e4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -135,6 +135,8 @@ 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(~) as 
signed integers 
+  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..47566d3db82d3 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10069,6 +10069,17 @@ static std::optional 
TryGetExprRange(ASTContext &C, const Expr *E,
 case UO_AddrOf: // should be impossible
   return IntRange::forValueOfType(C, GetExprType(E));
 
+case UO_Not: {
+  std::optional 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..faa17ae7546dd 100644
--- a/clang/test/Sema/compare.c
+++ b/clang/test/Sema/compare.c
@@ -419,3 +419,7 @@ 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}}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-02-23 Thread Yutong Zhu via cfe-commits

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 
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 
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 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}}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-02-23 Thread Yutong Zhu via cfe-commits


@@ -10069,6 +10069,17 @@ static std::optional 
TryGetExprRange(ASTContext &C, const Expr *E,
 case UO_AddrOf: // should be impossible
   return IntRange::forValueOfType(C, GetExprType(E));
 
+case UO_Not: {
+  std::optional 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);

YutongZhuu wrote:

Fixed as suggested. Thanks for the help!

https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-02-26 Thread Yutong Zhu via cfe-commits

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 
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 
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 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}}
+}

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-02-26 Thread Yutong Zhu via cfe-commits

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 
Date: Sun, 23 Feb 2025 18:16:06 -0500
Subject: [PATCH 1/2] 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 
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 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}}
+}

>From 5c6ff16bde1c2968315a1f4e2e7002ff3f612fb8 Mon Sep 17 00:00:00 2001
From: Yutong Zhu <115899167+yutongz...@users.noreply.github.com>
Date: Wed, 26 Feb 2025 21:53:33 -0500
Subject: [PATCH 2/2] Update ReleaseNotes.rst

---
 clang/docs/ReleaseNotes.rst | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c73e99919b838..db53121c03a51 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -201,6 +201,7 @@ Improvements to Clang's diagnostics
   under the subgroup ``-Wunsafe-buffer-usage-in-libc-call``.
 - Diagnostics on chained comparisons (``a < b < c``) are now an error by 
default. This can be disabled with
   ``-Wno-error=parentheses``.
+
 - The :doc:`ThreadSafetyAnalysis` now supports ``-Wthread-safety-pointer``,
   which enables warning on passing or returning pointers to guarded variables
   as function arguments or return value respectively. Note that

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-07 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/126846

>From 106a982e3c6bcfa3ee7c26133f0919791699f31a Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 23 Feb 2025 18:16:06 -0500
Subject: [PATCH 1/5] Fix signed-unsigned comparison with UO_Not and UO_Minus

---
 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 307edf77ebb58..03058205e61cd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -204,6 +204,9 @@ Improvements to Clang's diagnostics
   as function arguments or return value respectively. Note that
   :doc:`ThreadSafetyAnalysis` still does not perform alias analysis. The
   feature will be default-enabled with ``-Wthread-safety`` in a future release.
+- 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 f9926c6b4adab..4d5a318f7a01f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10619,6 +10619,24 @@ static std::optional 
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 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}}
+}

>From b8f947a4cc2795451d6c4f8912859e9ce20502dd Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 2 Mar 2025 14:24:01 -0500
Subject: [PATCH 2/5] Add logic for UO_Minus

---
 clang/lib/Sema/SemaChecking.cpp | 15 ++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4d5a318f7a01f..082613ab07102 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10619,7 +10619,20 @@ static std::optional 
TryGetExprRange(ASTContext &C, const Expr *E,
 case UO_AddrOf: // should be impossible
   return IntRange::forValueOfType(C, GetExprType(E));
 
-case UO_Minus:
+case UO_Minus: {
+  if (E->getType()->isUnsignedIntegerType()) {
+return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, 
InConstantContext,
+   Approximate);
+  }
+
+  std::optional SubRange = TryGetExprRange(
+  C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
+
+  if (!SubRange)
+return std::nullopt;
+
+  return IntRange(SubRange->Width + 1, false);
+}
 case UO_Not: {
   if (E->getType()->isUnsignedIntegerType()) {
 return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, 
InConstantContext,

>From 9f6897d8affa185eac0b4ef790870b06ae97d110 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 2 Mar 2025 14:24:01 -0500
Subject: [PATCH 3/5] Fix incorrect range for UO_Minus


>From b9081bbb6ba446a9158d5f6eebd9c442fbcf666b Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 2 Mar 2025 20:27:19 -0500
Subject: [PATCH 4/5] Add additional test and comment

---
 clang/lib/Sema/SemaChecking.cpp |  5 +
 clang/test/Sema/compare.c   | 18 +-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 082613ab07102..84d4341e323d3 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/Sema

[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-06 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

Sorry, I accidentally requested for a review. Did not mean it.

https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-07 Thread Yutong Zhu via cfe-commits

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 
Date: Sun, 23 Feb 2025 18:16:06 -0500
Subject: [PATCH 1/4] 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 
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 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}}
+}

>From 3dfac146b0af7a7d41cb4ec3f8409bc3fc9ebaf8 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 2 Mar 2025 14:24:01 -0500
Subject: [PATCH 2/4] Fix incorrect range for UO_Minus

---
 clang/docs/ReleaseNotes.rst |  1 +
 clang/lib/Sema/SemaChecking.cpp | 15 ++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c73e99919b838..db53121c03a51 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -201,6 +201,7 @@ Improvements to Clang's diagnostics
   under the subgroup ``-Wunsafe-buffer-usage-in-libc-call``.
 - Diagnostics on chained comparisons (``a < b < c``) are now an error by 
default. This can be disabled with
   ``-Wno-error=parentheses``.
+
 - The :doc:`ThreadSafetyAnalysis` now supports ``-Wthread-safety-pointer``,
   which enables warning on passing or returning pointers to guarded variables
   as function arguments or return value respectively. Note that
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4d5a318f7a01f..082613ab07102 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10619,7 +10619,20 @@ static std::optional 
TryGetExprRange(ASTContext &C, const Expr *E,
 case UO_AddrOf: // should be impossible
   return IntRange::forValueOfType(C, GetExprType(E));
 
-case UO_Minus:
+case UO_Minus: {
+  if (E->getType()->isUnsignedIntegerType()) {
+return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, 
InConstantContext,
+   Approximate);
+  }
+
+  std::optional SubRange = TryGetExprRange(
+  C, UO->getSubExpr(), MaxWidth, InConstantContext, Approximate);
+
+  if (!SubRange)
+return std::nullopt;
+
+  return IntRange(SubRange->Width + 1, false);
+}
 case UO_Not: {
   if (E->getType()->isUnsignedIntegerType()) {
 return TryGetExprRange(

[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-07 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/126846

>From 331ff18c3c3c3a16b7833e6c5299dc40cfacf694 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 23 Feb 2025 18:16:06 -0500
Subject: [PATCH] Fix signed-unsigned integer comparison diagnosis that
 contains UO_Not and UO_Minus

---
 clang/docs/ReleaseNotes.rst |  3 ++
 clang/lib/Sema/SemaChecking.cpp | 36 ++
 clang/test/Sema/compare.c   | 66 -
 3 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 307edf77ebb58..03058205e61cd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -204,6 +204,9 @@ Improvements to Clang's diagnostics
   as function arguments or return value respectively. Note that
   :doc:`ThreadSafetyAnalysis` still does not perform alias analysis. The
   feature will be default-enabled with ``-Wthread-safety`` in a future release.
+- 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 f9926c6b4adab..84d4341e323d3 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -10619,6 +10619,42 @@ static std::optional 
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 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_Not: {
+  if (E->getType()->isUnsignedIntegerType()) {
+return TryGetExprRange(C, UO->getSubExpr(), MaxWidth, 
InConstantContext,
+   Approximate);
+  }
+
+  std::optional 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..fdae3bc19841e 100644
--- a/clang/test/Sema/compare.c
+++ b/clang/test/Sema/compare.c
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify 
-Wsign-compare -Wtautological-constant-in-range-compare %s -Wno-unreachable-code
-// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify 
-Wsign-compare -Wtype-limits %s -Wno-unreachable-code
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify 
-Wsign-compare -Wtautological-constant-in-range-compare %s 
-Wno-unreachable-code -DTEST=1
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify 
-Wsign-compare -Wtype-limits %s -Wno-unreachable-code -DTEST=2
 
 int test(char *C) { // nothing here should warn.
   return C != ((void*)0);
@@ -419,3 +419,65 @@ 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, unsigned b) {
+  return a > ~b; // no-warning
+}
+
+int test15(unsigned a, int b) {
+  return a > -(95 != b); // expected-warning {{comparison of integers of 
different signs}}
+}
+
+int test16(unsigned a, unsigned b) {
+  return a > -b; // no-warning
+}
+
+int test17(int a, unsigned b) {
+  return a > -(-b); // expected-warning {{comparison of integers of different 
signs}}
+}
+
+int test18(int a) {
+  return a == -(-2147483648); // expected-warning {{result of comparison of 
constant 2147483648 with expression of type 'int' is always false}}
+}
+
+int test19(int n) {
+  return -(n & 15) <= -15; // no-warning
+}
+
+#if TEST == 1
+int test20(int n) {
+

[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-07 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> @YutongZhuu Will you need someone to merge this for you?

I think so. I don't think I have the permission to merge.

https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-09 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> Can you update the first comment on this PR to reflect what you want to be in 
> the commit message? I notice that it doesn't describe what you're doing with 
> negation yet. Thanks!

Addressed

https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-09 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> Can you update the first comment on this PR to reflect what you want to be in 
> the commit message? I notice that it doesn't describe what you're doing with 
> negation yet. Thanks!

Hello, can you merge this or delete the review request I sent? This was due to 
an unsuccessful git rebase :(
Sorry about that.

https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-09 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-09 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-15 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> This had the side effect of adding implicit-int-conversion warnings on e.g. 
> the following code:
> 
> ```
> unsigned char foo(unsigned char x)
> {
> return ~x;
> }
> ```
> 
> This seems correct, but this should probably be highlighted in the release 
> notes.
> 
> Another example is:
> 
> ```
> unsigned int foo(unsigned char x)
> {
> return ~(1< }
> ```

Hi, could you edit the release note for me? I don't have write access, so I 
would have to submit a PR to do so. I don't think a PR with changes only in the 
release notes is a great idea.

https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Implement Wpointer-bool-conversion-strict (PR #131523)

2025-03-17 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> I can't for the life of me see a good way to use this.  The `if(p)` pattern 
> is so common that no code base could ever turn on this warning.  We could 
> PERHAPS check that we're in the process of checking a condition expression 
> and suppress this diagnostic, or recognize this pattern somehow, but without 
> some more work to make this more useful, I don't think we can accept this 
> diagnostic.

Hi, I submitted this PR because of the implementation was easy and I wanted to 
know your opinions. Let me know if I should put more work into this issue. If 
the answer is No, I am okay with closing this MR, and we should also close all 
relevant issues. 

https://github.com/llvm/llvm-project/pull/131523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Implement Wpointer-bool-conversion-strict (PR #131523)

2025-03-16 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/131523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang]Implement Wpointer-bool-conversion-strict (PR #131523)

2025-03-16 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/131523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang]Implement Wpointer-bool-conversion-strict (PR #131523)

2025-03-16 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu updated 
https://github.com/llvm/llvm-project/pull/131523

>From f99d61ef3353e8559450e91ad8201f8fe7592a86 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 16 Mar 2025 11:15:31 -0400
Subject: [PATCH 1/2] Implement Wpointer-bool-conversion-strict

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/include/clang/Basic/DiagnosticGroups.td   |  1 +
 clang/include/clang/Basic/DiagnosticSemaKinds.td|  4 
 clang/lib/Sema/SemaChecking.cpp |  3 +++
 .../test/Sema/warn-pointer-bool-conversion-strict.c | 13 +
 .../Sema/warn-pointer-bool-conversion-strict.cpp| 12 
 6 files changed, 35 insertions(+)
 create mode 100644 clang/test/Sema/warn-pointer-bool-conversion-strict.c
 create mode 100644 clang/test/Sema/warn-pointer-bool-conversion-strict.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7859871b0493a..5c9783d051de5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -136,6 +136,8 @@ New Compiler Flags
   The feature has `existed 
`_)
   for a while and this is just a user facing option.
 
+- New Option ``-Wpointer-bool-conversion-strict`` has been added to warn about 
all implicit pointer-to-bool conversions (#GH9500). This option is ignored by 
default.
+
 Deprecated Compiler Flags
 -
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index fac80fb4009aa..4b2baa898a918 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -74,6 +74,7 @@ def LiteralConversion : DiagGroup<"literal-conversion">;
 def StringConversion : DiagGroup<"string-conversion">;
 def SignConversion : DiagGroup<"sign-conversion">;
 def PointerBoolConversion : DiagGroup<"pointer-bool-conversion">;
+def PointerBoolConversionStrict : DiagGroup<"pointer-bool-conversion-strict">;
 def UndefinedBoolConversion : DiagGroup<"undefined-bool-conversion">;
 def BitwiseInsteadOfLogical : DiagGroup<"bitwise-instead-of-logical">;
 def BoolOperation : DiagGroup<"bool-operation", [BitwiseInsteadOfLogical]>;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 14b0051709625..7f81b8ece55b5 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4337,6 +4337,10 @@ def warn_impcast_pointer_to_bool : Warning<
 "address of %select{'%1'|function '%1'|array '%1'|lambda function pointer "
 "conversion operator}0 will always evaluate to 'true'">,
 InGroup;
+def warn_impcast_pointer_to_bool_strict: Warning<
+"implicit conversion of pointer to bool">,
+InGroup,
+DefaultIgnore;
 def warn_cast_nonnull_to_bool : Warning<
 "nonnull %select{function call|parameter}0 '%1' will evaluate to "
 "'true' on first encounter">,
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 9cac9cf5c4df7..fee5611873232 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11699,6 +11699,9 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, 
SourceLocation CC,
   DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
SourceRange(CC));
 }
+if (Source->isPointerType()) {
+  Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool_strict);
+}
   }
 
   // If the we're converting a constant to an ObjC BOOL on a platform where 
BOOL
diff --git a/clang/test/Sema/warn-pointer-bool-conversion-strict.c 
b/clang/test/Sema/warn-pointer-bool-conversion-strict.c
new file mode 100644
index 0..5620f054d4354
--- /dev/null
+++ b/clang/test/Sema/warn-pointer-bool-conversion-strict.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -Wpointer-bool-conversion %s
+#include 
+_Bool f() {
+  int *p;
+  if (p) {} // expected-warning {{implicit conversion of pointer to bool}}
+  return (void *)0; // expected-warning {{implicit conversion of pointer to 
bool}}
+}
+
+_Bool g() {
+  int *p = (void *)0;
+  if (p == NULL) {} // no-warning
+  return (void *)0 == NULL; // no-warning
+}
diff --git a/clang/test/Sema/warn-pointer-bool-conversion-strict.cpp 
b/clang/test/Sema/warn-pointer-bool-conversion-strict.cpp
new file mode 100644
index 0..8e612d2459705
--- /dev/null
+++ b/clang/test/Sema/warn-pointer-bool-conversion-strict.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -Wpointer-bool-conversion %s
+bool f() {
+int *p;
+if (p) {} // expected-warning {{implicit conversion of pointer to bool}}
+return (void *)0; // expected-warning {{implicit conversion of pointer to 
bool}}
+}
+
+bool g() {
+int *p = nullptr;
+if (p == nullptr) {} // no-warning
+return (void *)0 == nullpt

[clang] Implement Wpointer-bool-conversion-strict (PR #131523)

2025-03-16 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu created 
https://github.com/llvm/llvm-project/pull/131523

This PR implements the feature request from issue #9500. From the original 
issue:

```c
_Bool foo(void) {
  _Bool x = (void *)0; // warn on this
  return (void *)0; // warn on this
}
```

However, I believe that using null checks like if ``(!p) return;`` is a common 
practice in C/C++ code. Because of this, I made the option ignored by default.

Additionally, the issue creator appears to be requesting more than just 
pointer-to-bool conversion checks. Please let me know your thoughts on this.

>From f99d61ef3353e8559450e91ad8201f8fe7592a86 Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 16 Mar 2025 11:15:31 -0400
Subject: [PATCH] Implement Wpointer-bool-conversion-strict

---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/include/clang/Basic/DiagnosticGroups.td   |  1 +
 clang/include/clang/Basic/DiagnosticSemaKinds.td|  4 
 clang/lib/Sema/SemaChecking.cpp |  3 +++
 .../test/Sema/warn-pointer-bool-conversion-strict.c | 13 +
 .../Sema/warn-pointer-bool-conversion-strict.cpp| 12 
 6 files changed, 35 insertions(+)
 create mode 100644 clang/test/Sema/warn-pointer-bool-conversion-strict.c
 create mode 100644 clang/test/Sema/warn-pointer-bool-conversion-strict.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7859871b0493a..5c9783d051de5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -136,6 +136,8 @@ New Compiler Flags
   The feature has `existed 
`_)
   for a while and this is just a user facing option.
 
+- New Option ``-Wpointer-bool-conversion-strict`` has been added to warn about 
all implicit pointer-to-bool conversions (#GH9500). This option is ignored by 
default.
+
 Deprecated Compiler Flags
 -
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index fac80fb4009aa..4b2baa898a918 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -74,6 +74,7 @@ def LiteralConversion : DiagGroup<"literal-conversion">;
 def StringConversion : DiagGroup<"string-conversion">;
 def SignConversion : DiagGroup<"sign-conversion">;
 def PointerBoolConversion : DiagGroup<"pointer-bool-conversion">;
+def PointerBoolConversionStrict : DiagGroup<"pointer-bool-conversion-strict">;
 def UndefinedBoolConversion : DiagGroup<"undefined-bool-conversion">;
 def BitwiseInsteadOfLogical : DiagGroup<"bitwise-instead-of-logical">;
 def BoolOperation : DiagGroup<"bool-operation", [BitwiseInsteadOfLogical]>;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 14b0051709625..7f81b8ece55b5 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4337,6 +4337,10 @@ def warn_impcast_pointer_to_bool : Warning<
 "address of %select{'%1'|function '%1'|array '%1'|lambda function pointer "
 "conversion operator}0 will always evaluate to 'true'">,
 InGroup;
+def warn_impcast_pointer_to_bool_strict: Warning<
+"implicit conversion of pointer to bool">,
+InGroup,
+DefaultIgnore;
 def warn_cast_nonnull_to_bool : Warning<
 "nonnull %select{function call|parameter}0 '%1' will evaluate to "
 "'true' on first encounter">,
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 9cac9cf5c4df7..fee5611873232 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11699,6 +11699,9 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, 
SourceLocation CC,
   DiagnoseAlwaysNonNullPointer(E, Expr::NPCK_NotNull, /*IsEqual*/ false,
SourceRange(CC));
 }
+if (Source->isPointerType()) {
+  Diag(E->getExprLoc(), diag::warn_impcast_pointer_to_bool_strict);
+}
   }
 
   // If the we're converting a constant to an ObjC BOOL on a platform where 
BOOL
diff --git a/clang/test/Sema/warn-pointer-bool-conversion-strict.c 
b/clang/test/Sema/warn-pointer-bool-conversion-strict.c
new file mode 100644
index 0..5620f054d4354
--- /dev/null
+++ b/clang/test/Sema/warn-pointer-bool-conversion-strict.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -Wpointer-bool-conversion %s
+#include 
+_Bool f() {
+  int *p;
+  if (p) {} // expected-warning {{implicit conversion of pointer to bool}}
+  return (void *)0; // expected-warning {{implicit conversion of pointer to 
bool}}
+}
+
+_Bool g() {
+  int *p = (void *)0;
+  if (p == NULL) {} // no-warning
+  return (void *)0 == NULL; // no-warning
+}
diff --git a/clang/test/Sema/warn-pointer-bool-conversion-strict.cpp 
b/clang/test/Sema/warn-pointer-bool-conversion-strict.cpp
new file mode 100

[clang] [Clang] Force expressions with UO_Not to not be non-negative (PR #126846)

2025-03-19 Thread Yutong Zhu via cfe-commits

YutongZhuu wrote:

> I think this change might went too far. We are seeing this check flagging 
> code like this:
> 
> ```
> error: implicit conversion loses integer precision: 'int' to 'int8_t' (aka 
> 'signed char') [-Werror,-Wimplicit-int-conversion]:
> int8_t shift = ...
> ...
> shift = -shift;
>   ~ ^~
> ```
> 
> While technically this is an implicit int conversion, as '-' promote value of 
> 'shift' from `int8_t` to `int` and the expression reassigns it back to 
> `int8_t`. But the implicit conversion here has no side effects and it is a 
> common practice. We appreciate the fact that clang now flagging cases like 
> the one in the summary, but flagging/fixing the cases like the one above 
> seems to be unnecessary. Would it be possible to provide better fine grained 
> control over the `-Wsign-compare` so the clang could still flag the the cases 
> like the one in the summary but ignore the cases where implicit conversion 
> has no side effects or UB?

I agree, I will look into this :)

https://github.com/llvm/llvm-project/pull/126846
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Implement Wpointer-bool-conversion-strict (PR #131523)

2025-03-19 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu converted_to_draft 
https://github.com/llvm/llvm-project/pull/131523
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Improved the ``-Wtautological-overlap-compare`` diagnostics to warn a… (PR #133653)

2025-03-31 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu converted_to_draft 
https://github.com/llvm/llvm-project/pull/133653
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Improved the ``-Wtautological-overlap-compare`` diagnostics to warn a… (PR #133653)

2025-03-30 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu created 
https://github.com/llvm/llvm-project/pull/133653

This PR attempts to improve the diagnostics flag 
``-Wtautological-overlap-compare`` (#13473). I have added code to warn about 
float-point literals and character literals. I have also changed the warning 
message for the non-overlapping case to provide a more correct hint to the 
user. 

>From 00ad6a946ffd2aef0ab63210e9cc9f1ef661507e Mon Sep 17 00:00:00 2001
From: Yutong Zhu 
Date: Sun, 30 Mar 2025 15:53:12 -0400
Subject: [PATCH] Improved the ``-Wtautological-overlap-compare`` diagnostics
 to warn about overlapping and non-overlapping ranges involving character
 literals and floating-point literals.

---
 clang/docs/ReleaseNotes.rst   |   3 +
 .../clang/Basic/DiagnosticSemaKinds.td|   5 +-
 clang/lib/Analysis/CFG.cpp| 213 --
 clang/lib/Sema/AnalysisBasedWarnings.cpp  |   9 +-
 clang/test/Sema/warn-overlap.c| 108 +++--
 5 files changed, 243 insertions(+), 95 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2a1c5ee2d788e..e6a31072567e7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -256,6 +256,9 @@ Improvements to Clang's diagnostics
 
 - Improve the diagnostics for shadows template parameter to report correct 
location (#GH129060).
 
+- Improved the ``-Wtautological-overlap-compare`` diagnostics to warn about 
overlapping and non-overlapping ranges involving character literals and 
floating-point literals. 
+  The warning message for non-overlapping cases has also been improved 
(#GH13473).
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 86c9c955c1c78..493a66df982c4 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10398,7 +10398,10 @@ def warn_tautological_negation_or_compare: Warning<
   "'||' of a value and its negation always evaluates to true">,
   InGroup, DefaultIgnore;
 def warn_tautological_overlap_comparison : Warning<
-  "overlapping comparisons always evaluate to %select{false|true}0">,
+  "overlapping comparisons always evaluate to true">,
+  InGroup, DefaultIgnore;
+def warn_tautological_non_overlap_comparison : Warning<
+  "non-overlapping comparisons always evaluate to false">,
   InGroup, DefaultIgnore;
 def warn_depr_array_comparison : Warning<
   "comparison between two arrays is deprecated; "
diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp
index 9af1e915482da..c6b7c49d4cb4a 100644
--- a/clang/lib/Analysis/CFG.cpp
+++ b/clang/lib/Analysis/CFG.cpp
@@ -70,13 +70,11 @@ static SourceLocation GetEndLoc(Decl *D) {
   return D->getLocation();
 }
 
-/// Returns true on constant values based around a single IntegerLiteral.
-/// Allow for use of parentheses, integer casts, and negative signs.
-/// FIXME: it would be good to unify this function with
-/// getIntegerLiteralSubexpressionValue at some point given the similarity
-/// between the functions.
+/// Returns true on constant values based around a single IntegerLiteral,
+/// CharacterLiteral, or FloatingLiteral. Allow for use of parentheses, integer
+/// casts, and negative signs.
 
-static bool IsIntegerLiteralConstantExpr(const Expr *E) {
+static bool IsLiteralConstantExpr(const Expr *E) {
   // Allow parentheses
   E = E->IgnoreParens();
 
@@ -93,16 +91,16 @@ static bool IsIntegerLiteralConstantExpr(const Expr *E) {
   return false;
 E = UO->getSubExpr();
   }
-
-  return isa(E);
+  return isa(E) || isa(E) ||
+ isa(E);
 }
 
 /// Helper for tryNormalizeBinaryOperator. Attempts to extract an 
IntegerLiteral
 /// constant expression or EnumConstantDecl from the given Expr. If it fails,
 /// returns nullptr.
-static const Expr *tryTransformToIntOrEnumConstant(const Expr *E) {
+static const Expr *tryTransformToLiteralConstants(const Expr *E) {
   E = E->IgnoreParens();
-  if (IsIntegerLiteralConstantExpr(E))
+  if (IsLiteralConstantExpr(E))
 return E;
   if (auto *DR = dyn_cast(E->IgnoreParenImpCasts()))
 return isa(DR->getDecl()) ? DR : nullptr;
@@ -119,7 +117,7 @@ tryNormalizeBinaryOperator(const BinaryOperator *B) {
   BinaryOperatorKind Op = B->getOpcode();
 
   const Expr *MaybeDecl = B->getLHS();
-  const Expr *Constant = tryTransformToIntOrEnumConstant(B->getRHS());
+  const Expr *Constant = tryTransformToLiteralConstants(B->getRHS());
   // Expr looked like `0 == Foo` instead of `Foo == 0`
   if (Constant == nullptr) {
 // Flip the operator
@@ -133,7 +131,7 @@ tryNormalizeBinaryOperator(const BinaryOperator *B) {
   Op = BO_GE;
 
 MaybeDecl = B->getRHS();
-Constant = tryTransformToIntOrEnumConstant(B->getLHS());
+Constant = tryTransformToLiteralConstants(B->getLHS());
   }
 
   return std::make_tuple(MaybeDecl, Op, C

[clang] [Clang] Improve ``-Wtautological-overlap-compare`` diagnostics flag (PR #133653)

2025-04-05 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu ready_for_review 
https://github.com/llvm/llvm-project/pull/133653
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Improve ``-Wtautological-overlap-compare`` diagnostics flag (PR #133653)

2025-04-05 Thread Yutong Zhu via cfe-commits

https://github.com/YutongZhuu edited 
https://github.com/llvm/llvm-project/pull/133653
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits