Change various llvm utilities to use PrettyStackTraceProgram in
[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/Module.h"
20 #include "llvm/PassManager.h"
21 #include "llvm/Bitcode/ReaderWriter.h"
22 #include "llvm/Assembly/PrintModulePass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/PrettyStackTrace.h"
27 #include "llvm/Support/Streams.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/System/Signals.h"
30 #include <iostream>
31 #include <fstream>
32 #include <memory>
33 using namespace llvm;
34
35 static cl::opt<std::string>
36 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
37
38 static cl::opt<std::string>
39 OutputFilename("o", cl::desc("Override output filename"),
40                cl::value_desc("filename"));
41
42 static cl::opt<bool>
43 Force("f", cl::desc("Overwrite output files"));
44
45 static cl::opt<bool>
46 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
47
48 int main(int argc, char **argv) {
49   // Print a stack trace if we signal out.
50   sys::PrintStackTraceOnErrorSignal();
51   PrettyStackTraceProgram X(argc, argv);
52   
53   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
54   try {
55     cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
56
57     std::ostream *Out = &std::cout;  // Default to printing to stdout.
58     std::string ErrorMessage;
59
60     std::auto_ptr<Module> M;
61    
62     if (MemoryBuffer *Buffer
63            = MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
64       M.reset(ParseBitcodeFile(Buffer, &ErrorMessage));
65       delete Buffer;
66     }
67
68     if (M.get() == 0) {
69       cerr << argv[0] << ": ";
70       if (ErrorMessage.size())
71         cerr << ErrorMessage << "\n";
72       else
73         cerr << "bitcode didn't read correctly.\n";
74       return 1;
75     }
76     
77     if (DontPrint) {
78       // Just use stdout.  We won't actually print anything on it.
79     } else if (OutputFilename != "") {   // Specified an output filename?
80       if (OutputFilename != "-") { // Not stdout?
81         if (!Force && std::ifstream(OutputFilename.c_str())) {
82           // If force is not specified, make sure not to overwrite a file!
83           cerr << argv[0] << ": error opening '" << OutputFilename
84                << "': file exists! Sending to standard output.\n";
85         } else {
86           Out = new std::ofstream(OutputFilename.c_str());
87         }
88       }
89     } else {
90       if (InputFilename == "-") {
91         OutputFilename = "-";
92       } else {
93         std::string IFN = InputFilename;
94         int Len = IFN.length();
95         if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
96           // Source ends in .bc
97           OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
98         } else {
99           OutputFilename = IFN+".ll";
100         }
101
102         if (!Force && std::ifstream(OutputFilename.c_str())) {
103           // If force is not specified, make sure not to overwrite a file!
104           cerr << argv[0] << ": error opening '" << OutputFilename
105                << "': file exists! Sending to standard output.\n";
106         } else {
107           Out = new std::ofstream(OutputFilename.c_str());
108
109           // Make sure that the Out file gets unlinked from the disk if we get a
110           // SIGINT
111           sys::RemoveFileOnSignal(sys::Path(OutputFilename));
112         }
113       }
114     }
115
116     if (!Out->good()) {
117       cerr << argv[0] << ": error opening " << OutputFilename
118            << ": sending to stdout instead!\n";
119       Out = &std::cout;
120     }
121
122     // All that llvm-dis does is write the assembly to a file.
123     if (!DontPrint) {
124       PassManager Passes;
125       raw_os_ostream L(*Out);
126       Passes.add(createPrintModulePass(&L));
127       Passes.run(*M.get());
128     }
129
130     if (Out != &std::cout) {
131       ((std::ofstream*)Out)->close();
132       delete Out;
133     }
134     return 0;
135   } catch (const std::string& msg) {
136     cerr << argv[0] << ": " << msg << "\n";
137   } catch (...) {
138     cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
139   }
140
141   return 1;
142 }
143