Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions clang/include/clang/AST/OperationKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ CAST_OPERATION(FunctionToPointerDecay)
/// CK_NullToPointer - Null pointer constant to pointer, ObjC
/// pointer, or block pointer.
/// (void*) 0
/// (void*) nullptr
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Copy-pasting comment since I accidentally put it on the commit, instead of on the review.)

That's not helpful? The issue isn't that the operand is a nullptr constant; the issue is that the result is a nullptr constant. Something like (std::nullptr_t)x.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misunderstood your previous comment. Now, is (std::nullptr_t)x what you'd like to see in that comment verbatim?

Copy link
Contributor

@rjmccall rjmccall Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Eli is asking for you to note in the text that the result of this conversion can still be a null pointer constant if it has type std::nullptr_t. While the conversion does always produce null pointer values, it does not otherwise produce null pointer constants as defined by the C++ standard. Only integer literal 0 and pr-values of type std::nullptr_t are null pointer constants.

I don't think we should have (std::nullptr_t) x in the list of examples because that is not generally valid C++; only (std::nullptr_t) 0 is valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, hopefully, I got it right this time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@efriedma-quic I'll land the fix now. Please let me know, if the comment on CK_NullToPointer needs to be revised.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can simplify it to just:

/// CK_NullToPointer - Null pointer constant to pointer, ObjC
/// pointer, block pointer, or std::nullptr_t.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Done in b509f7c.

/// 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