[llvm] r369961 - [ADT] Make StringRef(const char*) constexpr

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 26 13:47:56 PDT 2019


Author: d0k
Date: Mon Aug 26 13:47:56 2019
New Revision: 369961

URL: http://llvm.org/viewvc/llvm-project?rev=369961&view=rev
Log:
[ADT] Make StringRef(const char*) constexpr

This should let us get rid of StringLiteral in the long term and avoid
chasing accidental StringRef globals once and for all.

This requires C++14, I godbolted it on every compiler I know we support
so I hope there won't be much fallout.

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

Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=369961&r1=369960&r2=369961&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Mon Aug 26 13:47:56 2019
@@ -67,6 +67,20 @@ namespace llvm {
       return ::memcmp(Lhs,Rhs,Length);
     }
 
+    // Constexpr version of std::strlen.
+    static constexpr size_t strLen(const char *Str) {
+#if __cplusplus > 201402L
+      return std::char_traits<char>::length(Str);
+#elif __has_builtin(__builtin_strlen) || defined(__GNUC__)
+      return __builtin_strlen(Str);
+#else
+      const char *Begin = Str;
+      while (*Str != '\0')
+        ++Str;
+      return Str - Begin;
+#endif
+    }
+
   public:
     /// @name Constructors
     /// @{
@@ -79,8 +93,8 @@ namespace llvm {
     StringRef(std::nullptr_t) = delete;
 
     /// Construct a string ref from a cstring.
-    /*implicit*/ StringRef(const char *Str)
-        : Data(Str), Length(Str ? ::strlen(Str) : 0) {}
+    /*implicit*/ constexpr StringRef(const char *Str)
+        : Data(Str), Length(Str ? strLen(Str) : 0) {}
 
     /// Construct a string ref from a pointer and length.
     /*implicit*/ constexpr StringRef(const char *data, size_t length)

Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=369961&r1=369960&r2=369961&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Mon Aug 26 13:47:56 2019
@@ -1050,6 +1050,10 @@ TEST(StringRefTest, DropWhileUntil) {
 }
 
 TEST(StringRefTest, StringLiteral) {
+  constexpr StringRef StringRefs[] = {"Foo", "Bar"};
+  EXPECT_EQ(StringRef("Foo"), StringRefs[0]);
+  EXPECT_EQ(StringRef("Bar"), StringRefs[1]);
+
   constexpr StringLiteral Strings[] = {"Foo", "Bar"};
   EXPECT_EQ(StringRef("Foo"), Strings[0]);
   EXPECT_EQ(StringRef("Bar"), Strings[1]);

Modified: llvm/trunk/unittests/ProfileData/SampleProfTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/SampleProfTest.cpp?rev=369961&r1=369960&r2=369961&view=diff
==============================================================================
--- llvm/trunk/unittests/ProfileData/SampleProfTest.cpp (original)
+++ llvm/trunk/unittests/ProfileData/SampleProfTest.cpp Mon Aug 26 13:47:56 2019
@@ -305,7 +305,6 @@ TEST_F(SampleProfTest, sample_overflow_s
   const uint64_t Max = std::numeric_limits<uint64_t>::max();
   sampleprof_error Result;
 
-  StringRef FooName("_Z3fooi");
   FunctionSamples FooSamples;
   Result = FooSamples.addTotalSamples(1);
   ASSERT_EQ(Result, sampleprof_error::success);




More information about the llvm-commits mailing list