Convert debug messages to use dbgs(). Generally this means
[oota-llvm.git] / lib / Analysis / DbgInfoPrinter.cpp
1 //===- DbgInfoPrinter.cpp - Print debug info in a human readable form ------==//
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 file implements a pass that prints instructions, and associated debug
11 // info:
12 // 
13 //   - source/line/col information
14 //   - original variable name
15 //   - original type name
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "llvm/Pass.h"
20 #include "llvm/Function.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/Analysis/DebugInfo.h"
24 #include "llvm/Analysis/Passes.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/raw_ostream.h"
28
29 using namespace llvm;
30
31 static cl::opt<bool>
32 PrintDirectory("print-fullpath",
33                cl::desc("Print fullpath when printing debug info"),
34                cl::Hidden);
35
36 namespace {
37   class PrintDbgInfo : public FunctionPass {
38     raw_ostream &Out;
39     void printStopPoint(const DbgStopPointInst *DSI);
40     void printFuncStart(const DbgFuncStartInst *FS);
41     void printVariableDeclaration(const Value *V);
42   public:
43     static char ID; // Pass identification
44     PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
45
46     virtual bool runOnFunction(Function &F);
47     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48       AU.setPreservesAll();
49     }
50   };
51   char PrintDbgInfo::ID = 0;
52   static RegisterPass<PrintDbgInfo> X("print-dbginfo",
53                                      "Print debug info in human readable form");
54 }
55
56 FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
57
58 void PrintDbgInfo::printVariableDeclaration(const Value *V) {
59   std::string DisplayName, File, Directory, Type;
60   unsigned LineNo;
61
62   if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
63     return;
64
65   Out << "; ";
66   WriteAsOperand(Out, V, false, 0);
67   Out << " is variable " << DisplayName
68       << " of type " << Type << " declared at ";
69
70   if (PrintDirectory)
71     Out << Directory << "/";
72
73   Out << File << ":" << LineNo << "\n";
74 }
75
76 void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) {
77   if (PrintDirectory)
78     if (MDString *Str = dyn_cast<MDString>(DSI->getDirectory()))
79       Out << Str->getString() << '/';
80
81   if (MDString *Str = dyn_cast<MDString>(DSI->getFileName()))
82     Out << Str->getString();
83   Out << ':' << DSI->getLine();
84
85   if (unsigned Col = DSI->getColumn())
86     Out << ':' << Col;
87 }
88
89 void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) {
90   DISubprogram Subprogram(FS->getSubprogram());
91   Out << "; fully qualified function name: " << Subprogram.getDisplayName()
92       << " return type: " << Subprogram.getReturnTypeName()
93       << " at line " << Subprogram.getLineNumber()
94       << "\n\n";
95 }
96
97 bool PrintDbgInfo::runOnFunction(Function &F) {
98   if (F.isDeclaration())
99     return false;
100
101   Out << "function " << F.getName() << "\n\n";
102
103   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
104     BasicBlock *BB = I;
105
106     if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
107       // Skip dead blocks.
108       continue;
109
110     const DbgStopPointInst *DSI = findBBStopPoint(BB);
111     Out << BB->getName();
112     Out << ":";
113
114     if (DSI) {
115       Out << "; (";
116       printStopPoint(DSI);
117       Out << ")";
118     }
119
120     Out << "\n";
121
122     // A dbgstoppoint's information is valid until we encounter a new one.
123     const DbgStopPointInst *LastDSP = DSI;
124     bool Printed = DSI != 0;
125     for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
126          i != e; ++i) {
127       if (isa<DbgInfoIntrinsic>(i)) {
128         if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
129           if (DSI->getContext() == LastDSP->getContext() &&
130               DSI->getLineValue() == LastDSP->getLineValue() &&
131               DSI->getColumnValue() == LastDSP->getColumnValue())
132             // Don't print same location twice.
133             continue;
134
135           LastDSP = cast<DbgStopPointInst>(i);
136
137           // Don't print consecutive stoppoints, use a flag to know which one we
138           // printed.
139           Printed = false;
140         } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
141           printFuncStart(FS);
142         }
143       } else {
144         if (!Printed && LastDSP) {
145           Out << "; ";
146           printStopPoint(LastDSP);
147           Out << "\n";
148           Printed = true;
149         }
150
151         Out << *i << '\n';
152         printVariableDeclaration(i);
153
154         if (const User *U = dyn_cast<User>(i)) {
155           for(unsigned i=0;i<U->getNumOperands();i++)
156             printVariableDeclaration(U->getOperand(i));
157         }
158       }
159     }
160   }
161
162   return false;
163 }