llvm / llvm/llvm-project

[clang] False positive -Wmissing-variable-declarations with constexpr variable template instantiation

Open
#193,937 2 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics clang:frontend false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Example:

```cpp
template
struct Foo{};

template
constexpr auto x = Foo{};

constexpr auto y = x; // NOK

constexpr int z = 123; // OK
```

Gives:

```
:5:16: warning: no previous extern declaration for non-static variable 'x' [-Wmissing-variable-declarations]
5 | constexpr auto x = Foo{};
| ^
:7:20: note: in instantiation of variable template specialization 'x' requested here
7 | constexpr auto y = x;
| ^
:5:11: note: declare 'static' if the variable is not intended to be used outside of this translation unit
5 | constexpr auto x = Foo{};
| ^
1 warning generated.
```
[Godbolt](https://godbolt.org/z/4oehEsv5h).

`constexpr` variables are implicitly `static`, so the check should not warn here.

PS: it's also strange that it warns on line 5 instead of line 7, since it complains about `x`.

Contributor guide

Open the contributing guide

Research direction

Reproduce the example with Clang using -Wmissing-variable-declarations, then follow how the warning handles constexpr variable-template specializations. Done means this example emits no warning, while ordinary cases that need the diagnostic remain diagnosed and the reported location is appropriate.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.