Use binary mode for reading/writing bytecode files


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19751 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/bugpoint/OptimizerDriver.cpp b/tools/bugpoint/OptimizerDriver.cpp
index 709eff3..e97815f 100644
--- a/tools/bugpoint/OptimizerDriver.cpp
+++ b/tools/bugpoint/OptimizerDriver.cpp
@@ -15,6 +15,12 @@
 //
 //===----------------------------------------------------------------------===//
 
+// Note: as a short term hack, the old Unix-specific code and platform-
+// independent code co-exist via conditional compilation until it is verified
+// that the new code works correctly on Unix.
+
+#define PLATFORMINDEPENDENT
+
 #include "BugDriver.h"
 #include "llvm/Module.h"
 #include "llvm/PassManager.h"
@@ -24,9 +30,11 @@
 #include "llvm/Support/FileUtilities.h"
 #include "llvm/System/Path.h"
 #include <fstream>
+#ifndef PLATFORMINDEPENDENT
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#endif
 using namespace llvm;
 
 /// writeProgramToFile - This writes the current "Program" to the named bytecode
@@ -34,7 +42,9 @@
 ///
 bool BugDriver::writeProgramToFile(const std::string &Filename,
 				   Module *M) const {
-  std::ofstream Out(Filename.c_str());
+  std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                               std::ios::binary;
+  std::ofstream Out(Filename.c_str(), io_mode);
   if (!Out.good()) return true;
   WriteBytecodeToFile(M ? M : Program, Out, /*compression=*/true);
   return false;
@@ -76,7 +86,9 @@
 
 static void RunChild(Module *Program,const std::vector<const PassInfo*> &Passes,
                      const std::string &OutFilename) {
-  std::ofstream OutFile(OutFilename.c_str());
+  std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                               std::ios::binary;
+  std::ofstream OutFile(OutFilename.c_str(), io_mode);
   if (!OutFile.good()) {
     std::cerr << "Error opening bytecode file: " << OutFilename << "\n";
     exit(1);
@@ -119,6 +131,7 @@
   uniqueFilename.makeUnique();
   OutputFilename = uniqueFilename.toString();
 
+#ifndef PLATFORMINDEPENDENT
   pid_t child_pid;
   switch (child_pid = fork()) {
   case -1:    // Error occurred
@@ -139,12 +152,16 @@
   }
 
   bool ExitedOK = WIFEXITED(Status) && WEXITSTATUS(Status) == 0;
+#else
+  bool ExitedOK = false;
+#endif
 
   // If we are supposed to delete the bytecode file or if the passes crashed,
   // remove it now.  This may fail if the file was never created, but that's ok.
   if (DeleteOutput || !ExitedOK)
     sys::Path(OutputFilename).destroyFile();
-  
+
+#ifndef PLATFORMINDEPENDENT
   if (!Quiet) {
     if (ExitedOK)
       std::cout << "Success!\n";
@@ -159,6 +176,7 @@
     else
       std::cout << "Failed for unknown reason!\n";
   }
+#endif
 
   // Was the child successful?
   return !ExitedOK;
diff --git a/tools/extract/extract.cpp b/tools/extract/extract.cpp
index c7f942a..3f24e38 100644
--- a/tools/extract/extract.cpp
+++ b/tools/extract/extract.cpp
@@ -63,9 +63,8 @@
       return 1;
     }
 
-    // In addition to deleting all other functions, we also want to spiff it up a
-    // little bit.  Do this now.
-    //
+    // In addition to deleting all other functions, we also want to spiff it
+    // up a little bit.  Do this now.
     PassManager Passes;
     Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
     // Either isolate the function or delete it from the Module
@@ -84,8 +83,11 @@
                   << "Use -f command line argument to force output\n";
         return 1;
       }
-      Out = new std::ofstream(OutputFilename.c_str());
+      std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                   std::ios::binary;
+      Out = new std::ofstream(OutputFilename.c_str(), io_mode);
     } else {                      // Specified stdout
+      // FIXME: cout is not binary!
       Out = &std::cout;       
     }
 
diff --git a/tools/gccas/gccas.cpp b/tools/gccas/gccas.cpp
index deb9397..83de4eb 100644
--- a/tools/gccas/gccas.cpp
+++ b/tools/gccas/gccas.cpp
@@ -164,9 +164,12 @@
     }
 
     if (OutputFilename == "-")
+      // FIXME: cout is not binary!
       Out = &std::cout;
     else {
-      Out = new std::ofstream(OutputFilename.c_str(), std::ios::out);
+      std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                   std::ios::binary;
+      Out = new std::ofstream(OutputFilename.c_str(), io_mode);
 
       // Make sure that the Out file gets unlinked from the disk if we get a
       // signal
diff --git a/tools/gccld/gccld.cpp b/tools/gccld/gccld.cpp
index d33d64a..809db2a 100644
--- a/tools/gccld/gccld.cpp
+++ b/tools/gccld/gccld.cpp
@@ -238,7 +238,9 @@
     // Create the output file.
     std::string RealBytecodeOutput = OutputFilename;
     if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
-    std::ofstream Out(RealBytecodeOutput.c_str());
+    std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                 std::ios::binary;
+    std::ofstream Out(RealBytecodeOutput.c_str(), io_mode);
     if (!Out.good())
       return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
                                      "' for writing!");
diff --git a/tools/llvm-ar/llvm-ar.cpp b/tools/llvm-ar/llvm-ar.cpp
index 1755e18..3617da6 100644
--- a/tools/llvm-ar/llvm-ar.cpp
+++ b/tools/llvm-ar/llvm-ar.cpp
@@ -436,7 +436,9 @@
       }
 
       // Open up a file stream for writing
-      std::ofstream file(I->getPath().c_str());
+      std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                   std::ios::binary;
+      std::ofstream file(I->getPath().c_str(), io_mode);
 
       // Get the data and its length
       const char* data = reinterpret_cast<const char*>(I->getData());
diff --git a/tools/llvm-as/llvm-as.cpp b/tools/llvm-as/llvm-as.cpp
index ef1602b..a28e804 100644
--- a/tools/llvm-as/llvm-as.cpp
+++ b/tools/llvm-as/llvm-as.cpp
@@ -84,25 +84,26 @@
                     << "Use -f command line argument to force output\n";
           return 1;
         }
-        Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out | 
-                                std::ios_base::trunc | std::ios_base::binary);
+        Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | 
+                                std::ios::trunc | std::ios::binary);
       } else {                      // Specified stdout
-	Out = &std::cout;       
+        // FIXME: cout is not binary!
+        Out = &std::cout;
       }
     } else {
       if (InputFilename == "-") {
-	OutputFilename = "-";
-	Out = &std::cout;
+        OutputFilename = "-";
+        Out = &std::cout;
       } else {
-	std::string IFN = InputFilename;
-	int Len = IFN.length();
-	if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
-	  // Source ends in .ll
-	  OutputFilename = std::string(IFN.begin(), IFN.end()-3);
+        std::string IFN = InputFilename;
+        int Len = IFN.length();
+        if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
+          // Source ends in .ll
+          OutputFilename = std::string(IFN.begin(), IFN.end()-3);
         } else {
-	  OutputFilename = IFN;   // Append a .bc to it
-	}
-	OutputFilename += ".bc";
+          OutputFilename = IFN;   // Append a .bc to it
+        }
+        OutputFilename += ".bc";
 
         if (!Force && std::ifstream(OutputFilename.c_str())) {
           // If force is not specified, make sure not to overwrite a file!
@@ -112,8 +113,8 @@
           return 1;
         }
 
-	Out = new std::ofstream(OutputFilename.c_str(), std::ios_base::out | 
-                                std::ios_base::trunc | std::ios_base::binary);
+        Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | 
+                                std::ios::trunc | std::ios::binary);
         // Make sure that the Out file gets unlinked from the disk if we get a
         // SIGINT
         sys::RemoveFileOnSignal(sys::Path(OutputFilename));
diff --git a/tools/llvm-extract/llvm-extract.cpp b/tools/llvm-extract/llvm-extract.cpp
index c7f942a..3f24e38 100644
--- a/tools/llvm-extract/llvm-extract.cpp
+++ b/tools/llvm-extract/llvm-extract.cpp
@@ -63,9 +63,8 @@
       return 1;
     }
 
-    // In addition to deleting all other functions, we also want to spiff it up a
-    // little bit.  Do this now.
-    //
+    // In addition to deleting all other functions, we also want to spiff it
+    // up a little bit.  Do this now.
     PassManager Passes;
     Passes.add(new TargetData("extract", M.get())); // Use correct TargetData
     // Either isolate the function or delete it from the Module
@@ -84,8 +83,11 @@
                   << "Use -f command line argument to force output\n";
         return 1;
       }
-      Out = new std::ofstream(OutputFilename.c_str());
+      std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                   std::ios::binary;
+      Out = new std::ofstream(OutputFilename.c_str(), io_mode);
     } else {                      // Specified stdout
+      // FIXME: cout is not binary!
       Out = &std::cout;       
     }
 
diff --git a/tools/llvm-ld/llvm-ld.cpp b/tools/llvm-ld/llvm-ld.cpp
index 07879ac..f8287f8 100644
--- a/tools/llvm-ld/llvm-ld.cpp
+++ b/tools/llvm-ld/llvm-ld.cpp
@@ -186,7 +186,9 @@
 void GenerateBytecode(Module* M, const std::string& FileName) {
 
   // Create the output file.
-  std::ofstream Out(FileName.c_str());
+  std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                               std::ios::binary;
+  std::ofstream Out(FileName.c_str(), io_mode);
   if (!Out.good()) {
     PrintAndReturn("error opening '" + FileName + "' for writing!");
     return;
diff --git a/tools/llvm-link/llvm-link.cpp b/tools/llvm-link/llvm-link.cpp
index a32b884..16e882e 100644
--- a/tools/llvm-link/llvm-link.cpp
+++ b/tools/llvm-link/llvm-link.cpp
@@ -112,6 +112,7 @@
 
     if (DumpAsm) std::cerr << "Here's the assembly:\n" << Composite.get();
 
+    // FIXME: cout is not binary!
     std::ostream *Out = &std::cout;  // Default to printing to stdout...
     if (OutputFilename != "-") {
       if (!Force && std::ifstream(OutputFilename.c_str())) {
@@ -121,7 +122,9 @@
                   << "Use -f command line argument to force output\n";
         return 1;
       }
-      Out = new std::ofstream(OutputFilename.c_str());
+      std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                   std::ios::binary;
+      Out = new std::ofstream(OutputFilename.c_str(), io_mode);
       if (!Out->good()) {
         std::cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
         return 1;
diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp
index fd339d9..f54844c 100644
--- a/tools/opt/opt.cpp
+++ b/tools/opt/opt.cpp
@@ -95,6 +95,7 @@
     }
 
     // Figure out what stream we are supposed to write to...
+    // FIXME: cout is not binary!
     std::ostream *Out = &std::cout;  // Default to printing to stdout...
     if (OutputFilename != "-") {
       if (!Force && std::ifstream(OutputFilename.c_str())) {
@@ -104,7 +105,9 @@
                   << "Use -f command line argument to force output\n";
         return 1;
       }
-      Out = new std::ofstream(OutputFilename.c_str());
+      std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                   std::ios::binary;
+      Out = new std::ofstream(OutputFilename.c_str(), io_mode);
 
       if (!Out->good()) {
         std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
@@ -117,8 +120,8 @@
     }
 
     // If the output is set to be emitted to standard out, and standard out is a
-    // console, print out a warning message and refuse to do it.  We don't impress
-    // anyone by spewing tons of binary goo to a terminal.
+    // console, print out a warning message and refuse to do it.  We don't
+    // impress anyone by spewing tons of binary goo to a terminal.
     if (!Force && !NoOutput && CheckBytecodeOutputToConsole(Out,!Quiet)) {
       NoOutput = true;
     }