From 9911589f5d4abd9994f294816eb5aa522f4d0458 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Wed, 8 Dec 2021 13:41:31 -0800 Subject: [PATCH] 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 --- llvm/include/llvm/ADT/StringRef.h | 4 ++-- llvm/unittests/ADT/StringRefTest.cpp | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index 9f4b89218042..3950910f0635 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -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 diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index 87285e0e61ce..41c35804f122 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -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::value)); + EXPECT_EQ(false, (std::integral_constant::value)); EXPECT_EQ(StringRef("Bar"), StringRefs[1]); constexpr StringLiteral Strings[] = {"Foo", "Bar"}; EXPECT_EQ(StringRef("Foo"), Strings[0]); + EXPECT_EQ(3u, (std::integral_constant::value)); + EXPECT_EQ(false, (std::integral_constant::value)); EXPECT_EQ(StringRef("Bar"), Strings[1]); }