Skip to content

Commit 1a73654

Browse files
authored
[Clang] disallow attributes after namespace identifier (llvm#121614)
Fixes llvm#121407
1 parent aa0191e commit 1a73654

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,8 @@ Attribute Changes in Clang
553553
- Clang now permits the usage of the placement new operator in ``[[msvc::constexpr]]``
554554
context outside of the std namespace. (#GH74924)
555555

556+
- Clang now disallows the use of attributes after the namespace name. (#GH121407)
557+
556558
Improvements to Clang's diagnostics
557559
-----------------------------------
558560

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,26 +81,16 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
8181

8282
ParsedAttributes attrs(AttrFactory);
8383

84-
auto ReadAttributes = [&] {
85-
bool MoreToParse;
86-
do {
87-
MoreToParse = false;
88-
if (Tok.is(tok::kw___attribute)) {
89-
ParseGNUAttributes(attrs);
90-
MoreToParse = true;
91-
}
92-
if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
84+
while (MaybeParseGNUAttributes(attrs) || isAllowedCXX11AttributeSpecifier()) {
85+
if (isAllowedCXX11AttributeSpecifier()) {
86+
if (getLangOpts().CPlusPlus11)
9387
Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
9488
? diag::warn_cxx14_compat_ns_enum_attribute
9589
: diag::ext_ns_enum_attribute)
9690
<< 0 /*namespace*/;
97-
ParseCXX11Attributes(attrs);
98-
MoreToParse = true;
99-
}
100-
} while (MoreToParse);
101-
};
102-
103-
ReadAttributes();
91+
ParseCXX11Attributes(attrs);
92+
}
93+
}
10494

10595
if (Tok.is(tok::identifier)) {
10696
Ident = Tok.getIdentifierInfo();
@@ -126,7 +116,9 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
126116
}
127117
}
128118

129-
ReadAttributes();
119+
DiagnoseAndSkipCXX11Attributes();
120+
MaybeParseGNUAttributes(attrs);
121+
DiagnoseAndSkipCXX11Attributes();
130122

131123
SourceLocation attrLoc = attrs.Range.getBegin();
132124

clang/test/Parser/namespace-attributes.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,34 @@ namespace __attribute__(()) A
44
{
55
}
66

7-
namespace A __attribute__(())
7+
namespace A __attribute__(()) [[]] // expected-error {{an attribute list cannot appear here}}
88
{
99
}
1010

11-
namespace __attribute__(()) [[]] A
12-
{
13-
}
14-
15-
namespace [[]] __attribute__(()) A
11+
namespace A [[]] __attribute__(()) // expected-error {{an attribute list cannot appear here}}
1612
{
1713
}
1814

19-
namespace A __attribute__(()) [[]]
15+
namespace [[]] A __attribute__(())
2016
{
2117
}
2218

23-
namespace A [[]] __attribute__(())
19+
namespace [[]] __attribute__(()) A
2420
{
2521
}
2622

27-
namespace [[]] A __attribute__(())
23+
namespace __attribute__(()) [[]] A
2824
{
2925
}
3026

31-
namespace __attribute__(()) A [[]]
27+
namespace __attribute__(()) A [[]] // expected-error {{an attribute list cannot appear here}}
3228
{
3329
}
3430

35-
namespace A::B __attribute__(()) // expected-error{{attributes cannot be specified on a nested namespace definition}}
31+
namespace A::B __attribute__(()) // expected-error {{attributes cannot be specified on a nested namespace definition}}
3632
{
3733
}
3834

39-
namespace __attribute__(()) A::B // expected-error{{attributes cannot be specified on a nested namespace definition}}
35+
namespace __attribute__(()) A::B // expected-error {{attributes cannot be specified on a nested namespace definition}}
4036
{
4137
}

0 commit comments

Comments
 (0)