[clang] [clang][ExprConst] Consider integer pointers of value 0 nullptr (PR #150164)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 22 21:08:56 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/150164.diff


2 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+2-1) 
- (modified) clang/test/CodeGenCXX/mangle-class-nttp.cpp (-6) 


``````````diff
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 at 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];

``````````

</details>


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


More information about the cfe-commits mailing list