Skip to content

Commit

Permalink
[Clang] disallow attributes after namespace identifier (llvm#121614)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored Jan 9, 2025
1 parent aa0191e commit 1a73654
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------------------------------

Expand Down
26 changes: 9 additions & 17 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -126,7 +116,9 @@ Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
}
}

ReadAttributes();
DiagnoseAndSkipCXX11Attributes();
MaybeParseGNUAttributes(attrs);
DiagnoseAndSkipCXX11Attributes();

SourceLocation attrLoc = attrs.Range.getBegin();

Expand Down
20 changes: 8 additions & 12 deletions clang/test/Parser/namespace-attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
{
}

0 comments on commit 1a73654

Please sign in to comment.