This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] cppcoreguidelines-slicing: display discarded state in bits
ClosedPublic

Authored by courbet on Nov 29 2016, 6:45 AM.

Details

Summary

I can't do bytes since the char width is not guaranteed to be a multiple of 8 bits. The message now looks like:

warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards 32 bits of state [cppcoreguidelines-slicing]

This fixes https://llvm.org/bugs/show_bug.cgi?id=28675

Diff Detail

Event Timeline

courbet updated this revision to Diff 79562.Nov 29 2016, 6:45 AM
courbet retitled this revision from to [clang-tidy] cppcoreguidelines-slicing: display discarded state in bits.
courbet updated this object.
courbet added a reviewer: Eugene.Zelenko.
Eugene.Zelenko set the repository for this revision to rL LLVM.
Eugene.Zelenko added a subscriber: Prazek.
alexfh added inline comments.Dec 13 2016, 7:36 AM
test/clang-tidy/cppcoreguidelines-slicing.cpp
34

It would be nice to test the values the check prints. It might be non-trivial to do really platform-neutral, but we can specify the -triple= in tests or assume char is always 8 bit on the platforms LLVM is compiled (I believe, this is true now).

alexfh edited edge metadata.Dec 13 2016, 7:57 AM

I can't do bytes since the char width is not guaranteed to be a multiple of 8 bits.

The standard uses the word "byte" ([intro.memory]) though it doesn't guarantee it's an octet, so it seems quite reasonable to use "byte" in diagnostics too, since we're in the context of C++. If we see an evidence of this wording causing confusion, we can reconsider. WDYT?

So you would advocate just replacing "sizeof(char)" with "bytes" ? I'm all for it if people think that there's no possible confusion.

Personally, I think it's reasonably clear from the context and greatly benefits the readability of the actual number.

So you would advocate just replacing "sizeof(char)" with "bytes" ? I'm all for it if people think that there's no possible confusion.

Yes, "bytes" is a perfectly cromulent term to use here. Byte does not mean the same thing as Octet, at least as far as C and C++ are concerned. Bits is also reasonable, as you've used here, but I think it makes the common case a bit harder to read. It's easier to read "4096 bytes" than "32768 bits".

clang-tidy/cppcoreguidelines/SlicingCheck.cpp
127

Please don't use auto here, as the type is not spelled out in the initialization.

130

Is int safe to use here, or can the multiplication cause this to become negative? I'd like to see a test case where the number of bits sliced is too large to fit into an int.

courbet updated this revision to Diff 82317.Dec 22 2016, 1:57 AM
courbet edited edge metadata.
courbet removed rL LLVM as the repository for this revision.
courbet marked an inline comment as done.

Just use "bytes" to man "a certain number of char size units" as per the consensus.

clang-tidy/cppcoreguidelines/SlicingCheck.cpp
130

no longer relevant

Sounds good, thanks for the comments.

alexfh accepted this revision.Dec 22 2016, 6:08 AM
alexfh edited edge metadata.

LG

This revision is now accepted and ready to land.Dec 22 2016, 6:08 AM
alexfh closed this revision.Mar 1 2017, 2:12 AM

This was committed a while ago as r290340.