Make sure to close the file before deleting it
[oota-llvm.git] / tools / llvm-dis / llvm-dis.cpp
1 //===----------------------------------------------------------------------===//
2 // LLVM 'DIS' UTILITY 
3 //
4 // This utility may be invoked in the following manner:
5 //  dis [options]      - Read LLVM bytecode from stdin, write assembly to stdout
6 //  dis [options] x.bc - Read LLVM bytecode from the x.bc file, write assembly
7 //                       to the x.ll file.
8 //  Options:
9 //      --help   - Output information about command line switches
10 //       -c      - Print C code instead of LLVM assembly
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Module.h"
15 #include "llvm/PassManager.h"
16 #include "llvm/Bytecode/Reader.h"
17 #include "llvm/Assembly/CWriter.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "Support/CommandLine.h"
20 #include "Support/Signals.h"
21 #include <fstream>
22 #include <memory>
23 using std::cerr;
24
25 // OutputMode - The different orderings to print basic blocks in...
26 enum OutputMode {
27   llvm = 0,           // Generate LLVM assembly (the default)
28   c,                  // Generate C code
29 };
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 static cl::opt<enum OutputMode>
42 WriteMode(cl::desc("Specify the output format:"),
43           cl::values(
44                      clEnumVal(llvm, "Output LLVM assembly"),
45                      clEnumVal(c   , "Output C code for program"),
46                     0));
47
48 int main(int argc, char **argv) {
49   cl::ParseCommandLineOptions(argc, argv, " llvm .bc -> .ll disassembler\n");
50   std::ostream *Out = &std::cout;  // Default to printing to stdout...
51
52   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
53   if (M.get() == 0) {
54     cerr << argv[0] << ": bytecode didn't read correctly.\n";
55     return 1;
56   }
57   
58   if (OutputFilename != "") {   // Specified an output filename?
59     if (!Force && std::ifstream(OutputFilename.c_str())) {
60       // If force is not specified, make sure not to overwrite a file!
61       cerr << argv[0] << ": error opening '" << OutputFilename
62            << "': file exists! Sending to standard output.\n";
63     } else {
64       Out = new std::ofstream(OutputFilename.c_str());
65     }
66   } else {
67     if (InputFilename == "-") {
68       OutputFilename = "-";
69     } else {
70       std::string IFN = InputFilename;
71       int Len = IFN.length();
72       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
73         // Source ends in .bc
74         OutputFilename = std::string(IFN.begin(), IFN.end()-3);
75       } else {
76         OutputFilename = IFN;   // Append a .ll to it
77       }
78       if (WriteMode == c)
79         OutputFilename += ".c";
80       else
81         OutputFilename += ".ll";
82
83       if (!Force && std::ifstream(OutputFilename.c_str())) {
84         // If force is not specified, make sure not to overwrite a file!
85         cerr << argv[0] << ": error opening '" << OutputFilename
86              << "': file exists! Sending to standard output.\n";
87       } else {
88         Out = new std::ofstream(OutputFilename.c_str());
89
90         // Make sure that the Out file gets unlink'd from the disk if we get a
91         // SIGINT
92         RemoveFileOnSignal(OutputFilename);
93       }
94     }
95   }
96
97   if (!Out->good()) {
98     cerr << argv[0] << ": error opening " << OutputFilename
99          << ": sending to stdout instead!\n";
100     Out = &std::cout;
101   }
102
103   // All that dis does is write the assembly or C out to a file...
104   //
105   PassManager Passes;
106
107   switch (WriteMode) {
108   case llvm:           // Output LLVM assembly
109     Passes.add(new PrintModulePass(Out));
110     break;
111   case c:              // Convert LLVM to C
112     Passes.add(createWriteToCPass(*Out));
113     break;
114   }
115
116   Passes.run(*M.get());
117
118   if (Out != &std::cout) {
119     ((std::ofstream*)Out)->close();
120     delete Out;
121   }
122   return 0;
123 }
124