Skip to content

Commit efefee2

Browse files
committed
[C2y] Modify diagnostics for generic selection with a type operand
We implemented WG14 N3260 as an extension, now it's a feature of C2y.
1 parent 380beae commit efefee2

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ def CPre23CompatPedantic : DiagGroup<"pre-c23-compat-pedantic",
299299
def : DiagGroup<"pre-c2x-compat", [CPre23Compat]>;
300300
def : DiagGroup<"pre-c2x-compat-pedantic", [CPre23CompatPedantic]>;
301301

302+
def CPre2yCompat : DiagGroup<"pre-c2y-compat">;
303+
def CPre2yCompatPedantic : DiagGroup<"pre-c2y-compat-pedantic",
304+
[CPre2yCompat]>;
305+
302306
// Warnings for C++ code which is not compatible with previous C++ standards.
303307
def CXXPre14Compat : DiagGroup<"pre-c++14-compat">;
304308
def : DiagGroup<"c++98-c++11-compat", [CXXPre14Compat]>;
@@ -1197,6 +1201,9 @@ def C23 : DiagGroup<"c23-extensions">;
11971201

11981202
def : DiagGroup<"c2x-extensions", [C23]>;
11991203

1204+
// A warning group for warnings about using C2y features as extensions.
1205+
def C2y : DiagGroup<"c2y-extensions">;
1206+
12001207
// Previously supported warning group which is no longer pertinent as binary
12011208
// literals are a C++14 and C23 extension now instead of a GNU extension.
12021209
def GNUBinaryLiteral : DiagGroup<"gnu-binary-literal">;

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,12 @@ def err_duplicate_default_assoc : Error<
157157
"duplicate default generic association">;
158158
def note_previous_default_assoc : Note<
159159
"previous default generic association is here">;
160-
def ext_generic_with_type_arg : Extension<
161-
"passing a type argument as the first operand to '_Generic' is a Clang "
162-
"extension">, InGroup<DiagGroup<"generic-type-extension">>;
160+
def ext_c2y_generic_with_type_arg : Extension<
161+
"passing a type argument as the first operand to '_Generic' is a C2y "
162+
"extension">, InGroup<C2y>;
163+
def warn_c2y_compat_generic_with_type_arg : Warning<
164+
"passing a type argument as the first operand to '_Generic' is incompatible "
165+
"with C standards before C2y">, InGroup<CPre2yCompat>, DefaultIgnore;
163166

164167
def ext_c99_feature : Extension<
165168
"'%0' is a C99 extension">, InGroup<C99>;

clang/lib/Parse/ParseExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3449,7 +3449,8 @@ ExprResult Parser::ParseGenericSelectionExpression() {
34493449
}
34503450
const auto *LIT = cast<LocInfoType>(ControllingType.get().get());
34513451
SourceLocation Loc = LIT->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
3452-
Diag(Loc, diag::ext_generic_with_type_arg);
3452+
Diag(Loc, getLangOpts().C2y ? diag::warn_c2y_compat_generic_with_type_arg
3453+
: diag::ext_c2y_generic_with_type_arg);
34533454
} else {
34543455
// C11 6.5.1.1p3 "The controlling expression of a generic selection is
34553456
// not evaluated."

clang/test/C/C2y/n3260.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -verify -std=c2y -Wall -pedantic -Wpre-c2y-compat %s
2+
// RUN: %clang_cc1 -verify=pre-c2y -std=c23 -Wall -pedantic %s
3+
4+
/* WG14 N3260: Clang 17
5+
* Generic selection expression with a type operand
6+
*/
7+
8+
static_assert(
9+
_Generic(
10+
const int, /* pre-c2y-warning {{passing a type argument as the first operand to '_Generic' is a C2y extension}}
11+
expected-warning {{passing a type argument as the first operand to '_Generic' is incompatible with C standards before C2y}}
12+
*/
13+
int : 0,
14+
const int : 1
15+
)
16+
);

clang/test/Parser/generic-selection-type-extension-pedantic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// in the right location.
55
void test(void) {
66
(void)_Generic(
7-
int, // expected-warning {{passing a type argument as the first operand to '_Generic' is a Clang extension}}
7+
int, // expected-warning {{passing a type argument as the first operand to '_Generic' is a C2y extension}}
88
int : 0);
99
(void)_Generic(
1010
12,

0 commit comments

Comments
 (0)