[clang] [Clang] Warning as error Array Comparisons from C++26 (PR #118872)

Amr Hesham via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 6 10:30:00 PST 2024


https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/118872

>From 30eb50c2fff14d4f79b3f3bd2b36e511bcb2db27 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Thu, 5 Dec 2024 22:18:37 +0100
Subject: [PATCH 1/3] [Clang] Warning as error Array Comparisons from C++26

Starting from C++26 the array comparison warning should converted to an error.
---
 clang/docs/ReleaseNotes.rst                      | 3 +++
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 5 +++++
 clang/lib/Sema/SemaExpr.cpp                      | 4 +++-
 clang/test/Sema/warn-stringcompare.c             | 9 +++++----
 clang/test/SemaCXX/warn-array-comparion.cpp      | 6 ++++--
 clang/test/SemaCXX/warn-self-comparisons.cpp     | 2 ++
 clang/www/cxx_status.html                        | 2 +-
 7 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ad5ef2119d3654..6719cf42970ba4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -428,6 +428,9 @@ New Compiler Flags
 - The ``-Warray-compare`` warning has been added to warn about array comparison
   on versions older than C++20.
 
+- The ``-Warray-compare-cxx26`` warning has been added to warn about array comparison
+  starting from C++26, this warn is error by default.
+
 Deprecated Compiler Flags
 -------------------------
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 447358f0a5f382..0bf9303ba50f45 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10274,6 +10274,11 @@ def warn_array_comparison : Warning<
   "to compare array addresses, use unary '+' to decay operands to pointers">,
   InGroup<DiagGroup<"array-compare">>;
 
+def warn_array_comparison_cxx26 : Warning<
+  "comparison between two arrays compare their addresses not their contents; "
+  "to compare array addresses, use unary '+' to decay operands to pointers">,
+  InGroup<DiagGroup<"array-compare-cxx26">>, DefaultError;
+
 def warn_stringcompare : Warning<
   "result of comparison against %select{a string literal|@encode}0 is "
   "unspecified (use an explicit string comparison function instead)">,
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 4ffce2c1236610..14564b99de44c5 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -11843,7 +11843,9 @@ static void diagnoseTautologicalComparison(Sema &S, SourceLocation Loc,
       RHSStripped->getType()->isArrayType()) {
     auto IsDeprArrayComparionIgnored =
         S.getDiagnostics().isIgnored(diag::warn_depr_array_comparison, Loc);
-    auto DiagID = !S.getLangOpts().CPlusPlus20 || IsDeprArrayComparionIgnored
+    auto DiagID = S.getLangOpts().CPlusPlus26
+                      ? diag::warn_array_comparison_cxx26
+                  : !S.getLangOpts().CPlusPlus20 || IsDeprArrayComparionIgnored
                       ? diag::warn_array_comparison
                       : diag::warn_depr_array_comparison;
     S.Diag(Loc, DiagID) << LHS->getSourceRange() << RHS->getSourceRange()
diff --git a/clang/test/Sema/warn-stringcompare.c b/clang/test/Sema/warn-stringcompare.c
index 78145cf42578a4..93c2bc31fea0a3 100644
--- a/clang/test/Sema/warn-stringcompare.c
+++ b/clang/test/Sema/warn-stringcompare.c
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -x c -fsyntax-only -verify %s
 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cxx %s
+// RUN: %clang_cc1 -x c++ -std=c++26 -fsyntax-only -verify=expected,cxx-26 %s
 
 #define DELIM "/"
 #define DOT "."
@@ -15,15 +16,15 @@ void test(const char *d) {
   if (NULL == "/")
     return;
   if ("/" != DELIM) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
   if (DELIM == "/") // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
   if (DELIM != NULL)
     return;
   if (NULL == DELIM)
     return;
   if (DOT != DELIM) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
   if (DELIM == DOT) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
 }
diff --git a/clang/test/SemaCXX/warn-array-comparion.cpp b/clang/test/SemaCXX/warn-array-comparion.cpp
index a6eaaab22fc16d..87b5c4fb91e3d7 100644
--- a/clang/test/SemaCXX/warn-array-comparion.cpp
+++ b/clang/test/SemaCXX/warn-array-comparion.cpp
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s -verify=expected,not-cxx20
 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wno-deprecated-array-compare -verify %s -verify=expected,not-cxx20
 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx20
+// RUN: %clang_cc1 -std=c++26 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx26
 
 typedef struct {
   char str[16];
@@ -9,13 +10,14 @@ typedef struct {
 
 bool object_equal(const Object &obj1, const Object &obj2) {
   if (obj1.str != obj2.str) // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
-    return false;
+    return false;           // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
   if (obj1.id != obj2.id) // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
-    return false;
+    return false;         // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
   return true;
 }
 
 
 void foo(int (&array1)[2], int (&array2)[2]) {
   if (array1 == array2) { } // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
+                            // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
 }
diff --git a/clang/test/SemaCXX/warn-self-comparisons.cpp b/clang/test/SemaCXX/warn-self-comparisons.cpp
index 3847c2d918bf61..c7bb5fcc8fd00d 100644
--- a/clang/test/SemaCXX/warn-self-comparisons.cpp
+++ b/clang/test/SemaCXX/warn-self-comparisons.cpp
@@ -1,6 +1,8 @@
 // RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s -verify=expected,not-cxx20
 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx20
+// RUN: %clang_cc1 -std=c++26 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx26
 
 void f(int (&array1)[2], int (&array2)[2]) {
   if (array1 == array2) { } // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
+                            // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
 }
diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html
index fdb9807b1168c7..839b66e7a082d3 100755
--- a/clang/www/cxx_status.html
+++ b/clang/www/cxx_status.html
@@ -239,7 +239,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
  <tr>
   <td>Remove Deprecated Array Comparisons from C++26</td>
   <td><a href="https://wg21.link/P2865R6">P2865R6</a></td>
-  <td class="none" align="center">No</td>
+  <td class="none" align="center">Clang 20</td>
  </tr>
  <tr>
   <td>Structured Bindings can introduce a Pack</td>

>From f91c1a39bc6eb6c9ca6883f4625e55a0a79ec6e9 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Fri, 6 Dec 2024 18:39:57 +0100
Subject: [PATCH 2/3] Modify release note and CXX status

---
 clang/docs/ReleaseNotes.rst | 2 +-
 clang/www/cxx_status.html   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6719cf42970ba4..2c1d39350da18c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -429,7 +429,7 @@ New Compiler Flags
   on versions older than C++20.
 
 - The ``-Warray-compare-cxx26`` warning has been added to warn about array comparison
-  starting from C++26, this warn is error by default.
+  starting from C++26, this warning is enabled as an error by default.
 
 Deprecated Compiler Flags
 -------------------------
diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html
index 839b66e7a082d3..a8e79cd3475abc 100755
--- a/clang/www/cxx_status.html
+++ b/clang/www/cxx_status.html
@@ -239,7 +239,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
  <tr>
   <td>Remove Deprecated Array Comparisons from C++26</td>
   <td><a href="https://wg21.link/P2865R6">P2865R6</a></td>
-  <td class="none" align="center">Clang 20</td>
+  <td class="unreleased" align="center">Clang 20</td>
  </tr>
  <tr>
   <td>Structured Bindings can introduce a Pack</td>

>From c3b15cab3460a27c63a2eed73c81cc1af083b5c3 Mon Sep 17 00:00:00 2001
From: AmrDeveloper <amr96 at programmer.net>
Date: Fri, 6 Dec 2024 18:52:16 +0100
Subject: [PATCH 3/3] Update C++26 Diagnostic message

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 clang/test/Sema/warn-stringcompare.c             | 8 ++++----
 clang/test/SemaCXX/warn-array-comparion.cpp      | 6 +++---
 clang/test/SemaCXX/warn-self-comparisons.cpp     | 2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0bf9303ba50f45..1a3c5c70f7e976 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10275,7 +10275,7 @@ def warn_array_comparison : Warning<
   InGroup<DiagGroup<"array-compare">>;
 
 def warn_array_comparison_cxx26 : Warning<
-  "comparison between two arrays compare their addresses not their contents; "
+  "comparison between two arrays is ll-formed in C++26; "
   "to compare array addresses, use unary '+' to decay operands to pointers">,
   InGroup<DiagGroup<"array-compare-cxx26">>, DefaultError;
 
diff --git a/clang/test/Sema/warn-stringcompare.c b/clang/test/Sema/warn-stringcompare.c
index 93c2bc31fea0a3..7b3f47a79f0ec7 100644
--- a/clang/test/Sema/warn-stringcompare.c
+++ b/clang/test/Sema/warn-stringcompare.c
@@ -16,15 +16,15 @@ void test(const char *d) {
   if (NULL == "/")
     return;
   if ("/" != DELIM) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
   if (DELIM == "/") // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
   if (DELIM != NULL)
     return;
   if (NULL == DELIM)
     return;
   if (DOT != DELIM) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
   if (DELIM == DOT) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
-    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+    return;         // cxx-warning at -1 {{comparison between two arrays}} cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
 }
diff --git a/clang/test/SemaCXX/warn-array-comparion.cpp b/clang/test/SemaCXX/warn-array-comparion.cpp
index 87b5c4fb91e3d7..99344981ef5d1a 100644
--- a/clang/test/SemaCXX/warn-array-comparion.cpp
+++ b/clang/test/SemaCXX/warn-array-comparion.cpp
@@ -10,14 +10,14 @@ typedef struct {
 
 bool object_equal(const Object &obj1, const Object &obj2) {
   if (obj1.str != obj2.str) // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
-    return false;           // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+    return false;           // cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
   if (obj1.id != obj2.id) // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
-    return false;         // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+    return false;         // cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
   return true;
 }
 
 
 void foo(int (&array1)[2], int (&array2)[2]) {
   if (array1 == array2) { } // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
-                            // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+                            // cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
 }
diff --git a/clang/test/SemaCXX/warn-self-comparisons.cpp b/clang/test/SemaCXX/warn-self-comparisons.cpp
index c7bb5fcc8fd00d..7798bac31d7ece 100644
--- a/clang/test/SemaCXX/warn-self-comparisons.cpp
+++ b/clang/test/SemaCXX/warn-self-comparisons.cpp
@@ -4,5 +4,5 @@
 
 void f(int (&array1)[2], int (&array2)[2]) {
   if (array1 == array2) { } // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
-                            // cxx26-error at -1 {{comparison between two arrays compare their addresses not their contents}}
+                            // cxx26-error at -1 {{comparison between two arrays is ll-formed in C++26}}
 }



More information about the cfe-commits mailing list