[llvm-commits] [llvm] r79066 - /llvm/trunk/lib/Support/raw_ostream.cpp

Dan Gohman gohman at apple.com
Fri Aug 14 19:05:19 PDT 2009


Author: djg
Date: Fri Aug 14 21:05:19 2009
New Revision: 79066

URL: http://llvm.org/viewvc/llvm-project?rev=79066&view=rev
Log:
Always check to see if raw_fd_ostream's file descriptor is attached to
a terminal, not just when it's STDOUT_FILENO.

Modified:
    llvm/trunk/lib/Support/raw_ostream.cpp

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=79066&r1=79065&r2=79066&view=diff

==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Fri Aug 14 21:05:19 2009
@@ -72,7 +72,11 @@
 
 void raw_ostream::SetBuffered() {
   // Ask the subclass to determine an appropriate buffer size.
-  SetBufferSize(preferred_buffer_size());
+  if (size_t Size = preferred_buffer_size())
+    SetBufferSize(Size);
+  else
+    // It may return 0, meaning this stream should be unbuffered.
+    SetUnbuffered();
 }
 
 void raw_ostream::SetBufferSize(size_t Size) {
@@ -315,10 +319,6 @@
     if (Binary)
       sys::Program::ChangeStdoutToBinary();
     ShouldClose = false;
-    // Mimic stdout by defaulting to unbuffered if the output device
-    // is a terminal.
-    if (sys::Process::StandardOutIsDisplayed())
-      SetUnbuffered();
     return;
   }
   
@@ -375,8 +375,15 @@
 #if !defined(_MSC_VER) // Windows reportedly doesn't have st_blksize.
   assert(FD >= 0 && "File not yet open!");
   struct stat statbuf;
-  if (fstat(FD, &statbuf) == 0)
+  if (fstat(FD, &statbuf) == 0) {
+    // If this is a terminal, don't use buffering. Line buffering
+    // would be a more traditional thing to do, but it's not worth
+    // the complexity.
+    if (S_ISCHR(statbuf.st_mode) && isatty(FD))
+      return 0;
+    // Return the preferred block size.
     return statbuf.st_blksize;
+  }
   error_detected();
 #endif
   return raw_ostream::preferred_buffer_size();
@@ -416,13 +423,9 @@
 //===----------------------------------------------------------------------===//
 
 // Set buffer settings to model stdout and stderr behavior.
-// raw_ostream doesn't support line buffering, so set standard output to be
-// unbuffered when the output device is a terminal. Set standard error to
-// be unbuffered.
-raw_stdout_ostream::raw_stdout_ostream()
-  : raw_fd_ostream(STDOUT_FILENO, false,
-                   sys::Process::StandardOutIsDisplayed()) {}
-raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false, 
+// Set standard error to be unbuffered by default.
+raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
+raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false,
                                                         true) {}
 
 // An out of line virtual method to provide a home for the class vtable.





More information about the llvm-commits mailing list