Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

May a compiler report errors on unknown attributes? Even with scope?

In N3291 "7.6.1.(3/5) Attribute Syntax and semantics [decl.attr.grammar]" concerning how attributes are written in source code I read

The use of an attribute-scoped-token is conditionally-supported, with implementation-defined behavior.

and

For an attribute-token not specified in this International Standard, the behavior is implementation-defined.

Does this mean, that attribute specifications like

[[ dllexport ]]
[[ hiding ]]
[[ unused ]]
[[ vendor::attrib ]]

may be complained about by a standard conforming compiler? So, it could report an error and stop compilation?

I would have hoped that a compiler should ignore attributes that it does not know how to handle them. Ok, that might be dangerous, because typos in attributes may be overlooked, for example [[ noretrun ]] or [[ carrys_dependencie ]] :-)

But especially namespaces would help here, right? When I compile [[ gcc::mips ]] on an Microsoft compiler that one should be able to ignore that and not reject it, right?

like image 517
towi Avatar asked Sep 14 '11 10:09

towi


2 Answers

C++17 adds an explicit statement that attributes not recognized by an implementation are to be ignored. Before C++17, all attribute behavior was implementation defined, so the implementation could do anything.

Part of the purpose of the attribute specification is so that compiler makers will stop cluttering the language up with special syntax and keywords. To give them a single grammar for specifying these things. It is a way to associate a compiler-defined string with a particular object or definition.

like image 96
Nicol Bolas Avatar answered Nov 05 '22 06:11

Nicol Bolas


[[ vendor::attrib ]]

may be complained about by a standard conforming compiler?

Yes, especially this one should be complained about a standard conforming compiler that doesn't support the conditionally supported behavior (a diagnostic message is required).

However, I'm not sure about the others. The standard contains also this:

... If an attribute-specifier-seq that appertains to some entity or statement contains an attribute that is not allowed to apply to that entity or statement, the program is ill-formed. ...

I believe it could be read permissively, that only those attributes that surely are in violation of the rule make the program ill-formed (ie. the ones that the compiler doesn't know don't), however, I'm not quite sure. Anyway, using implementation-defined behavior does not make the program ill-formed.

Currently, gcc accepts unknown attributes with a warning, so I believe it will work like that with the new syntax, too.

like image 20
jpalecek Avatar answered Nov 05 '22 04:11

jpalecek