diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2021-12-08 13:41:31 -0800 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2021-12-08 16:15:33 -0800 |
commit | 9911589f5d4abd9994f294816eb5aa522f4d0458 (patch) | |
tree | fe76477c335f9c20ed4dbcfc25bc7d8b8a074293 /llvm | |
parent | d04ea509df4ff736e7c2b0cbf561ffcc48101943 (diff) | |
download | llvm-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.h | 4 | ||||
-rw-r--r-- | 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 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]); } |