C doesn't allow empty structs but Clang/GCC support them and give them a size of 0.
LLDB implements this by checking the tag kind and if it's `DW_TAG_structure_type` then
we give it a size of 0 via an empty external RecordLayout. This is done because our
internal TypeSystem is always in C++ mode (which means we would give them a size
of 1).
The current check for when we have this special case is currently too lax as types with
`DW_TAG_structure_type` can also occur in C++ with types defined using the `struct`
keyword. This means that in a C++ program with `struct Empty{};`, LLDB would return
`0` for `sizeof(Empty)` even though the correct size is 1.
This patch removes this special case and replaces it with a generic approach that just
assigns empty structs the byte_size as specified in DWARF. The GCC/Clang special
case is handles as they both emit an explicit `DW_AT_byte_size` of 0. And if another
compiler decides to use a different byte size for this case then this should also be
handled by the same code as long as that information is provided via `DW_AT_byte_size`.
Reviewed By: werat, shafik
Differential Revision: https://reviews.llvm.org/D105471
38 lines
880 B
C++
38 lines
880 B
C++
struct Empty {};
|
|
class EmptyClass {};
|
|
|
|
struct SingleMember {
|
|
int i;
|
|
};
|
|
class SingleMemberClass {
|
|
int i;
|
|
};
|
|
|
|
struct PaddingMember {
|
|
int i;
|
|
char c;
|
|
};
|
|
class PaddingMemberClass {
|
|
int i;
|
|
char c;
|
|
};
|
|
|
|
const unsigned sizeof_empty = sizeof(Empty);
|
|
const unsigned sizeof_empty_class = sizeof(EmptyClass);
|
|
const unsigned sizeof_single = sizeof(SingleMember);
|
|
const unsigned sizeof_single_class = sizeof(SingleMemberClass);
|
|
const unsigned sizeof_padding = sizeof(PaddingMember);
|
|
const unsigned sizeof_padding_class = sizeof(PaddingMemberClass);
|
|
|
|
int main() {
|
|
Empty empty;
|
|
EmptyClass empty_class;
|
|
SingleMember single;
|
|
SingleMemberClass single_class;
|
|
PaddingMember padding;
|
|
PaddingMemberClass padding_class;
|
|
// Make sure globals are used.
|
|
return sizeof_empty + sizeof_empty_class + sizeof_single +
|
|
sizeof_single_class + sizeof_padding + sizeof_padding_class;
|
|
}
|