Skip to content

Commit 974bda8

Browse files
authored
[clang][bytecode] Reject constexpr-unknown pointers from Inc ops (#135548)
We used to accept c++ as a known value here, causing wrong codegen.
1 parent dd107b2 commit 974bda8

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

clang/lib/AST/ByteCode/Interp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ bool isConstexprUnknown(const Pointer &P) {
307307
if (P.isDummy())
308308
return false;
309309
const VarDecl *VD = P.block()->getDescriptor()->asVarDecl();
310-
return VD && VD->hasLocalStorage();
310+
return VD && VD->hasLocalStorage() && !isa<ParmVarDecl>(VD);
311311
}
312312

313313
bool CheckBCPResult(InterpState &S, const Pointer &Ptr) {

clang/lib/AST/ByteCode/Interp.h

+5
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,11 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
771771
bool CanOverflow) {
772772
assert(!Ptr.isDummy());
773773

774+
if (!S.inConstantContext()) {
775+
if (isConstexprUnknown(Ptr))
776+
return false;
777+
}
778+
774779
if constexpr (std::is_same_v<T, Boolean>) {
775780
if (!S.getLangOpts().CPlusPlus14)
776781
return Invalid(S, OpPC);

clang/test/AST/ByteCode/codegen.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s | FileCheck %s
22
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm -o - %s -fexperimental-new-constant-interpreter | FileCheck %s
33

44
#ifdef __SIZEOF_INT128__
@@ -95,3 +95,12 @@ void f(A *a) {
9595
// CHECK: call void @_ZN1AD1Ev(
9696
A::E e3 = A().Foo;
9797
}
98+
99+
int notdead() {
100+
auto l = [c=0]() mutable {
101+
return c++ < 5 ? 10 : 12;
102+
};
103+
return l();
104+
}
105+
// CHECK: _ZZ7notdeadvEN3$_0clEv
106+
// CHECK: ret i32 %cond

0 commit comments

Comments
 (0)