This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Use proper integral cast for an enumerate with a fixed bool type
ClosedPublic

Authored by Mordante on Aug 9 2020, 9:19 AM.

Details

Summary

When casting an enumerate with a fixed bool type the casting should use an IntegralToBoolean instead of an IntegralCast.

Fixes PR47055: Incorrect codegen for enum with bool underlying type

Diff Detail

Event Timeline

Mordante created this revision.Aug 9 2020, 9:19 AM
Mordante requested review of this revision.Aug 9 2020, 9:19 AM
riccibruno added inline comments.
clang/test/AST/ast-dump-enum-bool.cpp
1 ↗(On Diff #284216)

Why a (pretty unreadable) json test? There is also make-ast-dump-check.sh to auto-generate an ast dump test.

aaron.ballman added inline comments.Aug 9 2020, 9:56 AM
clang/test/AST/ast-dump-enum-bool.cpp
1 ↗(On Diff #284216)

I'd also like to see some CodeGen tests for various fixed underlying types that ensures the conversion to the underlying type happens before the conversion to the enumeration.

Also, at least one test showing this behaves properly with a C-style cast instead of a named cast.

rsmith added inline comments.Aug 9 2020, 9:30 PM
clang/test/AST/ast-dump-enum-bool.cpp
1 ↗(On Diff #284216)

In addition to CodeGen tests, we should also test that constant evaluation does the right thing.

Mordante marked 3 inline comments as done.Aug 10 2020, 12:37 PM
Mordante added inline comments.
clang/test/AST/ast-dump-enum-bool.cpp
1 ↗(On Diff #284216)

@riccibruno Thanks for the hint! I wasn't aware of this tool. I've modified the AST test.

@aaron.ballman I've added a C-style AST test and CodeGen tests. However I'm not sure what you expect to see in the CodeGen tests. As I expected the CodeGen doesn't know about the enum, but it just uses the underlying type of the enum. Please let me know if you want to see additional tests.

Mordante updated this revision to Diff 284478.Aug 10 2020, 12:39 PM

Addresses the review comments.

rsmith added inline comments.Aug 10 2020, 3:43 PM
clang/test/AST/ast-dump-enum-bool.cpp
1 ↗(On Diff #284478)

I don't think we really need a dedicated AST test for this. Such tests create a maintenance burden, and they don't really capture what we care about here: that all non-zero values are correctly converted to the true value of the enumeration type.

clang/test/CodeGen/enum-bool.cpp
8–15

Some general guidance for writing IR testcases:

  • Don't test things that aren't relevant to the test case; doing so will make the test brittle as IR generation changes. In particular, don't test the function attribute comments, the #0 introducing function metadata, instruction names, alignments.
  • Use CHECK-LABEL for each function definition to improve matching semantics and diagnostics on mismatches. (The CHECK-LABELs are checked first, then the input is sliced up into pieces between them, and those pieces are checked independently.)
  • Don't use CHECK-NEXT unless it's relevant to your test that no other instructions appear in between.
clang/test/SemaCXX/cxx2a-consteval.cpp
597–609 ↗(On Diff #284478)

This DR test should go in test/CXX/drs/dr23xx.cpp, along with a suitable "magic comment" // dr2338: 12 to indicate this DR is implemented in Clang 12 onwards. (We have tooling that generates the clang.llvm.org/cxx_dr_status.html page from those magic comments.)

I don't think this has anything to do with consteval; a more-reduced test should work just as well (eg, static_assert((int)(E)2 == 1, "");) and would allow us to test this in C++11 onwards, not only in C++20.

Mordante marked 2 inline comments as done.Aug 14 2020, 9:46 AM
Mordante added inline comments.
clang/test/AST/ast-dump-enum-bool.cpp
1 ↗(On Diff #284478)

I'll remove the test.

clang/test/CodeGen/enum-bool.cpp
8–15

Thanks for the information, I'll reduce the test case.

clang/test/SemaCXX/cxx2a-consteval.cpp
597–609 ↗(On Diff #284478)

I thought the magic comment could be done in any test file. It'll move the test and add the comment.

Mordante marked 3 inline comments as done.Aug 14 2020, 9:46 AM
Mordante updated this revision to Diff 285685.Aug 14 2020, 10:25 AM

Addresses review comments.

aaron.ballman accepted this revision.Aug 15 2020, 7:03 AM

LGTM, thank you for the fix!

This revision is now accepted and ready to land.Aug 15 2020, 7:03 AM
This revision was automatically updated to reflect the committed changes.