Genericize the ReversePostOrderIterator.
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
index 27a3c9399604f249b5deb60597a21dda86d66754..04fc60b2f1fef3c89b9414af1fa7b3f4af609a47 100644 (file)
@@ -1,4 +1,4 @@
-//===------------------------------------------------------------------------===
+//===----------------------------------------------------------------------===//
 // LLVM 'DIS' UTILITY 
 //
 // This utility may be invoked in the following manner:
 //
 // TODO: add -vcg which prints VCG compatible output.
 //
-//===------------------------------------------------------------------------===
+//===----------------------------------------------------------------------===//
 
-#include <iostream.h>
-#include <fstream.h>
 #include "llvm/Module.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/Bytecode/Reader.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Method.h"
-#include "llvm/Support/DepthFirstIterator.h"
-#include "llvm/Support/PostOrderIterator.h"
+#include "llvm/Support/CFG.h"
+#include "Support/DepthFirstIterator.h"
+#include "Support/PostOrderIterator.h"
+#include "Support/CommandLine.h"
+#include <fstream>
+#include <iostream>
+using std::cerr;
 
 // OutputMode - The different orderings to print basic blocks in...
 enum OutputMode {
@@ -48,7 +50,7 @@ cl::EnumFlags<enum OutputMode> WriteMode(cl::NoFlags,
 
 int main(int argc, char **argv) {
   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
-  ostream *Out = &cout;  // Default to printing to stdout...
+  std::ostream *Out = &std::cout;  // Default to printing to stdout...
 
   Module *C = ParseBytecodeFile(InputFilename);
   if (C == 0) {
@@ -57,31 +59,41 @@ int main(int argc, char **argv) {
   }
   
   if (OutputFilename != "") {   // Specified an output filename?
-    Out = new ofstream(OutputFilename.c_str(), 
-                      (Force ? 0 : ios::noreplace)|ios::out);
+    if (!Force && std::ifstream(OutputFilename.c_str())) {
+      // If force is not specified, make sure not to overwrite a file!
+      cerr << "Error opening '" << OutputFilename
+           << "': File exists! Sending to standard output.\n";
+    } else {
+      Out = new std::ofstream(OutputFilename.c_str());
+    }
   } else {
     if (InputFilename == "-") {
       OutputFilename = "-";
-      Out = &cout;
     } else {
-      string IFN = InputFilename;
+      std::string IFN = InputFilename;
       int Len = IFN.length();
       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
        // Source ends in .bc
-       OutputFilename = string(IFN.begin(), IFN.end()-3);
+       OutputFilename = std::string(IFN.begin(), IFN.end()-3);
       } else {
        OutputFilename = IFN;   // Append a .ll to it
       }
       OutputFilename += ".ll";
-      Out = new ofstream(OutputFilename.c_str(), 
-                        (Force ? 0 : ios::noreplace)|ios::out);
+
+      if (!Force && std::ifstream(OutputFilename.c_str())) {
+        // If force is not specified, make sure not to overwrite a file!
+        cerr << "Error opening '" << OutputFilename
+             << "': File exists! Sending to standard output.\n";
+      } else {
+        Out = new std::ofstream(OutputFilename.c_str());
+      }
     }
   }
 
   if (!Out->good()) {
     cerr << "Error opening " << OutputFilename
         << ": sending to stdout instead!\n";
-    Out = &cout;
+    Out = &std::cout;
   }
 
   // All that dis does is write the assembly out to a file... which is exactly
@@ -101,20 +113,20 @@ int main(int argc, char **argv) {
       switch (WriteMode) {
       case dfo:                   // Depth First ordering
        copy(df_begin(M), df_end(M),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       case rdfo:            // Reverse Depth First ordering
        copy(df_begin(M, true), df_end(M),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       case po:                    // Post Order
        copy(po_begin(M), po_end(M),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       case rpo: {           // Reverse Post Order
-       ReversePostOrderTraversal RPOT(M);
+       ReversePostOrderTraversal<Method*> RPOT(M);
        copy(RPOT.begin(), RPOT.end(),
-            ostream_iterator<BasicBlock*>(*Out, "\n"));
+            std::ostream_iterator<BasicBlock*>(*Out, "\n"));
        break;
       }
       default:
@@ -125,6 +137,6 @@ int main(int argc, char **argv) {
   }
   delete C;
 
-  if (Out != &cout) delete Out;
+  if (Out != &std::cout) delete Out;
   return 0;
 }