[llvm] [llvm] Avoid 'raw_string_ostream::str()' (NFC) (PR #97203)
Youngsuk Kim via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 30 03:13:46 PDT 2024
https://github.com/JOE1994 created https://github.com/llvm/llvm-project/pull/97203
Since `raw_string_ostream` doesn't own the string buffer, it is desirable (in terms of memory safety) for users to directly reference the string buffer rather than use `raw_string_ostream::str()`.
Work towards TODO comment to remove `raw_string_ostream::str()`.
>From 6b2dd01ffd547405821160643f1f5dc810522e19 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Sun, 30 Jun 2024 04:59:05 -0500
Subject: [PATCH] [llvm] Avoid 'raw_string_ostream::str()' (NFC)
Since `raw_string_ostream` doesn't own the string buffer, it is desirable
(in terms of memory safety) for users to directly reference the string buffer
rather than use `raw_string_ostream::str()`.
Work towards TODO comment to remove `raw_string_ostream::str()`.
---
llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h | 2 +-
llvm/include/llvm/Support/Error.h | 10 +++++-----
llvm/include/llvm/Support/GraphWriter.h | 4 ++--
llvm/include/llvm/Support/ScopedPrinter.h | 2 +-
llvm/include/llvm/Support/YAMLTraits.h | 6 +++---
5 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h
index 7be65a8b45e2d..d800850450eb3 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h
@@ -422,7 +422,7 @@ class DWARFContext : public DIContext {
for (unsigned Size : DWARFContext::getSupportedAddressSizes())
Stream << LS << Size;
Stream << ')';
- return make_error<StringError>(Stream.str(), EC);
+ return make_error<StringError>(Buffer, EC);
}
std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath);
diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index 5120f6ab57c03..cb06ac19f0bb7 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -54,7 +54,7 @@ class ErrorInfoBase {
std::string Msg;
raw_string_ostream OS(Msg);
log(OS);
- return OS.str();
+ return Msg;
}
/// Convert this error to a std::error_code.
@@ -761,7 +761,7 @@ inline void cantFail(Error Err, const char *Msg = nullptr) {
std::string Str;
raw_string_ostream OS(Str);
OS << Msg << "\n" << Err;
- Msg = OS.str().c_str();
+ Msg = Str.c_str();
#endif
llvm_unreachable(Msg);
}
@@ -792,7 +792,7 @@ T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {
raw_string_ostream OS(Str);
auto E = ValOrErr.takeError();
OS << Msg << "\n" << E;
- Msg = OS.str().c_str();
+ Msg = Str.c_str();
#endif
llvm_unreachable(Msg);
}
@@ -823,7 +823,7 @@ T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {
raw_string_ostream OS(Str);
auto E = ValOrErr.takeError();
OS << Msg << "\n" << E;
- Msg = OS.str().c_str();
+ Msg = Str.c_str();
#endif
llvm_unreachable(Msg);
}
@@ -1338,7 +1338,7 @@ class FileError final : public ErrorInfo<FileError> {
std::string Msg;
raw_string_ostream OS(Msg);
Err->log(OS);
- return OS.str();
+ return Msg;
}
StringRef getFileName() const { return FileName; }
diff --git a/llvm/include/llvm/Support/GraphWriter.h b/llvm/include/llvm/Support/GraphWriter.h
index dfda605365de3..359b608626dff 100644
--- a/llvm/include/llvm/Support/GraphWriter.h
+++ b/llvm/include/llvm/Support/GraphWriter.h
@@ -228,9 +228,9 @@ class GraphWriter {
O << "|";
if (RenderUsingHTML)
- O << EdgeSourceLabels.str();
+ O << edgeSourceLabels;
else
- O << "{" << EdgeSourceLabels.str() << "}";
+ O << "{" << edgeSourceLabels << "}";
if (DTraits.renderGraphFromBottomUp())
if (!RenderUsingHTML)
diff --git a/llvm/include/llvm/Support/ScopedPrinter.h b/llvm/include/llvm/Support/ScopedPrinter.h
index 596b73bd27e49..675c0ea4457cf 100644
--- a/llvm/include/llvm/Support/ScopedPrinter.h
+++ b/llvm/include/llvm/Support/ScopedPrinter.h
@@ -86,7 +86,7 @@ template <class T> std::string to_string(const T &Value) {
std::string number;
raw_string_ostream stream(number);
stream << Value;
- return stream.str();
+ return number;
}
template <typename T, typename TEnum>
diff --git a/llvm/include/llvm/Support/YAMLTraits.h b/llvm/include/llvm/Support/YAMLTraits.h
index a234e00c76086..1d04783753d5c 100644
--- a/llvm/include/llvm/Support/YAMLTraits.h
+++ b/llvm/include/llvm/Support/YAMLTraits.h
@@ -1026,7 +1026,7 @@ yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) {
std::string Storage;
raw_string_ostream Buffer(Storage);
BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
- StringRef Str = Buffer.str();
+ StringRef Str(Storage);
YamlIO.blockScalarString(Str);
} else {
StringRef Str;
@@ -1046,8 +1046,8 @@ yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage);
TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer,
TagBuffer);
- io.scalarTag(TagBuffer.str());
- StringRef ScalarStr = ScalarBuffer.str();
+ io.scalarTag(TagStorage);
+ StringRef ScalarStr(ScalarStorage);
io.scalarString(ScalarStr,
TaggedScalarTraits<T>::mustQuote(Val, ScalarStr));
} else {
More information about the llvm-commits
mailing list