Skip to content

[CodeGen] Fix handling of nullptr in initializers #137364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/include/clang/AST/OperationKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ CAST_OPERATION(ArrayToPointerDecay)
CAST_OPERATION(FunctionToPointerDecay)

/// CK_NullToPointer - Null pointer constant to pointer, ObjC
/// pointer, or block pointer.
/// pointer, or block pointer. The result of this conversion can
/// still be a null pointer constant if it has type std::nullptr_t.
/// (void*) 0
/// void (^block)() = 0;
CAST_OPERATION(NullToPointer)
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/CodeGen/CodeGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,12 +850,14 @@ CodeGenTypes::getCGRecordLayout(const RecordDecl *RD) {
}

bool CodeGenTypes::isPointerZeroInitializable(QualType T) {
assert((T->isAnyPointerType() || T->isBlockPointerType()) && "Invalid type");
assert((T->isAnyPointerType() || T->isBlockPointerType() ||
T->isNullPtrType()) &&
"Invalid type");
return isZeroInitializable(T);
}

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

if (const auto *AT = Context.getAsArrayType(T)) {
Expand Down
7 changes: 7 additions & 0 deletions clang/test/CodeGenCXX/nullptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,10 @@ namespace PR39528 {
void f(nullptr_t);
void g() { f(null); }
}

// CHECK-LABEL: define {{.*}}pr137276
// CHECK: {{^}} store i64 0, ptr %arr, align 8{{$}}
void pr137276(nullptr_t np, int i) {
long arr[] = { long(np), i, 0 };
(void)arr;
}
Loading