1 //===- BreakpointPrinter.cpp - Breakpoint location printer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Breakpoint location printer.
13 //===----------------------------------------------------------------------===//
14 #include "BreakpointPrinter.h"
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/IR/DebugInfo.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/raw_ostream.h"
25 struct BreakpointPrinter
: public ModulePass
{
28 DITypeIdentifierMap TypeIdentifierMap
;
30 BreakpointPrinter(raw_ostream
&out
) : ModulePass(ID
), Out(out
) {}
32 void getContextName(DIDescriptor Context
, std::string
&N
) {
33 if (Context
.isNameSpace()) {
34 DINameSpace
NS(Context
);
35 if (!NS
.getName().empty()) {
36 getContextName(NS
.getContext(), N
);
37 N
= N
+ NS
.getName().str() + "::";
39 } else if (Context
.isType()) {
41 if (!TY
.getName().empty()) {
42 getContextName(TY
.getContext().resolve(TypeIdentifierMap
), N
);
43 N
= N
+ TY
.getName().str() + "::";
48 bool runOnModule(Module
&M
) override
{
49 TypeIdentifierMap
.clear();
50 NamedMDNode
*CU_Nodes
= M
.getNamedMetadata("llvm.dbg.cu");
52 TypeIdentifierMap
= generateDITypeIdentifierMap(CU_Nodes
);
54 StringSet
<> Processed
;
55 if (NamedMDNode
*NMD
= M
.getNamedMetadata("llvm.dbg.sp"))
56 for (unsigned i
= 0, e
= NMD
->getNumOperands(); i
!= e
; ++i
) {
58 DISubprogram
SP(NMD
->getOperand(i
));
59 assert((!SP
|| SP
.isSubprogram()) &&
60 "A MDNode in llvm.dbg.sp should be null or a DISubprogram.");
63 getContextName(SP
.getContext().resolve(TypeIdentifierMap
), Name
);
64 Name
= Name
+ SP
.getDisplayName().str();
65 if (!Name
.empty() && Processed
.insert(Name
).second
) {
72 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
77 char BreakpointPrinter::ID
= 0;
80 ModulePass
*llvm::createBreakpointPrinter(raw_ostream
&out
) {
81 return new BreakpointPrinter(out
);