Add r234615 back, but make sure outs() is binary.

Original message.

Have one raw_fd_ostream constructor forward to the other.

This fixes some odd behaviour differences between the two. In particular,
the version that takes a FD no longer unconditionally sets stdout to binary.

llvm-svn: 234734
This commit is contained in:
Rafael Espindola 2015-04-13 10:28:56 +00:00
parent f40ef51feb
commit 79be4cc634
1 changed files with 21 additions and 25 deletions

View File

@ -487,47 +487,41 @@ void format_object_base::home() {
// raw_fd_ostream // raw_fd_ostream
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC, static int getFD(StringRef Filename, std::error_code &EC,
sys::fs::OpenFlags Flags) sys::fs::OpenFlags Flags) {
: Error(false), UseAtomicWrites(false), pos(0) {
EC = std::error_code();
// Handle "-" as stdout. Note that when we do this, we consider ourself // Handle "-" as stdout. Note that when we do this, we consider ourself
// the owner of stdout. This means that we can do things like close the // the owner of stdout. This means that we can do things like close the
// file descriptor when we're done and set the "binary" flag globally. // file descriptor when we're done and set the "binary" flag globally.
if (Filename == "-") { if (Filename == "-") {
FD = STDOUT_FILENO; EC = std::error_code();
// If user requested binary then put stdout into binary mode if // If user requested binary then put stdout into binary mode if
// possible. // possible.
if (!(Flags & sys::fs::F_Text)) if (!(Flags & sys::fs::F_Text))
sys::ChangeStdoutToBinary(); sys::ChangeStdoutToBinary();
// Close stdout when we're done, to detect any output errors. return STDOUT_FILENO;
ShouldClose = true;
return;
} }
int FD;
EC = sys::fs::openFileForWrite(Filename, FD, Flags); EC = sys::fs::openFileForWrite(Filename, FD, Flags);
if (EC)
return -1;
if (EC) { return FD;
ShouldClose = false;
return;
}
// Ok, we successfully opened the file, so it'll need to be closed.
ShouldClose = true;
} }
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
sys::fs::OpenFlags Flags)
: raw_fd_ostream(getFD(Filename, EC, Flags), true) {}
/// FD is the file descriptor that this writes to. If ShouldClose is true, this /// FD is the file descriptor that this writes to. If ShouldClose is true, this
/// closes the file when the stream is destroyed. /// closes the file when the stream is destroyed.
raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered) raw_fd_ostream::raw_fd_ostream(int fd, bool shouldClose, bool unbuffered)
: raw_ostream(unbuffered), FD(fd), ShouldClose(shouldClose), Error(false), : raw_ostream(unbuffered), FD(fd),
UseAtomicWrites(false) { ShouldClose(shouldClose), Error(false), UseAtomicWrites(false) {
#ifdef O_BINARY if (FD < 0 ) {
// Setting STDOUT to binary mode is necessary in Win32 ShouldClose = false;
// to avoid undesirable linefeed conversion. return;
// Don't touch STDERR, or w*printf() (in assert()) would barf wide chars. }
if (fd == STDOUT_FILENO)
setmode(fd, O_BINARY);
#endif
// Get the starting position. // Get the starting position.
off_t loc = ::lseek(FD, 0, SEEK_CUR); off_t loc = ::lseek(FD, 0, SEEK_CUR);
@ -709,7 +703,9 @@ raw_ostream &llvm::outs() {
// Set buffer settings to model stdout behavior. // Set buffer settings to model stdout behavior.
// Delete the file descriptor when the program exits, forcing error // Delete the file descriptor when the program exits, forcing error
// detection. If you don't want this behavior, don't use outs(). // detection. If you don't want this behavior, don't use outs().
static raw_fd_ostream S(STDOUT_FILENO, true); std::error_code EC;
static raw_fd_ostream S("-", EC, sys::fs::F_None);
assert(!EC);
return S; return S;
} }