[llvm] 3be2ba0 - [SystemZ][z/OS][Windows] Add new functions that set Text/Binary mode for Stdin and Stdout based on OpenFlags

Abhina Sreeskantharajan via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 16 05:09:25 PDT 2021


Author: Abhina Sreeskantharajan
Date: 2021-04-16T08:09:19-04:00
New Revision: 3be2ba0ba38a1799d937a0ef302fb792d12d75a8

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

LOG: [SystemZ][z/OS][Windows] Add new functions that set Text/Binary mode for Stdin and Stdout based on OpenFlags

On Windows, we want to open a file in Binary mode if OF_CRLF bit is not set. On z/OS, we want to open a file in Binary mode if the OF_Text bit is not set.

This patch creates two new functions called ChangeStdinMode and ChangeStdoutMode which will take OpenFlags as an arg to determine which mode to set stdin and stdout to. This will enable patches like https://reviews.llvm.org/D100056 to not affect Windows when setting the OF_Text flag for raw_fd_streams.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D100130

Added: 
    

Modified: 
    llvm/include/llvm/Support/Program.h
    llvm/lib/Support/MemoryBuffer.cpp
    llvm/lib/Support/Unix/Program.inc
    llvm/lib/Support/Windows/Program.inc
    llvm/lib/Support/raw_ostream.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/Program.h b/llvm/include/llvm/Support/Program.h
index bfd2719587882..f91fca1c4464a 100644
--- a/llvm/include/llvm/Support/Program.h
+++ b/llvm/include/llvm/Support/Program.h
@@ -19,6 +19,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Config/llvm-config.h"
 #include "llvm/Support/ErrorOr.h"
+#include "llvm/Support/FileSystem.h"
 #include <chrono>
 #include <system_error>
 
@@ -77,6 +78,12 @@ namespace sys {
   ErrorOr<std::string>
   findProgramByName(StringRef Name, ArrayRef<StringRef> Paths = {});
 
+  // These functions change the specified standard stream (stdin or stdout) mode
+  // based on the Flags. They return errc::success if the specified stream was
+  // changed. Otherwise, a platform dependent error is returned.
+  std::error_code ChangeStdinMode(fs::OpenFlags Flags);
+  std::error_code ChangeStdoutMode(fs::OpenFlags Flags);
+
   // These functions change the specified standard stream (stdin or stdout) to
   // binary mode. They return errc::success if the specified stream
   // was changed. Otherwise a platform dependent error is returned.

diff  --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index dbdf41f255ebc..49524f3f6d5ed 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -512,7 +512,7 @@ ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
   //
   // FIXME: That isn't necessarily true, we should try to mmap stdin and
   // fallback if it fails.
-  sys::ChangeStdinToBinary();
+  sys::ChangeStdinMode(sys::fs::OF_Text);
 
   return getMemoryBufferForStream(sys::fs::getStdinHandle(), "<stdin>");
 }

diff  --git a/llvm/lib/Support/Unix/Program.inc b/llvm/lib/Support/Unix/Program.inc
index 3f18d89fbdefc..be59bb0232def 100644
--- a/llvm/lib/Support/Unix/Program.inc
+++ b/llvm/lib/Support/Unix/Program.inc
@@ -493,6 +493,18 @@ ProcessInfo llvm::sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
   return WaitResult;
 }
 
+std::error_code llvm::sys::ChangeStdinMode(fs::OpenFlags Flags){
+  if (!(Flags & fs::OF_Text))
+    return ChangeStdinToBinary();
+  return std::error_code();
+}
+
+std::error_code llvm::sys::ChangeStdoutMode(fs::OpenFlags Flags){
+  if (!(Flags & fs::OF_Text))
+    return ChangeStdoutToBinary();
+  return std::error_code();
+}
+
 std::error_code llvm::sys::ChangeStdinToBinary() {
   // Do nothing, as Unix doesn't 
diff erentiate between text and binary.
   return std::error_code();

diff  --git a/llvm/lib/Support/Windows/Program.inc b/llvm/lib/Support/Windows/Program.inc
index 92a607180c351..824834c1cbbe9 100644
--- a/llvm/lib/Support/Windows/Program.inc
+++ b/llvm/lib/Support/Windows/Program.inc
@@ -488,6 +488,18 @@ ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
   return WaitResult;
 }
 
+std::error_code llvm::sys::ChangeStdinMode(sys::fs::OpenFlags Flags){
+  if (!(Flags & fs::OF_CRLF))
+    return ChangeStdinToBinary();
+  return std::error_code();
+}
+
+std::error_code llvm::sys::ChangeStdoutMode(sys::fs::OpenFlags Flags){
+  if (!(Flags & fs::OF_CRLF))
+    return ChangeStdoutToBinary();
+  return std::error_code();
+}
+
 std::error_code sys::ChangeStdinToBinary() {
   int result = _setmode(_fileno(stdin), _O_BINARY);
   if (result == -1)

diff  --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp
index 440b49f8b8dd8..d4e1c884d125c 100644
--- a/llvm/lib/Support/raw_ostream.cpp
+++ b/llvm/lib/Support/raw_ostream.cpp
@@ -574,10 +574,8 @@ static int getFD(StringRef Filename, std::error_code &EC,
   // the owner of stdout and may set the "binary" flag globally based on Flags.
   if (Filename == "-") {
     EC = std::error_code();
-    // If user requested binary then put stdout into binary mode if
-    // possible.
-    if (!(Flags & sys::fs::OF_Text))
-      sys::ChangeStdoutToBinary();
+    // Change stdout's text/binary mode based on the Flags.
+    sys::ChangeStdoutMode(Flags);
     return STDOUT_FILENO;
   }
 


        


More information about the llvm-commits mailing list