[llvm] r282433 - Allow StringRef to be constructed from a null pointer.

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


Author: zturner
Date: Mon Sep 26 15:08:05 2016
New Revision: 282433

URL: http://llvm.org/viewvc/llvm-project?rev=282433&view=rev
Log:
Allow StringRef to be constructed from a null pointer.

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

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

Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=282433&r1=282432&r2=282433&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Mon Sep 26 15:08:05 2016
@@ -73,14 +73,14 @@ namespace llvm {
     /// 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




More information about the llvm-commits mailing list