Clang accepts duplicate maybe_unused without diagnostic
What code / commands /steps will reproduce the problem? Compile the sample code:
_int foo([[maybe_unused, maybe_unused]] int a) {
return 1;
}
int main() {
return foo(3);
}
Compiler Explorer link: https://godbolt.org/z/67EWeqYnE
What is the expected result? As per C++17 standard, The attribute-token maybe_unused indicates that a name or entity is possibly intentionally unused. It shall appear at most once in each attribute-list and no attribute-argument-clause shall be present.
What happens instead?
GCC throws diagnostic warning. It meets C++17 standard. Clang passes the compilation without any diagnostic warning or error. This doesnot meet C++17 standard.
To meet C++17 standard, Clang should diagnose the duplicate maybe_unused attribute with diagnostic warning or error.
We do this w/ multiple attributes: https://godbolt.org/z/6M4Tvs7Yq
EDG also seems to allow this.
int foo(int a) {
switch(a) {
case 1:
[[fallthrough,fallthrough]];
case 2:
break;
}
return 1;
}
[[noreturn,noreturn]] void g() {
__builtin_unreachable();
}
CC @erichkeane
Accepting this multiple times is a reasonable extension :) We typically allow things like this because folks get silly with macros.
That said, I'd be open to a warning here, and probably one that applies to all of the standard attributes without arguments. This should be reasonably easy, as all the work is in SemaDeclAttr.cpp (and perhaps in StmtAttr?), so this sounds like a 'good first issue'.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Vrukesh Panse (vrukesh)
_int foo([[maybe_unused, maybe_unused]] int a) {
return 1;
}
int main() {
return foo(3);
}
Compiler Explorer link: https://godbolt.org/z/67EWeqYnE
What is the expected result? As per C++17 standard, The attribute-token maybe_unused indicates that a name or entity is possibly intentionally unused. It shall appear at most once in each attribute-list and no attribute-argument-clause shall be present.
What happens instead?
GCC throws diagnostic warning. It meets C++17 standard. Clang passes the compilation without any diagnostic warning or error. This doesnot meet C++17 standard.
To meet C++17 standard, Clang should diagnose the duplicate maybe_unused attribute with diagnostic warning or error.
Hi, could I be assigned this issue if that's okay? I'll reach out in this thread if I have questions.
Hi, could I be assigned this issue if that's okay? I'll reach out in this thread if I have questions.
We assign issue after a PR is created and we make sure the PR is heading in the right direction.
Hey @shafik @erichkeane , I've just raised a PR addressing this issue. Could you please review the changes?
Thanks!
I don't know whether it is intended to be a DR, but this was changed with https://cplusplus.github.io/CWG/issues/1914.html / https://github.com/cplusplus/papers/issues/864.
Assuming this should be a DR, there shouldn't be a warning by default IMO. It might still be helpful though as a warning the user can enable.
It's not been changed because EWG still hasn't processed the issue in the 12 years since I filed it. Based on the amount of time that's passed, I feel comfortable just asserting a direction. I don't think duplicate standard attributes should be diagnosed in general; there is no harm in [[noreturn, noreturn]] or [[noreturn]] [[noreturn]], they specify the same information and this situation can arise through macro shenanigans. One way I've seen that happen is stuff where people do:
#define FOO [[whatever, another_one]]
#define BAR [[another_one]]
FOO BAR int x; // Oops, duplicated another_one
The one case I think is worth thinking about is:
[[deprecated("foo"), deprecated("bar")]] void func();
but honestly, I'm not certain that telling the user the messages are different is the best thing. For starters, the semantics are the same either way -- the user gets a diagnostic with a message in it. Same happens for nodiscard. Maybe the better approach is to show both messages instead of warning about the duplication?
And now that I think about it, there are standard attributes where a duplicate with different arguments is fine. e.g.,
[[assume(x), assume(y)]];
So yeah, I'm not certain we should make any changes here yet.
Ooof I missed that CWG issue.
Here's the PR at least: https://github.com/llvm/llvm-project/pull/176252
It's not been changed because EWG still hasn't processed the issue in the 12 years since I filed it. Based on the amount of time that's passed, I feel comfortable just asserting a direction. I don't think duplicate standard attributes should be diagnosed in general; there is no harm in
[[noreturn, noreturn]]or[[noreturn]] [[noreturn]], they specify the same information and this situation can arise through macro shenanigans. One way I've seen that happen is stuff where people do:#define FOO [[whatever, another_one]] #define BAR [[another_one]] FOO BAR int x; // Oops, duplicated another_oneThe one case I think is worth thinking about is:
[[deprecated("foo"), deprecated("bar")]] void func();but honestly, I'm not certain that telling the user the messages are different is the best thing. For starters, the semantics are the same either way -- the user gets a diagnostic with a message in it. Same happens for
nodiscard. Maybe the better approach is to show both messages instead of warning about the duplication?And now that I think about it, there are standard attributes where a duplicate with different arguments is fine. e.g.,
[[assume(x), assume(y)]];So yeah, I'm not certain we should make any changes here yet.
SO.... yeah, the paper I wrote and you presented in SG22 removes these lines as DRs, so I'm going to say this is not a valid bug anymore. I don't see the value of the warning (and agreed above as a 'its harmless and technically the standard?'... but its no longer hte standard, so...).