Skip to content

Commit d40768f

Browse files
authored
[llvm] Avoid 'raw_string_ostream::str()' (NFC) (#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()`.
1 parent 66d86a6 commit d40768f

File tree

5 files changed

+12
-12
lines changed

5 files changed

+12
-12
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class DWARFContext : public DIContext {
422422
for (unsigned Size : DWARFContext::getSupportedAddressSizes())
423423
Stream << LS << Size;
424424
Stream << ')';
425-
return make_error<StringError>(Stream.str(), EC);
425+
return make_error<StringError>(Buffer, EC);
426426
}
427427

428428
std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath);

llvm/include/llvm/Support/Error.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ErrorInfoBase {
5454
std::string Msg;
5555
raw_string_ostream OS(Msg);
5656
log(OS);
57-
return OS.str();
57+
return Msg;
5858
}
5959

6060
/// Convert this error to a std::error_code.
@@ -761,7 +761,7 @@ inline void cantFail(Error Err, const char *Msg = nullptr) {
761761
std::string Str;
762762
raw_string_ostream OS(Str);
763763
OS << Msg << "\n" << Err;
764-
Msg = OS.str().c_str();
764+
Msg = Str.c_str();
765765
#endif
766766
llvm_unreachable(Msg);
767767
}
@@ -792,7 +792,7 @@ T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {
792792
raw_string_ostream OS(Str);
793793
auto E = ValOrErr.takeError();
794794
OS << Msg << "\n" << E;
795-
Msg = OS.str().c_str();
795+
Msg = Str.c_str();
796796
#endif
797797
llvm_unreachable(Msg);
798798
}
@@ -823,7 +823,7 @@ T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {
823823
raw_string_ostream OS(Str);
824824
auto E = ValOrErr.takeError();
825825
OS << Msg << "\n" << E;
826-
Msg = OS.str().c_str();
826+
Msg = Str.c_str();
827827
#endif
828828
llvm_unreachable(Msg);
829829
}
@@ -1338,7 +1338,7 @@ class FileError final : public ErrorInfo<FileError> {
13381338
std::string Msg;
13391339
raw_string_ostream OS(Msg);
13401340
Err->log(OS);
1341-
return OS.str();
1341+
return Msg;
13421342
}
13431343

13441344
StringRef getFileName() const { return FileName; }

llvm/include/llvm/Support/GraphWriter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ class GraphWriter {
228228
O << "|";
229229

230230
if (RenderUsingHTML)
231-
O << EdgeSourceLabels.str();
231+
O << edgeSourceLabels;
232232
else
233-
O << "{" << EdgeSourceLabels.str() << "}";
233+
O << "{" << edgeSourceLabels << "}";
234234

235235
if (DTraits.renderGraphFromBottomUp())
236236
if (!RenderUsingHTML)

llvm/include/llvm/Support/ScopedPrinter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ template <class T> std::string to_string(const T &Value) {
8686
std::string number;
8787
raw_string_ostream stream(number);
8888
stream << Value;
89-
return stream.str();
89+
return number;
9090
}
9191

9292
template <typename T, typename TEnum>

llvm/include/llvm/Support/YAMLTraits.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) {
10261026
std::string Storage;
10271027
raw_string_ostream Buffer(Storage);
10281028
BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
1029-
StringRef Str = Buffer.str();
1029+
StringRef Str(Storage);
10301030
YamlIO.blockScalarString(Str);
10311031
} else {
10321032
StringRef Str;
@@ -1046,8 +1046,8 @@ yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
10461046
raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage);
10471047
TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer,
10481048
TagBuffer);
1049-
io.scalarTag(TagBuffer.str());
1050-
StringRef ScalarStr = ScalarBuffer.str();
1049+
io.scalarTag(TagStorage);
1050+
StringRef ScalarStr(ScalarStorage);
10511051
io.scalarString(ScalarStr,
10521052
TaggedScalarTraits<T>::mustQuote(Val, ScalarStr));
10531053
} else {

0 commit comments

Comments
 (0)