Clang accepts non-conforming [[deprecated]] attribute with parenthesized string literal
What code / commands /steps will reproduce the problem? Compile the below sample code for clang std c++17.
[[deprecated(("Use still allowed, but discouraged"))]] int a = 13;
int main() {}
Compiler Explorer link: https://godbolt.org/z/eYWWGjsjM
What is the expected result? As per C++17 standard,
The attribute-token deprecated can be used to mark names and entities whose use is still allowed, but is discouraged for some reason. [ Note: In particular, deprecated is appropriate for names and entities that are deemed obsolescent or unsafe. — end note ] It shall appear at most once in each attribute-list. An attribute-argument-clause may be present and, if present, it shall have the form: ( string-literal ) [ Note: The string-literal in the attribute-argument-clause could be used to explain the rationale for deprecation and/or to suggest a replacing entity. — end note ]
So, to meet the C++17 standard, Clang should throw diagnostic warning or error.
What happens instead?
Both Clang and GCC accept code, and do not emit any diagnostic warning or error. This does not conform to the C++17 standard.
@llvm/issue-subscribers-clang-frontend
Author: Vrukesh Panse (vrukesh)
[[deprecated(("Use still allowed, but discouraged"))]] int a = 13;
int main() {}
Compiler Explorer link: https://godbolt.org/z/eYWWGjsjM
What is the expected result? As per C++17 standard,
The attribute-token deprecated can be used to mark names and entities whose use is still allowed, but is discouraged for some reason. [ Note: In particular, deprecated is appropriate for names and entities that are deemed obsolescent or unsafe. — end note ] It shall appear at most once in each attribute-list. An attribute-argument-clause may be present and, if present, it shall have the form: ( string-literal ) [ Note: The string-literal in the attribute-argument-clause could be used to explain the rationale for deprecation and/or to suggest a replacing entity. — end note ]
So, to meet the C++17 standard, Clang should throw diagnostic warning or error.
What happens instead?
Both Clang and GCC accept code, and do not emit any diagnostic warning or error. This does not conform to the C++17 standard.
This is not correct, if we look at attribute-argument-clause it contains a balanced-token-seq which allows parens.
An attribute-argument-clause may be present and, if present, it shall have the form: ( string-literal )
... makes it look like clang is wrong to accept a double-parenthesized string literal, which I think is what's at issue here.
An attribute-argument-clause may be present and, if present, it shall have the form: ( string-literal )
... makes it look like clang is wrong to accept a double-parenthesized string literal, which I think is what's at issue here.
Yeah, it does look that way although it is weird gcc accepts it till C++23: https://godbolt.org/z/TM8jc711M but rejects in C++26 mode.
The followin logic skips an extra pair of parentheses and was introduced in https://reviews.llvm.org/D156237 as part of P2361 unevaluated string literals.
https://github.com/llvm/llvm-project/blob/e841d5fdb096efd994e1182137b57f77f9ec6ecf/clang/lib/Parse/ParseDecl.cpp#L409-L415
I’m not sure whether this behavior is actually intended by P2361 or not. Also, should the following be supported?
#define MSG (("discouraged"))
[[deprecated(MSG)]] int b;
/cc @shafik @cor3ntin @erichkeane