[clang] 7c3d8c8 - Fix warnings when `-Wdeprecated-enum-enum-conversion` is enabled
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 7 05:23:19 PDT 2022
Author: Antonio Frighetto
Date: 2022-04-07T08:20:54-04:00
New Revision: 7c3d8c8977cfc013254783b85b9fc6026566b35f
URL: https://github.com/llvm/llvm-project/commit/7c3d8c8977cfc013254783b85b9fc6026566b35f
DIFF: https://github.com/llvm/llvm-project/commit/7c3d8c8977cfc013254783b85b9fc6026566b35f.diff
LOG: Fix warnings when `-Wdeprecated-enum-enum-conversion` is enabled
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.
This adds the `addEnumValues()` helper function to STLExtras.h to hide
the details of adding enumeration values together from two different
enumerations.
Added:
Modified:
clang/include/clang/AST/DeclarationName.h
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/STLExtrasTest.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/DeclarationName.h b/clang/include/clang/AST/DeclarationName.h
index 0762e0a478eac..1a1cbbf9f6eb5 100644
--- a/clang/include/clang/AST/DeclarationName.h
+++ b/clang/include/clang/AST/DeclarationName.h
@@ -21,6 +21,7 @@
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/FoldingSet.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/type_traits.h"
#include <cassert>
@@ -192,6 +193,13 @@ class DeclarationName {
"The various classes that DeclarationName::Ptr can point to"
" must be at least aligned to 8 bytes!");
+ static_assert(
+ std::is_same<std::underlying_type_t<StoredNameKind>,
+ std::underlying_type_t<
+ detail::DeclarationNameExtra::ExtraKind>>::value,
+ "The various enums used to compute values for NameKind should "
+ "all have the same underlying type");
+
public:
/// The kind of the name stored in this DeclarationName.
/// The first 7 enumeration values are stored inline and correspond
@@ -205,15 +213,18 @@ class DeclarationName {
CXXDestructorName = StoredCXXDestructorName,
CXXConversionFunctionName = StoredCXXConversionFunctionName,
CXXOperatorName = StoredCXXOperatorName,
- CXXDeductionGuideName = UncommonNameKindOffset +
- detail::DeclarationNameExtra::CXXDeductionGuideName,
- CXXLiteralOperatorName =
- UncommonNameKindOffset +
- detail::DeclarationNameExtra::CXXLiteralOperatorName,
- CXXUsingDirective = UncommonNameKindOffset +
- detail::DeclarationNameExtra::CXXUsingDirective,
- ObjCMultiArgSelector = UncommonNameKindOffset +
- detail::DeclarationNameExtra::ObjCMultiArgSelector
+ CXXDeductionGuideName = llvm::addEnumValues(
+ UncommonNameKindOffset,
+ detail::DeclarationNameExtra::CXXDeductionGuideName),
+ CXXLiteralOperatorName = llvm::addEnumValues(
+ UncommonNameKindOffset,
+ detail::DeclarationNameExtra::CXXLiteralOperatorName),
+ CXXUsingDirective =
+ llvm::addEnumValues(UncommonNameKindOffset,
+ detail::DeclarationNameExtra::CXXUsingDirective),
+ ObjCMultiArgSelector =
+ llvm::addEnumValues(UncommonNameKindOffset,
+ detail::DeclarationNameExtra::ObjCMultiArgSelector),
};
private:
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index fc461f8fe7e50..b186489295b41 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -206,6 +206,17 @@ struct FirstIndexOfType<T, T, Us...> : std::integral_constant<size_t, 0> {};
template <size_t I, typename... Ts>
using TypeAtIndex = std::tuple_element_t<I, std::tuple<Ts...>>;
+/// Helper which adds two underlying types of enumeration type.
+/// Implicit conversion to a common type is accepted.
+template <typename EnumTy1, typename EnumTy2,
+ typename UT1 = std::enable_if_t<std::is_enum<EnumTy1>::value,
+ std::underlying_type_t<EnumTy1>>,
+ typename UT2 = std::enable_if_t<std::is_enum<EnumTy2>::value,
+ std::underlying_type_t<EnumTy2>>>
+constexpr auto addEnumValues(EnumTy1 LHS, EnumTy2 RHS) {
+ return static_cast<UT1>(LHS) + static_cast<UT2>(RHS);
+}
+
//===----------------------------------------------------------------------===//
// Extra additions to <iterator>
//===----------------------------------------------------------------------===//
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index 09ff556f3ea15..a32c823e73b66 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -9,6 +9,7 @@
#include "llvm/ADT/STLExtras.h"
#include "gtest/gtest.h"
+#include <climits>
#include <list>
#include <vector>
@@ -989,4 +990,32 @@ TEST(STLExtrasTest, IsContainedInitializerList) {
static_assert(!is_contained({1, 2, 3, 4}, 5), "It's not there :(");
}
+TEST(STLExtrasTest, addEnumValues) {
+ enum A { Zero = 0, One = 1 };
+ enum B { IntMax = INT_MAX, ULongLongMax = ULLONG_MAX };
+ enum class C : unsigned { Two = 2 };
+
+ // Non-fixed underlying types, with same underlying types
+ static_assert(addEnumValues(Zero, One) == 1,
+ "addEnumValues(Zero, One) failed.");
+ static_assert(addEnumValues(IntMax, ULongLongMax) ==
+ INT_MAX + static_cast<unsigned long long>(ULLONG_MAX),
+ "addEnumValues(IntMax, ULongLongMax) failed.");
+ // Non-fixed underlying types, with
diff erent underlying types
+ static_assert(addEnumValues(Zero, IntMax) == INT_MAX,
+ "addEnumValues(Zero, IntMax) failed.");
+ static_assert(addEnumValues(One, ULongLongMax) ==
+ 1 + static_cast<unsigned long long>(ULLONG_MAX),
+ "addEnumValues(One, ULongLongMax) failed.");
+ // Non-fixed underlying type enum and fixed underlying type enum, with same
+ // underlying types
+ static_assert(addEnumValues(One, C::Two) == 3,
+ "addEnumValues(One, C::Two) failed.");
+ // Non-fixed underlying type enum and fixed underlying type enum, with
+ //
diff erent underlying types
+ static_assert(addEnumValues(ULongLongMax, C::Two) ==
+ static_cast<unsigned long long>(ULLONG_MAX) + 2,
+ "addEnumValues(ULongLongMax, C::Two) failed.");
+}
+
} // namespace
More information about the cfe-commits
mailing list