diff options
| -rw-r--r-- | llvm/include/llvm/ADT/StringRef.h | 18 | ||||
| -rw-r--r-- | llvm/unittests/ADT/StringRefTest.cpp | 4 | ||||
| -rw-r--r-- | llvm/unittests/ProfileData/SampleProfTest.cpp | 1 | 
3 files changed, 20 insertions, 3 deletions
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h index 4661b1e68b2..52baab17bed 100644 --- a/llvm/include/llvm/ADT/StringRef.h +++ b/llvm/include/llvm/ADT/StringRef.h @@ -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) diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp index f341cf402a0..c37ecf36b63 100644 --- a/llvm/unittests/ADT/StringRefTest.cpp +++ b/llvm/unittests/ADT/StringRefTest.cpp @@ -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]); diff --git a/llvm/unittests/ProfileData/SampleProfTest.cpp b/llvm/unittests/ProfileData/SampleProfTest.cpp index a28a1d8a1aa..ebf66b22c5b 100644 --- a/llvm/unittests/ProfileData/SampleProfTest.cpp +++ b/llvm/unittests/ProfileData/SampleProfTest.cpp @@ -305,7 +305,6 @@ TEST_F(SampleProfTest, sample_overflow_saturation) {    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);  | 

