[PATCH] D97362: [clang][parser] Allow attributes in explicit template instantiations

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 23 23:59:18 PST 2021


tbaeder created this revision.
tbaeder added a reviewer: aaron.ballman.
Herald added a subscriber: jdoerfert.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This call to ProhibitAttributes() is almost always dead code.

For GNU attributes, it always is, even though they are being used in explicit template instantiations in, e.g. in `clang/test/CodeGenCXX/visibility.cpp`:

  #define DEFAULT __attribute__((visibility("default")))
  namespace PR11690 {
    template<class T> struct Class {
      void size() const {
      }
    };
    template class DEFAULT Class<char>;
  }

This code is accepted by current `g++` and `clang++`, even though the code claims to reject it because of the attributes.

If the attributes are changed to C++ attributes however, `clang++` _does_ reject it:

  #define DEFAULT [[]]
  namespace PR11690 {
    template<class T> struct Class {
      void size() const {
      }
    };
    template class DEFAULT Class<char>;
  }

But `g++` still accepts the above code. Simply ignoring the attributes does not work either as it breaks tests (e.g. the visibility test from above).

I am not sure why or if GNU-style and C++ attributes should be handled differently here, so I am open for a discussion but this call seems mislead to me.

Thanks,
Timm


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97362

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


Index: clang/test/Parser/cxx0x-attributes.cpp
===================================================================
--- clang/test/Parser/cxx0x-attributes.cpp
+++ clang/test/Parser/cxx0x-attributes.cpp
@@ -178,7 +178,7 @@
 struct [[]] N::S s; // expected-error {{an attribute list cannot appear here}}
 struct [[]] Template<int> t; // expected-error {{an attribute list cannot appear here}}
 struct [[]] ::template Template<int> u; // expected-error {{an attribute list cannot appear here}}
-template struct [[]] Template<char>; // expected-error {{an attribute list cannot appear here}}
+template struct [[]] Template<char>;
 template <> struct [[]] Template<void>;
 
 enum [[]] E1 {};
Index: clang/lib/Parse/ParseDeclCXX.cpp
===================================================================
--- clang/lib/Parse/ParseDeclCXX.cpp
+++ clang/lib/Parse/ParseDeclCXX.cpp
@@ -1824,8 +1824,6 @@
     } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
         TUK == Sema::TUK_Declaration) {
       // This is an explicit instantiation of a class template.
-      ProhibitAttributes(attrs);
-
       TagOrTempResult = Actions.ActOnExplicitInstantiation(
           getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
           TagType, StartLoc, SS, TemplateId->Template,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97362.325998.patch
Type: text/x-patch
Size: 1306 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210224/4d9073d1/attachment.bin>


More information about the cfe-commits mailing list