From 08358b1ba17be2ecf115f4c7bf9490d9ebbde377 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Wed, 18 Sep 2019 09:24:40 +0000 Subject: [PATCH] [Alignment] Add a None() member function Summary: This will allow writing `if(A != llvm::Align::None())` which is clearer than `if(A > llvm::Align(1))` This is patch is part of a series to introduce an Alignment type. See this thread for context: http://lists.llvm.org/pipermail/llvm-dev/2019-July/133851.html See this patch for the introduction of the type: https://reviews.llvm.org/D64790 Reviewers: courbet Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D67697 llvm-svn: 372207 --- include/llvm/Support/Alignment.h | 9 ++++++++- unittests/Support/AlignmentTest.cpp | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/llvm/Support/Alignment.h b/include/llvm/Support/Alignment.h index 07a69e12c8a..c2673faf317 100644 --- a/include/llvm/Support/Alignment.h +++ b/include/llvm/Support/Alignment.h @@ -55,7 +55,7 @@ private: public: /// Default is byte-aligned. - Align() = default; + constexpr Align() = default; /// Do not perform checks in case of copy/move construct/assign, because the /// checks have been performed when building `Other`. Align(const Align &Other) = default; @@ -73,6 +73,13 @@ public: /// This is a hole in the type system and should not be abused. /// Needed to interact with C for instance. uint64_t value() const { return uint64_t(1) << ShiftValue; } + + /// Returns a default constructed Align which corresponds to no alignment. + /// This is useful to test for unalignment as it conveys clear semantic. + /// `if (A != llvm::Align::None())` + /// would be better than + /// `if (A > llvm::Align(1))` + constexpr static const Align None() { return llvm::Align(); } }; /// Treats the value 0 as a 1, so Align is always at least 1. diff --git a/unittests/Support/AlignmentTest.cpp b/unittests/Support/AlignmentTest.cpp index 420772541d4..f9cc27b8f42 100644 --- a/unittests/Support/AlignmentTest.cpp +++ b/unittests/Support/AlignmentTest.cpp @@ -28,7 +28,10 @@ std::vector getValidAlignments() { return Out; } -TEST(AlignmentTest, AlignDefaultCTor) { EXPECT_EQ(Align().value(), 1ULL); } +TEST(AlignmentTest, AlignDefaultCTor) { + EXPECT_EQ(Align().value(), 1ULL); + EXPECT_EQ(Align::None().value(), 1ULL); +} TEST(AlignmentTest, MaybeAlignDefaultCTor) { EXPECT_FALSE(MaybeAlign().hasValue());