[llvm] e11b5b8 - Add Twine support for std::string_view.

Sterling Augustine via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 8 20:30:06 PDT 2021


Author: Sterling Augustine
Date: 2021-06-08T20:19:04-07:00
New Revision: e11b5b87bebf2aad41ad769015a21567198291b9

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

LOG: Add Twine support for std::string_view.

With Twine now ubiquitous after rG92a79dbe91413f685ab19295fc7a6297dbd6c824,
it needs support for string_view when building clang with newer C++ standards.

This is similar to how StringRef is handled.

Differential Revision: https://reviews.llvm.org/D103935

Added: 
    

Modified: 
    llvm/include/llvm/ADT/Twine.h
    llvm/include/llvm/Support/raw_ostream.h
    llvm/lib/Support/Twine.cpp
    llvm/unittests/ADT/TwineTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/Twine.h b/llvm/include/llvm/ADT/Twine.h
index 4140c22aad3d9..235b814f4d399 100644
--- a/llvm/include/llvm/ADT/Twine.h
+++ b/llvm/include/llvm/ADT/Twine.h
@@ -15,6 +15,9 @@
 #include <cassert>
 #include <cstdint>
 #include <string>
+#if __cplusplus > 201402L
+#include <string_view>
+#endif
 
 namespace llvm {
 
@@ -99,6 +102,11 @@ namespace llvm {
       /// A pointer to a StringRef instance.
       StringRefKind,
 
+#if __cplusplus > 201402L
+      // A pointer to a std::string_view instance.
+      StdStringViewKind,
+#endif
+
       /// A pointer to a SmallString instance.
       SmallStringKind,
 
@@ -139,6 +147,9 @@ namespace llvm {
       const char *cString;
       const std::string *stdString;
       const StringRef *stringRef;
+#if __cplusplus > 201402L
+      const std::string_view *stdStringView;
+#endif
       const SmallVectorImpl<char> *smallString;
       const formatv_object_base *formatvObject;
       char character;
@@ -283,6 +294,15 @@ namespace llvm {
       assert(isValid() && "Invalid twine!");
     }
 
+#if __cplusplus > 201402L
+    /// Construct from an std::string_view.
+    /*implicit*/ Twine(const std::string_view &Str)
+        : LHSKind(StdStringViewKind) {
+      LHS.stdStringView = &Str;
+      assert(isValid() && "Invalid twine!");
+    }
+#endif
+
     /// Construct from a StringRef.
     /*implicit*/ Twine(const StringRef &Str) : LHSKind(StringRefKind) {
       LHS.stringRef = &Str;
@@ -410,6 +430,9 @@ namespace llvm {
       case EmptyKind:
       case CStringKind:
       case StdStringKind:
+#if __cplusplus > 201402L
+      case StdStringViewKind:
+#endif
       case StringRefKind:
       case SmallStringKind:
         return true;
@@ -440,10 +463,18 @@ namespace llvm {
       assert(isSingleStringRef() &&"This cannot be had as a single stringref!");
       switch (getLHSKind()) {
       default: llvm_unreachable("Out of sync with isSingleStringRef");
-      case EmptyKind:      return StringRef();
-      case CStringKind:    return StringRef(LHS.cString);
-      case StdStringKind:  return StringRef(*LHS.stdString);
-      case StringRefKind:  return *LHS.stringRef;
+      case EmptyKind:
+        return StringRef();
+      case CStringKind:
+        return StringRef(LHS.cString);
+      case StdStringKind:
+        return StringRef(*LHS.stdString);
+#if __cplusplus > 201402L
+      case StdStringViewKind:
+        return StringRef(*LHS.stdStringView);
+#endif
+      case StringRefKind:
+        return *LHS.stringRef;
       case SmallStringKind:
         return StringRef(LHS.smallString->data(), LHS.smallString->size());
       }

diff  --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index 86d6570dfb0f4..c669c2babad9e 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -22,6 +22,9 @@
 #include <cstdint>
 #include <cstring>
 #include <string>
+#if __cplusplus > 201402L
+#include <string_view>
+#endif
 #include <system_error>
 #include <type_traits>
 
@@ -233,6 +236,12 @@ class raw_ostream {
     return write(Str.data(), Str.length());
   }
 
+#if __cplusplus > 201402L
+  raw_ostream &operator<<(const std::string_view &Str) {
+    return write(Str.data(), Str.length());
+  }
+#endif
+
   raw_ostream &operator<<(const SmallVectorImpl<char> &Str) {
     return write(Str.data(), Str.size());
   }

diff  --git a/llvm/lib/Support/Twine.cpp b/llvm/lib/Support/Twine.cpp
index fbbcd8848f1cd..c1a254c99c700 100644
--- a/llvm/lib/Support/Twine.cpp
+++ b/llvm/lib/Support/Twine.cpp
@@ -68,6 +68,11 @@ void Twine::printOneChild(raw_ostream &OS, Child Ptr,
   case Twine::StdStringKind:
     OS << *Ptr.stdString;
     break;
+#if __cplusplus > 201402L
+  case StdStringViewKind:
+    OS << StringRef(*Ptr.stdStringView);
+    break;
+#endif
   case Twine::StringRefKind:
     OS << *Ptr.stringRef;
     break;
@@ -123,6 +128,11 @@ void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
     OS << "std::string:\""
        << Ptr.stdString << "\"";
     break;
+#if __cplusplus > 201402L
+  case Twine::StdStringViewKind:
+    OS << "std::string_view:\"" << StringRef(*Ptr.stdStringView) << "\"";
+    break;
+#endif
   case Twine::StringRefKind:
     OS << "stringref:\""
        << Ptr.stringRef << "\"";

diff  --git a/llvm/unittests/ADT/TwineTest.cpp b/llvm/unittests/ADT/TwineTest.cpp
index 52cec68210b7d..b9d107dea6772 100644
--- a/llvm/unittests/ADT/TwineTest.cpp
+++ b/llvm/unittests/ADT/TwineTest.cpp
@@ -32,6 +32,9 @@ TEST(TwineTest, Construction) {
   EXPECT_EQ("hi", Twine(StringRef("hithere", 2)).str());
   EXPECT_EQ("hi", Twine(SmallString<4>("hi")).str());
   EXPECT_EQ("hi", Twine(formatv("{0}", "hi")).str());
+#if __cplusplus > 201402L
+  EXPECT_EQ("hi", Twine(std::string_view("hi")).str());
+#endif
 }
 
 TEST(TwineTest, Numbers) {
@@ -73,6 +76,10 @@ TEST(TwineTest, Concat) {
             repr(Twine().concat(Twine(formatv("howdy")))));
   EXPECT_EQ("(Twine smallstring:\"hey\" cstring:\"there\")", 
             repr(Twine(SmallString<7>("hey")).concat(Twine("there"))));
+#if __cplusplus > 201402L
+  EXPECT_EQ("(Twine std::string_view:\"hey\" cstring:\"there\")",
+            repr(Twine(std::string_view("hey")).concat(Twine("there"))));
+#endif
 
   // Concatenation of unary ropes.
   EXPECT_EQ("(Twine cstring:\"a\" cstring:\"b\")", 


        


More information about the llvm-commits mailing list