llvm / llvm-project

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

[Clang] Specialize diagnostics for std::launder #108160

Open philnik777 opened 1 month ago

philnik777 commented 1 month ago

Clang diagnoses erroneous uses of __builtin_launder, but doesn't produce nice diagnostics when it's used in std::launder currently. __builtin_is_constant_evaluated detects when it's used inside std::is_constant_evaluated to produce nicer diagnostics. It would be great if clang could do this too for __builtin_launder, since it would allow us to remove a few static_asserts inside std::launder in libc++ to get some really nice diagnostics for this.

shafik commented 1 month ago

Do you have a reproducer?

philnik777 commented 1 month ago

https://godbolt.org/z/vso3T86Me

namespace std {
template <class T>
T* launder(T* ptr) noexcept {
  return __builtin_launder(ptr); // expected-error {{function pointer argument to '__builtin_launder' is not allowed}}
}

inline constexpr bool is_constant_evaluated() {
  return __builtin_is_constant_evaluated();
}
}

void func();

auto test() {
  std::launder(&func);

  if constexpr (std::is_constant_evaluated()) {} // expected-warning {{'std::is_constant_evaluated' will always evaluate to 'true' in a manifestly constant-evaluated expression}}
}