[PATCH] D122808: [clang] Fix warnings with `-Wdeprecated-enum-enum-conversion` is enabled

Antonio Frighetto via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 31 06:24:10 PDT 2022


antoniofrighetto created this revision.
antoniofrighetto added reviewers: aaron.ballman, dblaikie.
antoniofrighetto added a project: clang.
Herald added a project: All.
antoniofrighetto requested review of this revision.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122808

Files:
  clang/include/clang/AST/DeclarationName.h


Index: clang/include/clang/AST/DeclarationName.h
===================================================================
--- clang/include/clang/AST/DeclarationName.h
+++ clang/include/clang/AST/DeclarationName.h
@@ -27,6 +27,7 @@
 #include <cstdint>
 #include <cstring>
 #include <string>
+#include <type_traits>
 
 namespace clang {
 
@@ -192,6 +193,13 @@
                 "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,30 @@
     CXXDestructorName = StoredCXXDestructorName,
     CXXConversionFunctionName = StoredCXXConversionFunctionName,
     CXXOperatorName = StoredCXXOperatorName,
-    CXXDeductionGuideName = UncommonNameKindOffset +
-                            detail::DeclarationNameExtra::CXXDeductionGuideName,
+    CXXDeductionGuideName =
+        static_cast<std::underlying_type_t<StoredNameKind>>(
+            UncommonNameKindOffset) +
+        static_cast<
+            std::underlying_type_t<detail::DeclarationNameExtra::ExtraKind>>(
+            detail::DeclarationNameExtra::CXXDeductionGuideName),
     CXXLiteralOperatorName =
-        UncommonNameKindOffset +
-        detail::DeclarationNameExtra::CXXLiteralOperatorName,
-    CXXUsingDirective = UncommonNameKindOffset +
-                        detail::DeclarationNameExtra::CXXUsingDirective,
-    ObjCMultiArgSelector = UncommonNameKindOffset +
-                           detail::DeclarationNameExtra::ObjCMultiArgSelector
+        static_cast<std::underlying_type_t<StoredNameKind>>(
+            UncommonNameKindOffset) +
+        static_cast<
+            std::underlying_type_t<detail::DeclarationNameExtra::ExtraKind>>(
+            detail::DeclarationNameExtra::CXXLiteralOperatorName),
+    CXXUsingDirective =
+        static_cast<std::underlying_type_t<StoredNameKind>>(
+            UncommonNameKindOffset) +
+        static_cast<
+            std::underlying_type_t<detail::DeclarationNameExtra::ExtraKind>>(
+            detail::DeclarationNameExtra::CXXUsingDirective),
+    ObjCMultiArgSelector =
+        static_cast<std::underlying_type_t<StoredNameKind>>(
+            UncommonNameKindOffset) +
+        static_cast<
+            std::underlying_type_t<detail::DeclarationNameExtra::ExtraKind>>(
+            detail::DeclarationNameExtra::ObjCMultiArgSelector),
   };
 
 private:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122808.419417.patch
Type: text/x-patch
Size: 2822 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220331/69534af5/attachment-0001.bin>


More information about the cfe-commits mailing list