This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Generalize empty record size computation to avoid giving empty C++ structs a size of 0
ClosedPublic

Authored by teemperor on Jul 6 2021, 3:36 AM.

Details

Summary

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.

Diff Detail

Event Timeline

teemperor created this revision.Jul 6 2021, 3:36 AM
teemperor requested review of this revision.Jul 6 2021, 3:36 AM
teemperor added inline comments.Jul 6 2021, 3:38 AM
lldb/test/API/lang/cpp/sizeof/TestCPPSizeof.py
21

I actually don't like these kinds of checks as they don't give very useful errors. I want to add do some kind of assertExprEqual check that would do these kind of checks + nice error handling, but for now I don't think we have a better way to do this.

werat accepted this revision.Jul 7 2021, 5:23 AM
This revision is now accepted and ready to land.Jul 7 2021, 5:23 AM
shafik accepted this revision.Jul 8 2021, 9:37 AM

LGTM

shafik added inline comments.Jul 8 2021, 9:38 AM
lldb/test/API/lang/cpp/sizeof/main.cpp
3

Just for completeness, we might want to add the scenario where the base class is Empty or EmptyClass and then both should be considered size zero.

teemperor added inline comments.Jul 8 2021, 10:11 AM
lldb/test/API/lang/cpp/sizeof/main.cpp
3

I think I need an example of what is supposed to be zero in that scenario to understand what you mean.

This revision was landed with ongoing or failed builds.Jul 22 2021, 4:31 AM
This revision was automatically updated to reflect the committed changes.

I think this change broke the Windows LLDB bot. More specifically the TestStructTypes test:

https://lab.llvm.org/buildbot/#/builders/83/builds/8528

I think this change broke the Windows LLDB bot. More specifically the TestStructTypes test:

https://lab.llvm.org/buildbot/#/builders/83/builds/8528

Looks like you fixed it already :)

I think this change broke the Windows LLDB bot. More specifically the TestStructTypes test:

https://lab.llvm.org/buildbot/#/builders/83/builds/8528

Looks like you fixed it already :)

Lots of practice from breaking that bot far too often in the past :)