DebugInfo: Gut DIVariable and DIGlobalVariable
[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/IR/LLVMContext.h"
20 #include "llvm/Bitcode/ReaderWriter.h"
21 #include "llvm/IR/AssemblyAnnotationWriter.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/IR/DiagnosticInfo.h"
24 #include "llvm/IR/DiagnosticPrinter.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/DataStream.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/ManagedStatic.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/PrettyStackTrace.h"
35 #include "llvm/Support/Signals.h"
36 #include "llvm/Support/ToolOutputFile.h"
37 #include <system_error>
38 using namespace llvm;
39
40 static cl::opt<std::string>
41 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
42
43 static cl::opt<std::string>
44 OutputFilename("o", cl::desc("Override output filename"),
45                cl::value_desc("filename"));
46
47 static cl::opt<bool>
48 Force("f", cl::desc("Enable binary output on terminals"));
49
50 static cl::opt<bool>
51 DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden);
52
53 static cl::opt<bool>
54 ShowAnnotations("show-annotations",
55                 cl::desc("Add informational comments to the .ll file"));
56
57 namespace {
58
59 static void printDebugLoc(const DebugLoc &DL, formatted_raw_ostream &OS) {
60   OS << DL.getLine() << ":" << DL.getCol();
61   if (MDLocation *IDL = DL.getInlinedAt()) {
62     OS << "@";
63     printDebugLoc(IDL, OS);
64   }
65 }
66 class CommentWriter : public AssemblyAnnotationWriter {
67 public:
68   void emitFunctionAnnot(const Function *F,
69                          formatted_raw_ostream &OS) override {
70     OS << "; [#uses=" << F->getNumUses() << ']';  // Output # uses
71     OS << '\n';
72   }
73   void printInfoComment(const Value &V, formatted_raw_ostream &OS) override {
74     bool Padded = false;
75     if (!V.getType()->isVoidTy()) {
76       OS.PadToColumn(50);
77       Padded = true;
78       OS << "; [#uses=" << V.getNumUses() << " type=" << *V.getType() << "]";  // Output # uses and type
79     }
80     if (const Instruction *I = dyn_cast<Instruction>(&V)) {
81       if (const DebugLoc &DL = I->getDebugLoc()) {
82         if (!Padded) {
83           OS.PadToColumn(50);
84           Padded = true;
85           OS << ";";
86         }
87         OS << " [debug line = ";
88         printDebugLoc(DL,OS);
89         OS << "]";
90       }
91       if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I)) {
92         DIVariable Var(DDI->getVariable());
93         if (!Padded) {
94           OS.PadToColumn(50);
95           OS << ";";
96         }
97         OS << " [debug variable = " << Var->getName() << "]";
98       }
99       else if (const DbgValueInst *DVI = dyn_cast<DbgValueInst>(I)) {
100         DIVariable Var(DVI->getVariable());
101         if (!Padded) {
102           OS.PadToColumn(50);
103           OS << ";";
104         }
105         OS << " [debug variable = " << Var->getName() << "]";
106       }
107     }
108   }
109 };
110
111 } // end anon namespace
112
113 static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
114   raw_ostream &OS = errs();
115   OS << (char *)Context << ": ";
116   switch (DI.getSeverity()) {
117   case DS_Error: OS << "error: "; break;
118   case DS_Warning: OS << "warning: "; break;
119   case DS_Remark: OS << "remark: "; break;
120   case DS_Note: OS << "note: "; break;
121   }
122
123   DiagnosticPrinterRawOStream DP(OS);
124   DI.print(DP);
125   OS << '\n';
126
127   if (DI.getSeverity() == DS_Error)
128     exit(1);
129 }
130
131 int main(int argc, char **argv) {
132   // Print a stack trace if we signal out.
133   sys::PrintStackTraceOnErrorSignal();
134   PrettyStackTraceProgram X(argc, argv);
135
136   LLVMContext &Context = getGlobalContext();
137   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
138
139   Context.setDiagnosticHandler(diagnosticHandler, argv[0]);
140
141   cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
142
143   std::string ErrorMessage;
144   std::unique_ptr<Module> M;
145
146   // Use the bitcode streaming interface
147   DataStreamer *Streamer = getDataFileStreamer(InputFilename, &ErrorMessage);
148   if (Streamer) {
149     std::string DisplayFilename;
150     if (InputFilename == "-")
151       DisplayFilename = "<stdin>";
152     else
153       DisplayFilename = InputFilename;
154     ErrorOr<std::unique_ptr<Module>> MOrErr =
155         getStreamedBitcodeModule(DisplayFilename, Streamer, Context);
156     M = std::move(*MOrErr);
157     M->materializeAllPermanently();
158   }
159
160   // Just use stdout.  We won't actually print anything on it.
161   if (DontPrint)
162     OutputFilename = "-";
163
164   if (OutputFilename.empty()) { // Unspecified output, infer it.
165     if (InputFilename == "-") {
166       OutputFilename = "-";
167     } else {
168       const std::string &IFN = InputFilename;
169       int Len = IFN.length();
170       // If the source ends in .bc, strip it off.
171       if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
172         OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
173       else
174         OutputFilename = IFN+".ll";
175     }
176   }
177
178   std::error_code EC;
179   std::unique_ptr<tool_output_file> Out(
180       new tool_output_file(OutputFilename, EC, sys::fs::F_None));
181   if (EC) {
182     errs() << EC.message() << '\n';
183     return 1;
184   }
185
186   std::unique_ptr<AssemblyAnnotationWriter> Annotator;
187   if (ShowAnnotations)
188     Annotator.reset(new CommentWriter());
189
190   // All that llvm-dis does is write the assembly to a file.
191   if (!DontPrint)
192     M->print(Out->os(), Annotator.get());
193
194   // Declare success.
195   Out->keep();
196
197   return 0;
198 }