llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.32k stars 12.11k forks source link

[Clang] [[clang::require_constant_initialization]] seems to be broken in C++03 #115779

Open philnik777 opened 2 weeks ago

philnik777 commented 2 weeks ago
[[clang::require_constant_initialization]]
inline const __INTMAX_TYPE__ value = __INTMAX_MAX__ + 1;

is accepted in C++03 but in no other dialect.

llvmbot commented 2 weeks ago

@llvm/issue-subscribers-clang-frontend

Author: Nikolas Klauser (philnik777)

```c++ [[clang::require_constant_initialization]] inline const __INTMAX_TYPE__ value = __INTMAX_MAX__ + 1; ``` is accepted in C++03 but in no other dialect.
frederick-vs-ja commented 2 weeks ago

This is CWG1313. It's unclear to me whether CWG1313 should be treated as a patch to C++11 constant expression additions or C++98 rules.

AaronBallman commented 2 weeks ago

The logic for this diagnostic lives in Sema::CheckCompleteVariableDeclaration(), and the issue is that we get into if (!getLangOpts().CPlusPlus11 && !getLangOpts().C23) { which sets HasConstInit to true, and we wind up in if (HasConstInit) { and not } else if (GlobalStorage && var->hasAttr<ConstInitAttr>()) {.

zygoloid commented 2 weeks ago

The C++98 constant expression rules are very much like the C ones, and are almost entirely concerned only with the operations performed and not their semantics and the values involved. Given the rules are so different, I don't think it makes sense to apply CWG1313 retroactively.

Do we emit a dynamic initializer for the variable in question? That'd be a clear bug.

AaronBallman commented 2 weeks ago

Do we emit a dynamic initializer for the variable in question? That'd be a clear bug.

No, we don't seem to, at least from my testing.