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

Youngsuk Kim via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 4 20:00:23 PDT 2024


Author: Youngsuk Kim
Date: 2024-07-04T21:59:19-05:00
New Revision: 9e211744b82a72e08c137f75b1c671713e2891af

URL: https://github.com/llvm/llvm-project/commit/9e211744b82a72e08c137f75b1c671713e2891af
DIFF: https://github.com/llvm/llvm-project/commit/9e211744b82a72e08c137f75b1c671713e2891af.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 item to remove `raw_string_ostream::str()`.

Added: 
    

Modified: 
    clang/lib/InstallAPI/DiagnosticBuilderWrappers.cpp
    clang/unittests/Frontend/CompilerInstanceTest.cpp
    clang/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
    clang/utils/TableGen/ClangAttrEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/InstallAPI/DiagnosticBuilderWrappers.cpp b/clang/lib/InstallAPI/DiagnosticBuilderWrappers.cpp
index cc252d51e3b67..c8d07f229902b 100644
--- a/clang/lib/InstallAPI/DiagnosticBuilderWrappers.cpp
+++ b/clang/lib/InstallAPI/DiagnosticBuilderWrappers.cpp
@@ -48,7 +48,7 @@ const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
           Stream << PV.second.getAsString();
       });
   Stream << " ]";
-  DB.AddString(Stream.str());
+  DB.AddString(PlatformAsString);
   return DB;
 }
 
@@ -91,7 +91,7 @@ const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
   std::string VersionString;
   raw_string_ostream OS(VersionString);
   OS << Version;
-  DB.AddString(OS.str());
+  DB.AddString(VersionString);
   return DB;
 }
 
@@ -102,7 +102,7 @@ operator<<(const clang::DiagnosticBuilder &DB,
   raw_string_ostream OS(IFAsString);
 
   OS << LibAttr.getKey() << " [ " << LibAttr.getValue() << " ]";
-  DB.AddString(OS.str());
+  DB.AddString(IFAsString);
   return DB;
 }
 

diff  --git a/clang/unittests/Frontend/CompilerInstanceTest.cpp b/clang/unittests/Frontend/CompilerInstanceTest.cpp
index a28d72e7d9531..8bc705dd21993 100644
--- a/clang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/clang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -91,7 +91,7 @@ TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
       DiagOpts, DiagPrinter.get(), /*ShouldOwnClient=*/false);
 
   Diags->Report(diag::err_expected) << "no crash";
-  ASSERT_EQ(DiagnosticsOS.str(), "error: expected no crash\n");
+  ASSERT_EQ(DiagnosticOutput, "error: expected no crash\n");
 }
 
 } // anonymous namespace

diff  --git a/clang/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp b/clang/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
index 38079f706f398..5d16595aec801 100644
--- a/clang/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
+++ b/clang/unittests/Tooling/LexicallyOrderedRecursiveASTVisitorTest.cpp
@@ -87,7 +87,7 @@ bool LexicallyOrderedDeclVisitor::VisitNamedDecl(const NamedDecl *D) {
   }
   if (EmitDeclIndices)
     OS << "@" << Index++;
-  Matcher.match(OS.str(), D);
+  Matcher.match(Path, D);
   return true;
 }
 
@@ -96,7 +96,7 @@ bool LexicallyOrderedDeclVisitor::VisitDeclRefExpr(const DeclRefExpr *D) {
   llvm::raw_string_ostream OS(Name);
   if (EmitStmtIndices)
     OS << "@" << Index++;
-  Matcher.match(OS.str(), D);
+  Matcher.match(Name, D);
   return true;
 }
 

diff  --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 7bf6091e63d18..1f83440680330 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -4786,7 +4786,7 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
   // Write out the declaration merging check logic.
   OS << "static bool DiagnoseMutualExclusions(Sema &S, const NamedDecl *D, "
      << "const Attr *A) {\n";
-  OS << MergeDeclOS.str();
+  OS << DeclMergeChecks;
   OS << "  return true;\n";
   OS << "}\n\n";
 
@@ -4796,7 +4796,7 @@ void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
   OS << "static bool DiagnoseMutualExclusions(Sema &S, "
      << "const SmallVectorImpl<const Attr *> &C) {\n";
   OS << "  for (const Attr *A : C) {\n";
-  OS << MergeStmtOS.str();
+  OS << StmtMergeChecks;
   OS << "  }\n";
   OS << "  return true;\n";
   OS << "}\n\n";
@@ -4939,7 +4939,7 @@ void EmitClangAttrTextNodeDump(RecordKeeper &Records, raw_ostream &OS) {
       if (!Args.empty())
         OS << "    const auto *SA = cast<" << R.getName()
            << "Attr>(A); (void)SA;\n";
-      OS << SS.str();
+      OS << FunctionContent;
       OS << "  }\n";
     }
   }
@@ -4968,7 +4968,7 @@ void EmitClangAttrNodeTraverse(RecordKeeper &Records, raw_ostream &OS) {
       if (!Args.empty())
         OS << "    const auto *SA = cast<" << R.getName()
            << "Attr>(A); (void)SA;\n";
-      OS << SS.str();
+      OS << FunctionContent;
       OS << "  }\n";
     }
   }


        


More information about the cfe-commits mailing list