ADT: Make StringRef::size() and StringRef::empty() constexpr

This unblocks using `StringLiteral::size()` for a SmallVector size in
another patch.

Differential Revision: https://reviews.llvm.org/D115395
This commit is contained in:
Duncan P. N. Exon Smith 2021-12-08 13:41:31 -08:00
parent d04ea509df
commit 9911589f5d
2 changed files with 6 additions and 2 deletions

View file

@ -149,11 +149,11 @@ namespace llvm {
/// empty - Check if the string is empty.
LLVM_NODISCARD
bool empty() const { return Length == 0; }
constexpr bool empty() const { return Length == 0; }
/// size - Get the string size.
LLVM_NODISCARD
size_t size() const { return Length; }
constexpr size_t size() const { return Length; }
/// front - Get the first character in the string.
LLVM_NODISCARD

View file

@ -1092,10 +1092,14 @@ TEST(StringRefTest, DropWhileUntil) {
TEST(StringRefTest, StringLiteral) {
constexpr StringRef StringRefs[] = {"Foo", "Bar"};
EXPECT_EQ(StringRef("Foo"), StringRefs[0]);
EXPECT_EQ(3u, (std::integral_constant<size_t, StringRefs[0].size()>::value));
EXPECT_EQ(false, (std::integral_constant<bool, StringRefs[0].empty()>::value));
EXPECT_EQ(StringRef("Bar"), StringRefs[1]);
constexpr StringLiteral Strings[] = {"Foo", "Bar"};
EXPECT_EQ(StringRef("Foo"), Strings[0]);
EXPECT_EQ(3u, (std::integral_constant<size_t, Strings[0].size()>::value));
EXPECT_EQ(false, (std::integral_constant<bool, Strings[0].empty()>::value));
EXPECT_EQ(StringRef("Bar"), Strings[1]);
}