This is an archive of the discontinued LLVM Phabricator instance.

[clang][parser] Allow GNU attributes before namespace identifier
ClosedPublic

Authored by ArcsinX on Mar 8 2022, 12:37 PM.

Details

Summary

GCC supports:

  • namespace <gnu attributes> identifier
  • namespace identifier <gnu attributes>

But clang supports only namespace identifier <gnu attributes> and diagnostics for namespace <gnu attributes> identifier case looks unclear:
Code:

namespace __attribute__((visibility("hidden"))) A
{
}

Diags:

test.cpp:1:49: error: expected identifier or '{'
namespace __attribute__((visibility("hidden"))) A
                                                ^
test.cpp:1:49: error: C++ requires a type specifier for all declarations
test.cpp:3:2: error: expected ';' after top level declarator
}

This patch adds support for namespace <gnu attributes> identifier and also forbids gnu attributes for nested namespaces (this already done for C++ attributes).

Diff Detail

Event Timeline

ArcsinX created this revision.Mar 8 2022, 12:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2022, 12:37 PM
Herald added a subscriber: jdoerfert. · View Herald Transcript
ArcsinX requested review of this revision.Mar 8 2022, 12:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2022, 12:37 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Oh wow, GCC has supported this for a long time; I wasn't aware of that. Thanks for this patch!

clang/lib/Parse/ParseDeclCXX.cpp
78

However, I don't think there's a reason we need this lambda -- it seems we can call MaybeParseGNUAttributes() instead, and get the attribute location from the ParsedAttributesWithRange object passed in.

clang/test/Parser/namespace-attributes.cpp
4

Another test case that I think we need is to test attribute parsing order between the different syntaxes. e.g.,

namespace __attribute__(()) [[]] A
{
}

namespace [[]] __attribute__(()) B
{
}

It's not clear to me that we want to match how GCC handles this case: https://godbolt.org/z/e1bjcEje6 but instead handle it more like how Clang handles it in structure declarations: https://godbolt.org/z/drGhb65ET.

ArcsinX updated this revision to Diff 414868.Mar 12 2022, 12:00 PM
  • get attributes location from ParsedAttributesWithRange object
  • parse subsequent attributes of different kinds in a loop
ArcsinX marked an inline comment as done.Mar 12 2022, 12:13 PM
ArcsinX added inline comments.
clang/lib/Parse/ParseDeclCXX.cpp
78

MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs) parses different kind of attributes in a loop (e.g. struct __attribute__(()) [[]] [[]] __attribute__(()) S {}; is valid for clang), but we can't use it because we need to warn about c++11 attributes usage for namespace if c++ < 17. So, I added a lambda which works similar to MaybeParseAttributes().

Thus, now clang handles attributes for namespace like it handle attributes for structure.

aaron.ballman accepted this revision.Mar 14 2022, 12:35 PM

LGTM! Might be worth adding a release note for it (does this close any bugs in the bug database? If so, it'd be worth mentioning those in the commit message and release note).

This revision is now accepted and ready to land.Mar 14 2022, 12:35 PM
ArcsinX updated this revision to Diff 415470.Mar 15 2022, 9:26 AM

Update release notes
Rebase

LGTM! Might be worth adding a release note for it (does this close any bugs in the bug database? If so, it'd be worth mentioning those in the commit message and release note).

Thank you for review.

I failed to find any opened bugs related to this. I'm not sure how detailed information about this patch should be in release notes. Can you please take a look at ReleaseNotes.rst modifications?

aaron.ballman accepted this revision.Mar 15 2022, 9:37 AM

LGTM! Might be worth adding a release note for it (does this close any bugs in the bug database? If so, it'd be worth mentioning those in the commit message and release note).

Thank you for review.

I failed to find any opened bugs related to this. I'm not sure how detailed information about this patch should be in release notes. Can you please take a look at ReleaseNotes.rst modifications?

The release notes look great to me, thank you for adding them!