diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 93915e5db7d13..125da934caff8 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -553,6 +553,8 @@ Attribute Changes in Clang - Clang now permits the usage of the placement new operator in ``[[msvc::constexpr]]`` context outside of the std namespace. (#GH74924) +- Clang now disallows the use of attributes after the namespace name. (#GH121407) + Improvements to Clang's diagnostics ----------------------------------- diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp index f30603feb65c5..fddb8a5729ee8 100644 --- a/clang/lib/Parse/ParseDeclCXX.cpp +++ b/clang/lib/Parse/ParseDeclCXX.cpp @@ -81,26 +81,16 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context, ParsedAttributes attrs(AttrFactory); - auto ReadAttributes = [&] { - bool MoreToParse; - do { - MoreToParse = false; - if (Tok.is(tok::kw___attribute)) { - ParseGNUAttributes(attrs); - MoreToParse = true; - } - if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) { + while (MaybeParseGNUAttributes(attrs) || isAllowedCXX11AttributeSpecifier()) { + if (isAllowedCXX11AttributeSpecifier()) { + if (getLangOpts().CPlusPlus11) Diag(Tok.getLocation(), getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_ns_enum_attribute : diag::ext_ns_enum_attribute) << 0 /*namespace*/; - ParseCXX11Attributes(attrs); - MoreToParse = true; - } - } while (MoreToParse); - }; - - ReadAttributes(); + ParseCXX11Attributes(attrs); + } + } if (Tok.is(tok::identifier)) { Ident = Tok.getIdentifierInfo(); @@ -126,7 +116,9 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context, } } - ReadAttributes(); + DiagnoseAndSkipCXX11Attributes(); + MaybeParseGNUAttributes(attrs); + DiagnoseAndSkipCXX11Attributes(); SourceLocation attrLoc = attrs.Range.getBegin(); diff --git a/clang/test/Parser/namespace-attributes.cpp b/clang/test/Parser/namespace-attributes.cpp index 9f925b742dfeb..11bf8711cfad5 100644 --- a/clang/test/Parser/namespace-attributes.cpp +++ b/clang/test/Parser/namespace-attributes.cpp @@ -4,38 +4,34 @@ namespace __attribute__(()) A { } -namespace A __attribute__(()) +namespace A __attribute__(()) [[]] // expected-error {{an attribute list cannot appear here}} { } -namespace __attribute__(()) [[]] A -{ -} - -namespace [[]] __attribute__(()) A +namespace A [[]] __attribute__(()) // expected-error {{an attribute list cannot appear here}} { } -namespace A __attribute__(()) [[]] +namespace [[]] A __attribute__(()) { } -namespace A [[]] __attribute__(()) +namespace [[]] __attribute__(()) A { } -namespace [[]] A __attribute__(()) +namespace __attribute__(()) [[]] A { } -namespace __attribute__(()) A [[]] +namespace __attribute__(()) A [[]] // expected-error {{an attribute list cannot appear here}} { } -namespace A::B __attribute__(()) // expected-error{{attributes cannot be specified on a nested namespace definition}} +namespace A::B __attribute__(()) // expected-error {{attributes cannot be specified on a nested namespace definition}} { } -namespace __attribute__(()) A::B // expected-error{{attributes cannot be specified on a nested namespace definition}} +namespace __attribute__(()) A::B // expected-error {{attributes cannot be specified on a nested namespace definition}} { }