aboutsummaryrefslogtreecommitdiff
path: root/llvm
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2021-12-08 13:41:31 -0800
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2021-12-08 16:15:33 -0800
commit9911589f5d4abd9994f294816eb5aa522f4d0458 (patch)
treefe76477c335f9c20ed4dbcfc25bc7d8b8a074293 /llvm
parentd04ea509df4ff736e7c2b0cbf561ffcc48101943 (diff)
downloadllvm-9911589f5d4abd9994f294816eb5aa522f4d0458.zip
llvm-9911589f5d4abd9994f294816eb5aa522f4d0458.tar.gz
llvm-9911589f5d4abd9994f294816eb5aa522f4d0458.tar.bz2
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
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/ADT/StringRef.h4
-rw-r--r--llvm/unittests/ADT/StringRefTest.cpp4
2 files changed, 6 insertions, 2 deletions
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 9f4b892..3950910 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 87285e0..41c3580 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<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]);
}