[llvm] 9911589 - ADT: Make StringRef::size() and StringRef::empty() constexpr

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 8 16:15:42 PST 2021


Author: Duncan P. N. Exon Smith
Date: 2021-12-08T16:15:33-08:00
New Revision: 9911589f5d4abd9994f294816eb5aa522f4d0458

URL: https://github.com/llvm/llvm-project/commit/9911589f5d4abd9994f294816eb5aa522f4d0458
DIFF: https://github.com/llvm/llvm-project/commit/9911589f5d4abd9994f294816eb5aa522f4d0458.diff

LOG: 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

Added: 
    

Modified: 
    llvm/include/llvm/ADT/StringRef.h
    llvm/unittests/ADT/StringRefTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 9f4b892180428..3950910f0635a 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 87285e0e61ce5..41c35804f1226 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]);
 }
 


        


More information about the llvm-commits mailing list