[llvm] r290188 - Re-add the assert to StringRef's const char *, length constructor.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 20 09:57:56 PST 2016


Author: zturner
Date: Tue Dec 20 11:57:56 2016
New Revision: 290188

URL: http://llvm.org/viewvc/llvm-project?rev=290188&view=rev
Log:
Re-add the assert to StringRef's const char *, length constructor.

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.

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=290188&r1=290187&r2=290188&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Tue Dec 20 11:57:56 2016
@@ -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




More information about the llvm-commits mailing list