[clang] [clang][bytecode] Register global constexpr-unknown variables with their pointee type (PR #201347)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 3 05:52:35 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/201347.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+3)
- (modified) clang/lib/AST/ByteCode/Program.cpp (+6)
- (modified) clang/test/SemaCXX/constant-expression-p2280r4.cpp (+1-2)
``````````diff
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index ee3813a9287be..501c7f76a0376 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -370,6 +370,9 @@ static bool interp__builtin_strlen(InterpState &S, CodePtr OpPC,
if (ID == Builtin::BIstrlen || ID == Builtin::BIwcslen)
diagnoseNonConstexprBuiltin(S, OpPC, ID);
+ if (StrPtr.isConstexprUnknown())
+ return false;
+
if (!CheckArray(S, OpPC, StrPtr))
return false;
diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index 1f251fef8a36d..7a6eeb4588117 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -249,6 +249,12 @@ UnsignedOrNone Program::createGlobal(const DeclTy &D, QualType Ty,
bool IsStatic, bool IsExtern, bool IsWeak,
bool IsConstexprUnknown,
const Expr *Init) {
+ // Since this global variable is constexpr-unknown and a reference, register
+ // the pointee type instead. When referencing the variable, the pointer will
+ // then be of the pointee type instead of just PT_Ptr.
+ if (Ty->isReferenceType() && IsConstexprUnknown)
+ Ty = Ty->getPointeeType();
+
// Create a descriptor for the global.
Descriptor *Desc;
const bool IsConst = Ty.isConstQualified();
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index 649ed41ca58c4..90f8458609680 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -371,8 +371,7 @@ namespace enable_if_2 {
namespace GH150015 {
extern int (& c)[8];
- constexpr int x = c <= c+8; // interpreter-error {{constexpr variable 'x' must be initialized by a constant expression}} \
- // interpreter-note {{cannot refer to element 8 of non-array object in a constant expression}}
+ constexpr int x = c <= c+8;
struct X {};
struct Y {};
``````````
</details>
https://github.com/llvm/llvm-project/pull/201347
More information about the cfe-commits
mailing list