[cfe-dev] c++ 11 attribute support in clang

Richard Smith richard at metafoo.co.uk
Mon Oct 1 18:16:10 PDT 2012


On Mon, Oct 1, 2012 at 3:56 PM, Michael Han <Michael.Han at autodesk.com>wrote:

> Hi,
>
> Attached patch is trying to improve the C++ 11 generalized attributes
> support in Clang:
>
> - Parse generic C++ 11 attributes into AST instead of parsing and ignoring
> them.
> - Parse attributes arguments if the attributes are in gnu scope.
> - Update several tests since with the unknown attributes introduced to AST
> some expected diagnostics shall be emitted.
>
> This would allow one to extend Clang attributes using C++ 11 attribute
> syntax and migrate existing GNU style attributes with new syntax.
>

We had been deliberately holding back on that, waiting for the GCC guys to
commit to a particular approach. It looks like they're just putting all
their existing attributes into a gnu:: namespace, though, so this is fine:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53528
http://gcc.gnu.org/ml/gcc-patches/2012-07/msg01348.html


> Are there any ideas and plans on adding generalized C++ 11 attribute
> support to Clang? I feel migrating existing GNU attributes to new syntax
> might be a good start. Any feedback are appreciated.


C++11 attributes certainly need more love, and such work would be very much
appreciated. In terms of the standard C++11 pieces, we have a few
deficiencies: We don't accept attributes (and alignas) in quite the right
set of places, nor do we apply them to the right set of entities, we get
the semantics of [[noreturn]] wrong (we use the GNU
__attribute__((noreturn)) semantics, which don't quite match), and we don't
issue an error for an attribute which is applied to an entity to which it
can't appertain.

On to your patch:

+++ lib/Parse/ParseDeclCXX.cpp (working copy)
@@ -2963,46 +2963,35 @@
+      if (ScopeName && ScopeName->getName() == "gnu") {
+        ParseCXX11AttributeGNUStyleArgs(AttrName, AttrLoc, ScopeName,
+                                        ScopeLoc, attrs);

Please add some tests for this.

+        // FIXME: handle other formats of c++11 attribute arguments
+        ConsumeParen();
+        SkipUntil(tok::r_paren, false);

It looks like this silently drops unknown attributes followed by parens.
Can we add them to the list in this case, so we'll get a warning?

[...]
+      if (StandardAttr)
         Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
           << AttrName->getName();

Please reject ellipses for GNU attributes too, for now (with an
'unsupported' error), rather than silently dropping them.

@@ -3014,6 +3003,75 @@
     SkipUntil(tok::r_square, false);
 }

+bool Parser::IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
+                                               IdentifierInfo *ScopeName) {

Make this a static, non-member function.

+void Parser::ParseCXX11AttributeGNUStyleArgs(IdentifierInfo *AttrName,
+                                             SourceLocation AttrNameLoc,
+                                             IdentifierInfo *ScopeName,
+                                             SourceLocation ScopeLoc,
+                                             ParsedAttributes &Attrs) {

Can you share this code between C++11 and GNU attribute parsing?

+++ test/Parser/cxx11-stmt-attributes.cpp (working copy)
@@ -2,53 +2,55 @@

 void foo(int i) {
+  [[unknown_attribute]] ; // expected-warning {{attribute
unknown_attribute cannot be specified on a statement}}

Yuck. Would you be interested in teaching this warning the difference
between 'attribute cannot be specified on a statement' and 'unknown
attribute'?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121001/d3969b83/attachment.html>


More information about the cfe-dev mailing list