[PATCH] D121245: [clang][parser] Allow GNU attributes before namespace identifier

Aleksandr Platonov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 8 12:37:32 PST 2022


ArcsinX created this revision.
ArcsinX added reviewers: aaron.ballman, rsmith.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
ArcsinX requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121245

Files:
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/test/Parser/namespace-attributes.cpp


Index: clang/test/Parser/namespace-attributes.cpp
===================================================================
--- /dev/null
+++ clang/test/Parser/namespace-attributes.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -std=c++17 -verify %s
+
+namespace __attribute__((visibility("hidden"))) A
+{
+}
+
+namespace B __attribute__((visibility("hidden")))
+{
+}
+
+namespace C::D __attribute__((visibility("hidden"))) // expected-error{{attributes cannot be specified on a nested namespace definition}}
+{
+}
+
+namespace __attribute__((visibility("hidden"))) E::F // expected-error{{attributes cannot be specified on a nested namespace definition}}
+{
+}
Index: clang/lib/Parse/ParseDeclCXX.cpp
===================================================================
--- clang/lib/Parse/ParseDeclCXX.cpp
+++ clang/lib/Parse/ParseDeclCXX.cpp
@@ -75,6 +75,17 @@
 
   ParsedAttributesWithRange attrs(AttrFactory);
   SourceLocation attrLoc;
+
+  auto ReadLabelAttrubutes = [&] {
+    // Read label attributes, if present.
+    if (Tok.is(tok::kw___attribute)) {
+      attrLoc = Tok.getLocation();
+      ParseGNUAttributes(attrs);
+    }
+  };
+
+  ReadLabelAttrubutes();
+
   if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
     Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
                                 ? diag::warn_cxx14_compat_ns_enum_attribute
@@ -108,16 +119,12 @@
     }
   }
 
+  ReadLabelAttrubutes();
+
   // A nested namespace definition cannot have attributes.
   if (!ExtraNSs.empty() && attrLoc.isValid())
     Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
 
-  // Read label attributes, if present.
-  if (Tok.is(tok::kw___attribute)) {
-    attrLoc = Tok.getLocation();
-    ParseGNUAttributes(attrs);
-  }
-
   if (Tok.is(tok::equal)) {
     if (!Ident) {
       Diag(Tok, diag::err_expected) << tok::identifier;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121245.413910.patch
Type: text/x-patch
Size: 1861 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220308/4e6b9464/attachment.bin>


More information about the cfe-commits mailing list