[clang] [clang][bytecode] Don't emit checkNull for function pointers (PR #164376)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 21 01:26:33 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/164376
Diagnose them later when we try to call the function pointer.
>From e2f4478289e83ca3b9ee7596ae09046949812340 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 21 Oct 2025 10:24:19 +0200
Subject: [PATCH] [clang][bytecode] Don't emit checkNull for function pointers
Diagnose them later when we try to call the function pointer.
---
clang/lib/AST/ByteCode/Compiler.cpp | 2 +-
clang/test/AST/ByteCode/cxx14.cpp | 9 +++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
create mode 100644 clang/test/AST/ByteCode/cxx14.cpp
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 74cae030bb9bb..28b9d84f6a76d 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6633,7 +6633,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
if (!this->visit(SubExpr))
return false;
- if (!this->emitCheckNull(E))
+ if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
return false;
if (classifyPrim(SubExpr) == PT_Ptr)
diff --git a/clang/test/AST/ByteCode/cxx14.cpp b/clang/test/AST/ByteCode/cxx14.cpp
new file mode 100644
index 0000000000000..9622311e100cb
--- /dev/null
+++ b/clang/test/AST/ByteCode/cxx14.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -std=c++14 -verify=both,expected %s -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -std=c++14 -verify=both,ref %s
+
+
+
+constexpr int(*null_ptr)() = nullptr;
+constexpr int test4 = (*null_ptr)(); // both-error {{must be initialized by a constant expression}} \
+ // both-note {{evaluates to a null function pointer}}
+
More information about the cfe-commits
mailing list