Skip to content

Template arguments can't be used in attributes #67154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
abrachet opened this issue Sep 22, 2023 · 11 comments · May be fixed by #67376
Open

Template arguments can't be used in attributes #67154

abrachet opened this issue Sep 22, 2023 · 11 comments · May be fixed by #67376
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" good first issue https://github.com/llvm/llvm-project/contribute

Comments

@abrachet
Copy link
Member

abrachet commented Sep 22, 2023

constexpr int constexpr_var = 101;
[[gnu::constructor(constexpr_var)]] void a() {}

template <int TemplateArg>
[[gnu::constructor(TemplateArg)]] void b() {}

template void b<101>();

This code compiles fine with gcc, and clang can successfully compile a but errors on b with "'constructor' attribute requires an integer constant".

Note clang doesn't error on the instantiation of b but rather on the template definition. so even if the template void b<101>(); line is removed clang still gives an error.

@abrachet abrachet added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Sep 22, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2023

@llvm/issue-subscribers-clang-frontend

``` constexpr int constexpr_var = 101; [[gnu::constructor(constexpr_var)]] void a() {}

template <int TemplateArg>
[[gnu::constructor(TemplateArg)]] void b() {}

template void b<101>();

This code compiles fine with gcc, and clang can successfully compile `a` but errors on `b` with "'constructor' attribute requires an integer constant".

Note clang doesn't error on the instantiation of b but rather on the template definition. so even if the `template void b&lt;101&gt;();` line is removed clang still gives an error.
</details>

@shafik
Copy link
Collaborator

shafik commented Sep 22, 2023

It looks like the diagnostic is from err_attribute_argument_type but not sure what the trigger code is right now.

CC @erichkeane @AaronBallman who may know if this is a purposeful restriction or not.

@abrachet
Copy link
Member Author

It's in handleConstructorAttr which eventually bottoms out in getIntegerConstantExpr

@erichkeane
Copy link
Collaborator

It's in handleConstructorAttr which eventually bottoms out in getIntegerConstantExpr

Was just confirming that :) yeah, unfortunately the infrastructure to have attributes work with templates is pretty manual, so each individual attribute has to be programmed to work with template arguments individually. In this case, the handleConstructorAttr isn't template-aware, so it is just looking for an integral argument. You have to change the definition in Attr.td to be an ExprArgument, then set TemplateDependent, then I think the function gets re-called with the instantiated version.

There's quite a few examples on how to do it, but GuardedBy is pretty easy to copy, as this should be. Basically, the 'handle' function will be called 1x for uninstantiated, then 1x for each instantiation, and can be re-checked. This isn't a bad 'beginner' bug if this is important to you.

@shafik shafik added the good first issue https://github.com/llvm/llvm-project/contribute label Sep 22, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 22, 2023

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

```c++ constexpr int constexpr_var = 101; [[gnu::constructor(constexpr_var)]] void a() {}

template <int TemplateArg>
[[gnu::constructor(TemplateArg)]] void b() {}

template void b<101>();

This code compiles fine with gcc, and clang can successfully compile `a` but errors on `b` with "'constructor' attribute requires an integer constant".

Note clang doesn't error on the instantiation of b but rather on the template definition. so even if the `template void b&lt;101&gt;();` line is removed clang still gives an error.
</details>

@jmintb
Copy link

jmintb commented Oct 2, 2023

Can I have a go at this? :)

@cor3ntin
Copy link
Contributor

cor3ntin commented Oct 2, 2023

@jmintb Of course!

@erichkeane
Copy link
Collaborator

@jmintb : It actually looks like @abrachet has a review up to do this. See above.

@jmintb
Copy link

jmintb commented Oct 2, 2023

Ah yes I missed that, my bad

@abrachet
Copy link
Member Author

@jmintb sorry for the late reply. Feel free to commandeer that patch. I likely won't have time to look at it for a little while

@erichkeane
Copy link
Collaborator

Unassigning as @jmintb seems to have not made progress on this in more than a year. Feel free to ask for reassignment if you wish to pick this up/pick this up again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" good first issue https://github.com/llvm/llvm-project/contribute
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants