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

When casting a 0 to a pointer type, the IsNullPtr flag was always set to false, 
leading to weird results like a pointer with value 0 that isn't a null pointer.

This caused

```c++
struct B { const int *p;};
template<B> void f() {}
template void f<B{nullptr}>();
template void f<B{fold(reinterpret_cast<int*>(0))}>();
```

to be valid code, since nullptr and (int*)0 aren't equal. This seems weird and 
GCC doesn't behave like this.

>From e3b1adf3702f3e4e85ea9398811070b1c51f5c75 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com>
Date: Wed, 23 Jul 2025 06:05:21 +0200
Subject: [PATCH] [clang][ExprConst] Consider integer pointers of value 0
 nullptr

When casting a 0 to a pointer type, the IsNullPtr flag was always set
to false, leading to weird results like a pointer with value 0 that
isn't a null pointer.

This caused

struct B { const int *p;};
template<B> void f() {}
template void f<B{nullptr}>();
template void f<B{fold(reinterpret_cast<int*>(0))}>();

to be valid code, since nullptr and (int*)0 aren't equal. This seems
weird and GCC doesn't behave like this.
---
 clang/lib/AST/ExprConstant.cpp              | 3 ++-
 clang/test/CodeGenCXX/mangle-class-nttp.cpp | 6 ------
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 0d12161756467..5fe2bd59fcc98 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9834,7 +9834,8 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr 
*E) {
       Result.InvalidBase = false;
       Result.Offset = CharUnits::fromQuantity(N);
       Result.Designator.setInvalid();
-      Result.IsNullPtr = false;
+      Result.IsNullPtr =
+          (N == Info.Ctx.getTargetNullPointerValue(E->getType()));
       return true;
     } else {
       // In rare instances, the value isn't an lvalue.
diff --git a/clang/test/CodeGenCXX/mangle-class-nttp.cpp 
b/clang/test/CodeGenCXX/mangle-class-nttp.cpp
index 12c81f2ba0514..c250be7c73c72 100644
--- a/clang/test/CodeGenCXX/mangle-class-nttp.cpp
+++ b/clang/test/CodeGenCXX/mangle-class-nttp.cpp
@@ -27,12 +27,6 @@ template void f<B{nullptr}>();
 // CHECK: define weak_odr void @_Z1fIXtl1BLPKi32EEEEvv(
 // MSABI: define {{.*}} @"??$f@$2UB@@PEBH0CA@H0A@@@@YAXXZ"
 template void f<B{fold((int*)32)}>();
-#ifndef _WIN32
-// FIXME: On MS ABI, we mangle this the same as nullptr, despite considering a
-// null pointer and zero bitcast to a pointer to be distinct pointer values.
-// CHECK: define weak_odr void @_Z1fIXtl1BrcPKiLi0EEEEvv(
-template void f<B{fold(reinterpret_cast<int*>(0))}>();
-#endif
 
 // Pointers to subobjects.
 struct Nested { union { int k; int arr[2]; }; } nested[2];

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

Reply via email to