Skip to content

Commit ac66469

Browse files
committed
[clang] Tidy uses of raw_string_ostream (NFC)
As specified in the docs, 1) raw_string_ostream is always unbuffered and 2) the underlying buffer may be used directly ( 65b1361 for further reference ) * Don't call raw_string_ostream::flush(), which is essentially a no-op. * Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
1 parent 434c9c5 commit ac66469

File tree

10 files changed

+16
-26
lines changed

10 files changed

+16
-26
lines changed

clang/lib/ExtractAPI/DeclarationFragments.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,6 @@ DeclarationFragmentsBuilder::getFragmentsForTemplateArguments(
11101110
Spelling.clear();
11111111
raw_string_ostream OutStream(Spelling);
11121112
CTA.print(Context.getPrintingPolicy(), OutStream, false);
1113-
OutStream.flush();
11141113
}
11151114
}
11161115

clang/lib/Frontend/ChainedIncludesSource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ IntrusiveRefCntPtr<ExternalSemaSource> clang::createChainedIncludesSource(
159159
std::string pchName = includes[i-1];
160160
llvm::raw_string_ostream os(pchName);
161161
os << ".pch" << i-1;
162-
serialBufNames.push_back(os.str());
162+
serialBufNames.push_back(pchName);
163163

164164
IntrusiveRefCntPtr<ASTReader> Reader;
165165
Reader = createASTReader(

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,6 @@ static bool compileModule(CompilerInstance &ImportingInstance,
13831383
std::string InferredModuleMapContent;
13841384
llvm::raw_string_ostream OS(InferredModuleMapContent);
13851385
Module->print(OS);
1386-
OS.flush();
13871386

13881387
Result = compileModuleImpl(
13891388
ImportingInstance, ImportLoc, Module->getTopLevelModuleName(),

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,6 @@ static bool RoundTrip(ParseFn Parse, GenerateFn Generate,
817817
llvm::sys::printArg(OS, Arg, /*Quote=*/true);
818818
OS << ' ';
819819
}
820-
OS.flush();
821820
return Buffer;
822821
};
823822

@@ -1186,7 +1185,6 @@ static bool ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args,
11861185
os << " ";
11871186
os << Args.getArgString(i);
11881187
}
1189-
os.flush();
11901188

11911189
return Diags.getNumErrors() == NumErrorsBefore;
11921190
}
@@ -3735,7 +3733,7 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
37353733
llvm::interleave(
37363734
Opts.OMPTargetTriples, OS,
37373735
[&OS](const llvm::Triple &T) { OS << T.str(); }, ",");
3738-
GenerateArg(Consumer, OPT_fopenmp_targets_EQ, OS.str());
3736+
GenerateArg(Consumer, OPT_fopenmp_targets_EQ, Targets);
37393737
}
37403738

37413739
if (!Opts.OMPHostIRFile.empty())

clang/lib/Frontend/Rewrite/RewriteObjC.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,9 @@ namespace {
221221
return;
222222
}
223223
// Get the new text.
224-
std::string SStr;
225-
llvm::raw_string_ostream S(SStr);
224+
std::string Str;
225+
llvm::raw_string_ostream S(Str);
226226
New->printPretty(S, nullptr, PrintingPolicy(LangOpts));
227-
const std::string &Str = S.str();
228227

229228
// If replacement succeeded or warning disabled return with no warning.
230229
if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) {
@@ -1702,7 +1701,7 @@ Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
17021701
llvm::raw_string_ostream syncExprBuf(syncExprBufS);
17031702
assert(syncExpr != nullptr && "Expected non-null Expr");
17041703
syncExpr->printPretty(syncExprBuf, nullptr, PrintingPolicy(LangOpts));
1705-
syncBuf += syncExprBuf.str();
1704+
syncBuf += syncExprBufS;
17061705
syncBuf += ");";
17071706

17081707
buf += syncBuf;
@@ -2508,7 +2507,7 @@ Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) {
25082507
std::string prettyBufS;
25092508
llvm::raw_string_ostream prettyBuf(prettyBufS);
25102509
Exp->getString()->printPretty(prettyBuf, nullptr, PrintingPolicy(LangOpts));
2511-
Preamble += prettyBuf.str();
2510+
Preamble += prettyBufS;
25122511
Preamble += ",";
25132512
Preamble += utostr(Exp->getString()->getByteLength()) + "};\n";
25142513

clang/lib/StaticAnalyzer/Checkers/OSObjectCStyleCast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void emitDiagnostics(const BoundNodes &Nodes,
6868
Checker,
6969
/*Name=*/"OSObject C-Style Cast",
7070
categories::SecurityError,
71-
OS.str(),
71+
Diagnostics,
7272
PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), ADC),
7373
CE->getSourceRange());
7474
}

clang/lib/StaticAnalyzer/Checkers/ObjCSuperDeallocChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void ObjCSuperDeallocChecker::checkLocation(SVal L, bool IsLoad, const Stmt *S,
164164
if (IvarRegion) {
165165
OS << "Use of instance variable '" << *IvarRegion->getDecl() <<
166166
"' after 'self' has been deallocated";
167-
Desc = OS.str();
167+
Desc = Buf;
168168
}
169169

170170
reportUseAfterDealloc(BaseSym, Desc, S, C);

clang/lib/StaticAnalyzer/Checkers/PointerIterationChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void emitDiagnostics(const BoundNodes &Match, const Decl *D,
5151

5252
BR.EmitBasicReport(ADC->getDecl(), Checker,
5353
"Iteration of pointer-like elements", "Non-determinism",
54-
OS.str(), Location, Range);
54+
Diagnostics, Location, Range);
5555
}
5656

5757
// Assumption: Iteration of ordered containers of pointers is deterministic.

clang/lib/StaticAnalyzer/Core/BugReporter.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,7 @@ PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForSwitchOP(
804804
os << "'Default' branch taken. ";
805805
End = ExecutionContinues(os, C);
806806
}
807-
return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
808-
os.str());
807+
return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, sbuf);
809808
}
810809

811810
PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForGotoOP(
@@ -816,7 +815,7 @@ PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForGotoOP(
816815
const PathDiagnosticLocation &End =
817816
getEnclosingStmtLocation(S, C.getCurrLocationContext());
818817
os << "Control jumps to line " << End.asLocation().getExpansionLineNumber();
819-
return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str());
818+
return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, sbuf);
820819
}
821820

822821
PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForBinaryOP(
@@ -863,8 +862,7 @@ PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForBinaryOP(
863862
PathDiagnosticLocation::createOperatorLoc(B, SM);
864863
}
865864
}
866-
return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
867-
os.str());
865+
return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, sbuf);
868866
}
869867

870868
void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
@@ -900,7 +898,7 @@ void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
900898
llvm::raw_string_ostream os(sbuf);
901899
PathDiagnosticLocation End = ExecutionContinues(os, C);
902900
C.getActivePath().push_front(
903-
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
901+
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, sbuf));
904902
break;
905903
}
906904

@@ -922,7 +920,7 @@ void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
922920
End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
923921

924922
C.getActivePath().push_front(
925-
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
923+
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, sbuf));
926924
break;
927925
}
928926

@@ -947,8 +945,7 @@ void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
947945
End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
948946

949947
C.getActivePath().push_front(
950-
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
951-
os.str()));
948+
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, sbuf));
952949
} else {
953950
PathDiagnosticLocation End = ExecutionContinues(C);
954951

@@ -973,8 +970,7 @@ void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
973970
End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
974971

975972
C.getActivePath().push_front(
976-
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
977-
os.str()));
973+
std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, sbuf));
978974
} else {
979975
PathDiagnosticLocation End = ExecutionContinues(C);
980976
if (const Stmt *S = End.asStmt())

clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,6 @@ void PlistDiagnostics::FlushDiagnosticsImpl(
812812
std::string stats;
813813
llvm::raw_string_ostream os(stats);
814814
llvm::PrintStatisticsJSON(os);
815-
os.flush();
816815
EmitString(o, html::EscapeText(stats)) << '\n';
817816
}
818817

0 commit comments

Comments
 (0)