[clang] 918972b - [clang] Strip unneeded calls to raw_string_ostream::str() (NFC)

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 14 01:48:57 PDT 2024


Author: JOE1994
Date: 2024-09-14T04:38:50-04:00
New Revision: 918972bded27de6a2bfacc15b4ad3edebd81f405

URL: https://github.com/llvm/llvm-project/commit/918972bded27de6a2bfacc15b4ad3edebd81f405
DIFF: https://github.com/llvm/llvm-project/commit/918972bded27de6a2bfacc15b4ad3edebd81f405.diff

LOG: [clang] Strip unneeded calls to raw_string_ostream::str() (NFC)

Avoid extra layer of indirection.

p.s.
Also, remove calls to raw_string_ostream::flush(), which are no-ops.

Added: 
    

Modified: 
    clang/tools/clang-refactor/ClangRefactor.cpp
    clang/tools/driver/cc1gen_reproducer_main.cpp
    clang/unittests/AST/SourceLocationTest.cpp
    clang/unittests/AST/TemplateNameTest.cpp
    clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
    clang/unittests/Frontend/OutputStreamTest.cpp
    clang/unittests/StaticAnalyzer/RangeSetTest.cpp
    clang/unittests/Tooling/ASTSelectionTest.cpp
    clang/unittests/Tooling/DiagnosticsYamlTest.cpp
    clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
    clang/unittests/Tooling/RecursiveASTVisitorTests/TemplateArgumentLocTraverser.cpp
    clang/unittests/Tooling/RefactoringTest.cpp
    clang/unittests/Tooling/RewriterTestContext.h
    clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
    clang/utils/TableGen/MveEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-refactor/ClangRefactor.cpp b/clang/tools/clang-refactor/ClangRefactor.cpp
index 175a2b8234e9ac..9310263c446aea 100644
--- a/clang/tools/clang-refactor/ClangRefactor.cpp
+++ b/clang/tools/clang-refactor/ClangRefactor.cpp
@@ -560,7 +560,6 @@ class ClangRefactorTool {
          << "' can't be invoked with the given arguments:\n";
       for (const auto &Opt : MissingOptions)
         OS << "  missing '-" << Opt.getKey() << "' option\n";
-      OS.flush();
       return llvm::make_error<llvm::StringError>(
           Error, llvm::inconvertibleErrorCode());
     }
@@ -591,7 +590,6 @@ class ClangRefactorTool {
       OS << "note: the following actions are supported:\n";
       for (const auto &Subcommand : SubCommands)
         OS.indent(2) << Subcommand->getName() << "\n";
-      OS.flush();
       return llvm::make_error<llvm::StringError>(
           Error, llvm::inconvertibleErrorCode());
     }

diff  --git a/clang/tools/driver/cc1gen_reproducer_main.cpp b/clang/tools/driver/cc1gen_reproducer_main.cpp
index e97fa3d2775667..be081cac8c03b1 100644
--- a/clang/tools/driver/cc1gen_reproducer_main.cpp
+++ b/clang/tools/driver/cc1gen_reproducer_main.cpp
@@ -105,8 +105,8 @@ static std::string generateReproducerMetaInfo(const ClangInvocationInfo &Info) {
   OS << '}';
   // FIXME: Compare unsaved file hashes and report mismatch in the reproducer.
   if (Info.Dump)
-    llvm::outs() << "REPRODUCER METAINFO: " << OS.str() << "\n";
-  return std::move(OS.str());
+    llvm::outs() << "REPRODUCER METAINFO: " << Result << "\n";
+  return Result;
 }
 
 /// Generates a reproducer for a set of arguments from a specific invocation.

diff  --git a/clang/unittests/AST/SourceLocationTest.cpp b/clang/unittests/AST/SourceLocationTest.cpp
index 66daa56aace28a..daea2d62fe4968 100644
--- a/clang/unittests/AST/SourceLocationTest.cpp
+++ b/clang/unittests/AST/SourceLocationTest.cpp
@@ -105,7 +105,7 @@ class WhileParenLocationVerifier : public MatchVerifier<WhileStmt> {
       RParenLoc.print(Msg, *Result.SourceManager);
       Msg << ">";
 
-      this->setFailure(Msg.str());
+      this->setFailure(MsgStr);
     }
   }
 };

diff  --git a/clang/unittests/AST/TemplateNameTest.cpp b/clang/unittests/AST/TemplateNameTest.cpp
index 444ccfb5c9c811..2eac5c508d0595 100644
--- a/clang/unittests/AST/TemplateNameTest.cpp
+++ b/clang/unittests/AST/TemplateNameTest.cpp
@@ -21,7 +21,7 @@ std::string printTemplateName(TemplateName TN, const PrintingPolicy &Policy,
   std::string Result;
   llvm::raw_string_ostream Out(Result);
   TN.print(Out, Policy, Qual);
-  return Out.str();
+  return Result;
 }
 
 TEST(TemplateName, PrintTemplate) {

diff  --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
index 9c4ec07e139a12..137baab53301ae 100644
--- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
+++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
@@ -78,7 +78,7 @@ mutatedBy(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
     std::string Buffer;
     llvm::raw_string_ostream Stream(Buffer);
     By->printPretty(Stream, nullptr, AST->getASTContext().getPrintingPolicy());
-    Chain.emplace_back(StringRef(Stream.str()).trim().str());
+    Chain.emplace_back(StringRef(Buffer).trim().str());
     E = dyn_cast<DeclRefExpr>(By);
   }
   return Chain;

diff  --git a/clang/unittests/Frontend/OutputStreamTest.cpp b/clang/unittests/Frontend/OutputStreamTest.cpp
index 7d360f661daa30..2618558c7e11ee 100644
--- a/clang/unittests/Frontend/OutputStreamTest.cpp
+++ b/clang/unittests/Frontend/OutputStreamTest.cpp
@@ -67,7 +67,7 @@ TEST(FrontendOutputTests, TestVerboseOutputStreamShared) {
 
   bool Success = ExecuteCompilerInvocation(&Compiler);
   EXPECT_FALSE(Success);
-  EXPECT_TRUE(!VerboseStream.str().empty());
+  EXPECT_TRUE(!VerboseBuffer.empty());
   EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated"));
 }
 

diff  --git a/clang/unittests/StaticAnalyzer/RangeSetTest.cpp b/clang/unittests/StaticAnalyzer/RangeSetTest.cpp
index 318877c5625682..9e36aabcf6644c 100644
--- a/clang/unittests/StaticAnalyzer/RangeSetTest.cpp
+++ b/clang/unittests/StaticAnalyzer/RangeSetTest.cpp
@@ -25,7 +25,7 @@ template <class RangeOrSet> static std::string toString(const RangeOrSet &Obj) {
   std::string ObjRepresentation;
   llvm::raw_string_ostream SS(ObjRepresentation);
   Obj.dump(SS);
-  return SS.str();
+  return ObjRepresentation;
 }
 LLVM_ATTRIBUTE_UNUSED static std::string toString(const llvm::APSInt &Point) {
   return toString(Point, 10);

diff  --git a/clang/unittests/Tooling/ASTSelectionTest.cpp b/clang/unittests/Tooling/ASTSelectionTest.cpp
index 38b7df8fd564f9..113165f68449ca 100644
--- a/clang/unittests/Tooling/ASTSelectionTest.cpp
+++ b/clang/unittests/Tooling/ASTSelectionTest.cpp
@@ -171,7 +171,7 @@ TEST(ASTSelectionFinder, CursorAtStartOfFunction) {
         std::string DumpValue;
         llvm::raw_string_ostream OS(DumpValue);
         Node->Children[0].dump(OS);
-        ASSERT_EQ(OS.str(), "FunctionDecl \"f\" contains-selection\n");
+        ASSERT_EQ(DumpValue, "FunctionDecl \"f\" contains-selection\n");
       });
 }
 

diff  --git a/clang/unittests/Tooling/DiagnosticsYamlTest.cpp b/clang/unittests/Tooling/DiagnosticsYamlTest.cpp
index 6d3b4b9939f0d5..52d81948a46095 100644
--- a/clang/unittests/Tooling/DiagnosticsYamlTest.cpp
+++ b/clang/unittests/Tooling/DiagnosticsYamlTest.cpp
@@ -151,7 +151,7 @@ TEST(DiagnosticsYamlTest, serializesDiagnostics) {
   yaml::Output YAML(YamlContentStream);
   YAML << TUD;
 
-  EXPECT_EQ(YAMLContent, YamlContentStream.str());
+  EXPECT_EQ(YAMLContent, YamlContent);
 }
 
 TEST(DiagnosticsYamlTest, deserializesDiagnostics) {

diff  --git a/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
index e207f03971ad2f..d72a110d37e0fd 100644
--- a/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTestDeclVisitor.cpp
@@ -67,7 +67,7 @@ class NamedDeclVisitor
     Decl->getNameForDiagnostic(OS,
                                Decl->getASTContext().getPrintingPolicy(),
                                true);
-    Match(OS.str(), Decl->getLocation());
+    Match(NameWithTemplateArgs, Decl->getLocation());
     return true;
   }
 };

diff  --git a/clang/unittests/Tooling/RecursiveASTVisitorTests/TemplateArgumentLocTraverser.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTests/TemplateArgumentLocTraverser.cpp
index f068e53ae9c288..b87e89f3fd56ff 100644
--- a/clang/unittests/Tooling/RecursiveASTVisitorTests/TemplateArgumentLocTraverser.cpp
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTests/TemplateArgumentLocTraverser.cpp
@@ -21,7 +21,7 @@ class TemplateArgumentLocTraverser
     const TemplateArgument &Arg = ArgLoc.getArgument();
 
     Arg.print(Context->getPrintingPolicy(), Stream, /*IncludeType*/ true);
-    Match(Stream.str(), ArgLoc.getLocation());
+    Match(ArgStr, ArgLoc.getLocation());
     return ExpectedLocationVisitor<TemplateArgumentLocTraverser>::
       TraverseTemplateArgumentLoc(ArgLoc);
   }

diff  --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp
index 77d410f5d3b604..4f0cccdc274963 100644
--- a/clang/unittests/Tooling/RefactoringTest.cpp
+++ b/clang/unittests/Tooling/RefactoringTest.cpp
@@ -135,7 +135,6 @@ static bool checkReplacementError(llvm::Error &&Error,
            << "\n";
     }
   });
-  OS.flush();
   if (ErrorMessage.empty()) return true;
   llvm::errs() << ErrorMessage;
   return false;

diff  --git a/clang/unittests/Tooling/RewriterTestContext.h b/clang/unittests/Tooling/RewriterTestContext.h
index a618ebd3a86577..b7aa1a133c83ec 100644
--- a/clang/unittests/Tooling/RewriterTestContext.h
+++ b/clang/unittests/Tooling/RewriterTestContext.h
@@ -109,7 +109,6 @@ class RewriterTestContext {
     std::string Result;
     llvm::raw_string_ostream OS(Result);
     Rewrite.getEditBuffer(ID).write(OS);
-    OS.flush();
     return Result;
   }
 

diff  --git a/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp b/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
index d68dcc472a7bdb..58c82873967cd2 100644
--- a/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
+++ b/clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp
@@ -378,7 +378,6 @@ void BuiltinNameEmitter::ExtractEnumTypes(ArrayRef<const Record *> Types,
       TypesSeen.insert(std::make_pair(T->getValueAsString("Name"), true));
     }
   }
-  SS.flush();
 }
 
 void BuiltinNameEmitter::EmitDeclarations() {
@@ -731,7 +730,6 @@ void BuiltinNameEmitter::EmitStringMatcher() {
       raw_string_ostream SS(RetStmt);
       SS << "return std::make_pair(" << CumulativeIndex << ", " << Ovl.size()
          << ");";
-      SS.flush();
       ValidBuiltins.push_back(
           StringMatcher::StringPair(std::string(FctName), RetStmt));
     }

diff  --git a/clang/utils/TableGen/MveEmitter.cpp b/clang/utils/TableGen/MveEmitter.cpp
index 6cfaa891241fa9..153ad083029b83 100644
--- a/clang/utils/TableGen/MveEmitter.cpp
+++ b/clang/utils/TableGen/MveEmitter.cpp
@@ -1583,7 +1583,6 @@ void EmitterBase::EmitBuiltinCG(raw_ostream &OS) {
     CodeGenParamAllocator ParamAllocPrelim{&MG.ParamTypes, &OI.ParamValues};
     raw_string_ostream OS(MG.Code);
     Int.genCode(OS, ParamAllocPrelim, 1);
-    OS.flush();
 
     MergeableGroupsPrelim[MG].insert(OI);
   }
@@ -1655,7 +1654,6 @@ void EmitterBase::EmitBuiltinCG(raw_ostream &OS) {
                                        &ParamNumbers};
       raw_string_ostream OS(MG.Code);
       Int->genCode(OS, ParamAlloc, 2);
-      OS.flush();
 
       MergeableGroups[MG].insert(OI);
     }


        


More information about the cfe-commits mailing list