Skip to content

Commit ac84ada

Browse files
committed
[clang] 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()`.
1 parent 2051736 commit ac84ada

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

clang/lib/Tooling/Transformer/Stencil.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static Error printNode(StringRef Id, const MatchFinder::MatchResult &Match,
5151
if (auto Err = NodeOrErr.takeError())
5252
return Err;
5353
NodeOrErr->print(Os, PrintingPolicy(Match.Context->getLangOpts()));
54-
*Result += Os.str();
54+
*Result += Output;
5555
return Error::success();
5656
}
5757

@@ -371,7 +371,7 @@ class SelectBoundStencil : public clang::transformer::StencilInterface {
371371
Stream << ", " << DefaultStencil->toString();
372372
}
373373
Stream << ")";
374-
return Stream.str();
374+
return Buffer;
375375
}
376376

377377
private:

clang/unittests/AST/MatchVerifier.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class LocationVerifier : public MatchVerifier<NodeType> {
209209
<< ">, found <";
210210
Loc.print(Msg, *Result.SourceManager);
211211
Msg << '>';
212-
this->setFailure(Msg.str());
212+
this->setFailure(MsgStr);
213213
}
214214
}
215215

@@ -256,7 +256,7 @@ class RangeVerifier : public MatchVerifier<NodeType> {
256256
Msg << '-';
257257
End.print(Msg, *Result.SourceManager);
258258
Msg << '>';
259-
this->setFailure(Msg.str());
259+
this->setFailure(MsgStr);
260260
}
261261
}
262262

@@ -282,12 +282,12 @@ class DumpVerifier : public MatchVerifier<DynTypedNode> {
282282
llvm::raw_string_ostream Dump(DumpStr);
283283
Node.dump(Dump, *Result.Context);
284284

285-
if (Dump.str().find(ExpectSubstring) == std::string::npos) {
285+
if (DumpStr.find(ExpectSubstring) == std::string::npos) {
286286
std::string MsgStr;
287287
llvm::raw_string_ostream Msg(MsgStr);
288288
Msg << "Expected dump substring <" << ExpectSubstring << ">, found <"
289-
<< Dump.str() << '>';
290-
this->setFailure(Msg.str());
289+
<< DumpStr << '>';
290+
this->setFailure(MsgStr);
291291
}
292292
}
293293

@@ -309,12 +309,12 @@ class PrintVerifier : public MatchVerifier<DynTypedNode> {
309309
llvm::raw_string_ostream Print(PrintStr);
310310
Node.print(Print, Result.Context->getPrintingPolicy());
311311

312-
if (Print.str() != ExpectString) {
312+
if (PrintStr != ExpectString) {
313313
std::string MsgStr;
314314
llvm::raw_string_ostream Msg(MsgStr);
315315
Msg << "Expected pretty print <" << ExpectString << ">, found <"
316-
<< Print.str() << '>';
317-
this->setFailure(Msg.str());
316+
<< PrintStr << '>';
317+
this->setFailure(MsgStr);
318318
}
319319
}
320320

clang/unittests/Interpreter/InterpreterTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ TEST_F(InterpreterTest, Errors) {
101101
auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get());
102102
auto Err = Interp->Parse("intentional_error v1 = 42; ").takeError();
103103
using ::testing::HasSubstr;
104-
EXPECT_THAT(DiagnosticsOS.str(),
104+
EXPECT_THAT(DiagnosticOutput,
105105
HasSubstr("error: unknown type name 'intentional_error'"));
106106
EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
107107

@@ -186,7 +186,7 @@ static std::string MangleName(NamedDecl *ND) {
186186
std::string mangledName;
187187
llvm::raw_string_ostream RawStr(mangledName);
188188
MangleC->mangleName(ND, RawStr);
189-
return RawStr.str();
189+
return mangledName;
190190
}
191191

192192
TEST_F(InterpreterTest, FindMangledNameSymbol) {

clang/unittests/Tooling/RecursiveASTVisitorTests/DeductionGuide.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DeductionGuideVisitor
2222
std::string Storage;
2323
llvm::raw_string_ostream Stream(Storage);
2424
D->print(Stream);
25-
Match(Stream.str(), D->getLocation());
25+
Match(Storage, D->getLocation());
2626
return true;
2727
}
2828

clang/unittests/Tooling/ReplacementsYamlTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TEST(ReplacementsYamlTest, serializesReplacements) {
4343
" Length: 2\n"
4444
" ReplacementText: 'replacement #2'\n"
4545
"...\n",
46-
YamlContentStream.str().c_str());
46+
YamlContent.c_str());
4747
}
4848

4949
TEST(ReplacementsYamlTest, serializesNewLines) {
@@ -67,7 +67,7 @@ TEST(ReplacementsYamlTest, serializesNewLines) {
6767
" Length: 0\n"
6868
" ReplacementText: \"#include <utility>\\n\"\n"
6969
"...\n",
70-
YamlContentStream.str().c_str());
70+
YamlContent.c_str());
7171
}
7272

7373
TEST(ReplacementsYamlTest, deserializesReplacements) {

0 commit comments

Comments
 (0)