[clang] bef9eb8 - [clang] NFC: Refactor custom class into a lambda in CompilerInvocation

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 22 05:45:50 PST 2020


Author: Jan Svoboda
Date: 2020-12-22T14:45:29+01:00
New Revision: bef9eb84b2fb17b22ca771c8c5c34a85f141168d

URL: https://github.com/llvm/llvm-project/commit/bef9eb84b2fb17b22ca771c8c5c34a85f141168d
DIFF: https://github.com/llvm/llvm-project/commit/bef9eb84b2fb17b22ca771c8c5c34a85f141168d.diff

LOG: [clang] NFC: Refactor custom class into a lambda in CompilerInvocation

Change `makeFlagToValueNormalizer` so that one specialization converts all integral/enum arguments into `uint64_t` and forwards them to the more generic version.

This makes it easy to replace the custom `FlagToValueNormalizer` struct with a lambda, which is the common approach in other (de)normalizers.

Finally, drop custom `is_int_convertbile` in favor of `llvm::is_integral_or_enum`.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D93628

Added: 
    

Modified: 
    clang/lib/Frontend/CompilerInvocation.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index d7c1a6ffd600..44453ad462c4 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -157,33 +157,26 @@ static void denormalizeSimpleFlag(SmallVectorImpl<const char *> &Args,
   Args.push_back(Spelling);
 }
 
-namespace {
-template <typename T> struct FlagToValueNormalizer {
-  T Value;
+template <typename T> static constexpr bool is_uint64_t_convertible() {
+  return !std::is_same<T, uint64_t>::value &&
+         llvm::is_integral_or_enum<T>::value;
+}
 
-  Optional<T> operator()(OptSpecifier Opt, unsigned, const ArgList &Args,
-                         DiagnosticsEngine &) {
+template <typename T,
+          std::enable_if_t<!is_uint64_t_convertible<T>(), bool> = false>
+static auto makeFlagToValueNormalizer(T Value) {
+  return [Value](OptSpecifier Opt, unsigned, const ArgList &Args,
+                 DiagnosticsEngine &) -> Optional<T> {
     if (Args.hasArg(Opt))
       return Value;
     return None;
-  }
-};
-} // namespace
-
-template <typename T> static constexpr bool is_int_convertible() {
-  return sizeof(T) <= sizeof(uint64_t) &&
-         std::is_trivially_constructible<T, uint64_t>::value &&
-         std::is_trivially_constructible<uint64_t, T>::value;
-}
-
-template <typename T, std::enable_if_t<is_int_convertible<T>(), bool> = false>
-static FlagToValueNormalizer<uint64_t> makeFlagToValueNormalizer(T Value) {
-  return FlagToValueNormalizer<uint64_t>{Value};
+  };
 }
 
-template <typename T, std::enable_if_t<!is_int_convertible<T>(), bool> = false>
-static FlagToValueNormalizer<T> makeFlagToValueNormalizer(T Value) {
-  return FlagToValueNormalizer<T>{std::move(Value)};
+template <typename T,
+          std::enable_if_t<is_uint64_t_convertible<T>(), bool> = false>
+static auto makeFlagToValueNormalizer(T Value) {
+  return makeFlagToValueNormalizer(uint64_t(Value));
 }
 
 static auto makeBooleanOptionNormalizer(bool Value, bool OtherValue,


        


More information about the cfe-commits mailing list