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

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 27 01:11:28 PST 2021


tbaeder updated this revision to Diff 326882.
tbaeder added a comment.

You are right of course. I changed the diff to allow GNU-style attributes and reject everything else.

This is a bit hairy because `[[]]` is an empty attribute list but the `SourceRange` is still valid. We don't know if the source range was for `[[]]` or `__attribute__(())` though. I opted for simply rejecting the empty GNU-style attribute list anyway.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D97362/new/

https://reviews.llvm.org/D97362

Files:
  clang/include/clang/Parse/Parser.h
  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
@@ -179,6 +179,7 @@
 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 __attribute__((pure)) Template<std::size_t>; // We still allow GNU-style attributes here
 template <> struct [[]] Template<void>;
 
 enum [[]] E1 {};
Index: clang/lib/Parse/ParseDeclCXX.cpp
===================================================================
--- clang/lib/Parse/ParseDeclCXX.cpp
+++ clang/lib/Parse/ParseDeclCXX.cpp
@@ -1824,7 +1824,10 @@
     } else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
         TUK == Sema::TUK_Declaration) {
       // This is an explicit instantiation of a class template.
-      ProhibitAttributes(attrs);
+
+      // Allow only GNU attributes here.
+      if (!attrs.hasOnlyGNUAttributes())
+        ProhibitAttributes(attrs);
 
       TagOrTempResult = Actions.ActOnExplicitInstantiation(
           getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
Index: clang/include/clang/Parse/Parser.h
===================================================================
--- clang/include/clang/Parse/Parser.h
+++ clang/include/clang/Parse/Parser.h
@@ -1581,6 +1581,19 @@
       Range = SourceRange();
     }
 
+    /// Returns whether all attributes are GNU attributes.
+    bool hasOnlyGNUAttributes() const {
+      for (const ParsedAttr &A : *this) {
+        if (A.getSyntax() != ParsedAttr::Syntax::AS_GNU)
+          return false;
+      }
+
+      // If the attr list is empty, but the range is still set, we did parse
+      // some attributes. Return false in this case, even though we might've
+      // parsed [[]].
+      return Range.isInvalid();
+    }
+
     SourceRange Range;
   };
   struct ParsedAttributesViewWithRange : ParsedAttributesView {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97362.326882.patch
Type: text/x-patch
Size: 2165 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210227/dea53efd/attachment.bin>


More information about the cfe-commits mailing list