Use binary mode for reading/writing bytecode files
authorJeff Cohen <jeffc@jolt-lang.org>
Sat, 22 Jan 2005 17:36:17 +0000 (17:36 +0000)
committerJeff Cohen <jeffc@jolt-lang.org>
Sat, 22 Jan 2005 17:36:17 +0000 (17:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19751 91177308-0d34-0410-b5e6-96231b3b80d8

14 files changed:
lib/Archive/ArchiveWriter.cpp
lib/Bytecode/Archive/ArchiveWriter.cpp
lib/Support/FileUtilities.cpp
lib/VMCore/Verifier.cpp
tools/bugpoint/OptimizerDriver.cpp
tools/extract/extract.cpp
tools/gccas/gccas.cpp
tools/gccld/gccld.cpp
tools/llvm-ar/llvm-ar.cpp
tools/llvm-as/llvm-as.cpp
tools/llvm-extract/llvm-extract.cpp
tools/llvm-ld/llvm-ld.cpp
tools/llvm-link/llvm-link.cpp
tools/opt/opt.cpp

index 5c7b12632049df359e19a3baf241f200eccbb969..f16376725d0a35c24fc77ae39533c5951e9afc9d 100644 (file)
@@ -375,7 +375,9 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress){
   // Ensure we can remove the temporary even in the face of an exception
   try {
     // Create archive file for output.
-    std::ofstream ArchiveFile(TmpArchive.c_str());
+    std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                 std::ios::binary;
+    std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
   
     // Check for errors opening or creating archive file.
     if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
@@ -413,7 +415,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress){
       const char* base = (const char*) arch.map();
 
       // Open the final file to write and check it.
-      std::ofstream FinalFile(archPath.c_str());
+      std::ofstream FinalFile(archPath.c_str(), io_mode);
       if ( !FinalFile.is_open() || FinalFile.bad() ) {
         throw std::string("Error opening archive file: ") + archPath.toString();
       }
index 5c7b12632049df359e19a3baf241f200eccbb969..f16376725d0a35c24fc77ae39533c5951e9afc9d 100644 (file)
@@ -375,7 +375,9 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress){
   // Ensure we can remove the temporary even in the face of an exception
   try {
     // Create archive file for output.
-    std::ofstream ArchiveFile(TmpArchive.c_str());
+    std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                                 std::ios::binary;
+    std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
   
     // Check for errors opening or creating archive file.
     if ( !ArchiveFile.is_open() || ArchiveFile.bad() ) {
@@ -413,7 +415,7 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress){
       const char* base = (const char*) arch.map();
 
       // Open the final file to write and check it.
-      std::ofstream FinalFile(archPath.c_str());
+      std::ofstream FinalFile(archPath.c_str(), io_mode);
       if ( !FinalFile.is_open() || FinalFile.bad() ) {
         throw std::string("Error opening archive file: ") + archPath.toString();
       }
index 5255df64e5fe0a64235a10c9e81ec9d47ab4af73..234cf7d2604d43706ad0d84632ae4a35abda41fb 100644 (file)
@@ -27,13 +27,14 @@ using namespace llvm;
 ///
 bool llvm::DiffFiles(const std::string &FileA, const std::string &FileB,
                      std::string *Error) {
-  std::ifstream FileAStream(FileA.c_str());
+  std::ios::openmode io_mode = std::ios::in | std::ios::binary;
+  std::ifstream FileAStream(FileA.c_str(), io_mode);
   if (!FileAStream) {
     if (Error) *Error = "Couldn't open file '" + FileA + "'";
     return true;
   }
 
-  std::ifstream FileBStream(FileB.c_str());
+  std::ifstream FileBStream(FileB.c_str(), io_mode);
   if (!FileBStream) {
     if (Error) *Error = "Couldn't open file '" + FileB + "'";
     return true;
index 0998720a4a7d2a095d78ad26b47920367845c118..ad1074d8d335bc69820e050fd7575ac83707b00e 100644 (file)
@@ -78,17 +78,17 @@ namespace {  // Anonymous namespace for class
 
     Verifier() 
         : Broken(false), RealPass(true), action(AbortProcessAction),
-          DS(0), msgs( std::ios_base::app | std::ios_base::out ) {}
+          DS(0), msgs( std::ios::app | std::ios::out ) {}
     Verifier( VerifierFailureAction ctn )
         : Broken(false), RealPass(true), action(ctn), DS(0), 
-          msgs( std::ios_base::app | std::ios_base::out ) {}
+          msgs( std::ios::app | std::ios::out ) {}
     Verifier(bool AB ) 
         : Broken(false), RealPass(true), 
           action( AB ? AbortProcessAction : PrintMessageAction), DS(0), 
-          msgs( std::ios_base::app | std::ios_base::out ) {}
+          msgs( std::ios::app | std::ios::out ) {}
     Verifier(DominatorSet &ds) 
       : Broken(false), RealPass(false), action(PrintMessageAction),
-        DS(&ds), msgs( std::ios_base::app | std::ios_base::out ) {}
+        DS(&ds), msgs( std::ios::app | std::ios::out ) {}
 
 
     bool doInitialization(Module &M) {
index 709eff37e461f83894c509de8e3827e50df68c18..e97815f8a7e67481c305ed83144182a71a3151fe 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+// 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"
 #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 @@ using namespace llvm;
 ///
 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 @@ void BugDriver::EmitProgressBytecode(const std::string &ID, bool NoFlyer) {
 
 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 @@ bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
   uniqueFilename.makeUnique();
   OutputFilename = uniqueFilename.toString();
 
+#ifndef PLATFORMINDEPENDENT
   pid_t child_pid;
   switch (child_pid = fork()) {
   case -1:    // Error occurred
@@ -139,12 +152,16 @@ bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
   }
 
   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 @@ bool BugDriver::runPasses(const std::vector<const PassInfo*> &Passes,
     else
       std::cout << "Failed for unknown reason!\n";
   }
+#endif
 
   // Was the child successful?
   return !ExitedOK;
index c7f942af27ead1376a2884d406d9961c9fae8b16..3f24e387e6ed9a90f37ea8bd4ec00de0f69d3cd5 100644 (file)
@@ -63,9 +63,8 @@ int main(int argc, char **argv) {
       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 @@ int main(int argc, char **argv) {
                   << "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;       
     }
 
index deb9397fecf48306ed6a06f68cf2e512ce47d6ca..83de4ebe253d5c4c125e2c085e3e04a6bbc50576 100644 (file)
@@ -164,9 +164,12 @@ int main(int argc, char **argv) {
     }
 
     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
index d33d64a01d68cb29901774412b4e410eb785e6f0..809db2adacc4d0492b76f31712436425819cc532 100644 (file)
@@ -238,7 +238,9 @@ int main(int argc, char **argv, char **envp ) {
     // 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!");
index 1755e18413efc98722cdf257b0d65301d08c702f..3617da6279d0d4da04c665f17a620daa18572133 100644 (file)
@@ -436,7 +436,9 @@ void doExtract() {
       }
 
       // 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());
index ef1602b6a72c7ee7e84ad67fe9c736838da101bf..a28e804ffc2ce31a6ae900e9fe75771555026bc5 100644 (file)
@@ -84,25 +84,26 @@ int main(int argc, char **argv) {
                     << "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 @@ int main(int argc, char **argv) {
           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));
index c7f942af27ead1376a2884d406d9961c9fae8b16..3f24e387e6ed9a90f37ea8bd4ec00de0f69d3cd5 100644 (file)
@@ -63,9 +63,8 @@ int main(int argc, char **argv) {
       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 @@ int main(int argc, char **argv) {
                   << "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;       
     }
 
index 07879acbc1832d0bda0eaf172c1d47d53164e0de..f8287f8e8d421ef84b33dfe5186ded0230de9407 100644 (file)
@@ -186,7 +186,9 @@ static void RemoveEnv(const char * name, char ** const envp) {
 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;
index a32b884e56a47fde043994fa130125ddbde4ff92..16e882ee1a0c46ed2176c8d44336928d2763d2be 100644 (file)
@@ -112,6 +112,7 @@ int main(int argc, char **argv) {
 
     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 @@ int main(int argc, char **argv) {
                   << "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;
index fd339d92d98d91ecc49ef7c6553a2c0b3a3ea61c..f54844cb590a49b7911401e6815d5f8992a05345 100644 (file)
@@ -95,6 +95,7 @@ int main(int argc, char **argv) {
     }
 
     // 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 @@ int main(int argc, char **argv) {
                   << "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 @@ int main(int argc, char **argv) {
     }
 
     // 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;
     }