[clang][Interp][NFC] Simplify Integral using constexpr if
authorTimm Bäder <tbaeder@redhat.com>
Tue, 27 Sep 2022 15:10:20 +0000 (17:10 +0200)
committerTimm Bäder <tbaeder@redhat.com>
Fri, 14 Oct 2022 10:47:07 +0000 (12:47 +0200)
Just keep one version of the function and differentiate between
std::is_signed() and unsigned using a constexpr if, instead of having
two different versions for the signed and unsigned cases.

clang/lib/AST/Interp/Integral.h

index 50c9d7b..b5d20fb 100644 (file)
@@ -213,55 +213,38 @@ public:
   }
 
 private:
-  template <typename T>
-  static std::enable_if_t<std::is_signed<T>::value, bool> CheckAddUB(T A, T B,
-                                                                     T &R) {
-    return llvm::AddOverflow<T>(A, B, R);
+  template <typename T> static bool CheckAddUB(T A, T B, T &R) {
+    if constexpr (std::is_signed_v<T>) {
+      return llvm::AddOverflow<T>(A, B, R);
+    } else {
+      R = A + B;
+      return false;
+    }
   }
 
-  template <typename T>
-  static std::enable_if_t<std::is_unsigned<T>::value, bool> CheckAddUB(T A, T B,
-                                                                       T &R) {
-    R = A + B;
-    return false;
-  }
-
-  template <typename T>
-  static std::enable_if_t<std::is_signed<T>::value, bool> CheckSubUB(T A, T B,
-                                                                     T &R) {
-    return llvm::SubOverflow<T>(A, B, R);
-  }
-
-  template <typename T>
-  static std::enable_if_t<std::is_unsigned<T>::value, bool> CheckSubUB(T A, T B,
-                                                                       T &R) {
-    R = A - B;
-    return false;
+  template <typename T> static bool CheckSubUB(T A, T B, T &R) {
+    if constexpr (std::is_signed_v<T>) {
+      return llvm::SubOverflow<T>(A, B, R);
+    } else {
+      R = A - B;
+      return false;
+    }
   }
 
-  template <typename T>
-  static std::enable_if_t<std::is_signed<T>::value, bool> CheckMulUB(T A, T B,
-                                                                     T &R) {
-    return llvm::MulOverflow<T>(A, B, R);
+  template <typename T> static bool CheckMulUB(T A, T B, T &R) {
+    if constexpr (std::is_signed_v<T>) {
+      return llvm::MulOverflow<T>(A, B, R);
+    } else {
+      R = A * B;
+      return false;
+    }
   }
-
-  template <typename T>
-  static std::enable_if_t<std::is_unsigned<T>::value, bool> CheckMulUB(T A, T B,
-                                                                       T &R) {
-    R = A * B;
-    return false;
-  }
-
-  template <typename T, T Min, T Max>
-  static std::enable_if_t<std::is_signed<T>::value, bool>
-  CheckRange(int64_t V) {
-    return Min <= V && V <= Max;
-  }
-
-  template <typename T, T Min, T Max>
-  static std::enable_if_t<std::is_unsigned<T>::value, bool>
-  CheckRange(int64_t V) {
-    return V >= 0 && static_cast<uint64_t>(V) <= Max;
+  template <typename T, T Min, T Max> static bool CheckRange(int64_t V) {
+    if constexpr (std::is_signed_v<T>) {
+      return Min <= V && V <= Max;
+    } else {
+      return V >= 0 && static_cast<uint64_t>(V) <= Max;
+    }
   }
 };