Author: Alexander Kornienko
Date: 2025-04-28T12:14:05+02:00
New Revision: cf17ee1d3a7ec78dbd922b3eb3944ac49cb78737

URL: 
https://github.com/llvm/llvm-project/commit/cf17ee1d3a7ec78dbd922b3eb3944ac49cb78737
DIFF: 
https://github.com/llvm/llvm-project/commit/cf17ee1d3a7ec78dbd922b3eb3944ac49cb78737.diff

LOG: [CodeGen] Fix handling of nullptr in initializers (#137364)

Fixes https://github.com/llvm/llvm-project/issues/137276.

Added: 
    

Modified: 
    clang/include/clang/AST/OperationKinds.def
    clang/lib/CodeGen/CodeGenTypes.cpp
    clang/test/CodeGenCXX/nullptr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/AST/OperationKinds.def 
b/clang/include/clang/AST/OperationKinds.def
index 790dd572a7c99..d33d608a16f24 100644
--- a/clang/include/clang/AST/OperationKinds.def
+++ b/clang/include/clang/AST/OperationKinds.def
@@ -119,7 +119,8 @@ CAST_OPERATION(ArrayToPointerDecay)
 CAST_OPERATION(FunctionToPointerDecay)
 
 /// CK_NullToPointer - Null pointer constant to pointer, ObjC
-/// pointer, or block pointer.
+/// pointer, or block pointer. The result of this conversion can
+/// still be a null pointer constant if it has type std::nullptr_t.
 ///   (void*) 0
 ///   void (^block)() = 0;
 CAST_OPERATION(NullToPointer)

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/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;
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to