Skip to content

Commit 27757fb

Browse files
authored
[Clang] Treat constexpr-unknown value as invalid in EvaluateAsInitializer (#128409)
It is an alternative to #127525. Close #127475.
1 parent 736205d commit 27757fb

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

clang/lib/AST/ExprConstant.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -3628,8 +3628,6 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
36283628
if (AllowConstexprUnknown) {
36293629
if (!Result)
36303630
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
3631-
else
3632-
Result->setConstexprUnknown();
36333631
}
36343632
return true;
36353633
}
@@ -16995,6 +16993,18 @@ bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,
1699516993

1699616994
if (!Info.discardCleanups())
1699716995
llvm_unreachable("Unhandled cleanup; missing full expression marker?");
16996+
16997+
if (Value.allowConstexprUnknown()) {
16998+
assert(Value.isLValue() && "Expected an lvalue");
16999+
auto Base = Value.getLValueBase();
17000+
const auto *NewVD = Base.dyn_cast<const ValueDecl *>();
17001+
if (!NewVD)
17002+
NewVD = VD;
17003+
Info.FFDiag(getExprLoc(), diag::note_constexpr_var_init_non_constant, 1)
17004+
<< NewVD;
17005+
NoteLValueLocation(Info, Base);
17006+
return false;
17007+
}
1699817008
}
1699917009

1700017010
return CheckConstantExpression(Info, DeclLoc, DeclTy, Value,

clang/lib/CodeGen/CGExprConstant.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1883,8 +1883,11 @@ llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
18831883

18841884
// Try to emit the initializer. Note that this can allow some things that
18851885
// are not allowed by tryEmitPrivateForMemory alone.
1886-
if (APValue *value = D.evaluateValue())
1886+
if (APValue *value = D.evaluateValue()) {
1887+
assert(!value->allowConstexprUnknown() &&
1888+
"Constexpr unknown values are not allowed in CodeGen");
18871889
return tryEmitPrivateForMemory(*value, destType);
1890+
}
18881891

18891892
return nullptr;
18901893
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++23 %s -emit-llvm -o - | FileCheck %s
2+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %s -emit-llvm -o - | FileCheck %s
3+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++17 %s -emit-llvm -o - | FileCheck %s
4+
5+
extern int& s;
6+
7+
// CHECK: @_Z4testv()
8+
// CHECK-NEXT: entry:
9+
// CHECK-NEXT: [[I:%.*]] = alloca ptr, align {{.*}}
10+
// CHECK-NEXT: [[X:%.*]] = load ptr, ptr @s, align {{.*}}
11+
// CHECK-NEXT: store ptr [[X]], ptr [[I]], align {{.*}}
12+
int& test() {
13+
auto &i = s;
14+
return i;
15+
}

0 commit comments

Comments
 (0)