This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix warnings when `-Wdeprecated-enum-enum-conversion` is enabled
ClosedPublic

Authored by antoniofrighetto on Mar 31 2022, 6:24 AM.

Details

Summary

clang may throw the following warning: include/clang/AST/DeclarationName.h:210:52: error: arithmetic between different enumeration types ('clang::DeclarationName::StoredNameKind' and 'clang::detail::DeclarationNameExtra::ExtraKind') is deprecated when flags -Werror,-Wdeprecated-enum-enum-conversion are on.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMar 31 2022, 6:24 AM
antoniofrighetto requested review of this revision.Mar 31 2022, 6:24 AM
antoniofrighetto retitled this revision from [clang] Fix warnings with `-Wdeprecated-enum-enum-conversion` is enabled to [clang] Fix warnings when `-Wdeprecated-enum-enum-conversion` is enabled.Mar 31 2022, 6:26 AM

The changes here look correct to me, but they complicate the code a reasonable amount. I almost wonder whether we want to add a helper function (perhaps even to STLExtras.h?) along the lines of a cleaned up version of:

template <typename EnumTy1, typename EnumTy2>
auto addEnumValues(EnumTy1 LHS, EnumTy2 RHS) {
  return static_cast<std::underlying_type_t<EnumTy1>>(LHS) +
    static_cast<std::underlying_type_t<EnumTy2>>(RHS);
}

(We'd probably want some enable_if magic to protect the interface a bit more as well). WDYT of something like that? (That change would require some unit testing coverage as well, but this strikes me as something we're likely to want to reuse given that the functionality is deprecated.)

Looks definitely better! How about this slightly changed version protecting the interface?

/// Helper which adds two underlying types of enumeration type.
template <typename EnumTy1,
          typename EnumTy2,
          typename UT1 = std::enable_if_t<std::is_enum_v<EnumTy1>, std::underlying_type_t<EnumTy1>>,
          typename UT2 = std::enable_if_t<std::is_enum_v<EnumTy2>, std::underlying_type_t<EnumTy2>>>
constexpr auto addEnumValues(EnumTy1 LHS, EnumTy2 RHS) {
  return static_cast<UT1>(LHS) + static_cast<UT2>(RHS);
}

I feel like this is something we may wish to readopt in the future for other similar cases as well (e.g., -Wdeprecated-anon-enum-enum-conversion in Comment.h).

Looks definitely better! How about this slightly changed version protecting the interface?

/// Helper which adds two underlying types of enumeration type.
template <typename EnumTy1,
          typename EnumTy2,
          typename UT1 = std::enable_if_t<std::is_enum_v<EnumTy1>, std::underlying_type_t<EnumTy1>>,
          typename UT2 = std::enable_if_t<std::is_enum_v<EnumTy2>, std::underlying_type_t<EnumTy2>>>
constexpr auto addEnumValues(EnumTy1 LHS, EnumTy2 RHS) {
  return static_cast<UT1>(LHS) + static_cast<UT2>(RHS);
}

I feel like this is something we may wish to readopt in the future for other similar cases as well (e.g., -Wdeprecated-anon-enum-enum-conversion in Comment.h).

I think that's a great interface for it!

Herald added a project: Restricted Project. · View Herald TranscriptApr 5 2022, 5:07 AM

Thanks! This is heading in the right direction. You should also add some test coverage to llvm/unittests/ADT/STLExtrasTests.cpp for the new interface.

llvm/include/llvm/ADT/STLExtras.h
207–211 ↗(On Diff #420458)

Oops, but is_enum_v<> is C++17 and up, and we currently still require only C++14, so this will have to use the long-hand form (precommit CI was failing because of this).

antoniofrighetto marked an inline comment as done.

Fixed the change, and added a new test. Hope the coverage is at a fairly good level now!

aaron.ballman added inline comments.Apr 6 2022, 3:19 PM
llvm/unittests/ADT/STLExtrasTest.cpp
994 ↗(On Diff #420957)

It looks like you need to include <climits> for the macros.

antoniofrighetto marked an inline comment as done.
llvm/unittests/ADT/STLExtrasTest.cpp
994 ↗(On Diff #420957)

Right, saw the CI, fixed, thanks! :)

aaron.ballman accepted this revision.Apr 7 2022, 3:53 AM

LGTM, thank you for the new interface and fixes! (Current Precommit CI failures look unrelated to me.)

This revision is now accepted and ready to land.Apr 7 2022, 3:53 AM

Awesome, thank you for reviewing this! PS: I do not have commit access, feel free to close it, if you say.

Awesome, thank you for reviewing this! PS: I do not have commit access, feel free to close it, if you say.

Ah, thank you for letting me know you don't have commit access. I'm happy to land this on your behalf. What name and email address would you like me to use for patch attribution?

aaron.ballman closed this revision.Apr 7 2022, 5:25 AM

Thank you for the fix! I've landed on your behalf in 7c3d8c8977cfc013254783b85b9fc6026566b35f.