blob: d3f54c034f55ecd5389927b1fe79780acc8ebd37 [file] [log] [blame]
Eli Benderskya0ea8fa2014-02-12 16:48:02 +00001//===- BreakpointPrinter.cpp - Breakpoint location printer ----------------===//
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/// \file
Adrian Prantl26b584c2018-05-01 15:54:18 +000011/// Breakpoint location printer.
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000012///
13//===----------------------------------------------------------------------===//
14#include "BreakpointPrinter.h"
15#include "llvm/ADT/StringSet.h"
Chandler Carruthf4ec8bf2014-03-06 00:46:21 +000016#include "llvm/IR/DebugInfo.h"
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000017#include "llvm/IR/Module.h"
18#include "llvm/Pass.h"
19#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22
23namespace {
24
25struct BreakpointPrinter : public ModulePass {
26 raw_ostream &Out;
27 static char ID;
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000028
29 BreakpointPrinter(raw_ostream &out) : ModulePass(ID), Out(out) {}
30
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000031 void getContextName(const DIScope *Context, std::string &N) {
32 if (auto *NS = dyn_cast<DINamespace>(Context)) {
Duncan P. N. Exon Smith41ed4932015-04-14 03:01:27 +000033 if (!NS->getName().empty()) {
34 getContextName(NS->getScope(), N);
35 N = N + NS->getName().str() + "::";
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000036 }
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000037 } else if (auto *TY = dyn_cast<DIType>(Context)) {
Duncan P. N. Exon Smith7f76d292015-04-16 01:01:28 +000038 if (!TY->getName().empty()) {
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +000039 getContextName(TY->getScope().resolve(), N);
Duncan P. N. Exon Smith7f76d292015-04-16 01:01:28 +000040 N = N + TY->getName().str() + "::";
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000041 }
42 }
43 }
44
Craig Topperc83e68f2014-03-08 08:27:28 +000045 bool runOnModule(Module &M) override {
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000046 StringSet<> Processed;
47 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
48 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
49 std::string Name;
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000050 auto *SP = cast_or_null<DISubprogram>(NMD->getOperand(i));
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000051 if (!SP)
52 continue;
Duncan P. N. Exon Smithde748402016-04-23 21:08:00 +000053 getContextName(SP->getScope().resolve(), Name);
Adrian Prantl9d291ed2017-04-26 23:59:52 +000054 Name = Name + SP->getName().str();
David Blaikie8f4a49f2014-11-19 02:56:00 +000055 if (!Name.empty() && Processed.insert(Name).second) {
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000056 Out << Name << "\n";
57 }
58 }
59 return false;
60 }
61
Craig Topperc83e68f2014-03-08 08:27:28 +000062 void getAnalysisUsage(AnalysisUsage &AU) const override {
Eli Benderskya0ea8fa2014-02-12 16:48:02 +000063 AU.setPreservesAll();
64 }
65};
66
67char BreakpointPrinter::ID = 0;
68}
69
70ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) {
71 return new BreakpointPrinter(out);
72}