Skip to content

Commit 9010db1

Browse files
dtcxzywtstellar
authored andcommitted
[Clang] Treat constexpr-unknown value as invalid in EvaluateAsInitializer (llvm#128409)
It is an alternative to llvm#127525. Close llvm#127475.
1 parent 32ce5b0 commit 9010db1

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
}
@@ -17000,6 +16998,18 @@ bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,
1700016998

1700116999
if (!Info.discardCleanups())
1700217000
llvm_unreachable("Unhandled cleanup; missing full expression marker?");
17001+
17002+
if (Value.allowConstexprUnknown()) {
17003+
assert(Value.isLValue() && "Expected an lvalue");
17004+
auto Base = Value.getLValueBase();
17005+
const auto *NewVD = Base.dyn_cast<const ValueDecl *>();
17006+
if (!NewVD)
17007+
NewVD = VD;
17008+
Info.FFDiag(getExprLoc(), diag::note_constexpr_var_init_non_constant, 1)
17009+
<< NewVD;
17010+
NoteLValueLocation(Info, Base);
17011+
return false;
17012+
}
1700317013
}
1700417014

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

clang/lib/CodeGen/CGExprConstant.cpp

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

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

18871890
return nullptr;
18881891
}
+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)