Skip to content

Commit 3f458cd

Browse files
authoredDec 7, 2024··
[Clang] Warning as error Array Comparisons from C++26 (#118872)
Starting from C++26 the array comparison warning should converted to an error. Fixes: #117859
1 parent 342c8db commit 3f458cd

File tree

8 files changed

+24
-9
lines changed

8 files changed

+24
-9
lines changed
 

‎clang-tools-extra/test/clang-tidy/checkers/misc/redundant-expression.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s misc-redundant-expression %t -- -- -fno-delayed-template-parsing
1+
// RUN: %check_clang_tidy %s misc-redundant-expression %t -- -- -fno-delayed-template-parsing -Wno-array-compare-cxx26
22

33
typedef __INT64_TYPE__ I64;
44

‎clang/docs/ReleaseNotes.rst

+3
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ New Compiler Flags
428428
- The ``-Warray-compare`` warning has been added to warn about array comparison
429429
on versions older than C++20.
430430

431+
- The ``-Warray-compare-cxx26`` warning has been added to warn about array comparison
432+
starting from C++26, this warning is enabled as an error by default.
433+
431434
Deprecated Compiler Flags
432435
-------------------------
433436

‎clang/include/clang/Basic/DiagnosticSemaKinds.td

+5
Original file line numberDiff line numberDiff line change
@@ -10274,6 +10274,11 @@ def warn_array_comparison : Warning<
1027410274
"to compare array addresses, use unary '+' to decay operands to pointers">,
1027510275
InGroup<DiagGroup<"array-compare">>;
1027610276

10277+
def warn_array_comparison_cxx26 : Warning<
10278+
"comparison between two arrays is ill-formed in C++26; "
10279+
"to compare array addresses, use unary '+' to decay operands to pointers">,
10280+
InGroup<DiagGroup<"array-compare-cxx26">>, DefaultError;
10281+
1027710282
def warn_stringcompare : Warning<
1027810283
"result of comparison against %select{a string literal|@encode}0 is "
1027910284
"unspecified (use an explicit string comparison function instead)">,

‎clang/lib/Sema/SemaExpr.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -11843,7 +11843,9 @@ static void diagnoseTautologicalComparison(Sema &S, SourceLocation Loc,
1184311843
RHSStripped->getType()->isArrayType()) {
1184411844
auto IsDeprArrayComparionIgnored =
1184511845
S.getDiagnostics().isIgnored(diag::warn_depr_array_comparison, Loc);
11846-
auto DiagID = !S.getLangOpts().CPlusPlus20 || IsDeprArrayComparionIgnored
11846+
auto DiagID = S.getLangOpts().CPlusPlus26
11847+
? diag::warn_array_comparison_cxx26
11848+
: !S.getLangOpts().CPlusPlus20 || IsDeprArrayComparionIgnored
1184711849
? diag::warn_array_comparison
1184811850
: diag::warn_depr_array_comparison;
1184911851
S.Diag(Loc, DiagID) << LHS->getSourceRange() << RHS->getSourceRange()

‎clang/test/Sema/warn-stringcompare.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -x c -fsyntax-only -verify %s
22
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=expected,cxx %s
3+
// RUN: %clang_cc1 -x c++ -std=c++26 -fsyntax-only -verify=expected,cxx26 %s
34

45
#define DELIM "/"
56
#define DOT "."
@@ -15,15 +16,15 @@ void test(const char *d) {
1516
if (NULL == "/")
1617
return;
1718
if ("/" != DELIM) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
18-
return; // cxx-warning@-1 {{comparison between two arrays}}
19+
return; // cxx-warning@-1 {{comparison between two arrays}} cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
1920
if (DELIM == "/") // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
20-
return; // cxx-warning@-1 {{comparison between two arrays}}
21+
return; // cxx-warning@-1 {{comparison between two arrays}} cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
2122
if (DELIM != NULL)
2223
return;
2324
if (NULL == DELIM)
2425
return;
2526
if (DOT != DELIM) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
26-
return; // cxx-warning@-1 {{comparison between two arrays}}
27+
return; // cxx-warning@-1 {{comparison between two arrays}} cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
2728
if (DELIM == DOT) // expected-warning {{result of comparison against a string literal is unspecified (use an explicit string comparison function instead)}}
28-
return; // cxx-warning@-1 {{comparison between two arrays}}
29+
return; // cxx-warning@-1 {{comparison between two arrays}} cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
2930
}
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s -verify=expected,not-cxx20
22
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wno-deprecated-array-compare -verify %s -verify=expected,not-cxx20
33
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx20
4+
// RUN: %clang_cc1 -std=c++26 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx26
45

56
typedef struct {
67
char str[16];
@@ -9,13 +10,14 @@ typedef struct {
910

1011
bool object_equal(const Object &obj1, const Object &obj2) {
1112
if (obj1.str != obj2.str) // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
12-
return false;
13+
return false; // cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
1314
if (obj1.id != obj2.id) // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
14-
return false;
15+
return false; // cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
1516
return true;
1617
}
1718

1819

1920
void foo(int (&array1)[2], int (&array2)[2]) {
2021
if (array1 == array2) { } // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
22+
// cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
2123
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s -verify=expected,not-cxx20
22
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx20
3+
// RUN: %clang_cc1 -std=c++26 -fsyntax-only -Wdeprecated -verify %s -verify=expected,cxx26
34

45
void f(int (&array1)[2], int (&array2)[2]) {
56
if (array1 == array2) { } // not-cxx20-warning {{comparison between two arrays compare their addresses}} cxx20-warning {{comparison between two arrays is deprecated}}
7+
// cxx26-error@-1 {{comparison between two arrays is ill-formed in C++26}}
68
}

‎clang/www/cxx_status.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
239239
<tr>
240240
<td>Remove Deprecated Array Comparisons from C++26</td>
241241
<td><a href="https://wg21.link/P2865R6">P2865R6</a></td>
242-
<td class="none" align="center">No</td>
242+
<td class="unreleased" align="center">Clang 20</td>
243243
</tr>
244244
<tr>
245245
<td>Structured Bindings can introduce a Pack</td>

0 commit comments

Comments
 (0)
Please sign in to comment.