llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang accepts duplicate maybe_unused without diagnostic

Open vrukesh opened this issue 10 months ago • 11 comments

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.

vrukesh avatar Jan 15 '26 11:01 vrukesh

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

shafik avatar Jan 15 '26 16:01 shafik

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'.

erichkeane avatar Jan 15 '26 16:01 erichkeane

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:

  1. 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.
  2. 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.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. 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.

llvmbot avatar Jan 15 '26 16:01 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Vrukesh Panse (vrukesh)

**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.

llvmbot avatar Jan 15 '26 16:01 llvmbot

Hi, could I be assigned this issue if that's okay? I'll reach out in this thread if I have questions.

dhr412 avatar Jan 15 '26 17:01 dhr412

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.

shafik avatar Jan 15 '26 17:01 shafik

Hey @shafik @erichkeane , I've just raised a PR addressing this issue. Could you please review the changes?

Thanks!

0bVdnt avatar Jan 15 '26 21:01 0bVdnt

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.

keinflue avatar Jan 16 '26 01:01 keinflue

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.

AaronBallman avatar Jan 16 '26 13:01 AaronBallman

Ooof I missed that CWG issue.

Here's the PR at least: https://github.com/llvm/llvm-project/pull/176252

erichkeane avatar Jan 16 '26 14:01 erichkeane

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.

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...).

erichkeane avatar Jan 16 '26 14:01 erichkeane