[PATCH] D100130: Set Text/Binary mode for Stdin and Stdout based on OpenFlags

Abhina Sree via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 8 12:20:10 PDT 2021


abhina.sreeskantharajan updated this revision to Diff 336193.
abhina.sreeskantharajan added a comment.

Fix syntax


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D100130/new/

https://reviews.llvm.org/D100130

Files:
  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


Index: llvm/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/lib/Support/raw_ostream.cpp
+++ llvm/lib/Support/raw_ostream.cpp
@@ -574,10 +574,8 @@
   // 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;
   }
 
Index: llvm/lib/Support/Windows/Program.inc
===================================================================
--- llvm/lib/Support/Windows/Program.inc
+++ llvm/lib/Support/Windows/Program.inc
@@ -488,6 +488,16 @@
   return WaitResult;
 }
 
+std::error_code llvm::sys::ChangeStdinMode(sys::fs::OpenFlags Flags){
+  if (!(Flags & fs::OF_CRLF))
+    ChangeStdinToBinary();
+}
+
+std::error_code llvm::sys::ChangeStdoutMode(sys::fs::OpenFlags Flags){
+  if (!(Flags & fs::OF_CRLF))
+    ChangeStdoutToBinary();
+}
+
 std::error_code sys::ChangeStdinToBinary() {
   int result = _setmode(_fileno(stdin), _O_BINARY);
   if (result == -1)
Index: llvm/lib/Support/Unix/Program.inc
===================================================================
--- llvm/lib/Support/Unix/Program.inc
+++ llvm/lib/Support/Unix/Program.inc
@@ -493,6 +493,18 @@
   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 differentiate between text and binary.
   return std::error_code();
Index: llvm/lib/Support/MemoryBuffer.cpp
===================================================================
--- llvm/lib/Support/MemoryBuffer.cpp
+++ llvm/lib/Support/MemoryBuffer.cpp
@@ -512,7 +512,7 @@
   //
   // 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>");
 }
Index: llvm/include/llvm/Support/Program.h
===================================================================
--- llvm/include/llvm/Support/Program.h
+++ 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 @@
   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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100130.336193.patch
Type: text/x-patch
Size: 3528 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210408/b1701bd8/attachment.bin>


More information about the llvm-commits mailing list