abhina.sreeskantharajan created this revision.
abhina.sreeskantharajan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This patch updates the functions createDefaultOutputFile, createOutputFile, 
createOutputFileImpl to use OpenFlags instead of a boolean binary flag.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100872

Files:
  clang/include/clang/Frontend/CompilerInstance.h
  clang/lib/CodeGen/CodeGenAction.cpp
  clang/lib/Frontend/CompilerInstance.cpp
  clang/lib/Frontend/FrontendActions.cpp
  clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
  clang/lib/Frontend/Rewrite/FrontendActions.cpp
  clang/tools/driver/cc1_main.cpp

Index: clang/tools/driver/cc1_main.cpp
===================================================================
--- clang/tools/driver/cc1_main.cpp
+++ clang/tools/driver/cc1_main.cpp
@@ -254,9 +254,10 @@
   if (llvm::timeTraceProfilerEnabled()) {
     SmallString<128> Path(Clang->getFrontendOpts().OutputFile);
     llvm::sys::path::replace_extension(Path, "json");
-    if (auto profilerOutput = Clang->createOutputFile(
-            Path.str(), /*Binary=*/false, /*RemoveFileOnSignal=*/false,
-            /*useTemporary=*/false)) {
+    if (auto profilerOutput =
+            Clang->createOutputFile(Path.str(), llvm::sys::fs::OF_TextWithCRLF,
+                                    /*RemoveFileOnSignal=*/false,
+                                    /*useTemporary=*/false)) {
       llvm::timeTraceProfilerWrite(*profilerOutput);
       // FIXME(ibiryukov): make profilerOutput flush in destructor instead.
       profilerOutput->flush();
Index: clang/lib/Frontend/Rewrite/FrontendActions.cpp
===================================================================
--- clang/lib/Frontend/Rewrite/FrontendActions.cpp
+++ clang/lib/Frontend/Rewrite/FrontendActions.cpp
@@ -40,7 +40,7 @@
 std::unique_ptr<ASTConsumer>
 HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
   if (std::unique_ptr<raw_ostream> OS =
-          CI.createDefaultOutputFile(false, InFile))
+          CI.createDefaultOutputFile(llvm::sys::fs::OF_TextWithCRLF, InFile))
     return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
   return nullptr;
 }
@@ -162,8 +162,8 @@
 
 std::unique_ptr<ASTConsumer>
 RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
-  if (std::unique_ptr<raw_ostream> OS =
-          CI.createDefaultOutputFile(false, InFile, "cpp")) {
+  if (std::unique_ptr<raw_ostream> OS = CI.createDefaultOutputFile(
+          llvm::sys::fs::OF_TextWithCRLF, InFile, "cpp")) {
     if (CI.getLangOpts().ObjCRuntime.isNonFragile())
       return CreateModernObjCRewriter(
           std::string(InFile), std::move(OS), CI.getDiagnostics(),
@@ -184,8 +184,8 @@
 
 void RewriteMacrosAction::ExecuteAction() {
   CompilerInstance &CI = getCompilerInstance();
-  std::unique_ptr<raw_ostream> OS =
-      CI.createDefaultOutputFile(/*Binary=*/true, getCurrentFileOrBufferName());
+  std::unique_ptr<raw_ostream> OS = CI.createDefaultOutputFile(
+      llvm::sys::fs::OF_None, getCurrentFileOrBufferName());
   if (!OS) return;
 
   RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
@@ -193,8 +193,8 @@
 
 void RewriteTestAction::ExecuteAction() {
   CompilerInstance &CI = getCompilerInstance();
-  std::unique_ptr<raw_ostream> OS =
-      CI.createDefaultOutputFile(/*Binary=*/false, getCurrentFileOrBufferName());
+  std::unique_ptr<raw_ostream> OS = CI.createDefaultOutputFile(
+      llvm::sys::fs::OF_TextWithCRLF, getCurrentFileOrBufferName());
   if (!OS) return;
 
   DoRewriteTest(CI.getPreprocessor(), OS.get());
@@ -269,8 +269,8 @@
 
 bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
   if (!OutputStream) {
-    OutputStream =
-        CI.createDefaultOutputFile(/*Binary=*/true, getCurrentFileOrBufferName());
+    OutputStream = CI.createDefaultOutputFile(llvm::sys::fs::OF_None,
+                                              getCurrentFileOrBufferName());
     if (!OutputStream)
       return false;
   }
Index: clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
===================================================================
--- clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
+++ clang/lib/Frontend/InterfaceStubFunctionsConsumer.cpp
@@ -266,7 +266,8 @@
     v.TraverseDecl(context.getTranslationUnitDecl());
 
     MangledSymbols Symbols;
-    auto OS = Instance.createDefaultOutputFile(/*Binary=*/false, InFile, "ifs");
+    auto OS = Instance.createDefaultOutputFile(llvm::sys::fs::OF_TextWithCRLF,
+                                               InFile, "ifs");
     if (!OS)
       return;
 
Index: clang/lib/Frontend/FrontendActions.cpp
===================================================================
--- clang/lib/Frontend/FrontendActions.cpp
+++ clang/lib/Frontend/FrontendActions.cpp
@@ -69,7 +69,7 @@
 std::unique_ptr<ASTConsumer>
 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
   if (std::unique_ptr<raw_ostream> OS =
-          CI.createDefaultOutputFile(false, InFile))
+          CI.createDefaultOutputFile(llvm::sys::fs::OF_TextWithCRLF, InFile))
     return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
   return nullptr;
 }
@@ -138,7 +138,8 @@
                                     std::string &OutputFile) {
   // Because this is exposed via libclang we must disable RemoveFileOnSignal.
   std::unique_ptr<raw_pwrite_stream> OS = CI.createDefaultOutputFile(
-      /*Binary=*/true, InFile, /*Extension=*/"", /*RemoveFileOnSignal=*/false);
+      llvm::sys::fs::OF_None, InFile, /*Extension=*/"",
+      /*RemoveFileOnSignal=*/false);
   if (!OS)
     return nullptr;
 
@@ -216,7 +217,8 @@
   }
 
   // Because this is exposed via libclang we must disable RemoveFileOnSignal.
-  return CI.createDefaultOutputFile(/*Binary=*/true, InFile, /*Extension=*/"",
+  return CI.createDefaultOutputFile(llvm::sys::fs::OF_None, InFile,
+                                    /*Extension=*/"",
                                     /*RemoveFileOnSignal=*/false,
                                     /*CreateMissingDirectories=*/true);
 }
@@ -236,7 +238,7 @@
 std::unique_ptr<raw_pwrite_stream>
 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
                                                 StringRef InFile) {
-  return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
+  return CI.createDefaultOutputFile(llvm::sys::fs::OF_None, InFile, "pcm");
 }
 
 bool GenerateHeaderModuleAction::PrepareToExecuteAction(
@@ -307,7 +309,7 @@
 std::unique_ptr<raw_pwrite_stream>
 GenerateHeaderModuleAction::CreateOutputFile(CompilerInstance &CI,
                                              StringRef InFile) {
-  return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
+  return CI.createDefaultOutputFile(llvm::sys::fs::OF_None, InFile, "pcm");
 }
 
 SyntaxOnlyAction::~SyntaxOnlyAction() {
@@ -808,9 +810,9 @@
   // concern, so if we scan for too long, we'll just assume the file should
   // be opened in binary mode.
 
-  bool BinaryMode = false;
+  llvm::sys::fs::OpenFlags Flags = llvm::sys::fs::OF_Text;
   if (llvm::Triple(LLVM_HOST_TRIPLE).isOSWindows()) {
-    BinaryMode = true;
+    Flags = llvm::sys::fs::OF_None;
     const SourceManager &SM = CI.getSourceManager();
     if (llvm::Optional<llvm::MemoryBufferRef> Buffer =
             SM.getBufferOrNone(SM.getMainFileID())) {
@@ -827,7 +829,7 @@
       while (next < end) {
         if (*cur == 0x0D) {  // CR
           if (*next == 0x0A) // CRLF
-            BinaryMode = false;
+            Flags = llvm::sys::fs::OF_TextWithCRLF;
 
           break;
         } else if (*cur == 0x0A) // LF
@@ -840,7 +842,7 @@
   }
 
   std::unique_ptr<raw_ostream> OS =
-      CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
+      CI.createDefaultOutputFile(Flags, getCurrentFileOrBufferName());
   if (!OS) return;
 
   // If we're preprocessing a module map, start by dumping the contents of the
@@ -895,8 +897,8 @@
 
 void DumpCompilerOptionsAction::ExecuteAction() {
   CompilerInstance &CI = getCompilerInstance();
-  std::unique_ptr<raw_ostream> OSP =
-      CI.createDefaultOutputFile(false, getCurrentFile());
+  std::unique_ptr<raw_ostream> OSP = CI.createDefaultOutputFile(
+      llvm::sys::fs::OF_TextWithCRLF, getCurrentFile());
   if (!OSP)
     return;
 
Index: clang/lib/Frontend/CompilerInstance.cpp
===================================================================
--- clang/lib/Frontend/CompilerInstance.cpp
+++ clang/lib/Frontend/CompilerInstance.cpp
@@ -731,11 +731,9 @@
   }
 }
 
-std::unique_ptr<raw_pwrite_stream>
-CompilerInstance::createDefaultOutputFile(bool Binary, StringRef InFile,
-                                          StringRef Extension,
-                                          bool RemoveFileOnSignal,
-                                          bool CreateMissingDirectories) {
+std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile(
+    llvm::sys::fs::OpenFlags Flags, StringRef InFile, StringRef Extension,
+    bool RemoveFileOnSignal, bool CreateMissingDirectories) {
   StringRef OutputPath = getFrontendOpts().OutputFile;
   Optional<SmallString<128>> PathStorage;
   if (OutputPath.empty()) {
@@ -749,7 +747,7 @@
   }
 
   // Force a temporary file if RemoveFileOnSignal was disabled.
-  return createOutputFile(OutputPath, Binary, RemoveFileOnSignal,
+  return createOutputFile(OutputPath, Flags, RemoveFileOnSignal,
                           getFrontendOpts().UseTemporary || !RemoveFileOnSignal,
                           CreateMissingDirectories);
 }
@@ -758,12 +756,11 @@
   return std::make_unique<llvm::raw_null_ostream>();
 }
 
-std::unique_ptr<raw_pwrite_stream>
-CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary,
-                                   bool RemoveFileOnSignal, bool UseTemporary,
-                                   bool CreateMissingDirectories) {
+std::unique_ptr<raw_pwrite_stream> CompilerInstance::createOutputFile(
+    StringRef OutputPath, llvm::sys::fs::OpenFlags Flags,
+    bool RemoveFileOnSignal, bool UseTemporary, bool CreateMissingDirectories) {
   Expected<std::unique_ptr<raw_pwrite_stream>> OS =
-      createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary,
+      createOutputFileImpl(OutputPath, Flags, RemoveFileOnSignal, UseTemporary,
                            CreateMissingDirectories);
   if (OS)
     return std::move(*OS);
@@ -773,7 +770,8 @@
 }
 
 Expected<std::unique_ptr<llvm::raw_pwrite_stream>>
-CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary,
+CompilerInstance::createOutputFileImpl(StringRef OutputPath,
+                                       llvm::sys::fs::OpenFlags Flags,
                                        bool RemoveFileOnSignal,
                                        bool UseTemporary,
                                        bool CreateMissingDirectories) {
@@ -816,18 +814,15 @@
     TempPath += OutputExtension;
     TempPath += ".tmp";
     int fd;
-    std::error_code EC = llvm::sys::fs::createUniqueFile(
-        TempPath, fd, TempPath,
-        Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_Text);
+    std::error_code EC =
+        llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath, Flags);
 
     if (CreateMissingDirectories &&
         EC == llvm::errc::no_such_file_or_directory) {
       StringRef Parent = llvm::sys::path::parent_path(OutputPath);
       EC = llvm::sys::fs::create_directories(Parent);
       if (!EC) {
-        EC = llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath,
-                                             Binary ? llvm::sys::fs::OF_None
-                                                    : llvm::sys::fs::OF_Text);
+        EC = llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath, Flags);
       }
     }
 
@@ -843,9 +838,7 @@
   if (!OS) {
     OSFile = OutputPath;
     std::error_code EC;
-    OS.reset(new llvm::raw_fd_ostream(
-        *OSFile, EC,
-        (Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_TextWithCRLF)));
+    OS.reset(new llvm::raw_fd_ostream(*OSFile, EC, Flags));
     if (EC)
       return llvm::errorCodeToError(EC);
   }
@@ -859,7 +852,7 @@
   OutputFiles.emplace_back(((OutputPath != "-") ? OutputPath : "").str(),
                            std::move(TempFile));
 
-  if (!Binary || OS->supportsSeeking())
+  if ((Flags & llvm::sys::fs::OF_Text) || OS->supportsSeeking())
     return std::move(OS);
 
   return std::make_unique<llvm::buffer_unique_ostream>(std::move(OS));
Index: clang/lib/CodeGen/CodeGenAction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenAction.cpp
+++ clang/lib/CodeGen/CodeGenAction.cpp
@@ -889,17 +889,19 @@
 GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
   switch (Action) {
   case Backend_EmitAssembly:
-    return CI.createDefaultOutputFile(false, InFile, "s");
+    return CI.createDefaultOutputFile(llvm::sys::fs::OF_TextWithCRLF, InFile,
+                                      "s");
   case Backend_EmitLL:
-    return CI.createDefaultOutputFile(false, InFile, "ll");
+    return CI.createDefaultOutputFile(llvm::sys::fs::OF_TextWithCRLF, InFile,
+                                      "ll");
   case Backend_EmitBC:
-    return CI.createDefaultOutputFile(true, InFile, "bc");
+    return CI.createDefaultOutputFile(llvm::sys::fs::OF_None, InFile, "bc");
   case Backend_EmitNothing:
     return nullptr;
   case Backend_EmitMCNull:
     return CI.createNullOutputFile();
   case Backend_EmitObj:
-    return CI.createDefaultOutputFile(true, InFile, "o");
+    return CI.createDefaultOutputFile(llvm::sys::fs::OF_None, InFile, "o");
   }
 
   llvm_unreachable("Invalid action!");
Index: clang/include/clang/Frontend/CompilerInstance.h
===================================================================
--- clang/include/clang/Frontend/CompilerInstance.h
+++ clang/include/clang/Frontend/CompilerInstance.h
@@ -702,19 +702,19 @@
   /// RemoveFileOnSignal, temporary files will be forced on.
   ///
   /// \return - Null on error.
-  std::unique_ptr<raw_pwrite_stream>
-  createDefaultOutputFile(bool Binary = true, StringRef BaseInput = "",
-                          StringRef Extension = "",
-                          bool RemoveFileOnSignal = true,
-                          bool CreateMissingDirectories = false);
+  std::unique_ptr<raw_pwrite_stream> createDefaultOutputFile(
+      llvm::sys::fs::OpenFlags Flags = llvm::sys::fs::OF_None,
+      StringRef BaseInput = "", StringRef Extension = "",
+      bool RemoveFileOnSignal = true, bool CreateMissingDirectories = false);
 
   /// Create a new output file, optionally deriving the output path name, and
   /// add it to the list of tracked output files.
   ///
   /// \return - Null on error.
   std::unique_ptr<raw_pwrite_stream>
-  createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal,
-                   bool UseTemporary, bool CreateMissingDirectories = false);
+  createOutputFile(StringRef OutputPath, llvm::sys::fs::OpenFlags Flags,
+                   bool RemoveFileOnSignal, bool UseTemporary,
+                   bool CreateMissingDirectories = false);
 
 private:
   /// Create a new output file and add it to the list of tracked output files.
@@ -735,7 +735,7 @@
   /// \param CreateMissingDirectories - When \p UseTemporary is true, create
   /// missing directories in the output path.
   Expected<std::unique_ptr<raw_pwrite_stream>>
-  createOutputFileImpl(StringRef OutputPath, bool Binary,
+  createOutputFileImpl(StringRef OutputPath, llvm::sys::fs::OpenFlags Flags,
                        bool RemoveFileOnSignal, bool UseTemporary,
                        bool CreateMissingDirectories);
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to