]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/tools/opt/BreakpointPrinter.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / tools / opt / BreakpointPrinter.cpp
1 //===- BreakpointPrinter.cpp - Breakpoint location printer ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// Breakpoint location printer.
11 ///
12 //===----------------------------------------------------------------------===//
13 #include "BreakpointPrinter.h"
14 #include "llvm/ADT/StringSet.h"
15 #include "llvm/IR/DebugInfo.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/raw_ostream.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 struct BreakpointPrinter : public ModulePass {
25   raw_ostream &Out;
26   static char ID;
27
28   BreakpointPrinter(raw_ostream &out) : ModulePass(ID), Out(out) {}
29
30   void getContextName(const DIScope *Context, std::string &N) {
31     if (auto *NS = dyn_cast<DINamespace>(Context)) {
32       if (!NS->getName().empty()) {
33         getContextName(NS->getScope(), N);
34         N = N + NS->getName().str() + "::";
35       }
36     } else if (auto *TY = dyn_cast<DIType>(Context)) {
37       if (!TY->getName().empty()) {
38         getContextName(TY->getScope(), N);
39         N = N + TY->getName().str() + "::";
40       }
41     }
42   }
43
44   bool runOnModule(Module &M) override {
45     StringSet<> Processed;
46     if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
47       for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
48         std::string Name;
49         auto *SP = cast_or_null<DISubprogram>(NMD->getOperand(i));
50         if (!SP)
51           continue;
52         getContextName(SP->getScope(), Name);
53         Name = Name + SP->getName().str();
54         if (!Name.empty() && Processed.insert(Name).second) {
55           Out << Name << "\n";
56         }
57       }
58     return false;
59   }
60
61   void getAnalysisUsage(AnalysisUsage &AU) const override {
62     AU.setPreservesAll();
63   }
64 };
65
66 char BreakpointPrinter::ID = 0;
67 }
68
69 ModulePass *llvm::createBreakpointPrinter(raw_ostream &out) {
70   return new BreakpointPrinter(out);
71 }