Skip to content

Commit e3b1adf

Browse files
committed
[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.
1 parent 01b47eb commit e3b1adf

File tree

2 files changed

+2
-7
lines changed

2 files changed

+2
-7
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9834,7 +9834,8 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
98349834
Result.InvalidBase = false;
98359835
Result.Offset = CharUnits::fromQuantity(N);
98369836
Result.Designator.setInvalid();
9837-
Result.IsNullPtr = false;
9837+
Result.IsNullPtr =
9838+
(N == Info.Ctx.getTargetNullPointerValue(E->getType()));
98389839
return true;
98399840
} else {
98409841
// In rare instances, the value isn't an lvalue.

clang/test/CodeGenCXX/mangle-class-nttp.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ template void f<B{nullptr}>();
2727
// CHECK: define weak_odr void @_Z1fIXtl1BLPKi32EEEEvv(
2828
// MSABI: define {{.*}} @"??$f@$2UB@@PEBH0CA@H0A@@@@YAXXZ"
2929
template void f<B{fold((int*)32)}>();
30-
#ifndef _WIN32
31-
// FIXME: On MS ABI, we mangle this the same as nullptr, despite considering a
32-
// null pointer and zero bitcast to a pointer to be distinct pointer values.
33-
// CHECK: define weak_odr void @_Z1fIXtl1BrcPKiLi0EEEEvv(
34-
template void f<B{fold(reinterpret_cast<int*>(0))}>();
35-
#endif
3630

3731
// Pointers to subobjects.
3832
struct Nested { union { int k; int arr[2]; }; } nested[2];

0 commit comments

Comments
 (0)