diff options
author | Alp Toker <alp@nuanti.com> | 2014-06-26 00:00:48 +0000 |
---|---|---|
committer | Alp Toker <alp@nuanti.com> | 2014-06-26 00:00:48 +0000 |
commit | 255907042245b77779e3e38c5ce66901866cabe5 (patch) | |
tree | 92db65aa99c37ab5f68cd6adaae1d8cf6d629b8f /lib/IR | |
parent | ce6e7c7a59e8595ea3f30d87cbad4af278cb5ef3 (diff) | |
download | llvm-255907042245b77779e3e38c5ce66901866cabe5.tar.gz llvm-255907042245b77779e3e38c5ce66901866cabe5.tar.bz2 llvm-255907042245b77779e3e38c5ce66901866cabe5.tar.xz |
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
Diffstat (limited to 'lib/IR')
-rw-r--r-- | lib/IR/Core.cpp | 37 | ||||
-rw-r--r-- | lib/IR/DataLayout.cpp | 3 | ||||
-rw-r--r-- | lib/IR/LLVMContext.cpp | 15 |
3 files changed, 22 insertions, 33 deletions
diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 2c49d5b949..bf936d6dc7 100644 --- a/lib/IR/Core.cpp +++ b/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 <null> 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 <null> Value"; - os.flush(); - - return strdup(buf.c_str()); + return strndup(os.str().data(), os.str().size()); } void LLVMReplaceAllUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) { diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index dea05fbef4..3aa775097d 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -519,8 +519,7 @@ const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { } std::string DataLayout::getStringRepresentation() const { - std::string Result; - raw_string_ostream OS(Result); + string_ostream OS; OS << (LittleEndian ? "e" : "E"); diff --git a/lib/IR/LLVMContext.cpp b/lib/IR/LLVMContext.cpp index de825f00b2..201b278285 100644 --- a/lib/IR/LLVMContext.cpp +++ b/lib/IR/LLVMContext.cpp @@ -164,23 +164,22 @@ void LLVMContext::diagnose(const DiagnosticInfo &DI) { } // Otherwise, print the message with a prefix based on the severity. - std::string MsgStorage; - raw_string_ostream Stream(MsgStorage); - DiagnosticPrinterRawOStream DP(Stream); + string_ostream Msg; + DiagnosticPrinterRawOStream DP(Msg); DI.print(DP); - Stream.flush(); + switch (DI.getSeverity()) { case DS_Error: - errs() << "error: " << MsgStorage << "\n"; + errs() << "error: " << Msg.str() << "\n"; exit(1); case DS_Warning: - errs() << "warning: " << MsgStorage << "\n"; + errs() << "warning: " << Msg.str() << "\n"; break; case DS_Remark: - errs() << "remark: " << MsgStorage << "\n"; + errs() << "remark: " << Msg.str() << "\n"; break; case DS_Note: - errs() << "note: " << MsgStorage << "\n"; + errs() << "note: " << Msg.str() << "\n"; break; } } |