[clang] [clang][bytecode] Register global constexpr-unknown variables with their pointee type (PR #201347)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 3 05:51:49 PDT 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/201347

None

>From 565413956a0d81835022da3bb4de9f5db134dff5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 3 Jun 2026 06:15:13 +0200
Subject: [PATCH] asdf

---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp           | 3 +++
 clang/lib/AST/ByteCode/Program.cpp                 | 6 ++++++
 clang/test/SemaCXX/constant-expression-p2280r4.cpp | 3 +--
 3 files changed, 10 insertions(+), 2 deletions(-)

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 {};



More information about the cfe-commits mailing list