Index: llvm/include/llvm/ADT/StringRef.h =================================================================== --- llvm/include/llvm/ADT/StringRef.h +++ llvm/include/llvm/ADT/StringRef.h @@ -13,6 +13,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/iterator_range.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/ErrorHandling.h" #include #include #include @@ -126,6 +127,18 @@ LLVM_ATTRIBUTE_ALWAYS_INLINE const char *data() const { return Data; } + /// Get a pointer to the start of the string, enforcing that it is null + /// terminated. This is intended to be used when the contract of the API is + /// to return a null terminated string. + const char *c_str() const { +#if !defined(NDEBUG) || LLVM_ADDRESS_SANITIZER_BUILD || \ + LLVM_MEMORY_SANITIZER_BUILD + if (Data[size()] != '\0') + report_fatal_error("c_str() called on a non-null terminated string"); +#endif + return Data; + } + /// empty - Check if the string is empty. LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const { return Length == 0; }