[clang] [clang][bytecode] Don't diagnose const extern reads in CPCE mode (PR #137285)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 24 23:09:12 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/137285
They might become constexpr later.
>From af0ed78bbab6b944d51b35ab2d2055c9f781662c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 25 Apr 2025 07:40:30 +0200
Subject: [PATCH] [clang][bytecode] Don't diagnose const extern reads in CPCE
mode
They might become constexpr later.
---
clang/lib/AST/ByteCode/Descriptor.cpp | 10 +++++-----
clang/lib/AST/ByteCode/Descriptor.h | 2 +-
clang/lib/AST/ByteCode/Interp.cpp | 14 ++++++++------
clang/lib/AST/ByteCode/Program.cpp | 8 ++++----
4 files changed, 18 insertions(+), 16 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Descriptor.cpp b/clang/lib/AST/ByteCode/Descriptor.cpp
index 6bc51596f441f..fc389e1c18c66 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -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");
}
diff --git a/clang/lib/AST/ByteCode/Descriptor.h b/clang/lib/AST/ByteCode/Descriptor.h
index 532b407c2c788..251443475efc8 100644
--- a/clang/lib/AST/ByteCode/Descriptor.h
+++ b/clang/lib/AST/ByteCode/Descriptor.h
@@ -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.
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 23fd894bd6a3e..9d7cea0de0182 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -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;
}
@@ -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))
diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index fdb7b960ff06d..2d9ed58effe16 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -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);
@@ -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(
More information about the cfe-commits
mailing list