Skip to content

Commit

Permalink
Correct size for std::nullptr_t
Browse files Browse the repository at this point in the history
  • Loading branch information
ahueck committed Dec 30, 2024
1 parent 671eae7 commit 28ad73b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/type/DimetaParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,16 @@ inline QualifiedCompound make_qualified_compound(const diparser::state::MetaData

QualifiedFundamental make_qualified_fundamental(const diparser::state::MetaData& meta_, std::string_view name,
FundamentalType::Encoding encoding) {
const auto size = (meta_.type->getSizeInBits() / 8);
const auto size = [&]() {
auto size = (meta_.type->getSizeInBits() / 8);
if (size == 0) {
if (encoding == FundamentalType::Encoding::kNullptr) {
// sizeof std::nullptr == sizeof void*
size = meta_.member_size > 0 ? meta_.member_size : sizeof(void*);
}
}
return size;
}();
auto fundamental = FundamentalType{std::string{name}, size, encoding};
return make_qual_type<FundamentalType>(fundamental, meta_);
}
Expand Down
15 changes: 15 additions & 0 deletions test/pass/cpp/stack_nullpointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %cpp-to-llvm %s | %apply-verifier 2>&1 | %filecheck %s

#include <cstddef>

void foo() {
std::nullptr_t null_ptr;
}

// CHECK: Final Type Stack: {{.*}} = !DIBasicType(tag: DW_TAG_unspecified_type, name: "decltype(nullptr)"

// CHECK: Line: 6
// CHECK-NEXT: Builtin: true
// CHECK-NEXT: Type:
// CHECK-NEXT: Fundamental: { Name: 'decltype(nullptr)', Extent: 8, Encoding: nullptr }
// CHECK-NEXT: Typedef: nullptr_t
2 changes: 1 addition & 1 deletion test/pass/cpp/stack_struct_all_built_in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void foo() {
// CHECK-NEXT: - Name: null_ptr_var
// CHECK-NEXT: Builtin: true
// CHECK-NEXT: Type:
// CHECK-NEXT: Fundamental: { Name: 'decltype(nullptr)', Extent: 0, Encoding: nullptr }
// CHECK-NEXT: Fundamental: { Name: 'decltype(nullptr)', Extent: 8, Encoding: nullptr }
// CHECK-NEXT: Typedef: nullptr_t
// CHECK-NEXT: - Name: size_var
// CHECK-NEXT: Builtin: true
Expand Down

0 comments on commit 28ad73b

Please sign in to comment.