Skip to content

Commit 223e2ef

Browse files
committed
[clang] Nits on uses of raw_string_ostream (NFC)
* Don't call raw_string_ostream::flush(), which is essentially a no-op. * Strip unneeded calls to raw_string_ostream::str(), to avoid extra indirection.
1 parent 18f1c98 commit 223e2ef

File tree

8 files changed

+12
-21
lines changed

8 files changed

+12
-21
lines changed

clang/lib/AST/APValue.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,6 @@ std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const {
947947
std::string Result;
948948
llvm::raw_string_ostream Out(Result);
949949
printPretty(Out, Ctx, Ty);
950-
Out.flush();
951950
return Result;
952951
}
953952

clang/lib/AST/DeclPrinter.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
629629
EOut << ")";
630630
}
631631
EOut << " ";
632-
EOut.flush();
633632
Out << Proto;
634633
}
635634

@@ -790,7 +789,6 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
790789
llvm::raw_string_ostream EOut(Proto);
791790
FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
792791
Indentation, "\n", &Context);
793-
EOut.flush();
794792
Proto += ")";
795793
}
796794
}

clang/lib/AST/Expr.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ std::string SYCLUniqueStableNameExpr::ComputeName(ASTContext &Context,
612612
llvm::raw_string_ostream Out(Buffer);
613613
Ctx->mangleCanonicalTypeName(Ty, Out);
614614

615-
return Out.str();
615+
return Buffer;
616616
}
617617

618618
PredefinedExpr::PredefinedExpr(SourceLocation L, QualType FNTy,
@@ -798,7 +798,6 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
798798
FD->printQualifiedName(POut, Policy);
799799

800800
if (IK == PredefinedIdentKind::Function) {
801-
POut.flush();
802801
Out << Proto;
803802
return std::string(Name);
804803
}
@@ -880,15 +879,12 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
880879
}
881880
}
882881

883-
TOut.flush();
884882
if (!TemplateParams.empty()) {
885883
// remove the trailing comma and space
886884
TemplateParams.resize(TemplateParams.size() - 2);
887885
POut << " [" << TemplateParams << "]";
888886
}
889887

890-
POut.flush();
891-
892888
// Print "auto" for all deduced return types. This includes C++1y return
893889
// type deduction and lambdas. For trailing return types resolve the
894890
// decltype expression. Otherwise print the real type when this is

clang/lib/AST/Mangle.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,9 @@ class ASTNameGenerator::Implementation {
574574
std::string BackendBuf;
575575
llvm::raw_string_ostream BOS(BackendBuf);
576576

577-
llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
577+
llvm::Mangler::getNameWithPrefix(BOS, FrontendBuf, DL);
578578

579-
return BOS.str();
579+
return BackendBuf;
580580
}
581581

582582
std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T,
@@ -589,9 +589,9 @@ class ASTNameGenerator::Implementation {
589589
std::string BackendBuf;
590590
llvm::raw_string_ostream BOS(BackendBuf);
591591

592-
llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
592+
llvm::Mangler::getNameWithPrefix(BOS, FrontendBuf, DL);
593593

594-
return BOS.str();
594+
return BackendBuf;
595595
}
596596
};
597597

clang/lib/AST/MicrosoftMangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ void MicrosoftCXXNameMangler::mangleNestedName(GlobalDecl GD) {
13961396
Stream << '_' << Discriminator;
13971397
if (ParameterDiscriminator)
13981398
Stream << '_' << ParameterDiscriminator;
1399-
return Stream.str();
1399+
return Buffer;
14001400
};
14011401

14021402
unsigned Discriminator = BD->getBlockManglingNumber();

clang/lib/AST/StmtViz.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
3434
static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
3535

3636
#ifndef NDEBUG
37-
std::string OutSStr;
38-
llvm::raw_string_ostream Out(OutSStr);
37+
std::string OutStr;
38+
llvm::raw_string_ostream Out(OutStr);
3939

4040
if (Node)
4141
Out << Node->getStmtClassName();
4242
else
4343
Out << "<NULL>";
4444

45-
std::string OutStr = Out.str();
4645
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
4746

4847
// Process string output to make it nicer...

clang/lib/ASTMatchers/Dynamic/Registry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ Registry::getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes) {
791791
TypedText += "\"";
792792
}
793793

794-
Completions.emplace_back(TypedText, OS.str(), MaxSpecificity);
794+
Completions.emplace_back(TypedText, Decl, MaxSpecificity);
795795
}
796796
}
797797

clang/lib/Analysis/CFG.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6164,7 +6164,7 @@ void CFGBlock::printTerminatorJson(raw_ostream &Out, const LangOptions &LO,
61646164

61656165
printTerminator(TempOut, LO);
61666166

6167-
Out << JsonFormat(TempOut.str(), AddQuotes);
6167+
Out << JsonFormat(Buf, AddQuotes);
61686168
}
61696169

61706170
// Returns true if by simply looking at the block, we can be sure that it
@@ -6345,10 +6345,9 @@ struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits {
63456345
DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
63466346

63476347
static std::string getNodeLabel(const CFGBlock *Node, const CFG *Graph) {
6348-
std::string OutSStr;
6349-
llvm::raw_string_ostream Out(OutSStr);
6348+
std::string OutStr;
6349+
llvm::raw_string_ostream Out(OutStr);
63506350
print_block(Out,Graph, *Node, *GraphHelper, false, false);
6351-
std::string& OutStr = Out.str();
63526351

63536352
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
63546353

0 commit comments

Comments
 (0)