[PATCH] D27686: Add llvm::StringLiteral

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 09:14:14 PST 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL289551: [ADT] Add llvm::StringLiteral. (authored by zturner).

Changed prior to commit:
  https://reviews.llvm.org/D27686?vs=81138&id=81237#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D27686

Files:
  llvm/trunk/include/llvm/ADT/StringRef.h
  llvm/trunk/unittests/ADT/StringRefTest.cpp


Index: llvm/trunk/include/llvm/ADT/StringRef.h
===================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h
+++ llvm/trunk/include/llvm/ADT/StringRef.h
@@ -84,11 +84,8 @@
 
     /// Construct a string ref from a pointer and length.
     LLVM_ATTRIBUTE_ALWAYS_INLINE
-    /*implicit*/ StringRef(const char *data, size_t length)
-      : Data(data), Length(length) {
-        assert((data || length == 0) &&
-        "StringRef cannot be built from a NULL argument with non-null length");
-      }
+    /*implicit*/ constexpr StringRef(const char *data, size_t length)
+        : Data(data), Length(length) {}
 
     /// Construct a string ref from an std::string.
     LLVM_ATTRIBUTE_ALWAYS_INLINE
@@ -839,6 +836,26 @@
     /// @}
   };
 
+  /// A wrapper around a string literal that serves as a proxy for constructing
+  /// global tables of StringRefs with the length computed at compile time.
+  /// Using this class with a non-literal char array is considered undefined
+  /// behavior.  To prevent this, it is recommended that StringLiteral *only*
+  /// be used in a constexpr context, as such:
+  ///
+  /// constexpr StringLiteral S("test");
+  ///
+  /// Note: There is a subtle behavioral difference in the constructor of
+  /// StringRef and StringLiteral, as illustrated below:
+  ///
+  /// constexpr StringLiteral S("a\0b");  // S.size() == 3
+  /// StringRef S("a\0b");  // S.size() == 1
+  ///
+  class StringLiteral : public StringRef {
+  public:
+    template <size_t N>
+    constexpr StringLiteral(const char (&Str)[N]) : StringRef(Str, N - 1) {}
+  };
+
   /// @name StringRef Comparison Operators
   /// @{
 
Index: llvm/trunk/unittests/ADT/StringRefTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp
@@ -1002,4 +1002,10 @@
   EXPECT_EQ("", Taken);
 }
 
+TEST(StringRefTest, StringLiteral) {
+  constexpr StringLiteral Strings[] = {"Foo", "Bar"};
+  EXPECT_EQ(StringRef("Foo"), Strings[0]);
+  EXPECT_EQ(StringRef("Bar"), Strings[1]);
+}
+
 } // end anonymous namespace


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27686.81237.patch
Type: text/x-patch
Size: 2164 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161213/16840424/attachment.bin>


More information about the llvm-commits mailing list