Skip to content

Commit 5eca2dd

Browse files
authored
[clang][bytecode] Don't diagnose const extern reads in CPCE mode (#137285)
They might become constexpr later.
1 parent 205d399 commit 5eca2dd

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

clang/lib/AST/ByteCode/Descriptor.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,13 @@ Descriptor::Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD,
358358

359359
/// Primitive unknown-size arrays.
360360
Descriptor::Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD,
361-
bool IsTemporary, UnknownSize)
361+
bool IsTemporary, bool IsConst, UnknownSize)
362362
: Source(D), ElemSize(primSize(Type)), Size(UnknownSizeMark),
363363
MDSize(MD.value_or(0)),
364-
AllocSize(MDSize + sizeof(InitMapPtr) + alignof(void *)), IsConst(true),
365-
IsMutable(false), IsTemporary(IsTemporary), IsArray(true),
366-
CtorFn(getCtorArrayPrim(Type)), DtorFn(getDtorArrayPrim(Type)),
367-
MoveFn(getMoveArrayPrim(Type)) {
364+
AllocSize(MDSize + sizeof(InitMapPtr) + alignof(void *)),
365+
IsConst(IsConst), IsMutable(false), IsTemporary(IsTemporary),
366+
IsArray(true), CtorFn(getCtorArrayPrim(Type)),
367+
DtorFn(getDtorArrayPrim(Type)), MoveFn(getMoveArrayPrim(Type)) {
368368
assert(Source && "Missing source");
369369
}
370370

clang/lib/AST/ByteCode/Descriptor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct Descriptor final {
184184
bool IsConst, bool IsTemporary, bool IsMutable);
185185

186186
/// Allocates a descriptor for an array of primitives of unknown size.
187-
Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize,
187+
Descriptor(const DeclTy &D, PrimType Type, MetadataSize MDSize, bool IsConst,
188188
bool IsTemporary, UnknownSize);
189189

190190
/// Allocates a descriptor for an array of composites.

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,12 @@ bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
396396
(Ptr.getDeclDesc()->asVarDecl() == S.EvaluatingDecl))
397397
return true;
398398

399-
if (!S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus) {
400-
const auto *VD = Ptr.getDeclDesc()->asValueDecl();
401-
diagnoseNonConstVariable(S, OpPC, VD);
402-
}
399+
if (S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus &&
400+
Ptr.isConst())
401+
return false;
402+
403+
const auto *VD = Ptr.getDeclDesc()->asValueDecl();
404+
diagnoseNonConstVariable(S, OpPC, VD);
403405
return false;
404406
}
405407

@@ -740,12 +742,12 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
740742
AccessKinds AK) {
741743
if (!CheckLive(S, OpPC, Ptr, AK))
742744
return false;
745+
if (!CheckExtern(S, OpPC, Ptr))
746+
return false;
743747
if (!CheckConstant(S, OpPC, Ptr))
744748
return false;
745749
if (!CheckDummy(S, OpPC, Ptr, AK))
746750
return false;
747-
if (!CheckExtern(S, OpPC, Ptr))
748-
return false;
749751
if (!CheckRange(S, OpPC, Ptr, AK))
750752
return false;
751753
if (!CheckActive(S, OpPC, Ptr, AK))

clang/lib/AST/ByteCode/Program.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ unsigned Program::getOrCreateDummy(const DeclTy &D) {
166166

167167
Descriptor *Desc;
168168
if (std::optional<PrimType> T = Ctx.classify(QT))
169-
Desc = createDescriptor(D, *T, nullptr, std::nullopt, /*IsTemporary=*/true,
170-
/*IsMutable=*/false);
169+
Desc = createDescriptor(D, *T, /*SourceTy=*/nullptr, std::nullopt,
170+
/*IsConst=*/QT.isConstQualified());
171171
else
172172
Desc = createDescriptor(D, QT.getTypePtr(), std::nullopt,
173-
/*IsTemporary=*/true, /*IsMutable=*/false);
173+
/*IsConst=*/QT.isConstQualified());
174174
if (!Desc)
175175
Desc = allocateDescriptor(D);
176176

@@ -431,7 +431,7 @@ Descriptor *Program::createDescriptor(const DeclTy &D, const Type *Ty,
431431
if (isa<IncompleteArrayType>(ArrayType) ||
432432
isa<VariableArrayType>(ArrayType)) {
433433
if (std::optional<PrimType> T = Ctx.classify(ElemTy)) {
434-
return allocateDescriptor(D, *T, MDSize, IsTemporary,
434+
return allocateDescriptor(D, *T, MDSize, IsConst, IsTemporary,
435435
Descriptor::UnknownSize{});
436436
} else {
437437
const Descriptor *Desc = createDescriptor(

0 commit comments

Comments
 (0)