summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2019-08-26 20:47:56 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2019-08-26 20:47:56 +0000
commit57effbdadc0b382db4ef86addcfd03a64c9eda0a (patch)
tree146f2b3a5ba8b98e50b61fcfb7d96de5595034ee /llvm
parent3c5bd65154acbe052c9be1a22bc9457fdc2836f6 (diff)
downloadbcm5719-llvm-57effbdadc0b382db4ef86addcfd03a64c9eda0a.tar.gz
bcm5719-llvm-57effbdadc0b382db4ef86addcfd03a64c9eda0a.zip
[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. llvm-svn: 369961
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/ADT/StringRef.h18
-rw-r--r--llvm/unittests/ADT/StringRefTest.cpp4
-rw-r--r--llvm/unittests/ProfileData/SampleProfTest.cpp1
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);
OpenPOWER on IntegriCloud