[clang] [clang][bytecode] Return succeess for pointers to locals (PR #151980)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 4 07:53:12 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/151980
They aren't valid, but we leave them successful here and CheckLValueConstantExpression will diagnose them later.
>From f1d8191ce663a4b4a04334a5bea1e7881f958ab3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 4 Aug 2025 16:51:51 +0200
Subject: [PATCH] [clang][bytecode] Return succeess for pointers to locals
They aren't valid, but we leave them successful here and
CheckLValueConstantExpression will diagnose them later.
---
clang/lib/AST/ByteCode/EvalEmitter.cpp | 8 ++++++++
clang/test/AST/ByteCode/cxx11.cpp | 7 +++++++
2 files changed, 15 insertions(+)
diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cpp
index fd7f3423c0f9b..408688c21fc5b 100644
--- a/clang/lib/AST/ByteCode/EvalEmitter.cpp
+++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp
@@ -233,6 +233,14 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
return false;
}
} else {
+ // If this is pointing to a local variable, just return
+ // the result, even if the pointer is dead.
+ // This will later be diagnosed by CheckLValueConstantExpression.
+ if (Ptr.isBlockPointer() && !Ptr.block()->isStatic()) {
+ EvalResult.setValue(Ptr.toAPValue(Ctx.getASTContext()));
+ return true;
+ }
+
if (!Ptr.isLive() && !Ptr.isTemporary())
return false;
diff --git a/clang/test/AST/ByteCode/cxx11.cpp b/clang/test/AST/ByteCode/cxx11.cpp
index 8a125a497c649..f5a2abbbe7813 100644
--- a/clang/test/AST/ByteCode/cxx11.cpp
+++ b/clang/test/AST/ByteCode/cxx11.cpp
@@ -301,4 +301,11 @@ namespace NonConstLocal {
}
}
+#define ATTR __attribute__((require_constant_initialization))
+int somefunc() {
+ const int non_global = 42; // both-note {{declared here}}
+ ATTR static const int &local_init = non_global; // both-error {{variable does not have a constant initializer}} \
+ // both-note {{required by}} \
+ // both-note {{reference to 'non_global' is not a constant expression}}
+}
More information about the cfe-commits
mailing list