In Thumb2, direct branches can be encoded as either a "short" conditional branch...
[oota-llvm.git] / utils / FileUpdate / FileUpdate.cpp
index 8d5d6b1b905ce776f4d7dbd95605e2787e32baa5..3514d0f21571bf79c2bb3b77ad2275656b1e5de7 100644 (file)
@@ -16,8 +16,9 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
-#include "llvm/Support/raw_ostream.h"
-#include "llvm/System/Signals.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 using namespace llvm;
 
 static cl::opt<bool>
@@ -36,25 +37,30 @@ int main(int argc, char **argv) {
   PrettyStackTraceProgram X(argc, argv);
   cl::ParseCommandLineOptions(argc, argv);
 
+  if (OutputFilename == "-") {
+    errs() << argv[0] << ": error: Can't update standard output\n";
+    return 1;
+  }
+
   // Get the input data.
-  std::string ErrorStr;
+  error_code ec;
   MemoryBuffer *In =
-    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), &ErrorStr);
+    MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), ec);
   if (In == 0) {
     errs() << argv[0] << ": error: Unable to get input '"
-           << InputFilename << "': " << ErrorStr << '\n';
+           << InputFilename << "': " << ec.message() << '\n';
     return 1;
   }
 
   // Get the output data.
-  MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), &ErrorStr);
+  MemoryBuffer *Out = MemoryBuffer::getFile(OutputFilename.c_str(), ec);
 
   // If the output exists and the contents match, we are done.
   if (Out && In->getBufferSize() == Out->getBufferSize() &&
       memcmp(In->getBufferStart(), Out->getBufferStart(),
              Out->getBufferSize()) == 0) {
     if (!Quiet)
-      outs() << argv[0] << ": Not updating '" << OutputFilename
+      errs() << argv[0] << ": Not updating '" << OutputFilename
              << "', contents match input.\n";
     return 0;
   }
@@ -63,24 +69,21 @@ int main(int argc, char **argv) {
 
   // Otherwise, overwrite the output.
   if (!Quiet)
-    outs() << argv[0] << ": Updating '" << OutputFilename
+    errs() << argv[0] << ": Updating '" << OutputFilename
            << "', contents changed.\n";
-  raw_fd_ostream OutStream(OutputFilename.c_str(), /*Binary=*/true,
-                           /*Force=*/true, ErrorStr);
+  std::string ErrorStr;
+  tool_output_file OutStream(OutputFilename.c_str(), ErrorStr,
+                             raw_fd_ostream::F_Binary);
   if (!ErrorStr.empty()) {
     errs() << argv[0] << ": Unable to write output '"
            << OutputFilename << "': " << ErrorStr << '\n';
     return 1;
   }
 
-  OutStream.write(In->getBufferStart(), In->getBufferSize());
-  OutStream.close();
+  OutStream.os().write(In->getBufferStart(), In->getBufferSize());
 
-  if (OutStream.has_error()) {
-    errs() << argv[0] << ": Could not open output file '"
-           << OutputFilename << "': " << ErrorStr << '\n';
-    return 1;
-  }
+  // Declare success.
+  OutStream.keep();
 
   return 0;
 }