Skip to content

Commit

Permalink
[clang] Nits on uses of raw_string_ostream (NFC)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
JOE1994 committed Sep 14, 2024
1 parent 18f1c98 commit 223e2ef
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 21 deletions.
1 change: 0 additions & 1 deletion clang/lib/AST/APValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,6 @@ std::string APValue::getAsString(const ASTContext &Ctx, QualType Ty) const {
std::string Result;
llvm::raw_string_ostream Out(Result);
printPretty(Out, Ctx, Ty);
Out.flush();
return Result;
}

Expand Down
2 changes: 0 additions & 2 deletions clang/lib/AST/DeclPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ static void printExplicitSpecifier(ExplicitSpecifier ES, llvm::raw_ostream &Out,
EOut << ")";
}
EOut << " ";
EOut.flush();
Out << Proto;
}

Expand Down Expand Up @@ -790,7 +789,6 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
llvm::raw_string_ostream EOut(Proto);
FT->getNoexceptExpr()->printPretty(EOut, nullptr, SubPolicy,
Indentation, "\n", &Context);
EOut.flush();
Proto += ")";
}
}
Expand Down
6 changes: 1 addition & 5 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ std::string SYCLUniqueStableNameExpr::ComputeName(ASTContext &Context,
llvm::raw_string_ostream Out(Buffer);
Ctx->mangleCanonicalTypeName(Ty, Out);

return Out.str();
return Buffer;
}

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

if (IK == PredefinedIdentKind::Function) {
POut.flush();
Out << Proto;
return std::string(Name);
}
Expand Down Expand Up @@ -880,15 +879,12 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
}
}

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

POut.flush();

// Print "auto" for all deduced return types. This includes C++1y return
// type deduction and lambdas. For trailing return types resolve the
// decltype expression. Otherwise print the real type when this is
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/Mangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@ class ASTNameGenerator::Implementation {
std::string BackendBuf;
llvm::raw_string_ostream BOS(BackendBuf);

llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
llvm::Mangler::getNameWithPrefix(BOS, FrontendBuf, DL);

return BOS.str();
return BackendBuf;
}

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

llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
llvm::Mangler::getNameWithPrefix(BOS, FrontendBuf, DL);

return BOS.str();
return BackendBuf;
}
};

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ void MicrosoftCXXNameMangler::mangleNestedName(GlobalDecl GD) {
Stream << '_' << Discriminator;
if (ParameterDiscriminator)
Stream << '_' << ParameterDiscriminator;
return Stream.str();
return Buffer;
};

unsigned Discriminator = BD->getBlockManglingNumber();
Expand Down
5 changes: 2 additions & 3 deletions clang/lib/AST/StmtViz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {

#ifndef NDEBUG
std::string OutSStr;
llvm::raw_string_ostream Out(OutSStr);
std::string OutStr;
llvm::raw_string_ostream Out(OutStr);

if (Node)
Out << Node->getStmtClassName();
else
Out << "<NULL>";

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

// Process string output to make it nicer...
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/ASTMatchers/Dynamic/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ Registry::getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes) {
TypedText += "\"";
}

Completions.emplace_back(TypedText, OS.str(), MaxSpecificity);
Completions.emplace_back(TypedText, Decl, MaxSpecificity);
}
}

Expand Down
7 changes: 3 additions & 4 deletions clang/lib/Analysis/CFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6164,7 +6164,7 @@ void CFGBlock::printTerminatorJson(raw_ostream &Out, const LangOptions &LO,

printTerminator(TempOut, LO);

Out << JsonFormat(TempOut.str(), AddQuotes);
Out << JsonFormat(Buf, AddQuotes);
}

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

static std::string getNodeLabel(const CFGBlock *Node, const CFG *Graph) {
std::string OutSStr;
llvm::raw_string_ostream Out(OutSStr);
std::string OutStr;
llvm::raw_string_ostream Out(OutStr);
print_block(Out,Graph, *Node, *GraphHelper, false, false);
std::string& OutStr = Out.str();

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

Expand Down

0 comments on commit 223e2ef

Please sign in to comment.