Skip to content

Commit cf17ee1

Browse files
authored
[CodeGen] Fix handling of nullptr in initializers (#137364)
Fixes #137276.
1 parent 475531b commit cf17ee1

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

clang/include/clang/AST/OperationKinds.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ CAST_OPERATION(ArrayToPointerDecay)
119119
CAST_OPERATION(FunctionToPointerDecay)
120120

121121
/// CK_NullToPointer - Null pointer constant to pointer, ObjC
122-
/// pointer, or block pointer.
122+
/// pointer, or block pointer. The result of this conversion can
123+
/// still be a null pointer constant if it has type std::nullptr_t.
123124
/// (void*) 0
124125
/// void (^block)() = 0;
125126
CAST_OPERATION(NullToPointer)

clang/lib/CodeGen/CodeGenTypes.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,14 @@ CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
850850
}
851851

852852
bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
853-
assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
853+
assert((T->isAnyPointerType() || T->isBlockPointerType() ||
854+
T->isNullPtrType()) &&
855+
"Invalid type");
854856
return isZeroInitializable(T);
855857
}
856858

857859
bool CodeGenTypes::isZeroInitializable(QualType T) {
858-
if (T->getAs<PointerType>())
860+
if (T->getAs<PointerType>() || T->isNullPtrType())
859861
return Context.getTargetNullPointerValue(T) == 0;
860862

861863
if (const auto *AT = Context.getAsArrayType(T)) {

clang/test/CodeGenCXX/nullptr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,10 @@ namespace PR39528 {
7070
void f(nullptr_t);
7171
void g() { f(null); }
7272
}
73+
74+
// CHECK-LABEL: define {{.*}}pr137276
75+
// CHECK: {{^}} store i64 0, ptr %arr, align 8{{$}}
76+
void pr137276(nullptr_t np, int i) {
77+
long arr[] = { long(np), i, 0 };
78+
(void)arr;
79+
}

0 commit comments

Comments
 (0)