Skip to content

Commit 3be2ba0

Browse files
committed
[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
1 parent 23f8993 commit 3be2ba0

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

llvm/include/llvm/Support/Program.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/StringRef.h"
2020
#include "llvm/Config/llvm-config.h"
2121
#include "llvm/Support/ErrorOr.h"
22+
#include "llvm/Support/FileSystem.h"
2223
#include <chrono>
2324
#include <system_error>
2425

@@ -77,6 +78,12 @@ namespace sys {
7778
ErrorOr<std::string>
7879
findProgramByName(StringRef Name, ArrayRef<StringRef> Paths = {});
7980

81+
// These functions change the specified standard stream (stdin or stdout) mode
82+
// based on the Flags. They return errc::success if the specified stream was
83+
// changed. Otherwise, a platform dependent error is returned.
84+
std::error_code ChangeStdinMode(fs::OpenFlags Flags);
85+
std::error_code ChangeStdoutMode(fs::OpenFlags Flags);
86+
8087
// These functions change the specified standard stream (stdin or stdout) to
8188
// binary mode. They return errc::success if the specified stream
8289
// was changed. Otherwise a platform dependent error is returned.

llvm/lib/Support/MemoryBuffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ ErrorOr<std::unique_ptr<MemoryBuffer>> MemoryBuffer::getSTDIN() {
512512
//
513513
// FIXME: That isn't necessarily true, we should try to mmap stdin and
514514
// fallback if it fails.
515-
sys::ChangeStdinToBinary();
515+
sys::ChangeStdinMode(sys::fs::OF_Text);
516516

517517
return getMemoryBufferForStream(sys::fs::getStdinHandle(), "<stdin>");
518518
}

llvm/lib/Support/Unix/Program.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,18 @@ ProcessInfo llvm::sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
493493
return WaitResult;
494494
}
495495

496+
std::error_code llvm::sys::ChangeStdinMode(fs::OpenFlags Flags){
497+
if (!(Flags & fs::OF_Text))
498+
return ChangeStdinToBinary();
499+
return std::error_code();
500+
}
501+
502+
std::error_code llvm::sys::ChangeStdoutMode(fs::OpenFlags Flags){
503+
if (!(Flags & fs::OF_Text))
504+
return ChangeStdoutToBinary();
505+
return std::error_code();
506+
}
507+
496508
std::error_code llvm::sys::ChangeStdinToBinary() {
497509
// Do nothing, as Unix doesn't differentiate between text and binary.
498510
return std::error_code();

llvm/lib/Support/Windows/Program.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,18 @@ ProcessInfo sys::Wait(const ProcessInfo &PI, unsigned SecondsToWait,
488488
return WaitResult;
489489
}
490490

491+
std::error_code llvm::sys::ChangeStdinMode(sys::fs::OpenFlags Flags){
492+
if (!(Flags & fs::OF_CRLF))
493+
return ChangeStdinToBinary();
494+
return std::error_code();
495+
}
496+
497+
std::error_code llvm::sys::ChangeStdoutMode(sys::fs::OpenFlags Flags){
498+
if (!(Flags & fs::OF_CRLF))
499+
return ChangeStdoutToBinary();
500+
return std::error_code();
501+
}
502+
491503
std::error_code sys::ChangeStdinToBinary() {
492504
int result = _setmode(_fileno(stdin), _O_BINARY);
493505
if (result == -1)

llvm/lib/Support/raw_ostream.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,8 @@ static int getFD(StringRef Filename, std::error_code &EC,
574574
// the owner of stdout and may set the "binary" flag globally based on Flags.
575575
if (Filename == "-") {
576576
EC = std::error_code();
577-
// If user requested binary then put stdout into binary mode if
578-
// possible.
579-
if (!(Flags & sys::fs::OF_Text))
580-
sys::ChangeStdoutToBinary();
577+
// Change stdout's text/binary mode based on the Flags.
578+
sys::ChangeStdoutMode(Flags);
581579
return STDOUT_FILENO;
582580
}
583581

0 commit comments

Comments
 (0)