Convert to using llvm streams instead of iostreams.
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
1 //===-- llvm-dis.cpp - The low-level LLVM disassembler --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This utility may be invoked in the following manner:
11 //  llvm-dis [options]      - Read LLVM bytecode from stdin, write asm to stdout
12 //  llvm-dis [options] x.bc - Read LLVM bytecode from the x.bc file, write asm
13 //                            to the x.ll file.
14 //  Options:
15 //      --help   - Output information about command line switches
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bytecode/Reader.h"
22 #include "llvm/Assembly/PrintModulePass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Streams.h"
25 #include "llvm/System/Signals.h"
26 #include <iostream>
27 #include <fstream>
28 #include <memory>
29 using namespace llvm;
30
31 static cl::opt<std::string>
32 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
33
34 static cl::opt<std::string>
35 OutputFilename("o", cl::desc("Override output filename"),
36                cl::value_desc("filename"));
37
38 static cl::opt<bool>
39 Force("f", cl::desc("Overwrite output files"));
40
41 int main(int argc, char **argv) {
42   try {
43     cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
44     sys::PrintStackTraceOnErrorSignal();
45
46     std::ostream *Out = &std::cout;  // Default to printing to stdout.
47     std::string ErrorMessage;
48
49     std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage));
50     if (M.get() == 0) {
51       llvm_cerr << argv[0] << ": ";
52       if (ErrorMessage.size())
53         llvm_cerr << ErrorMessage << "\n";
54       else
55         llvm_cerr << "bytecode didn't read correctly.\n";
56       return 1;
57     }
58
59     if (OutputFilename != "") {   // Specified an output filename?
60       if (OutputFilename != "-") { // Not stdout?
61         if (!Force && std::ifstream(OutputFilename.c_str())) {
62           // If force is not specified, make sure not to overwrite a file!
63           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
64                     << "': file exists! Sending to standard output.\n";
65         } else {
66           Out = new std::ofstream(OutputFilename.c_str());
67         }
68       }
69     } else {
70       if (InputFilename == "-") {
71         OutputFilename = "-";
72       } else {
73         std::string IFN = InputFilename;
74         int Len = IFN.length();
75         if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
76           // Source ends in .bc
77           OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
78         } else {
79           OutputFilename = IFN+".ll";
80         }
81
82         if (!Force && std::ifstream(OutputFilename.c_str())) {
83           // If force is not specified, make sure not to overwrite a file!
84           llvm_cerr << argv[0] << ": error opening '" << OutputFilename
85                     << "': file exists! Sending to standard output.\n";
86         } else {
87           Out = new std::ofstream(OutputFilename.c_str());
88
89           // Make sure that the Out file gets unlinked from the disk if we get a
90           // SIGINT
91           sys::RemoveFileOnSignal(sys::Path(OutputFilename));
92         }
93       }
94     }
95
96     if (!Out->good()) {
97       llvm_cerr << argv[0] << ": error opening " << OutputFilename
98                 << ": sending to stdout instead!\n";
99       Out = &std::cout;
100     }
101
102     // All that llvm-dis does is write the assembly to a file.
103     PassManager Passes;
104     llvm_ostream L(*Out);
105     Passes.add(new PrintModulePass(&L));
106     Passes.run(*M.get());
107
108     if (Out != &std::cout) {
109       ((std::ofstream*)Out)->close();
110       delete Out;
111     }
112     return 0;
113   } catch (const std::string& msg) {
114     llvm_cerr << argv[0] << ": " << msg << "\n";
115   } catch (...) {
116     llvm_cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
117   }
118   return 1;
119 }
120