[NFC] Use std::optional over llvm::Optional to implement MaybeAlign
authorGuillaume Chatelet <gchatelet@google.com>
Thu, 15 Dec 2022 13:52:00 +0000 (13:52 +0000)
committerGuillaume Chatelet <gchatelet@google.com>
Thu, 15 Dec 2022 15:17:14 +0000 (15:17 +0000)
Differential Revision: https://reviews.llvm.org/D140098

llvm/include/llvm/Support/Alignment.h
llvm/lib/Support/OptimizedStructLayout.cpp
llvm/unittests/Support/AlignmentTest.cpp

index 1543a57..4577641 100644 (file)
@@ -21,9 +21,9 @@
 #ifndef LLVM_SUPPORT_ALIGNMENT_H_
 #define LLVM_SUPPORT_ALIGNMENT_H_
 
-#include "llvm/ADT/Optional.h"
 #include "llvm/Support/MathExtras.h"
 #include <cassert>
+#include <optional>
 #ifndef NDEBUG
 #include <string>
 #endif // NDEBUG
@@ -93,13 +93,13 @@ public:
   }
 
   /// Allow constructions of constexpr Align.
-  template <size_t kValue> constexpr static LogValue Constant() {
+  template <size_t kValue> constexpr static Align Constant() {
     return LogValue{static_cast<uint8_t>(CTLog2<kValue>())};
   }
 
   /// Allow constructions of constexpr Align from types.
   /// Compile time equivalent to Align(alignof(T)).
-  template <typename T> constexpr static LogValue Of() {
+  template <typename T> constexpr static Align Of() {
     return Constant<std::alignment_of<T>::value>();
   }
 
@@ -114,9 +114,9 @@ inline Align assumeAligned(uint64_t Value) {
 
 /// This struct is a compact representation of a valid (power of two) or
 /// undefined (0) alignment.
-struct MaybeAlign : public llvm::Optional<Align> {
+struct MaybeAlign : public std::optional<Align> {
 private:
-  using UP = llvm::Optional<Align>;
+  using UP = std::optional<Align>;
 
 public:
   /// Default is undefined.
@@ -128,9 +128,8 @@ public:
   MaybeAlign(MaybeAlign &&Other) = default;
   MaybeAlign &operator=(MaybeAlign &&Other) = default;
 
-  /// Use llvm::Optional<Align> constructor.
-  using UP::UP;
-
+  constexpr MaybeAlign(std::nullopt_t None) : UP(None) {}
+  constexpr MaybeAlign(Align Value) : UP(Value) {}
   explicit MaybeAlign(uint64_t Value) {
     assert((Value == 0 || llvm::isPowerOf2_64(Value)) &&
            "Alignment is neither 0 nor a power of 2");
@@ -292,6 +291,22 @@ bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
 bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
 bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
 
+// Allow equality comparisons between Align and MaybeAlign.
+inline bool operator==(MaybeAlign Lhs, Align Rhs) { return Lhs && *Lhs == Rhs; }
+inline bool operator!=(MaybeAlign Lhs, Align Rhs) { return !(Lhs == Rhs); }
+inline bool operator==(Align Lhs, MaybeAlign Rhs) { return Rhs == Lhs; }
+inline bool operator!=(Align Lhs, MaybeAlign Rhs) { return !(Rhs == Lhs); }
+// Allow equality comparisons with MaybeAlign.
+inline bool operator==(MaybeAlign Lhs, MaybeAlign Rhs) {
+  return (Lhs && Rhs && (*Lhs == *Rhs)) || (!Lhs && !Rhs);
+}
+inline bool operator!=(MaybeAlign Lhs, MaybeAlign Rhs) { return !(Lhs == Rhs); }
+// Allow equality comparisons with std::nullopt.
+inline bool operator==(MaybeAlign Lhs, std::nullopt_t) { return !bool(Lhs); }
+inline bool operator!=(MaybeAlign Lhs, std::nullopt_t) { return bool(Lhs); }
+inline bool operator==(std::nullopt_t, MaybeAlign Rhs) { return !bool(Rhs); }
+inline bool operator!=(std::nullopt_t, MaybeAlign Rhs) { return bool(Rhs); }
+
 #ifndef NDEBUG
 // For usage in LLVM_DEBUG macros.
 inline std::string DebugStr(const Align &A) {
index 6e9eacb..8f04311 100644 (file)
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/OptimizedStructLayout.h"
+#include "llvm/ADT/Optional.h"
 
 using namespace llvm;
 
index bc28b55..07854dd 100644 (file)
@@ -235,7 +235,7 @@ std::vector<uint64_t> getValidAlignmentsForDeathTest() {
 std::vector<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
 
 TEST(AlignmentDeathTest, CantConvertUnsetMaybe) {
-  EXPECT_DEATH((*MaybeAlign(0)), ".*");
+  EXPECT_DEATH((MaybeAlign(0).value()), ".*");
 }
 
 TEST(AlignmentDeathTest, InvalidCTors) {