[clang] [Clang][CodeGen] Bail out on constexpr unknown values in ConstantEmitter (PR #127525)

Yingwei Zheng via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 20 19:43:53 PST 2025


dtcxzyw wrote:

> Does it even make sense for evaluateValue to return a constexpr-unknown value? If I'm following correctly, we can't actually do anything useful with such a result: we don't actually know what the value is. It would make things simpler if we kept such APValues as an internal implementation detail of ExprConstant.

Clang rejects the following case after modifying `evaluateValue` to treat constexpr-unknown value as invalid: https://godbolt.org/z/5K9vvnscT

```
struct Bottom { constexpr Bottom() {} };
struct Base : Bottom {
  constexpr Base(int a = 42, const char *b = "test") : a(a), b(b) {}
  int a;
  const char *b;
};
struct Base2 : Bottom {
  constexpr Base2(const int &r) : r(r) {}
  int q = 123;
  const int &r;
};
struct Derived : Base, Base2 {
  constexpr Derived() : Base(76), Base2(a) {}
  int c = r + b[1];
};

constexpr Derived derived;
constexpr Bottom &bot2 = (Base2&)derived;
constexpr Base2 &ok2 = (Base2&)bot2;
```
```
File /home/dtcxzyw/WorkSpace/Projects/compilers/llvm-project/clang/test/SemaCXX/constant-expression-cxx11.cpp Line 936: constexpr variable 'ok2' must be initialized by a constant expression
```


https://github.com/llvm/llvm-project/pull/127525


More information about the cfe-commits mailing list