Introduce a string_ostream string builder facilty

string_ostream is a safe and efficient string builder that combines opaque
stack storage with a built-in ostream interface.

small_string_ostream<bytes> additionally permits an explicit stack storage size
other than the default 128 bytes to be provided. Beyond that, storage is
transferred to the heap.

This convenient class can be used in most places an
std::string+raw_string_ostream pair or SmallString<>+raw_svector_ostream pair
would previously have been used, in order to guarantee consistent access
without byte truncation.

The patch also converts much of LLVM to use the new facility. These changes
include several probable bug fixes for truncated output, a programming error
that's no longer possible with the new interface.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211749 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alp Toker
2014-06-26 00:00:48 +00:00
parent ce6e7c7a59
commit 2559070422
57 changed files with 222 additions and 285 deletions

View File

@ -549,16 +549,17 @@ void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
break;
}
// Create the string that will be reported to the external diagnostic handler.
std::string MsgStorage;
raw_string_ostream Stream(MsgStorage);
DiagnosticPrinterRawOStream DP(Stream);
string_ostream Msg;
DiagnosticPrinterRawOStream DP(Msg);
DI.print(DP);
Stream.flush();
// Null-terminate the C string.
Msg << '\0';
// If this method has been called it means someone has set up an external
// diagnostic handler. Assert on that.
assert(DiagHandler && "Invalid diagnostic handler");
(*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
(*DiagHandler)(Severity, Msg.str().data(), DiagContext);
}
void