-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[llvm] Avoid 'raw_string_ostream::str()' (NFC) #97203
Conversation
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/pr-subscribers-llvm-support @llvm/pr-subscribers-debuginfo Author: Youngsuk Kim (JOE1994) ChangesSince Work towards TODO comment to remove Full diff: https://github.com/llvm/llvm-project/pull/97203.diff 5 Files Affected:
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 {
|
The TODO comment I'm referring to: llvm-project/llvm/include/llvm/Support/raw_ostream.h Lines 676 to 679 in 8044158
|
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()`.
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 useraw_string_ostream::str()
.Work towards TODO comment to remove
raw_string_ostream::str()
.