Skip to content

Commit 6a38205

Browse files
committed
[c++20] P1161R3: a[b,c] is deprecated.
llvm-svn: 366630
1 parent 7017a6d commit 6a38205

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def CXX11CompatDeprecatedWritableStr :
118118
DiagGroup<"c++11-compat-deprecated-writable-strings">;
119119

120120
def DeprecatedAttributes : DiagGroup<"deprecated-attributes">;
121+
def DeprecatedCommaSubscript : DiagGroup<"deprecated-comma-subscript">;
121122
def DeprecatedDeclarations : DiagGroup<"deprecated-declarations">;
122123
def UnavailableDeclarations : DiagGroup<"unavailable-declarations">;
123124
def UnguardedAvailabilityNew : DiagGroup<"unguarded-availability-new">;
@@ -135,6 +136,7 @@ def DeprecatedWritableStr : DiagGroup<"deprecated-writable-strings",
135136
[CXX11CompatDeprecatedWritableStr]>;
136137
// FIXME: Why is DeprecatedImplementations not in this group?
137138
def Deprecated : DiagGroup<"deprecated", [DeprecatedAttributes,
139+
DeprecatedCommaSubscript,
138140
DeprecatedDeclarations,
139141
DeprecatedDynamicExceptionSpec,
140142
DeprecatedIncrementBool,

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5719,6 +5719,9 @@ def err_arithmetic_nonfragile_interface : Error<
57195719
"arithmetic on pointer to interface %0, which is not a constant size for "
57205720
"this architecture and platform">;
57215721

5722+
def warn_deprecated_comma_subscript : Warning<
5723+
"top-level comma expression in array subscript is deprecated">,
5724+
InGroup<DeprecatedCommaSubscript>;
57225725

57235726
def ext_subscript_non_lvalue : Extension<
57245727
"ISO C90 does not allow subscripting non-lvalue array">;

clang/lib/Sema/SemaExpr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4317,6 +4317,15 @@ Sema::ActOnArraySubscriptExpr(Scope *S, Expr *base, SourceLocation lbLoc,
43174317
base = result.get();
43184318
}
43194319

4320+
// A comma-expression as the index is deprecated in C++2a onwards.
4321+
if (getLangOpts().CPlusPlus2a &&
4322+
((isa<BinaryOperator>(idx) && cast<BinaryOperator>(idx)->isCommaOp()) ||
4323+
(isa<CXXOperatorCallExpr>(idx) &&
4324+
cast<CXXOperatorCallExpr>(idx)->getOperator() == OO_Comma))) {
4325+
Diag(idx->getExprLoc(), diag::warn_deprecated_comma_subscript)
4326+
<< SourceRange(base->getBeginLoc(), rbLoc);
4327+
}
4328+
43204329
// Handle any non-overload placeholder types in the base and index
43214330
// expressions. We can't handle overloads here because the other
43224331
// operand might be an overloadable type, in which case the overload

clang/test/SemaCXX/deprecated.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %clang_cc1 -std=c++11 %s -Wdeprecated -verify -triple x86_64-linux-gnu
33
// RUN: %clang_cc1 -std=c++14 %s -Wdeprecated -verify -triple x86_64-linux-gnu
44
// RUN: %clang_cc1 -std=c++17 %s -Wdeprecated -verify -triple x86_64-linux-gnu
5+
// RUN: %clang_cc1 -std=c++2a %s -Wdeprecated -verify -triple x86_64-linux-gnu
56

67
// RUN: %clang_cc1 -std=c++14 %s -Wdeprecated -verify -triple x86_64-linux-gnu -Wno-deprecated-register -DNO_DEPRECATED_FLAGS
78

@@ -99,5 +100,30 @@ namespace DeprecatedCopy {
99100
}
100101
#endif
101102

103+
struct X {
104+
friend int operator,(X, X);
105+
void operator[](int);
106+
};
107+
void array_index_comma() {
108+
int arr[123];
109+
(void)arr[(void)1, 2];
110+
(void)arr[X(), X()];
111+
X()[(void)1, 2];
112+
X()[X(), X()];
113+
#if __cplusplus > 201703L
114+
// expected-warning@-5 {{deprecated}}
115+
// expected-warning@-5 {{deprecated}}
116+
// expected-warning@-5 {{deprecated}}
117+
// expected-warning@-5 {{deprecated}}
118+
#endif
119+
120+
(void)arr[((void)1, 2)];
121+
(void)arr[(X(), X())];
122+
(void)((void)1,2)[arr];
123+
(void)(X(), X())[arr];
124+
X()[((void)1, 2)];
125+
X()[(X(), X())];
126+
}
127+
102128
# 1 "/usr/include/system-header.h" 1 3
103129
void system_header_function(void) throw();

clang/www/cxx_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ <h2 id="cxx20">C++2a implementation status</h2>
10981098
<tr>
10991099
<td>Deprecate <tt>a[b,c]</tt></td>
11001100
<td><a href="http://wg21.link/p1161r3">P1161R3</a></td>
1101-
<td class="none" align="center">No</td>
1101+
<td class="svn" align="center">SVN</td>
11021102
</tr>
11031103
<tr>
11041104
<td>Deprecate some problematic uses of <tt>volatile</tt></td>

0 commit comments

Comments
 (0)