[clang] 81c5b5d - [clang][Interp][NFC] Simplify Integral using constexpr if

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 14 03:47:26 PDT 2022


Author: Timm Bäder
Date: 2022-10-14T12:47:07+02:00
New Revision: 81c5b5d80efab9de616d6f8e42cd007f9c16e36b

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

LOG: [clang][Interp][NFC] Simplify Integral using constexpr if

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.

Added: 
    

Modified: 
    clang/lib/AST/Interp/Integral.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Integral.h b/clang/lib/AST/Interp/Integral.h
index 50c9d7b6cc86..b5d20fb8c49a 100644
--- a/clang/lib/AST/Interp/Integral.h
+++ b/clang/lib/AST/Interp/Integral.h
@@ -213,55 +213,38 @@ template <unsigned Bits, bool Signed> class Integral final {
   }
 
 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;
+    }
   }
 };
 


        


More information about the cfe-commits mailing list