Skip to content

[clang][bytecode] Don't diagnose const extern reads in CPCE mode #137285

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

Merged
merged 1 commit into from
Apr 25, 2025
Merged
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
10 changes: 5 additions & 5 deletions clang/lib/AST/ByteCode/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ Descriptor::Descriptor(const DeclTy &D, PrimType Type, MetadataSize MD,

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

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ struct Descriptor final {
bool IsConst, bool IsTemporary, bool IsMutable);

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

/// Allocates a descriptor for an array of composites.
Expand Down
14 changes: 8 additions & 6 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,12 @@ bool CheckExtern(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
(Ptr.getDeclDesc()->asVarDecl() == S.EvaluatingDecl))
return true;

if (!S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus) {
const auto *VD = Ptr.getDeclDesc()->asValueDecl();
diagnoseNonConstVariable(S, OpPC, VD);
}
if (S.checkingPotentialConstantExpression() && S.getLangOpts().CPlusPlus &&
Ptr.isConst())
return false;

const auto *VD = Ptr.getDeclDesc()->asValueDecl();
diagnoseNonConstVariable(S, OpPC, VD);
return false;
}

Expand Down Expand Up @@ -740,12 +742,12 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
AccessKinds AK) {
if (!CheckLive(S, OpPC, Ptr, AK))
return false;
if (!CheckExtern(S, OpPC, Ptr))
return false;
if (!CheckConstant(S, OpPC, Ptr))
return false;
if (!CheckDummy(S, OpPC, Ptr, AK))
return false;
if (!CheckExtern(S, OpPC, Ptr))
return false;
if (!CheckRange(S, OpPC, Ptr, AK))
return false;
if (!CheckActive(S, OpPC, Ptr, AK))
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ByteCode/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ unsigned Program::getOrCreateDummy(const DeclTy &D) {

Descriptor *Desc;
if (std::optional<PrimType> T = Ctx.classify(QT))
Desc = createDescriptor(D, *T, nullptr, std::nullopt, /*IsTemporary=*/true,
/*IsMutable=*/false);
Desc = createDescriptor(D, *T, /*SourceTy=*/nullptr, std::nullopt,
/*IsConst=*/QT.isConstQualified());
else
Desc = createDescriptor(D, QT.getTypePtr(), std::nullopt,
/*IsTemporary=*/true, /*IsMutable=*/false);
/*IsConst=*/QT.isConstQualified());
if (!Desc)
Desc = allocateDescriptor(D);

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