This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Show size, offset and padding for bit fields on hover
ClosedPublic

Authored by SR_team on May 22 2023, 11:24 AM.

Details

Summary

Examle:

struct test {
	char a;
	char b : 3;
	char c : 5;
	int d;
	int e : 27;
};




Diff Detail

Event Timeline

SR_team created this revision.May 22 2023, 11:24 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 11:24 AM
SR_team requested review of this revision.May 22 2023, 11:24 AM

I do see the appeal of making this work for bitfields, but I think silently rounding offset/size/padding to whole bytes is pretty misleading.
For bitfields we'd really need to talk about layout at a bit level.
But we don't want to talk about regular fields in bits (I think?!)
And having a mixture seems like it's going to be pretty complicated both in implementation and for users, for a smallish feature.

I don't see a great sweet-spot design, maybe you/others have ideas (or can explain why the rounding behavior in this patch is reasonable.

I do see the appeal of making this work for bitfields, but I think silently rounding offset/size/padding to whole bytes is pretty misleading.
For bitfields we'd really need to talk about layout at a bit level.
But we don't want to talk about regular fields in bits (I think?!)
And having a mixture seems like it's going to be pretty complicated both in implementation and for users, for a smallish feature.

I don't see a great sweet-spot design, maybe you/others have ideas (or can explain why the rounding behavior in this patch is reasonable.

We can store this fields in bits, and show in bytes on hover, when it's possible, and in bits, when showing in bytes impossible. Would such an implementation be good?

SR_team updated this revision to Diff 524933.May 23 2023, 4:50 PM
This comment was removed by SR_team.
SR_team updated this revision to Diff 524938.May 23 2023, 4:58 PM

Stores Offset, Size and Padding fields of HoverInfo in bits. When hover present bits converted to bytes, if possible.

Examle:

struct test {
	char a;
	char b : 3;
	char c : 5;
	int d;
	int e : 31;
};




SR_team updated this revision to Diff 524946.May 23 2023, 5:25 PM

Extract formatting to lambda, for simplify code modification

SR_team edited the summary of this revision. (Show Details)May 23 2023, 5:59 PM
SR_team edited the summary of this revision. (Show Details)May 24 2023, 12:40 PM
nridge added a subscriber: nridge.May 27 2023, 5:16 PM

@sammccall review my code please

sammccall accepted this revision.Jun 5 2023, 6:08 AM

Sorry for the delay, I've been out on vacation.

Behavior looks great and isn't too complicated :-)
Let me know if you have commit access or I should land this for you (in which case: please provide a preferred name/email for attribution)

clang-tools-extra/clangd/Hover.cpp
1472

can you make this a separate (static) function rather than a lambda?
and the same for formatOffset()

(This is a long function as it is)

1479

The distinction between bits+bytes for offsets vs bits OR bytes for size is worth calling out with a comment:

// Sizes (and padding) are shown in bytes if possible, otherwise in bits.
string formatSize(...) { ... }

// Offsets are shown in bytes + bits, so offsets of different fields
// can always be easily compared.
string formatOffset(...) { ... }
1483

FWIW I think I slightly prefer printing bytes always, so that 4 prints as 0 bytes and 4 bits.

This makes a rare case slightly more regular (and the code is simpler). Up to you though.

clang-tools-extra/clangd/unittests/HoverTests.cpp
3292

can you add a testcase for render() where the size/offset/padding are not whole bytes?

This revision is now accepted and ready to land.Jun 5 2023, 6:08 AM
SR_team updated this revision to Diff 528458.Jun 5 2023, 8:38 AM
  • moved lambda FormatSize to static function formatSize();
  • extracted offset format code to static function formatOffset();
  • always shows bytes in offset (for offset 4 showed Offset: 0 bytes and 4 bits)
  • added render test for bit-fields

Let me know if you have commit access or I should land this for you (in which case: please provide a preferred name/email for attribution)

Land it for me, please. Name: SR_team, email: me@sr.team

This revision was landed with ongoing or failed builds.Jun 5 2023, 9:04 AM
This revision was automatically updated to reflect the committed changes.