https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/150164
>From b9655ff6d54475b5697a63571734192c92a6af07 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 | 14 +++++++++----- clang/test/AST/ByteCode/functions.cpp | 2 +- clang/test/CodeGenCXX/mangle-class-nttp.cpp | 6 ------ clang/test/SemaOpenCLCXX/amdgpu-nullptr.clcpp | 9 +++++++++ 4 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 clang/test/SemaOpenCLCXX/amdgpu-nullptr.clcpp diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 0d12161756467..8622162359cb1 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -9830,11 +9830,15 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { if (Value.isInt()) { unsigned Size = Info.Ctx.getTypeSize(E->getType()); uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); - Result.Base = (Expr*)nullptr; - Result.InvalidBase = false; - Result.Offset = CharUnits::fromQuantity(N); - Result.Designator.setInvalid(); - Result.IsNullPtr = false; + if (N == Info.Ctx.getTargetNullPointerValue(E->getType())) { + Result.setNull(Info.Ctx, E->getType()); + } else { + Result.Base = (Expr *)nullptr; + Result.InvalidBase = false; + Result.Offset = CharUnits::fromQuantity(N); + Result.Designator.setInvalid(); + Result.IsNullPtr = false; + } return true; } else { // In rare instances, the value isn't an lvalue. diff --git a/clang/test/AST/ByteCode/functions.cpp b/clang/test/AST/ByteCode/functions.cpp index b5e6f5bd7ece2..9b05d54fa8435 100644 --- a/clang/test/AST/ByteCode/functions.cpp +++ b/clang/test/AST/ByteCode/functions.cpp @@ -620,7 +620,7 @@ namespace FromIntegral { int a[(int)DoubleFn((void*)-1)()]; // both-error {{not allowed at file scope}} \ // both-warning {{variable length arrays}} int b[(int)DoubleFn((void*)(-1 + 1))()]; // both-error {{not allowed at file scope}} \ - // expected-note {{evaluates to a null function pointer}} \ + // both-note {{evaluates to a null function pointer}} \ // both-warning {{variable length arrays}} #endif } 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]; diff --git a/clang/test/SemaOpenCLCXX/amdgpu-nullptr.clcpp b/clang/test/SemaOpenCLCXX/amdgpu-nullptr.clcpp new file mode 100644 index 0000000000000..5bc147c2deba7 --- /dev/null +++ b/clang/test/SemaOpenCLCXX/amdgpu-nullptr.clcpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 -triple amdgcn -cl-std=clc++ -verify %s + +// expected-no-diagnostics + +#define fold(x) (__builtin_constant_p(x) ? (x) : (x)) +static_assert(nullptr != fold(reinterpret_cast<private int*>(0))); + +static_assert(nullptr == (private int *)0); + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits