Skip to content

release/20.x: [Clang] Treat constexpr-unknown value as invalid in EvaluateAsInitializer (#128409) #129836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3628,8 +3628,6 @@ static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
if (AllowConstexprUnknown) {
if (!Result)
Result = &Info.CurrentCall->createConstexprUnknownAPValues(VD, Base);
else
Result->setConstexprUnknown();
}
return true;
}
Expand Down Expand Up @@ -17000,6 +16998,18 @@ bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,

if (!Info.discardCleanups())
llvm_unreachable("Unhandled cleanup; missing full expression marker?");

if (Value.allowConstexprUnknown()) {
assert(Value.isLValue() && "Expected an lvalue");
auto Base = Value.getLValueBase();
const auto *NewVD = Base.dyn_cast<const ValueDecl *>();
if (!NewVD)
NewVD = VD;
Info.FFDiag(getExprLoc(), diag::note_constexpr_var_init_non_constant, 1)
<< NewVD;
NoteLValueLocation(Info, Base);
return false;
}
}

return CheckConstantExpression(Info, DeclLoc, DeclTy, Value,
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/CodeGen/CGExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,8 +1881,11 @@ llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {

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

return nullptr;
}
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CodeGenCXX/cxx23-p2280r4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++23 %s -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %s -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++17 %s -emit-llvm -o - | FileCheck %s

extern int& s;

// CHECK: @_Z4testv()
// CHECK-NEXT: entry:
// CHECK-NEXT: [[I:%.*]] = alloca ptr, align {{.*}}
// CHECK-NEXT: [[X:%.*]] = load ptr, ptr @s, align {{.*}}
// CHECK-NEXT: store ptr [[X]], ptr [[I]], align {{.*}}
int& test() {
auto &i = s;
return i;
}
Loading