[llvm] [ADT] Make StringRef std::string_view conversion operator constexpr. NFC (PR #77506)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 9 23:43:48 PST 2024


https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/77506

>From 56b76f9f0ff683be0ff95ebee7476e745488b156 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Wed, 10 Jan 2024 01:25:39 +0700
Subject: [PATCH 1/2] [ADT] Make StringRef std::string_view conversion operator
 constexpr. NFC

This would allow us to compare StringRefs via std::string_view, avoiding having
to make the existing StringRef compare machinery constexpr for now, which we
could then use in #77442
---
 llvm/include/llvm/ADT/StringRef.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index d892333de391ce..1c6c96678b5d27 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -128,7 +128,7 @@ namespace llvm {
 
     /// data - Get a pointer to the start of the string (which may not be null
     /// terminated).
-    [[nodiscard]] const char *data() const { return Data; }
+    [[nodiscard]] constexpr const char *data() const { return Data; }
 
     /// empty - Check if the string is empty.
     [[nodiscard]] constexpr bool empty() const { return Length == 0; }
@@ -245,7 +245,7 @@ namespace llvm {
     /// @name Type Conversions
     /// @{
 
-    operator std::string_view() const {
+    constexpr operator std::string_view() const {
       return std::string_view(data(), size());
     }
 

>From 5d77d92d5c91002e20370946dc71e1dcc0aa13f6 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Wed, 10 Jan 2024 14:43:21 +0700
Subject: [PATCH 2/2] Add some static_asserts to the unit tests

---
 llvm/unittests/ADT/StringRefTest.cpp | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp
index a208527b6c8032..8df71e8ad03378 100644
--- a/llvm/unittests/ADT/StringRefTest.cpp
+++ b/llvm/unittests/ADT/StringRefTest.cpp
@@ -59,6 +59,7 @@ TEST(StringRefTest, Construction) {
 TEST(StringRefTest, Conversion) {
   EXPECT_EQ("hello", std::string(StringRef("hello")));
   EXPECT_EQ("hello", std::string_view(StringRef("hello")));
+  static_assert(std::string_view(StringRef("hello")) == "hello");
 }
 
 TEST(StringRefTest, EmptyInitializerList) {
@@ -78,9 +79,22 @@ TEST(StringRefTest, Iteration) {
 
 TEST(StringRefTest, StringOps) {
   const char *p = "hello";
+
   EXPECT_EQ(p, StringRef(p, 0).data());
+  static_assert(StringRef("hello").data()[0] == 'h');
+  static_assert(StringRef("hello").data()[1] == 'e');
+  static_assert(StringRef("hello").data()[2] == 'l');
+  static_assert(StringRef("hello").data()[3] == 'l');
+  static_assert(StringRef("hello").data()[4] == 'o');
+  static_assert(StringRef("hello").data()[5] == '\0');
+
   EXPECT_TRUE(StringRef().empty());
+  static_assert(StringRef("").empty());
+  static_assert(!StringRef("hello").empty());
+
   EXPECT_EQ((size_t) 5, StringRef("hello").size());
+  static_assert(StringRef("hello").size() == 5);
+
   EXPECT_GT( 0, StringRef("aab").compare("aad"));
   EXPECT_EQ( 0, StringRef("aab").compare("aab"));
   EXPECT_LT( 0, StringRef("aab").compare("aaa"));



More information about the llvm-commits mailing list