This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Show type in enum out of range diagnostic
ClosedPublic

Authored by dim on Jun 13 2023, 2:02 AM.

Details

Summary

When the diagnostic for an out of range enum value is printed, it
currently does not show the actual enum type in question, for example:

v8/src/base/bit-field.h:43:29: error: integer value 7 is outside the valid range of values [0, 3] for this enumeration type [-Wenum-constexpr-conversion]
  static constexpr T kMax = static_cast<T>(kNumValues - 1);
                            ^

This can make it cumbersome to find the cause for the problem. Add the
enum type to the diagnostic message, to make it easier.

Diff Detail

Event Timeline

dim created this revision.Jun 13 2023, 2:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 13 2023, 2:02 AM
dim requested review of this revision.Jun 13 2023, 2:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 13 2023, 2:02 AM

Thank you for this, I like the changes! Can you add a test showing template instantiation (the case that you were struggling to identify the enumeration from) and a release note?

dim updated this revision to Diff 531011.Jun 13 2023, 11:36 AM

Add release note and test with template instantiation.

dim updated this revision to Diff 531013.Jun 13 2023, 11:37 AM

Squash.

aaron.ballman accepted this revision.Jun 14 2023, 9:30 AM

LGTM!

clang/test/SemaCXX/constant-expression-cxx11.cpp
2479–2486

Equivalent but a bit easier to tell where the diagnostic is expected to happen at.

Perhaps a good follow-up would be to find out why we're not printing an "instantiated from here" note for this case (CC @shafik), but no need to do that for this patch.

This revision is now accepted and ready to land.Jun 14 2023, 9:30 AM
dim added inline comments.Jun 14 2023, 11:08 AM
clang/test/SemaCXX/constant-expression-cxx11.cpp
2479–2486

Ah, I didn't know that syntax. At first I had the cxx11-error@ just below the static_cast in the Bitfield class, but it seemed clearer to do it the way I did.

That said, you are right that it would probably be handy to show where the instantiation is coming from (if there is a template involved), because currently it does not point you to the exact source line where the problem is: in this case the bad declaration.

This revision was landed with ongoing or failed builds.Jun 14 2023, 11:35 AM
This revision was automatically updated to reflect the committed changes.

Thank you for this improvement!

dim added a comment.Jun 22 2023, 3:04 AM

Thank you for this improvement!

Well, it would still be nice to have the instantiation trace, as Aaron suggested. So that is probably one of those "exercise for the reader" things. :D