Add a Force option to raw_fd_ostream to specify whether opening
authorDan Gohman <gohman@apple.com>
Wed, 15 Jul 2009 17:29:42 +0000 (17:29 +0000)
committerDan Gohman <gohman@apple.com>
Wed, 15 Jul 2009 17:29:42 +0000 (17:29 +0000)
an existing file is considered an error. Convert several tools
to use raw_fd_ostream instead of std::ostream, and to use this
new option instead of doing a manual check.

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

12 files changed:
include/llvm/Support/raw_ostream.h
lib/Support/raw_ostream.cpp
tools/bugpoint/ToolRunner.cpp
tools/gold/gold-plugin.cpp
tools/llc/llc.cpp
tools/llvm-as/llvm-as.cpp
tools/llvm-dis/llvm-dis.cpp
tools/llvm-extract/llvm-extract.cpp
tools/llvm-link/llvm-link.cpp
tools/lto/LTOCodeGenerator.cpp
tools/opt/opt.cpp
utils/TableGen/TableGen.cpp

index 261a9225c43e1eb83f98871ca5deb2219d4960e2..2b7b62bce4f7826fa9c1de6053daffa972db4155 100644 (file)
@@ -251,7 +251,10 @@ public:
   /// stream will use stdout instead.
   /// \param Binary - The file should be opened in binary mode on
   /// platforms that support this distinction.
-  raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
+  /// \param Force - Don't consider the case where the file already
+  /// exists to be an error.
+  raw_fd_ostream(const char *Filename, bool Binary, bool Force,
+                 std::string &ErrorInfo);
 
   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
   /// ShouldClose is true, this closes the file when the stream is destroyed.
index 0595fe47e12affae25a7dbeb565c725d68f07be3..b13d922b217724006f598bda8f9cfa6c2eeb5946 100644 (file)
@@ -246,7 +246,7 @@ void format_object_base::home() {
 /// occurs, information about the error is put into ErrorInfo, and the
 /// stream should be immediately destroyed; the string will be empty
 /// if no error occurred.
-raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
+raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
                                std::string &ErrorInfo) : pos(0) {
   ErrorInfo.clear();
 
@@ -266,6 +266,8 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
   if (Binary)
     Flags |= O_BINARY;
 #endif
+  if (!Force)
+    Flags |= O_EXCL;
   FD = open(Filename, Flags, 0664);
   if (FD < 0) {
     ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
index d610676d4dedecbd89fa94fca2d6a0e28c5ebbdb..a5e1e8b8a42affc09dee156acf40b4e1015389fd 100644 (file)
@@ -20,7 +20,6 @@
 #include "llvm/Support/FileUtilities.h"
 #include <fstream>
 #include <sstream>
-#include <iostream>
 using namespace llvm;
 
 namespace {
@@ -158,7 +157,7 @@ int LLI::ExecuteProgram(const std::string &Bitcode,
     LLIArgs.push_back(Args[i].c_str());
   LLIArgs.push_back(0);
 
-  std::cout << "<lli>" << std::flush;
+  outs() << "<lli>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
         for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
           errs() << " " << LLIArgs[i];
@@ -312,7 +311,7 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode,
   LLCArgs.push_back (Bitcode.c_str());      // This is the input bitcode
   LLCArgs.push_back (0);
 
-  std::cout << "<llc>" << std::flush;
+  outs() << "<llc>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
         for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
           errs() << " " << LLCArgs[i];
@@ -429,7 +428,7 @@ int JIT::ExecuteProgram(const std::string &Bitcode,
     JITArgs.push_back(Args[i].c_str());
   JITArgs.push_back(0);
 
-  std::cout << "<jit>" << std::flush;
+  outs() << "<jit>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
         for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
           errs() << " " << JITArgs[i];
@@ -478,7 +477,7 @@ GCC::FileType CBE::OutputCode(const std::string &Bitcode,
   LLCArgs.push_back (Bitcode.c_str());      // This is the input bitcode
   LLCArgs.push_back (0);
 
-  std::cout << "<cbe>" << std::flush;
+  outs() << "<cbe>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
         for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
           errs() << " " << LLCArgs[i];
@@ -621,7 +620,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
 #endif
   GCCArgs.push_back(0);                    // NULL terminator
 
-  std::cout << "<gcc>" << std::flush;
+  outs() << "<gcc>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
         for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
           errs() << " " << GCCArgs[i];
@@ -665,7 +664,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
   ProgramArgs.push_back(0);                // NULL terminator
 
   // Now that we have a binary, run it!
-  std::cout << "<program>" << std::flush;
+  outs() << "<program>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
         for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
           errs() << " " << ProgramArgs[i];
@@ -680,7 +679,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
         sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
         Timeout, MemoryLimit);
   } else {
-    std::cout << "<run remotely>" << std::flush;
+    outs() << "<run remotely>"; outs().flush();
     int RemoteClientStatus = RunProgramWithTimeout(sys::Path(RemoteClientPath),
         &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
         sys::Path(OutputFile), Timeout, MemoryLimit);
@@ -756,7 +755,7 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType,
 
   
 
-  std::cout << "<gcc>" << std::flush;
+  outs() << "<gcc>"; outs().flush();
   DEBUG(errs() << "\nAbout to run:\t";
         for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
           errs() << " " << GCCArgs[i];
index 146c53fbb71b50f296df5d534c682349fd325808..9554b8adc314b55c4df3ae050c0853a793a0caab 100644 (file)
@@ -362,7 +362,9 @@ ld_plugin_status all_symbols_read_hook(void) {
     (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
     return LDPS_ERR;
   }
-  raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), true,
+  raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(),
+                                               /*Binary=*/true,
+                                               /*Force=*/true,
                                                ErrMsg);
   if (!ErrMsg.empty()) {
     delete objFile;
index 9acfcd29000ae0890ad7072850d94d0a95d5abb9..c4bb41e1f064b52963b5b2e62716c395d6e39cc1 100644 (file)
@@ -41,8 +41,6 @@
 #include "llvm/Config/config.h"
 #include "llvm/LinkAllVMCore.h"
 #include "llvm/Target/TargetSelect.h"
-#include <fstream>
-#include <iostream>
 #include <memory>
 using namespace llvm;
 
@@ -133,28 +131,22 @@ static formatted_raw_ostream *GetOutputStream(const char *ProgName) {
     if (OutputFilename == "-")
       return &fouts();
 
-    // Specified an output filename?
-    if (!Force && std::ifstream(OutputFilename.c_str())) {
-      // If force is not specified, make sure not to overwrite a file!
-      errs() << ProgName << ": error opening '" << OutputFilename
-             << "': file exists!\n"
-             << "Use -f command line argument to force output\n";
-      return 0;
-    }
     // Make sure that the Out file gets unlinked from the disk if we get a
     // SIGINT
     sys::RemoveFileOnSignal(sys::Path(OutputFilename));
 
     std::string error;
     raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
-                                               true, error);
-    formatted_raw_ostream *Out =
-      new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
+                                               /*Binary=*/true, Force, error);
     if (!error.empty()) {
       errs() << error << '\n';
-      delete Out;
+      if (!Force)
+        errs() << "Use -f command line argument to force output\n";
+      delete FDOut;
       return 0;
     }
+    formatted_raw_ostream *Out =
+      new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
 
     return Out;
   }
@@ -189,29 +181,24 @@ static formatted_raw_ostream *GetOutputStream(const char *ProgName) {
     break;
   }
 
-  if (!Force && std::ifstream(OutputFilename.c_str())) {
-    // If force is not specified, make sure not to overwrite a file!
-    errs() << ProgName << ": error opening '" << OutputFilename
-                       << "': file exists!\n"
-                       << "Use -f command line argument to force output\n";
-    return 0;
-  }
-
   // Make sure that the Out file gets unlinked from the disk if we get a
   // SIGINT
   sys::RemoveFileOnSignal(sys::Path(OutputFilename));
 
   std::string error;
   raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
-                                             Binary, error);
-  formatted_raw_ostream *Out =
-    new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
+                                             Binary, Force, error);
   if (!error.empty()) {
     errs() << error << '\n';
-    delete Out;
+    if (!Force)
+      errs() << "Use -f command line argument to force output\n";
+    delete FDOut;
     return 0;
   }
 
+  formatted_raw_ostream *Out =
+    new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
+
   return Out;
 }
 
index eccabd5d14a4b3b67eb5a49abd5a58435dd42b9e..1d572f157846b80a4909962114f0907bc0fe1066 100644 (file)
@@ -28,8 +28,6 @@
 #include "llvm/Support/SystemUtils.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Signals.h"
-#include <fstream>
-#include <iostream>
 #include <memory>
 using namespace llvm;
 
@@ -62,7 +60,7 @@ int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
 
   int exitCode = 0;
-  std::ostream *Out = 0;
+  raw_ostream *Out = 0;
   try {
     // Parse the file now...
     SMDiagnostic Err;
@@ -86,23 +84,24 @@ int main(int argc, char **argv) {
 
     if (OutputFilename != "") {   // Specified an output filename?
       if (OutputFilename != "-") {  // Not stdout?
-        if (!Force && std::ifstream(OutputFilename.c_str())) {
-          // If force is not specified, make sure not to overwrite a file!
-          cerr << argv[0] << ": error opening '" << OutputFilename
-               << "': file exists!\n"
-               << "Use -f command line argument to force output\n";
+        std::string ErrorInfo;
+        Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
+                                 Force, ErrorInfo);
+        if (!ErrorInfo.empty()) {
+          errs() << ErrorInfo << '\n';
+          if (!Force)
+            errs() << "Use -f command line argument to force output\n";
+          delete Out;
           return 1;
         }
-        Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
-                                std::ios::trunc | std::ios::binary);
       } else {                      // Specified stdout
-        // FIXME: cout is not binary!
-        Out = &std::cout;
+        // FIXME: outs() is not binary!
+        Out = &outs();
       }
     } else {
       if (InputFilename == "-") {
         OutputFilename = "-";
-        Out = &std::cout;
+        Out = &outs();
       } else {
         std::string IFN = InputFilename;
         int Len = IFN.length();
@@ -114,27 +113,22 @@ int main(int argc, char **argv) {
         }
         OutputFilename += ".bc";
 
-        if (!Force && std::ifstream(OutputFilename.c_str())) {
-          // If force is not specified, make sure not to overwrite a file!
-          cerr << argv[0] << ": error opening '" << OutputFilename
-               << "': file exists!\n"
-               << "Use -f command line argument to force output\n";
+        std::string ErrorInfo;
+        Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
+                                 Force, ErrorInfo);
+        if (!ErrorInfo.empty()) {
+          errs() << ErrorInfo << '\n';
+          if (!Force)
+            errs() << "Use -f command line argument to force output\n";
+          delete Out;
           return 1;
         }
-
-        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));
       }
     }
 
-    if (!Out->good()) {
-      cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
-      return 1;
-    }
-
     if (!DisableOutput)
       if (Force || !CheckBitcodeOutputToConsole(Out,true))
         WriteBitcodeToFile(M.get(), *Out);
@@ -146,7 +140,7 @@ int main(int argc, char **argv) {
     exitCode = 1;
   }
 
-  if (Out != &std::cout) delete Out;
+  if (Out != &outs()) delete Out;
   return exitCode;
 }
 
index 901c8e9d3a93872d42b5a6cb33fc98ce2f8f725c..8fa00d5b032bfd4dc190b8f69718c95797c6df4b 100644 (file)
@@ -28,8 +28,6 @@
 #include "llvm/Support/Streams.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Signals.h"
-#include <iostream>
-#include <fstream>
 #include <memory>
 using namespace llvm;
 
@@ -56,7 +54,7 @@ int main(int argc, char **argv) {
   try {
     cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
 
-    std::ostream *Out = &std::cout;  // Default to printing to stdout.
+    raw_ostream *Out = &outs();  // Default to printing to stdout.
     std::string ErrorMessage;
 
     std::auto_ptr<Module> M;
@@ -80,12 +78,15 @@ int main(int argc, char **argv) {
       // Just use stdout.  We won't actually print anything on it.
     } else if (OutputFilename != "") {   // Specified an output filename?
       if (OutputFilename != "-") { // Not stdout?
-        if (!Force && std::ifstream(OutputFilename.c_str())) {
-          // If force is not specified, make sure not to overwrite a file!
-          cerr << argv[0] << ": error opening '" << OutputFilename
-               << "': file exists! Sending to standard output.\n";
-        } else {
-          Out = new std::ofstream(OutputFilename.c_str());
+        std::string ErrorInfo;
+        Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
+                                 Force, ErrorInfo);
+        if (!ErrorInfo.empty()) {
+          errs() << ErrorInfo << '\n';
+          if (!Force)
+            errs() << "Use -f command line argument to force output\n";
+          delete Out;
+          return 1;
         }
       }
     } else {
@@ -101,38 +102,32 @@ int main(int argc, char **argv) {
           OutputFilename = IFN+".ll";
         }
 
-        if (!Force && std::ifstream(OutputFilename.c_str())) {
-          // If force is not specified, make sure not to overwrite a file!
-          cerr << argv[0] << ": error opening '" << OutputFilename
-               << "': file exists! Sending to standard output.\n";
-        } else {
-          Out = new std::ofstream(OutputFilename.c_str());
-
-          // Make sure that the Out file gets unlinked from the disk if we get a
-          // SIGINT
-          sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+        std::string ErrorInfo;
+        Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
+                                 Force, ErrorInfo);
+        if (!ErrorInfo.empty()) {
+          errs() << ErrorInfo << '\n';
+          if (!Force)
+            errs() << "Use -f command line argument to force output\n";
+          delete Out;
+          return 1;
         }
-      }
-    }
 
-    if (!Out->good()) {
-      cerr << argv[0] << ": error opening " << OutputFilename
-           << ": sending to stdout instead!\n";
-      Out = &std::cout;
+        // Make sure that the Out file gets unlinked from the disk if we get a
+        // SIGINT
+        sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+      }
     }
 
     // All that llvm-dis does is write the assembly to a file.
     if (!DontPrint) {
       PassManager Passes;
-      raw_os_ostream L(*Out);
-      Passes.add(createPrintModulePass(&L));
+      Passes.add(createPrintModulePass(Out));
       Passes.run(*M.get());
     }
 
-    if (Out != &std::cout) {
-      ((std::ofstream*)Out)->close();
+    if (Out != &outs())
       delete Out;
-    }
     return 0;
   } catch (const std::string& msg) {
     cerr << argv[0] << ": " << msg << "\n";
index af0cf0705bf9c8c582eecfabc8dd0c40313a24fd..4809f9fae2455befc660b5a6ebc177e4ffe25f0d 100644 (file)
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Signals.h"
-#include <iostream>
 #include <memory>
-#include <fstream>
 using namespace llvm;
 
 // InputFilename - The filename to read from.
@@ -111,28 +110,28 @@ int main(int argc, char **argv) {
   Passes.add(createDeadTypeEliminationPass());   // Remove dead types...
   Passes.add(createStripDeadPrototypesPass());   // Remove dead func decls
 
-  std::ostream *Out = 0;
+  raw_ostream *Out = 0;
 
   if (OutputFilename != "-") {  // Not stdout?
-    if (!Force && std::ifstream(OutputFilename.c_str())) {
-      // If force is not specified, make sure not to overwrite a file!
-      cerr << argv[0] << ": error opening '" << OutputFilename
-           << "': file exists!\n"
-           << "Use -f command line argument to force output\n";
+    std::string ErrorInfo;
+    Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
+                             Force, ErrorInfo);
+    if (!ErrorInfo.empty()) {
+      errs() << ErrorInfo << '\n';
+      if (!Force)
+        errs() << "Use -f command line argument to force output\n";
+      delete Out;
       return 1;
     }
-    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;
+    // FIXME: errs() is not binary!
+    Out = &errs();
   }
 
-  Passes.add(CreateBitcodeWriterPass(*Out));
+  Passes.add(createBitcodeWriterPass(*Out));
   Passes.run(*M.get());
 
-  if (Out != &std::cout)
+  if (Out != &errs())
     delete Out;
   return 0;
 }
index f65e602f27f9c6ac40bbe23c65d11963ee8fe69b..e6349fae5bfe2bd916cc912a88edbfa5fae93355 100644 (file)
@@ -24,8 +24,6 @@
 #include "llvm/Support/Streams.h"
 #include "llvm/System/Signals.h"
 #include "llvm/System/Path.h"
-#include <fstream>
-#include <iostream>
 #include <memory>
 using namespace llvm;
 
@@ -122,20 +120,16 @@ int main(int argc, char **argv) {
   if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
 
   // FIXME: cout is not binary!
-  std::ostream *Out = &std::cout;  // Default to printing to stdout...
+  raw_ostream *Out = &outs();  // Default to printing to stdout...
   if (OutputFilename != "-") {
-    if (!Force && std::ifstream(OutputFilename.c_str())) {
-      // If force is not specified, make sure not to overwrite a file!
-      cerr << argv[0] << ": error opening '" << OutputFilename
-           << "': file exists!\n"
-           << "Use -f command line argument to force output\n";
-      return 1;
-    }
-    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()) {
-      cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
+    std::string ErrorInfo;
+    Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
+                             Force, ErrorInfo);
+    if (!ErrorInfo.empty()) {
+      errs() << ErrorInfo << '\n';
+      if (!Force)
+        errs() << "Use -f command line argument to force output\n";
+      delete Out;
       return 1;
     }
 
@@ -152,6 +146,6 @@ int main(int argc, char **argv) {
   if (Verbose) cerr << "Writing bitcode...\n";
   WriteBitcodeToFile(Composite.get(), *Out);
 
-  if (Out != &std::cout) delete Out;
+  if (Out != &outs()) delete Out;
   return 0;
 }
index b4c4e7767b983880c9e4df3e8adf910ad5a8797d..9596057733ea4290e6a140ad5b810f895afe29bf 100644 (file)
@@ -186,7 +186,8 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
     bool genResult = false;
     {
       raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(),
-                                          false, errMsg));
+                                          /*Binary=*/false, /*Force=*/true,
+                                          errMsg));
       formatted_raw_ostream asmFile(asmFD);
       if (!errMsg.empty())
         return NULL;
index 689161940240b39376e7f5070a1da8895b55fea3..c92fad8f6f8089015ff0733d89f8fe94edfe9089 100644 (file)
@@ -35,8 +35,6 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/LinkAllPasses.h"
 #include "llvm/LinkAllVMCore.h"
-#include <iostream>
-#include <fstream>
 #include <memory>
 #include <algorithm>
 using namespace llvm;
@@ -342,21 +340,16 @@ 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...
+    raw_ostream *Out = &outs();  // Default to printing to stdout...
     if (OutputFilename != "-") {
-      if (!Force && std::ifstream(OutputFilename.c_str())) {
-        // If force is not specified, make sure not to overwrite a file!
-        cerr << argv[0] << ": error opening '" << OutputFilename
-             << "': file exists!\n"
-             << "Use -f command line argument to force output\n";
-        return 1;
-      }
-      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()) {
-        cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
+      std::string ErrorInfo;
+      Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
+                               Force, ErrorInfo);
+      if (!ErrorInfo.empty()) {
+        errs() << ErrorInfo << '\n';
+        if (!Force)
+          errs() << "Use -f command line argument to force output\n";
+        delete Out;
         return 1;
       }
 
@@ -479,13 +472,13 @@ int main(int argc, char **argv) {
 
     // Write bitcode out to disk or cout as the last step...
     if (!NoOutput && !AnalyzeOnly)
-      Passes.add(CreateBitcodeWriterPass(*Out));
+      Passes.add(createBitcodeWriterPass(*Out));
 
     // Now that we have all of the passes ready, run them.
     Passes.run(*M.get());
 
     // Delete the ofstream.
-    if (Out != &std::cout) 
+    if (Out != &outs())
       delete Out;
     return 0;
 
index 376bec1eb9dae304473738abae50d187b96fd0f7..315bb657a07b079072a149e56e2195df041897d3 100644 (file)
@@ -171,7 +171,8 @@ int main(int argc, char **argv) {
   raw_ostream *Out = &outs();
   if (OutputFilename != "-") {
     std::string Error;
-    Out = new raw_fd_ostream(OutputFilename.c_str(), false, Error);
+    Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
+                             /*Force=*/true, Error);
 
     if (!Error.empty()) {
       errs() << argv[0] << ": error opening " << OutputFilename