[Lldb-commits] [lldb] 3dfacc0 - CheckedArithmetic: llvm::Optional => std::optional

Fangrui Song via lldb-commits lldb-commits at lists.llvm.org
Sun Dec 4 20:31:02 PST 2022


Author: Fangrui Song
Date: 2022-12-05T04:30:54Z
New Revision: 3dfacc0a56e9d5718f51b1d3ce63070e79fc2608

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

LOG: CheckedArithmetic: llvm::Optional => std::optional

Added: 
    

Modified: 
    lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
    llvm/include/llvm/Analysis/VectorUtils.h
    llvm/include/llvm/Support/CheckedArithmetic.h
    llvm/lib/FileCheck/FileCheck.cpp
    llvm/unittests/Support/CheckedArithmeticTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp b/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
index 3fc529ae20281..25042b12f1939 100644
--- a/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
+++ b/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
@@ -560,7 +560,7 @@ uint64_t EmulateInstructionARM64::
 AddWithCarry(uint32_t N, uint64_t x, uint64_t y, bit carry_in,
              EmulateInstructionARM64::ProcState &proc_state) {
   uint64_t unsigned_sum = UInt(x) + UInt(y) + UInt(carry_in);
-  llvm::Optional<int64_t> signed_sum = llvm::checkedAdd(SInt(x), SInt(y));
+  std::optional<int64_t> signed_sum = llvm::checkedAdd(SInt(x), SInt(y));
   bool overflow = !signed_sum;
   if (!overflow)
     overflow |= !llvm::checkedAdd(*signed_sum, SInt(carry_in));

diff  --git a/llvm/include/llvm/Analysis/VectorUtils.h b/llvm/include/llvm/Analysis/VectorUtils.h
index 0f2346cf7c57e..02271d5780c82 100644
--- a/llvm/include/llvm/Analysis/VectorUtils.h
+++ b/llvm/include/llvm/Analysis/VectorUtils.h
@@ -641,7 +641,7 @@ template <typename InstTy> class InterleaveGroup {
   /// \returns false if the instruction doesn't belong to the group.
   bool insertMember(InstTy *Instr, int32_t Index, Align NewAlign) {
     // Make sure the key fits in an int32_t.
-    Optional<int32_t> MaybeKey = checkedAdd(Index, SmallestKey);
+    std::optional<int32_t> MaybeKey = checkedAdd(Index, SmallestKey);
     if (!MaybeKey)
       return false;
     int32_t Key = *MaybeKey;
@@ -664,7 +664,7 @@ template <typename InstTy> class InterleaveGroup {
     } else if (Key < SmallestKey) {
 
       // Make sure the largest index fits in an int32_t.
-      Optional<int32_t> MaybeLargestIndex = checkedSub(LargestKey, Key);
+      std::optional<int32_t> MaybeLargestIndex = checkedSub(LargestKey, Key);
       if (!MaybeLargestIndex)
         return false;
 

diff  --git a/llvm/include/llvm/Support/CheckedArithmetic.h b/llvm/include/llvm/Support/CheckedArithmetic.h
index c9db239672d36..5bfc551b2cc5b 100644
--- a/llvm/include/llvm/Support/CheckedArithmetic.h
+++ b/llvm/include/llvm/Support/CheckedArithmetic.h
@@ -15,8 +15,8 @@
 #define LLVM_SUPPORT_CHECKEDARITHMETIC_H
 
 #include "llvm/ADT/APInt.h"
-#include "llvm/ADT/Optional.h"
 
+#include <optional>
 #include <type_traits>
 
 namespace {
@@ -26,7 +26,7 @@ namespace {
 /// \return Empty optional if the operation overflows, or result otherwise.
 template <typename T, typename F>
 std::enable_if_t<std::is_integral<T>::value && sizeof(T) * 8 <= 64,
-                 llvm::Optional<T>>
+                 std::optional<T>>
 checkedOp(T LHS, T RHS, F Op, bool Signed = true) {
   llvm::APInt ALHS(sizeof(T) * 8, LHS, Signed);
   llvm::APInt ARHS(sizeof(T) * 8, RHS, Signed);
@@ -44,7 +44,7 @@ namespace llvm {
 /// \return Optional of sum if no signed overflow occurred,
 /// \c None otherwise.
 template <typename T>
-std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
+std::enable_if_t<std::is_signed<T>::value, std::optional<T>>
 checkedAdd(T LHS, T RHS) {
   return checkedOp(LHS, RHS, &llvm::APInt::sadd_ov);
 }
@@ -53,7 +53,7 @@ checkedAdd(T LHS, T RHS) {
 /// \return Optional of sum if no signed overflow occurred,
 /// \c None otherwise.
 template <typename T>
-std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
+std::enable_if_t<std::is_signed<T>::value, std::optional<T>>
 checkedSub(T LHS, T RHS) {
   return checkedOp(LHS, RHS, &llvm::APInt::ssub_ov);
 }
@@ -62,7 +62,7 @@ checkedSub(T LHS, T RHS) {
 /// \return Optional of product if no signed overflow occurred,
 /// \c None otherwise.
 template <typename T>
-std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
+std::enable_if_t<std::is_signed<T>::value, std::optional<T>>
 checkedMul(T LHS, T RHS) {
   return checkedOp(LHS, RHS, &llvm::APInt::smul_ov);
 }
@@ -71,7 +71,7 @@ checkedMul(T LHS, T RHS) {
 /// \return Optional of result if no signed overflow occurred,
 /// \c None otherwise.
 template <typename T>
-std::enable_if_t<std::is_signed<T>::value, llvm::Optional<T>>
+std::enable_if_t<std::is_signed<T>::value, std::optional<T>>
 checkedMulAdd(T A, T B, T C) {
   if (auto Product = checkedMul(A, B))
     return checkedAdd(*Product, C);
@@ -82,7 +82,7 @@ checkedMulAdd(T A, T B, T C) {
 /// \return Optional of sum if no unsigned overflow occurred,
 /// \c None otherwise.
 template <typename T>
-std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>>
+std::enable_if_t<std::is_unsigned<T>::value, std::optional<T>>
 checkedAddUnsigned(T LHS, T RHS) {
   return checkedOp(LHS, RHS, &llvm::APInt::uadd_ov, /*Signed=*/false);
 }
@@ -91,7 +91,7 @@ checkedAddUnsigned(T LHS, T RHS) {
 /// \return Optional of product if no unsigned overflow occurred,
 /// \c None otherwise.
 template <typename T>
-std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>>
+std::enable_if_t<std::is_unsigned<T>::value, std::optional<T>>
 checkedMulUnsigned(T LHS, T RHS) {
   return checkedOp(LHS, RHS, &llvm::APInt::umul_ov, /*Signed=*/false);
 }
@@ -100,7 +100,7 @@ checkedMulUnsigned(T LHS, T RHS) {
 /// \return Optional of result if no unsigned overflow occurred,
 /// \c None otherwise.
 template <typename T>
-std::enable_if_t<std::is_unsigned<T>::value, llvm::Optional<T>>
+std::enable_if_t<std::is_unsigned<T>::value, std::optional<T>>
 checkedMulAddUnsigned(T A, T B, T C) {
   if (auto Product = checkedMulUnsigned(A, B))
     return checkedAddUnsigned(*Product, C);

diff  --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp
index f428e3f448ac1..e2cc6dc6763bb 100644
--- a/llvm/lib/FileCheck/FileCheck.cpp
+++ b/llvm/lib/FileCheck/FileCheck.cpp
@@ -206,7 +206,7 @@ Expected<ExpressionValue> llvm::operator+(const ExpressionValue &LeftOperand,
   if (LeftOperand.isNegative() && RightOperand.isNegative()) {
     int64_t LeftValue = cantFail(LeftOperand.getSignedValue());
     int64_t RightValue = cantFail(RightOperand.getSignedValue());
-    Optional<int64_t> Result = checkedAdd<int64_t>(LeftValue, RightValue);
+    std::optional<int64_t> Result = checkedAdd<int64_t>(LeftValue, RightValue);
     if (!Result)
       return make_error<OverflowError>();
 
@@ -224,7 +224,7 @@ Expected<ExpressionValue> llvm::operator+(const ExpressionValue &LeftOperand,
   // Both values are positive at this point.
   uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue());
   uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
-  Optional<uint64_t> Result =
+  std::optional<uint64_t> Result =
       checkedAddUnsigned<uint64_t>(LeftValue, RightValue);
   if (!Result)
     return make_error<OverflowError>();
@@ -241,7 +241,7 @@ Expected<ExpressionValue> llvm::operator-(const ExpressionValue &LeftOperand,
     // Result <= -1 - (max int64_t) which overflows on 1- and 2-complement.
     if (RightValue > (uint64_t)std::numeric_limits<int64_t>::max())
       return make_error<OverflowError>();
-    Optional<int64_t> Result =
+    std::optional<int64_t> Result =
         checkedSub(LeftValue, static_cast<int64_t>(RightValue));
     if (!Result)
       return make_error<OverflowError>();
@@ -306,7 +306,7 @@ Expected<ExpressionValue> llvm::operator*(const ExpressionValue &LeftOperand,
   // Result will be positive and can overflow.
   uint64_t LeftValue = cantFail(LeftOperand.getUnsignedValue());
   uint64_t RightValue = cantFail(RightOperand.getUnsignedValue());
-  Optional<uint64_t> Result =
+  std::optional<uint64_t> Result =
       checkedMulUnsigned<uint64_t>(LeftValue, RightValue);
   if (!Result)
     return make_error<OverflowError>();

diff  --git a/llvm/unittests/Support/CheckedArithmeticTest.cpp b/llvm/unittests/Support/CheckedArithmeticTest.cpp
index eae54eb1c0cba..0e331b2a31eb4 100644
--- a/llvm/unittests/Support/CheckedArithmeticTest.cpp
+++ b/llvm/unittests/Support/CheckedArithmeticTest.cpp
@@ -11,7 +11,7 @@ TEST(CheckedArithmetic, CheckedAdd) {
   EXPECT_EQ(checkedAdd<int64_t>(Max, Max), std::nullopt);
   EXPECT_EQ(checkedAdd<int64_t>(Min, -1), std::nullopt);
   EXPECT_EQ(checkedAdd<int64_t>(Max, 1), std::nullopt);
-  EXPECT_EQ(checkedAdd<int64_t>(10, 1), Optional<int64_t>(11));
+  EXPECT_EQ(checkedAdd<int64_t>(10, 1), std::optional<int64_t>(11));
 }
 
 TEST(CheckedArithmetic, CheckedAddSmall) {
@@ -20,7 +20,7 @@ TEST(CheckedArithmetic, CheckedAddSmall) {
   EXPECT_EQ(checkedAdd<int16_t>(Max, Max), std::nullopt);
   EXPECT_EQ(checkedAdd<int16_t>(Min, -1), std::nullopt);
   EXPECT_EQ(checkedAdd<int16_t>(Max, 1), std::nullopt);
-  EXPECT_EQ(checkedAdd<int16_t>(10, 1), Optional<int64_t>(11));
+  EXPECT_EQ(checkedAdd<int16_t>(10, 1), std::optional<int64_t>(11));
 }
 
 TEST(CheckedArithmetic, CheckedMul) {
@@ -29,7 +29,7 @@ TEST(CheckedArithmetic, CheckedMul) {
   EXPECT_EQ(checkedMul<int64_t>(Max, 2), std::nullopt);
   EXPECT_EQ(checkedMul<int64_t>(Max, Max), std::nullopt);
   EXPECT_EQ(checkedMul<int64_t>(Min, 2), std::nullopt);
-  EXPECT_EQ(checkedMul<int64_t>(10, 2), Optional<int64_t>(20));
+  EXPECT_EQ(checkedMul<int64_t>(10, 2), std::optional<int64_t>(20));
 }
 
 TEST(CheckedArithmetic, CheckedMulAdd) {
@@ -38,7 +38,7 @@ TEST(CheckedArithmetic, CheckedMulAdd) {
   EXPECT_EQ(checkedMulAdd<int64_t>(Max, 1, 2), std::nullopt);
   EXPECT_EQ(checkedMulAdd<int64_t>(1, 1, Max), std::nullopt);
   EXPECT_EQ(checkedMulAdd<int64_t>(1, -1, Min), std::nullopt);
-  EXPECT_EQ(checkedMulAdd<int64_t>(10, 2, 3), Optional<int64_t>(23));
+  EXPECT_EQ(checkedMulAdd<int64_t>(10, 2, 3), std::optional<int64_t>(23));
 }
 
 TEST(CheckedArithmetic, CheckedMulSmall) {
@@ -47,7 +47,7 @@ TEST(CheckedArithmetic, CheckedMulSmall) {
   EXPECT_EQ(checkedMul<int16_t>(Max, 2), std::nullopt);
   EXPECT_EQ(checkedMul<int16_t>(Max, Max), std::nullopt);
   EXPECT_EQ(checkedMul<int16_t>(Min, 2), std::nullopt);
-  EXPECT_EQ(checkedMul<int16_t>(10, 2), Optional<int16_t>(20));
+  EXPECT_EQ(checkedMul<int16_t>(10, 2), std::optional<int16_t>(20));
 }
 
 TEST(CheckedArithmetic, CheckedMulAddSmall) {
@@ -56,28 +56,28 @@ TEST(CheckedArithmetic, CheckedMulAddSmall) {
   EXPECT_EQ(checkedMulAdd<int16_t>(Max, 1, 2), std::nullopt);
   EXPECT_EQ(checkedMulAdd<int16_t>(1, 1, Max), std::nullopt);
   EXPECT_EQ(checkedMulAdd<int16_t>(1, -1, Min), std::nullopt);
-  EXPECT_EQ(checkedMulAdd<int16_t>(10, 2, 3), Optional<int16_t>(23));
+  EXPECT_EQ(checkedMulAdd<int16_t>(10, 2, 3), std::optional<int16_t>(23));
 }
 
 TEST(CheckedArithmetic, CheckedAddUnsigned) {
   const uint64_t Max = std::numeric_limits<uint64_t>::max();
   EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, Max), std::nullopt);
   EXPECT_EQ(checkedAddUnsigned<uint64_t>(Max, 1), std::nullopt);
-  EXPECT_EQ(checkedAddUnsigned<uint64_t>(10, 1), Optional<uint64_t>(11));
+  EXPECT_EQ(checkedAddUnsigned<uint64_t>(10, 1), std::optional<uint64_t>(11));
 }
 
 TEST(CheckedArithmetic, CheckedMulUnsigned) {
   const uint64_t Max = std::numeric_limits<uint64_t>::max();
   EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, 2), std::nullopt);
   EXPECT_EQ(checkedMulUnsigned<uint64_t>(Max, Max), std::nullopt);
-  EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2), Optional<uint64_t>(20));
+  EXPECT_EQ(checkedMulUnsigned<uint64_t>(10, 2), std::optional<uint64_t>(20));
 }
 
 TEST(CheckedArithmetic, CheckedMulAddUnsigned) {
   const uint64_t Max = std::numeric_limits<uint64_t>::max();
   EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(Max, 1, 2), std::nullopt);
   EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(1, 1, Max), std::nullopt);
-  EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(10, 2, 3), Optional<uint64_t>(23));
+  EXPECT_EQ(checkedMulAddUnsigned<uint64_t>(10, 2, 3), std::optional<uint64_t>(23));
 }
 
 


        


More information about the lldb-commits mailing list