[clang] [CodeGen] Fix handling of nullptr in initializers (PR #137364)

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 25 10:56:48 PDT 2025


https://github.com/alexfh updated https://github.com/llvm/llvm-project/pull/137364

>From 5431de1664814a7a2dcc93695eed8745ae6bfef5 Mon Sep 17 00:00:00 2001
From: Alexander Kornienko <alexfh at google.com>
Date: Fri, 25 Apr 2025 17:51:03 +0200
Subject: [PATCH 1/3] [CodeGen] Fix handling of nullptr in initializers

Fixes https://github.com/llvm/llvm-project/issues/137276.
---
 clang/lib/CodeGen/CodeGenTypes.cpp |  6 ++++--
 clang/test/CodeGenCXX/pr137276.cpp | 13 +++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenCXX/pr137276.cpp

diff --git a/clang/lib/CodeGen/CodeGenTypes.cpp b/clang/lib/CodeGen/CodeGenTypes.cpp
index b94c11802a268..d1b292f23c2d2 100644
--- a/clang/lib/CodeGen/CodeGenTypes.cpp
+++ b/clang/lib/CodeGen/CodeGenTypes.cpp
@@ -850,12 +850,14 @@ CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
 }
 
 bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
-  assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
+  assert((T->isAnyPointerType() || T->isBlockPointerType() ||
+          T->isNullPtrType()) &&
+         "Invalid type");
   return isZeroInitializable(T);
 }
 
 bool CodeGenTypes::isZeroInitializable(QualType T) {
-  if (T->getAs<PointerType>())
+  if (T->getAs<PointerType>() || T->isNullPtrType())
     return Context.getTargetNullPointerValue(T) == 0;
 
   if (const auto *AT = Context.getAsArrayType(T)) {
diff --git a/clang/test/CodeGenCXX/pr137276.cpp b/clang/test/CodeGenCXX/pr137276.cpp
new file mode 100644
index 0000000000000..0664a121ad6a0
--- /dev/null
+++ b/clang/test/CodeGenCXX/pr137276.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+using ulong = unsigned long;
+template <class... Ts>
+void g(Ts... args) {
+  ulong arr[3] = {ulong(args)...};
+  (void)arr;
+}
+extern void f() {
+  g(nullptr, 17);
+}
+
+// CHECK: {{^}}  store i64 0, ptr %arr, align 8{{$}}

>From a0e4f300a8171bec3a267c446e0a10dc1bf6f57f Mon Sep 17 00:00:00 2001
From: Alexander Kornienko <alexfh at google.com>
Date: Fri, 25 Apr 2025 19:21:59 +0200
Subject: [PATCH 2/3] Update CK_NullToPointer comment

---
 clang/include/clang/AST/OperationKinds.def | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/include/clang/AST/OperationKinds.def b/clang/include/clang/AST/OperationKinds.def
index 790dd572a7c99..1c03610affffe 100644
--- a/clang/include/clang/AST/OperationKinds.def
+++ b/clang/include/clang/AST/OperationKinds.def
@@ -121,6 +121,7 @@ CAST_OPERATION(FunctionToPointerDecay)
 /// CK_NullToPointer - Null pointer constant to pointer, ObjC
 /// pointer, or block pointer.
 ///   (void*) 0
+///   (void*) nullptr
 ///   void (^block)() = 0;
 CAST_OPERATION(NullToPointer)
 

>From 74e4ed26a4e88126fb0ebcd3a1ae3ca0b0f80f48 Mon Sep 17 00:00:00 2001
From: Alexander Kornienko <alexfh at google.com>
Date: Fri, 25 Apr 2025 19:55:42 +0200
Subject: [PATCH 3/3] Move the test to nullptr.cpp, simplified it, added
 CHECK-LABEL.

---
 clang/test/CodeGenCXX/nullptr.cpp  |  7 +++++++
 clang/test/CodeGenCXX/pr137276.cpp | 13 -------------
 2 files changed, 7 insertions(+), 13 deletions(-)
 delete mode 100644 clang/test/CodeGenCXX/pr137276.cpp

diff --git a/clang/test/CodeGenCXX/nullptr.cpp b/clang/test/CodeGenCXX/nullptr.cpp
index 0d8837b216bec..3c35bb1b64a6f 100644
--- a/clang/test/CodeGenCXX/nullptr.cpp
+++ b/clang/test/CodeGenCXX/nullptr.cpp
@@ -70,3 +70,10 @@ namespace PR39528 {
   void f(nullptr_t);
   void g() { f(null); }
 }
+
+// CHECK-LABEL: define {{.*}}pr137276
+// CHECK: {{^}}  store i64 0, ptr %arr, align 8{{$}}
+void pr137276(nullptr_t np, int i) {
+  long arr[] = { long(np), i, 0 };
+  (void)arr;
+}
diff --git a/clang/test/CodeGenCXX/pr137276.cpp b/clang/test/CodeGenCXX/pr137276.cpp
deleted file mode 100644
index 0664a121ad6a0..0000000000000
--- a/clang/test/CodeGenCXX/pr137276.cpp
+++ /dev/null
@@ -1,13 +0,0 @@
-// RUN: %clang_cc1 -std=c++20 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
-
-using ulong = unsigned long;
-template <class... Ts>
-void g(Ts... args) {
-  ulong arr[3] = {ulong(args)...};
-  (void)arr;
-}
-extern void f() {
-  g(nullptr, 17);
-}
-
-// CHECK: {{^}}  store i64 0, ptr %arr, align 8{{$}}



More information about the cfe-commits mailing list