[PATCH] D24904: Allow StringRef to be constructed with a null pointer

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 13:16:54 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL282433: Allow StringRef to be constructed from a null pointer. (authored by zturner).

Changed prior to commit:
  https://reviews.llvm.org/D24904?vs=72438&id=72550#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D24904

Files:
  llvm/trunk/include/llvm/ADT/StringRef.h

Index: llvm/trunk/include/llvm/ADT/StringRef.h
===================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h
+++ llvm/trunk/include/llvm/ADT/StringRef.h
@@ -73,14 +73,14 @@
     /// Construct an empty string ref.
     /*implicit*/ StringRef() : Data(nullptr), Length(0) {}
 
+    /// Disable conversion from nullptr.  This prevents things like
+    /// if (S == nullptr)
     StringRef(std::nullptr_t) = delete;
 
     /// Construct a string ref from a cstring.
+    LLVM_ATTRIBUTE_ALWAYS_INLINE
     /*implicit*/ StringRef(const char *Str)
-      : Data(Str) {
-        assert(Str && "StringRef cannot be built from a NULL argument");
-        Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
-      }
+        : Data(Str), Length(Str ? ::strlen(Str) : 0) {}
 
     /// Construct a string ref from a pointer and length.
     LLVM_ATTRIBUTE_ALWAYS_INLINE


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24904.72550.patch
Type: text/x-patch
Size: 927 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160926/d1dfc2cd/attachment.bin>


More information about the llvm-commits mailing list