Skip to content

Commit 9e21174

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 item to remove `raw_string_ostream::str()`.
1 parent ffc459d commit 9e21174

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

clang/lib/InstallAPI/DiagnosticBuilderWrappers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
4848
Stream << PV.second.getAsString();
4949
});
5050
Stream << " ]";
51-
DB.AddString(Stream.str());
51+
DB.AddString(PlatformAsString);
5252
return DB;
5353
}
5454

@@ -91,7 +91,7 @@ const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
9191
std::string VersionString;
9292
raw_string_ostream OS(VersionString);
9393
OS << Version;
94-
DB.AddString(OS.str());
94+
DB.AddString(VersionString);
9595
return DB;
9696
}
9797

@@ -102,7 +102,7 @@ operator<<(const clang::DiagnosticBuilder &DB,
102102
raw_string_ostream OS(IFAsString);
103103

104104
OS << LibAttr.getKey() << " [ " << LibAttr.getValue() << " ]";
105-
DB.AddString(OS.str());
105+
DB.AddString(IFAsString);
106106
return DB;
107107
}
108108

clang/unittests/Frontend/CompilerInstanceTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
9191
DiagOpts, DiagPrinter.get(), /*ShouldOwnClient=*/false);
9292

9393
Diags->Report(diag::err_expected) << "no crash";
94-
ASSERT_EQ(DiagnosticsOS.str(), "error: expected no crash\n");
94+
ASSERT_EQ(DiagnosticOutput, "error: expected no crash\n");
9595
}
9696

9797
} // anonymous namespace

clang/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool LexicallyOrderedDeclVisitor::VisitNamedDecl(const NamedDecl *D) {
8787
}
8888
if (EmitDeclIndices)
8989
OS << "@" << Index++;
90-
Matcher.match(OS.str(), D);
90+
Matcher.match(Path, D);
9191
return true;
9292
}
9393

@@ -96,7 +96,7 @@ bool LexicallyOrderedDeclVisitor::VisitDeclRefExpr(const DeclRefExpr *D) {
9696
llvm::raw_string_ostream OS(Name);
9797
if (EmitStmtIndices)
9898
OS << "@" << Index++;
99-
Matcher.match(OS.str(), D);
99+
Matcher.match(Name, D);
100100
return true;
101101
}
102102

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4786,7 +4786,7 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
47864786
// Write out the declaration merging check logic.
47874787
OS << "static bool DiagnoseMutualExclusions(Sema &S, const NamedDecl *D, "
47884788
<< "const Attr *A) {\n";
4789-
OS << MergeDeclOS.str();
4789+
OS << DeclMergeChecks;
47904790
OS << " return true;\n";
47914791
OS << "}\n\n";
47924792

@@ -4796,7 +4796,7 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
47964796
OS << "static bool DiagnoseMutualExclusions(Sema &S, "
47974797
<< "const SmallVectorImpl<const Attr *> &C) {\n";
47984798
OS << " for (const Attr *A : C) {\n";
4799-
OS << MergeStmtOS.str();
4799+
OS << StmtMergeChecks;
48004800
OS << " }\n";
48014801
OS << " return true;\n";
48024802
OS << "}\n\n";
@@ -4939,7 +4939,7 @@ void EmitClangAttrTextNodeDump(RecordKeeper &Records, raw_ostream &OS) {
49394939
if (!Args.empty())
49404940
OS << " const auto *SA = cast<" << R.getName()
49414941
<< "Attr>(A); (void)SA;\n";
4942-
OS << SS.str();
4942+
OS << FunctionContent;
49434943
OS << " }\n";
49444944
}
49454945
}
@@ -4968,7 +4968,7 @@ void EmitClangAttrNodeTraverse(RecordKeeper &Records, raw_ostream &OS) {
49684968
if (!Args.empty())
49694969
OS << " const auto *SA = cast<" << R.getName()
49704970
<< "Attr>(A); (void)SA;\n";
4971-
OS << SS.str();
4971+
OS << FunctionContent;
49724972
OS << " }\n";
49734973
}
49744974
}

0 commit comments

Comments
 (0)