[clang] ac84ada - [clang] Avoid 'raw_string_ostream::str' (NFC)

Youngsuk Kim via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 30 08:09:04 PDT 2024


Author: Youngsuk Kim
Date: 2024-06-30T10:02:49-05:00
New Revision: ac84ada9a169a72ad136ef05c2c194f594f24a37

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

LOG: [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 comment to remove `raw_string_ostream::str()`.

Added: 
    

Modified: 
    clang/lib/Tooling/Transformer/Stencil.cpp
    clang/unittests/AST/MatchVerifier.h
    clang/unittests/Interpreter/InterpreterTest.cpp
    clang/unittests/Tooling/RecursiveASTVisitorTests/DeductionGuide.cpp
    clang/unittests/Tooling/ReplacementsYamlTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/Transformer/Stencil.cpp b/clang/lib/Tooling/Transformer/Stencil.cpp
index d91c9e0a20cc1..bc4fa6e36057c 100644
--- a/clang/lib/Tooling/Transformer/Stencil.cpp
+++ b/clang/lib/Tooling/Transformer/Stencil.cpp
@@ -51,7 +51,7 @@ static Error printNode(StringRef Id, const MatchFinder::MatchResult &Match,
   if (auto Err = NodeOrErr.takeError())
     return Err;
   NodeOrErr->print(Os, PrintingPolicy(Match.Context->getLangOpts()));
-  *Result += Os.str();
+  *Result += Output;
   return Error::success();
 }
 
@@ -371,7 +371,7 @@ class SelectBoundStencil : public clang::transformer::StencilInterface {
       Stream << ", " << DefaultStencil->toString();
     }
     Stream << ")";
-    return Stream.str();
+    return Buffer;
   }
 
 private:

diff  --git a/clang/unittests/AST/MatchVerifier.h b/clang/unittests/AST/MatchVerifier.h
index da1e351da4a09..60bb4a8716ae8 100644
--- a/clang/unittests/AST/MatchVerifier.h
+++ b/clang/unittests/AST/MatchVerifier.h
@@ -209,7 +209,7 @@ class LocationVerifier : public MatchVerifier<NodeType> {
           << ">, found <";
       Loc.print(Msg, *Result.SourceManager);
       Msg << '>';
-      this->setFailure(Msg.str());
+      this->setFailure(MsgStr);
     }
   }
 
@@ -256,7 +256,7 @@ class RangeVerifier : public MatchVerifier<NodeType> {
       Msg << '-';
       End.print(Msg, *Result.SourceManager);
       Msg << '>';
-      this->setFailure(Msg.str());
+      this->setFailure(MsgStr);
     }
   }
 
@@ -282,12 +282,12 @@ class DumpVerifier : public MatchVerifier<DynTypedNode> {
     llvm::raw_string_ostream Dump(DumpStr);
     Node.dump(Dump, *Result.Context);
 
-    if (Dump.str().find(ExpectSubstring) == std::string::npos) {
+    if (DumpStr.find(ExpectSubstring) == std::string::npos) {
       std::string MsgStr;
       llvm::raw_string_ostream Msg(MsgStr);
       Msg << "Expected dump substring <" << ExpectSubstring << ">, found <"
-          << Dump.str() << '>';
-      this->setFailure(Msg.str());
+          << DumpStr << '>';
+      this->setFailure(MsgStr);
     }
   }
 
@@ -309,12 +309,12 @@ class PrintVerifier : public MatchVerifier<DynTypedNode> {
     llvm::raw_string_ostream Print(PrintStr);
     Node.print(Print, Result.Context->getPrintingPolicy());
 
-    if (Print.str() != ExpectString) {
+    if (PrintStr != ExpectString) {
       std::string MsgStr;
       llvm::raw_string_ostream Msg(MsgStr);
       Msg << "Expected pretty print <" << ExpectString << ">, found <"
-          << Print.str() << '>';
-      this->setFailure(Msg.str());
+          << PrintStr << '>';
+      this->setFailure(MsgStr);
     }
   }
 

diff  --git a/clang/unittests/Interpreter/InterpreterTest.cpp b/clang/unittests/Interpreter/InterpreterTest.cpp
index bbd854149d5f5..29c5ead60b81e 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -101,7 +101,7 @@ TEST_F(InterpreterTest, Errors) {
   auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get());
   auto Err = Interp->Parse("intentional_error v1 = 42; ").takeError();
   using ::testing::HasSubstr;
-  EXPECT_THAT(DiagnosticsOS.str(),
+  EXPECT_THAT(DiagnosticOutput,
               HasSubstr("error: unknown type name 'intentional_error'"));
   EXPECT_EQ("Parsing failed.", llvm::toString(std::move(Err)));
 
@@ -186,7 +186,7 @@ static std::string MangleName(NamedDecl *ND) {
   std::string mangledName;
   llvm::raw_string_ostream RawStr(mangledName);
   MangleC->mangleName(ND, RawStr);
-  return RawStr.str();
+  return mangledName;
 }
 
 TEST_F(InterpreterTest, FindMangledNameSymbol) {

diff  --git a/clang/unittests/Tooling/RecursiveASTVisitorTests/DeductionGuide.cpp b/clang/unittests/Tooling/RecursiveASTVisitorTests/DeductionGuide.cpp
index 274f275ea66a9..cd4bf0eb7bd5a 100644
--- a/clang/unittests/Tooling/RecursiveASTVisitorTests/DeductionGuide.cpp
+++ b/clang/unittests/Tooling/RecursiveASTVisitorTests/DeductionGuide.cpp
@@ -22,7 +22,7 @@ class DeductionGuideVisitor
     std::string Storage;
     llvm::raw_string_ostream Stream(Storage);
     D->print(Stream);
-    Match(Stream.str(), D->getLocation());
+    Match(Storage, D->getLocation());
     return true;
   }
 

diff  --git a/clang/unittests/Tooling/ReplacementsYamlTest.cpp b/clang/unittests/Tooling/ReplacementsYamlTest.cpp
index 3328d9bad55c7..9558d7270159f 100644
--- a/clang/unittests/Tooling/ReplacementsYamlTest.cpp
+++ b/clang/unittests/Tooling/ReplacementsYamlTest.cpp
@@ -43,7 +43,7 @@ TEST(ReplacementsYamlTest, serializesReplacements) {
                "    Length:          2\n"
                "    ReplacementText: 'replacement #2'\n"
                "...\n",
-               YamlContentStream.str().c_str());
+               YamlContent.c_str());
 }
 
 TEST(ReplacementsYamlTest, serializesNewLines) {
@@ -67,7 +67,7 @@ TEST(ReplacementsYamlTest, serializesNewLines) {
                "    Length:          0\n"
                "    ReplacementText: \"#include <utility>\\n\"\n"
                "...\n",
-               YamlContentStream.str().c_str());
+               YamlContent.c_str());
 }
 
 TEST(ReplacementsYamlTest, deserializesReplacements) {


        


More information about the cfe-commits mailing list