Skip to content

Commit

Permalink
[NFC] Use std::optional over llvm::Optional to implement MaybeAlign
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D140098
  • Loading branch information
gchatelet committed Dec 15, 2022
1 parent 19d300f commit faa1b57
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
31 changes: 23 additions & 8 deletions llvm/include/llvm/Support/Alignment.h
Expand Up @@ -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
Expand Down Expand Up @@ -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>();
}

Expand All @@ -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.
Expand All @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Support/OptimizedStructLayout.cpp
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/Support/OptimizedStructLayout.h"
#include "llvm/ADT/Optional.h"

using namespace llvm;

Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/Support/AlignmentTest.cpp
Expand Up @@ -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) {
Expand Down

0 comments on commit faa1b57

Please sign in to comment.