Print function info. Patch by Minjang Kim.
[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/Metadata.h"
23 #include "llvm/Assembly/Writer.h"
24 #include "llvm/Analysis/DebugInfo.h"
25 #include "llvm/Analysis/Passes.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/raw_ostream.h"
29
30 using namespace llvm;
31
32 static cl::opt<bool>
33 PrintDirectory("print-fullpath",
34                cl::desc("Print fullpath when printing debug info"),
35                cl::Hidden);
36
37 namespace {
38   class PrintDbgInfo : public FunctionPass {
39     raw_ostream &Out;
40     void printVariableDeclaration(const Value *V);
41   public:
42     static char ID; // Pass identification
43     PrintDbgInfo() : FunctionPass(ID), Out(errs()) {
44       initializePrintDbgInfoPass(*PassRegistry::getPassRegistry());
45     }
46
47     virtual bool runOnFunction(Function &F);
48     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49       AU.setPreservesAll();
50     }
51   };
52   char PrintDbgInfo::ID = 0;
53 }
54
55 INITIALIZE_PASS(PrintDbgInfo, "print-dbginfo",
56                 "Print debug info in human readable form", false, false)
57
58 FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
59
60 void PrintDbgInfo::printVariableDeclaration(const Value *V) {
61   std::string DisplayName, File, Directory, Type;
62   unsigned LineNo;
63
64   if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
65     return;
66
67   Out << "; ";
68   WriteAsOperand(Out, V, false, 0);
69   if (isa<Function>(V)) 
70     Out << " is function " << DisplayName
71         << " of type " << Type << " declared at ";
72   else
73     Out << " is variable " << DisplayName
74         << " of type " << Type << " declared at ";
75
76   if (PrintDirectory)
77     Out << Directory << "/";
78
79   Out << File << ":" << LineNo << "\n";
80 }
81
82 bool PrintDbgInfo::runOnFunction(Function &F) {
83   if (F.isDeclaration())
84     return false;
85
86   Out << "function " << F.getName() << "\n\n";
87
88   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
89     BasicBlock *BB = I;
90
91     if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
92       // Skip dead blocks.
93       continue;
94
95     Out << BB->getName();
96     Out << ":";
97
98     Out << "\n";
99
100     for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
101          i != e; ++i) {
102
103         printVariableDeclaration(i);
104
105         if (const User *U = dyn_cast<User>(i)) {
106           for(unsigned i=0;i<U->getNumOperands();i++)
107             printVariableDeclaration(U->getOperand(i));
108         }
109     }
110   }
111   return false;
112 }