File tree Expand file tree Collapse file tree 5 files changed +58
-7
lines changed Expand file tree Collapse file tree 5 files changed +58
-7
lines changed Original file line number Diff line number Diff line change @@ -767,6 +767,11 @@ Bug Fixes in This Version
767
767
flag and diagnostic because the macro injection was used to emit this warning.
768
768
Unfortunately there is no other good way to diagnose usage of ``static_assert ``
769
769
macro without inclusion of ``<assert.h> ``.
770
+ - In C23, something like ``[[/*possible attributes*/]]; `` is an attribute
771
+ declaration, not a statement. So it is not allowed by the syntax in places
772
+ where a statement is required, specifically as the secondary block of a
773
+ selection or iteration statement. This differs from C++, since C++ allows
774
+ declaration statements. Clang now emits a warning for these patterns. (#GH141659)
770
775
771
776
Bug Fixes to Compiler Builtins
772
777
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change @@ -276,6 +276,9 @@ def err_expected_while : Error<"expected 'while' in do/while loop">;
276
276
277
277
def err_expected_semi_after_stmt : Error<"expected ';' after %0 statement">;
278
278
def err_expected_semi_after_expr : Error<"expected ';' after expression">;
279
+ def warn_attr_in_secondary_block : ExtWarn<
280
+ "ISO C does not allow an attribute list to appear here">,
281
+ InGroup<DiagGroup<"c-attribute-extension">>;
279
282
def err_extraneous_token_before_semi : Error<"extraneous '%0' before ';'">;
280
283
281
284
def err_expected_semi_after_method_proto : Error<
Original file line number Diff line number Diff line change @@ -63,7 +63,8 @@ Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
63
63
// at the start of the statement. Thus, we're not using MaybeParseAttributes
64
64
// here because we don't want to allow arbitrary orderings.
65
65
ParsedAttributes CXX11Attrs (AttrFactory);
66
- MaybeParseCXX11Attributes (CXX11Attrs, /* MightBeObjCMessageSend*/ true );
66
+ bool HasStdAttr =
67
+ MaybeParseCXX11Attributes (CXX11Attrs, /* MightBeObjCMessageSend*/ true );
67
68
ParsedAttributes GNUOrMSAttrs (AttrFactory);
68
69
if (getLangOpts ().OpenCL )
69
70
MaybeParseGNUAttributes (GNUOrMSAttrs);
@@ -80,6 +81,13 @@ Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
80
81
assert ((CXX11Attrs.empty () || Res.isInvalid () || Res.isUsable ()) &&
81
82
" attributes on empty statement" );
82
83
84
+ if (HasStdAttr && getLangOpts ().C23 &&
85
+ (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) ==
86
+ ParsedStmtContext{} &&
87
+ isa_and_present<NullStmt>(Res.get ()))
88
+ Diag (CXX11Attrs.Range .getBegin (), diag::warn_attr_in_secondary_block)
89
+ << CXX11Attrs.Range ;
90
+
83
91
if (CXX11Attrs.empty () || Res.isInvalid ())
84
92
return Res;
85
93
Original file line number Diff line number Diff line change 1
1
// RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code
2
+ // RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s -Wno-unreachable-code
2
3
3
4
void test1 (void ) {
4
5
{ ; { ;;}} ;;
@@ -77,3 +78,32 @@ int test9(void) {
77
78
78
79
return 4 , // expected-error {{expected ';' after return statement}}
79
80
}
81
+
82
+ #if __STDC_VERSION__ >= 202311L
83
+ void attr_decl_in_selection_statement (int n ) {
84
+ if (1 )
85
+ [[]]; // expected-warning {{ISO C does not allow an attribute list to appear here}}
86
+
87
+ if (1 ) {
88
+
89
+ } else
90
+ [[]]; // expected-warning {{ISO C does not allow an attribute list to appear here}}
91
+
92
+
93
+ switch (n )
94
+ [[]]; // expected-warning {{ISO C does not allow an attribute list to appear here}}
95
+ }
96
+
97
+ void attr_decl_in_iteration_statement (int n ) {
98
+ int i ;
99
+ for (i = 0 ; i < n ; ++ i )
100
+ [[]]; // expected-warning {{ISO C does not allow an attribute list to appear here}}
101
+
102
+ while (i > 0 )
103
+ [[]]; // expected-warning {{ISO C does not allow an attribute list to appear here}}
104
+
105
+ do
106
+ [[]]; // expected-warning {{ISO C does not allow an attribute list to appear here}}
107
+ while (i > 0 );
108
+ }
109
+ #endif // __STDC_VERSION__ >= 202311L
Original file line number Diff line number Diff line change 1
- // RUN: %clang_cc1 -fsyntax-only -std=c2x -verify %s
1
+ // RUN: %clang_cc1 -fsyntax-only -std=c23 -verify %s
2
2
3
3
// This is the latest version of fallthrough that we support.
4
4
_Static_assert (__has_c_attribute (fallthrough ) == 201910L );
@@ -16,17 +16,22 @@ void f(int n) {
16
16
}
17
17
case 2 :
18
18
for (int n = 0 ; n != 10 ; ++ n )
19
- [[fallthrough ]]; // expected-error {{does not directly precede switch label}}
19
+ [[fallthrough ]]; // expected-error {{does not directly precede switch label}} \
20
+ // expected-warning {{ISO C does not allow an attribute list to appear here}}
20
21
case 3 :
21
22
while (1 )
22
- [[fallthrough ]]; // expected-error {{does not directly precede switch label}}
23
+ [[fallthrough ]]; // expected-error {{does not directly precede switch label}} \
24
+ // expected-warning {{ISO C does not allow an attribute list to appear here}}
23
25
case 4 :
24
26
while (0 )
25
- [[fallthrough ]]; // expected-error {{does not directly precede switch label}}
27
+ [[fallthrough ]]; // expected-error {{does not directly precede switch label}} \
28
+ // expected-warning {{ISO C does not allow an attribute list to appear here}}
26
29
case 5 :
27
- do [[fallthrough ]]; while (1 ); // expected-error {{does not directly precede switch label}}
30
+ do [[fallthrough ]]; while (1 ); // expected-error {{does not directly precede switch label}} \
31
+ // expected-warning {{ISO C does not allow an attribute list to appear here}}
28
32
case 6 :
29
- do [[fallthrough ]]; while (0 ); // expected-error {{does not directly precede switch label}}
33
+ do [[fallthrough ]]; while (0 ); // expected-error {{does not directly precede switch label}} \
34
+ // expected-warning {{ISO C does not allow an attribute list to appear here}}
30
35
case 7 :
31
36
switch (n ) {
32
37
case 0 :
You can’t perform that action at this time.
0 commit comments