[llvm] faa1b57 - [NFC] Use std::optional over llvm::Optional to implement MaybeAlign

Guillaume Chatelet via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 15 07:17:29 PST 2022


Author: Guillaume Chatelet
Date: 2022-12-15T15:17:14Z
New Revision: faa1b57d16009790ed6fd59342b12607c5460b49

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

LOG: [NFC] Use std::optional over llvm::Optional to implement MaybeAlign

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Alignment.h b/llvm/include/llvm/Support/Alignment.h
index 1543a5713d73..4577641818be 100644
--- a/llvm/include/llvm/Support/Alignment.h
+++ b/llvm/include/llvm/Support/Alignment.h
@@ -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 @@ struct Align {
   }
 
   /// 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 @@ struct MaybeAlign : public llvm::Optional<Align> {
   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) {

diff  --git a/llvm/lib/Support/OptimizedStructLayout.cpp b/llvm/lib/Support/OptimizedStructLayout.cpp
index 6e9eacb0a5ca..8f0431106657 100644
--- a/llvm/lib/Support/OptimizedStructLayout.cpp
+++ b/llvm/lib/Support/OptimizedStructLayout.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Support/OptimizedStructLayout.h"
+#include "llvm/ADT/Optional.h"
 
 using namespace llvm;
 

diff  --git a/llvm/unittests/Support/AlignmentTest.cpp b/llvm/unittests/Support/AlignmentTest.cpp
index bc28b55b4089..07854dd32b8b 100644
--- a/llvm/unittests/Support/AlignmentTest.cpp
+++ b/llvm/unittests/Support/AlignmentTest.cpp
@@ -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) {


        


More information about the llvm-commits mailing list