Skip to content

Commit ef8ef12

Browse files
committed
[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
1 parent d94c7bf commit ef8ef12

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

llvm/include/llvm/Support/Alignment.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct Align {
5555

5656
public:
5757
/// Default is byte-aligned.
58-
Align() = default;
58+
constexpr Align() = default;
5959
/// Do not perform checks in case of copy/move construct/assign, because the
6060
/// checks have been performed when building `Other`.
6161
Align(const Align &Other) = default;
@@ -73,6 +73,13 @@ struct Align {
7373
/// This is a hole in the type system and should not be abused.
7474
/// Needed to interact with C for instance.
7575
uint64_t value() const { return uint64_t(1) << ShiftValue; }
76+
77+
/// Returns a default constructed Align which corresponds to no alignment.
78+
/// This is useful to test for unalignment as it conveys clear semantic.
79+
/// `if (A != llvm::Align::None())`
80+
/// would be better than
81+
/// `if (A > llvm::Align(1))`
82+
constexpr static const Align None() { return llvm::Align(); }
7683
};
7784

7885
/// Treats the value 0 as a 1, so Align is always at least 1.

llvm/unittests/Support/AlignmentTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ std::vector<uint64_t> getValidAlignments() {
2828
return Out;
2929
}
3030

31-
TEST(AlignmentTest, AlignDefaultCTor) { EXPECT_EQ(Align().value(), 1ULL); }
31+
TEST(AlignmentTest, AlignDefaultCTor) {
32+
EXPECT_EQ(Align().value(), 1ULL);
33+
EXPECT_EQ(Align::None().value(), 1ULL);
34+
}
3235

3336
TEST(AlignmentTest, MaybeAlignDefaultCTor) {
3437
EXPECT_FALSE(MaybeAlign().hasValue());

0 commit comments

Comments
 (0)