Clean up the definition of Str in AsmParser::ParseDirectiveDarwinDumpOrLoad
[oota-llvm.git] / tools / opt / opt.cpp
index 689161940240b39376e7f5070a1da8895b55fea3..abfeace376b2fe3b5263055362edebbe8cc1de4b 100644 (file)
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PluginLoader.h"
 #include "llvm/Support/StandardPasses.h"
-#include "llvm/Support/Streams.h"
 #include "llvm/Support/SystemUtils.h"
 #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;
@@ -128,12 +125,15 @@ struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
 
   virtual bool runOnSCC(const std::vector<CallGraphNode *>&SCC) {
     if (!Quiet) {
-      cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+      outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
 
       for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
         Function *F = SCC[i]->getFunction();
-        if (F) 
+        if (F) {
+          outs().flush();
           getAnalysisID<Pass>(PassToPrint).print(cout, F->getParent());
+          cout << std::flush;
+        }
       }
     }
     // Get and print pass...
@@ -158,8 +158,10 @@ struct ModulePassPrinter : public ModulePass {
 
   virtual bool runOnModule(Module &M) {
     if (!Quiet) {
-      cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+      outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+      outs().flush();
       getAnalysisID<Pass>(PassToPrint).print(cout, &M);
+      cout << std::flush;
     }
 
     // Get and print pass...
@@ -183,11 +185,13 @@ struct FunctionPassPrinter : public FunctionPass {
 
   virtual bool runOnFunction(Function &F) {
     if (!Quiet) { 
-      cout << "Printing analysis '" << PassToPrint->getPassName()
-           << "' for function '" << F.getName() << "':\n";
+      outs() << "Printing analysis '" << PassToPrint->getPassName()
+              << "' for function '" << F.getName() << "':\n";
     }
     // Get and print pass...
+    outs().flush();
     getAnalysisID<Pass>(PassToPrint).print(cout, F.getParent());
+    cout << std::flush;
     return false;
   }
 
@@ -209,9 +213,11 @@ struct LoopPassPrinter : public LoopPass {
 
   virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
     if (!Quiet) {
-      cout << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-      getAnalysisID<Pass>(PassToPrint).print(cout, 
+      outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
+      outs().flush();
+      getAnalysisID<Pass>(PassToPrint).print(cout,
                                   L->getHeader()->getParent()->getParent());
+      cout << std::flush;
     }
     // Get and print pass...
     return false;
@@ -235,12 +241,14 @@ struct BasicBlockPassPrinter : public BasicBlockPass {
 
   virtual bool runOnBasicBlock(BasicBlock &BB) {
     if (!Quiet) {
-      cout << "Printing Analysis info for BasicBlock '" << BB.getName()
-           << "': Pass " << PassToPrint->getPassName() << ":\n";
+      outs() << "Printing Analysis info for BasicBlock '" << BB.getName()
+             << "': Pass " << PassToPrint->getPassName() << ":\n";
     }
 
     // Get and print pass...
+    outs().flush();
     getAnalysisID<Pass>(PassToPrint).print(cout, BB.getParent()->getParent());
+    cout << std::flush;
     return false;
   }
 
@@ -311,7 +319,7 @@ void AddStandardCompilePasses(PassManager &PM) {
 //
 int main(int argc, char **argv) {
   llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
-  LLVMContext Context;
+  LLVMContext &Context = getGlobalContext();
   try {
     cl::ParseCommandLineOptions(argc, argv,
       "llvm .bc -> .bc modular optimizer and analysis printer\n");
@@ -332,31 +340,26 @@ int main(int argc, char **argv) {
     }
     
     if (M.get() == 0) {
-      cerr << argv[0] << ": ";
+      errs() << argv[0] << ": ";
       if (ErrorMessage.size())
-        cerr << ErrorMessage << "\n";
+        errs() << ErrorMessage << "\n";
       else
-        cerr << "bitcode didn't read correctly.\n";
+        errs() << "bitcode didn't read correctly.\n";
       return 1;
     }
 
     // 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...
+    // FIXME: outs() is not binary!
+    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;
       }
 
@@ -421,8 +424,8 @@ int main(int argc, char **argv) {
       if (PassInf->getNormalCtor())
         P = PassInf->getNormalCtor()();
       else
-        cerr << argv[0] << ": cannot create pass: "
-             << PassInf->getPassName() << "\n";
+        errs() << argv[0] << ": cannot create pass: "
+               << PassInf->getPassName() << "\n";
       if (P) {
         bool isBBPass = dynamic_cast<BasicBlockPass*>(P) != 0;
         bool isLPass = !isBBPass && dynamic_cast<LoopPass*>(P) != 0;
@@ -477,22 +480,22 @@ int main(int argc, char **argv) {
     if (!NoVerify && !VerifyEach)
       Passes.add(createVerifierPass());
 
-    // Write bitcode out to disk or cout as the last step...
+    // Write bitcode out to disk or outs() 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) 
+    // Delete the raw_fd_ostream.
+    if (Out != &outs())
       delete Out;
     return 0;
 
   } catch (const std::string& msg) {
-    cerr << argv[0] << ": " << msg << "\n";
+    errs() << argv[0] << ": " << msg << "\n";
   } catch (...) {
-    cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
+    errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
   }
   llvm_shutdown();
   return 1;