Use the new tool_output_file in several tools. This fixes a variety
[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 is distributed under the University of Illinois Open Source
6 // 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 bitcode from stdin, write asm to stdout
12 //  llvm-dis [options] x.bc - Read LLVM bitcode 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/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/System/Signals.h"
28 using namespace llvm;
29
30 static cl::opt<std::string>
31 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
32
33 static cl::opt<std::string>
34 OutputFilename("o", cl::desc("Override output filename"),
35                cl::value_desc("filename"));
36
37 static cl::opt<bool>
38 Force("f", cl::desc("Enable binary output on terminals"));
39
40 static cl::opt<bool>
41 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
42
43 int main(int argc, char **argv) {
44   // Print a stack trace if we signal out.
45   sys::PrintStackTraceOnErrorSignal();
46   PrettyStackTraceProgram X(argc, argv);
47   
48   LLVMContext &Context = getGlobalContext();
49   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
50   
51   
52   cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
53
54   std::string ErrorMessage;
55   std::auto_ptr<Module> M;
56  
57   if (MemoryBuffer *Buffer
58          = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
59     M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
60     delete Buffer;
61   }
62
63   if (M.get() == 0) {
64     errs() << argv[0] << ": ";
65     if (ErrorMessage.size())
66       errs() << ErrorMessage << "\n";
67     else
68       errs() << "bitcode didn't read correctly.\n";
69     return 1;
70   }
71   
72   // Just use stdout.  We won't actually print anything on it.
73   if (DontPrint)
74     OutputFilename = "-";
75   
76   if (OutputFilename.empty()) { // Unspecified output, infer it.
77     if (InputFilename == "-") {
78       OutputFilename = "-";
79     } else {
80       const std::string &IFN = InputFilename;
81       int Len = IFN.length();
82       // If the source ends in .bc, strip it off.
83       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
84         OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
85       else
86         OutputFilename = IFN+".ll";
87     }
88   }
89
90   std::string ErrorInfo;
91   OwningPtr<tool_output_file> 
92   Out(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
93                            raw_fd_ostream::F_Binary));
94   if (!ErrorInfo.empty()) {
95     errs() << ErrorInfo << '\n';
96     return 1;
97   }
98
99   // All that llvm-dis does is write the assembly to a file.
100   if (!DontPrint)
101     *Out << *M;
102
103   // Declare success.
104   Out->keep();
105
106   return 0;
107 }
108