Re-add the assert to StringRef's const char *, length constructor.
authorZachary Turner <zturner@google.com>
Tue, 20 Dec 2016 17:57:56 +0000 (17:57 +0000)
committerZachary Turner <zturner@google.com>
Tue, 20 Dec 2016 17:57:56 +0000 (17:57 +0000)
By putting the assert behind a conditional in the initializer list
we can ensure that it will still work in a constexpr context as
the else branch of the ternary operator won't be examined unless
the condition fails.

llvm-svn: 290188

llvm/include/llvm/ADT/StringRef.h

index d80a848..2cea4ad 100644 (file)
@@ -85,7 +85,9 @@ namespace llvm {
     /// Construct a string ref from a pointer and length.
     LLVM_ATTRIBUTE_ALWAYS_INLINE
     /*implicit*/ constexpr StringRef(const char *data, size_t length)
-        : Data(data), Length(length) {}
+        : Data(data),
+          Length((data || length == 0) ? length : (assert(0 && "Bad StringRef"),
+                                                   length)) {}
 
     /// Construct a string ref from an std::string.
     LLVM_ATTRIBUTE_ALWAYS_INLINE