llvm.org GIT mirror llvm / 2499990
Make raw_fd_ostream consider itself the owner of STDOUT_FILENO when constructed with an output filename of "-". In particular, allow the file descriptor to be closed, and close the file descriptor in the destructor if it hasn't been explicitly closed already, to ensure that any write errors are detected. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111436 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
2 changed file(s) with 10 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
352352 /// be immediately destroyed; the string will be empty if no error occurred.
353353 /// This allows optional flags to control how the file will be opened.
354354 ///
355 /// \param Filename - The file to open. If this is "-" then the
356 /// stream will use stdout instead.
355 /// As a special case, if Filename is "-", then the stream will use
356 /// STDOUT_FILENO instead of opening a file. Note that it will still consider
357 /// itself to own the file descriptor. In particular, it will close the
358 /// file descriptor when it is done (this is necessary to detect
359 /// output errors).
357360 raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
358361 unsigned Flags = 0);
359362
376376
377377 ErrorInfo.clear();
378378
379 // Handle "-" as stdout.
379 // Handle "-" as stdout. Note that when we do this, we consider ourself
380 // the owner of stdout. This means that we can do things like close the
381 // file descriptor when we're done and set the "binary" flag globally.
380382 if (Filename[0] == '-' && Filename[1] == 0) {
381383 FD = STDOUT_FILENO;
382384 // If user requested binary then put stdout into binary mode if
383385 // possible.
384386 if (Flags & F_Binary)
385387 sys::Program::ChangeStdoutToBinary();
386 ShouldClose = false;
388 // Close stdout when we're done, to detect any output errors.
389 ShouldClose = true;
387390 return;
388391 }
389392