From 614717388cfccd6a6d34ed17a4680181e22cab22 Mon Sep 17 00:00:00 2001 From: Alp Toker Date: Thu, 26 Jun 2014 00:00:48 +0000 Subject: 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 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. llvm-svn: 211749 --- llvm/lib/IR/Core.cpp | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) (limited to 'llvm/lib/IR/Core.cpp') diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 2c49d5b949c..bf936d6dc7e 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -62,6 +62,11 @@ void LLVMShutdown() { /*===-- Error handling ----------------------------------------------------===*/ +static char *LLVMCreateMessage(StringRef Message) { + assert(Message.find('\0') == Message.npos); + return strndup(Message.data(), Message.size()); +} + char *LLVMCreateMessage(const char *Message) { return strdup(Message); } @@ -110,14 +115,10 @@ unsigned LLVMGetMDKindID(const char* Name, unsigned SLen) { } char *LLVMGetDiagInfoDescription(LLVMDiagnosticInfoRef DI) { - std::string MsgStorage; - raw_string_ostream Stream(MsgStorage); - DiagnosticPrinterRawOStream DP(Stream); - + string_ostream Msg; + DiagnosticPrinterRawOStream DP(Msg); unwrap(DI)->print(DP); - Stream.flush(); - - return LLVMCreateMessage(MsgStorage.c_str()); + return LLVMCreateMessage(Msg.str()); } LLVMDiagnosticSeverity LLVMGetDiagInfoSeverity(LLVMDiagnosticInfoRef DI){ @@ -201,13 +202,9 @@ LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename, } char *LLVMPrintModuleToString(LLVMModuleRef M) { - std::string buf; - raw_string_ostream os(buf); - + string_ostream os; unwrap(M)->print(os, nullptr); - os.flush(); - - return strdup(buf.c_str()); + return LLVMCreateMessage(os.str()); } /*--.. Operations on inline assembler ......................................--*/ @@ -278,17 +275,14 @@ void LLVMDumpType(LLVMTypeRef Ty) { } char *LLVMPrintTypeToString(LLVMTypeRef Ty) { - std::string buf; - raw_string_ostream os(buf); + string_ostream os; if (unwrap(Ty)) unwrap(Ty)->print(os); else os << "Printing Type"; - os.flush(); - - return strdup(buf.c_str()); + return strndup(os.str().data(), os.str().size()); } /*--.. Operations on integer types .........................................--*/ @@ -532,17 +526,14 @@ void LLVMDumpValue(LLVMValueRef Val) { } char* LLVMPrintValueToString(LLVMValueRef Val) { - std::string buf; - raw_string_ostream os(buf); + string_ostream os; if (unwrap(Val)) unwrap(Val)->print(os); else os << "Printing Value"; - os.flush(); - - return strdup(buf.c_str()); + return strndup(os.str().data(), os.str().size()); } void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) { -- cgit v1.2.3