Don't special-case stdout in llvm::WriteBitcodeToFile; just consider
authorDan Gohman <gohman@apple.com>
Thu, 27 May 2010 20:06:51 +0000 (20:06 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 27 May 2010 20:06:51 +0000 (20:06 +0000)
it to be the caller's responsibility to provide a stream in binary
mode. This fixes a layering violation and avoids an outs() call.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@104878 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Bitcode/ReaderWriter.h
lib/Bitcode/Writer/BitcodeWriter.cpp
tools/opt/opt.cpp

index 45eb801a8c031a38e9b18887e6c0c7cfe161fb0b..a186964743dc87026a9ed739ef30feab7724bd10 100644 (file)
@@ -40,7 +40,8 @@ namespace llvm {
                            std::string *ErrMsg = 0);
 
   /// WriteBitcodeToFile - Write the specified module to the specified
-  /// raw output stream.
+  /// raw output stream.  For streams where it matters, the given stream
+  /// should be in "binary" mode.
   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
 
   /// WriteBitcodeToStream - Write the specified module to the specified
index 9bda6dca3d5e9757e786c050c1477c7d6321ead7..884485dec9454c7c4b2ec9e5e8e76ca1ec0630f6 100644 (file)
@@ -1662,10 +1662,6 @@ void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
 
   WriteBitcodeToStream( M, Stream );
 
-  // If writing to stdout, set binary mode.
-  if (&llvm::outs() == &Out)
-    sys::Program::ChangeStdoutToBinary();
-
   // Write the generated bitstream to "Out".
   Out.write((char*)&Buffer.front(), Buffer.size());
 
index d094ed11de28ff5190f950cbdb11a8c80f45b600..cf520de7fc12c33dd0d9272f88aa728814dd96b7 100644 (file)
@@ -377,12 +377,16 @@ int main(int argc, char **argv) {
   }
 
   // Figure out what stream we are supposed to write to...
-  // FIXME: outs() is not binary!
   raw_ostream *Out = 0;
   bool DeleteStream = false;
   if (!NoOutput && !AnalyzeOnly) {
     if (OutputFilename == "-") {
-      Out = &outs();  // Default to printing to stdout...
+      // Print to stdout.
+      Out = &outs();
+      // If we're printing a bitcode file, switch stdout to binary mode.
+      // FIXME: This switches outs() globally, not just for the bitcode output.
+      if (!OutputAssembly)
+        sys::Program::ChangeStdoutToBinary(); 
     } else {
       if (NoOutput || AnalyzeOnly) {
         errs() << "WARNING: The -o (output filename) option is ignored when\n"