[flang-commits] [flang] 84d7f29 - [flang] Tidy uses of raw_string_ostream (NFC)

Youngsuk Kim via flang-commits flang-commits at lists.llvm.org
Wed Sep 18 11:28:21 PDT 2024


Author: Youngsuk Kim
Date: 2024-09-18T13:26:29-05:00
New Revision: 84d7f294c485e36947b412cbfa69ad706ce6c9f0

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

LOG: [flang] Tidy uses of raw_string_ostream (NFC)

As specified in the docs,
1) raw_string_ostream is always unbuffered and
2) the underlying buffer may be used directly

( 65b13610a5226b84889b923bae884ba395ad084d for further reference )

Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.

Added: 
    

Modified: 
    flang/lib/Frontend/FrontendActions.cpp
    flang/lib/Optimizer/Dialect/FIRType.cpp
    flang/lib/Parser/parsing.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index cda82bcb7ecc71..267c3ceb44f33e 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -425,7 +425,7 @@ void PrintPreprocessedAction::executeAction() {
   // If a pre-defined output stream exists, dump the preprocessed content there
   if (!ci.isOutputStreamNull()) {
     // Send the output to the pre-defined output buffer.
-    ci.writeOutputStream(outForPP.str());
+    ci.writeOutputStream(buf);
     return;
   }
 
@@ -436,7 +436,7 @@ void PrintPreprocessedAction::executeAction() {
     return;
   }
 
-  (*os) << outForPP.str();
+  (*os) << buf;
 }
 
 void DebugDumpProvenanceAction::executeAction() {
@@ -756,7 +756,7 @@ getRISCVVScaleRange(CompilerInstance &ci) {
       outputErrMsg << errMsg.getMessage();
     });
     ci.getDiagnostics().Report(clang::diag::err_invalid_feature_combination)
-        << outputErrMsg.str();
+        << buffer;
     return std::nullopt;
   }
 
@@ -1091,8 +1091,7 @@ class BackendRemarkConsumer : public llvm::DiagnosticHandler {
     msgStream << diagInfo.getMsg();
 
     // Emit message.
-    diags.Report(diagID) << clang::AddFlagValue(diagInfo.getPassName())
-                         << msgStream.str();
+    diags.Report(diagID) << clang::AddFlagValue(diagInfo.getPassName()) << msg;
   }
 
   void optimizationRemarkHandler(

diff  --git a/flang/lib/Optimizer/Dialect/FIRType.cpp b/flang/lib/Optimizer/Dialect/FIRType.cpp
index 05f644654efe1b..7a516298e5ef4f 100644
--- a/flang/lib/Optimizer/Dialect/FIRType.cpp
+++ b/flang/lib/Optimizer/Dialect/FIRType.cpp
@@ -533,9 +533,8 @@ int getTypeCode(mlir::Type ty, const fir::KindMapping &kindMap) {
 
 std::string getTypeAsString(mlir::Type ty, const fir::KindMapping &kindMap,
                             llvm::StringRef prefix) {
-  std::string buf;
+  std::string buf = prefix.str();
   llvm::raw_string_ostream name{buf};
-  name << prefix.str();
   if (!prefix.empty())
     name << "_";
   while (ty) {
@@ -606,7 +605,7 @@ std::string getTypeAsString(mlir::Type ty, const fir::KindMapping &kindMap,
       llvm::report_fatal_error("unsupported type");
     }
   }
-  return name.str();
+  return buf;
 }
 
 mlir::Type changeElementType(mlir::Type type, mlir::Type newElementType,

diff  --git a/flang/lib/Parser/parsing.cpp b/flang/lib/Parser/parsing.cpp
index 43a898ff120c5d..37dc113436aa0e 100644
--- a/flang/lib/Parser/parsing.cpp
+++ b/flang/lib/Parser/parsing.cpp
@@ -42,9 +42,9 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
     sourceFile =
         allSources.Open(path, fileError, "."s /*prepend to search path*/);
   }
-  if (!fileError.str().empty()) {
+  if (!buf.empty()) {
     ProvenanceRange range{allSources.AddCompilerInsertion(path)};
-    messages_.Say(range, "%s"_err_en_US, fileError.str());
+    messages_.Say(range, "%s"_err_en_US, buf);
     return sourceFile;
   }
   CHECK(sourceFile);


        


More information about the flang-commits mailing list