• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- PDBSymbolFunc.cpp - --------------------------------------*- C++ -*-===//
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 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
10 
11 #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
12 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
13 #include "llvm/DebugInfo/PDB/IPDBSession.h"
14 #include "llvm/DebugInfo/PDB/PDBSymDumper.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
17 #include "llvm/DebugInfo/PDB/PDBTypes.h"
18 
19 #include <unordered_set>
20 #include <utility>
21 #include <vector>
22 
23 using namespace llvm;
24 using namespace llvm::pdb;
25 
26 namespace {
27 class FunctionArgEnumerator : public IPDBEnumChildren<PDBSymbolData> {
28 public:
29   typedef ConcreteSymbolEnumerator<PDBSymbolData> ArgEnumeratorType;
30 
FunctionArgEnumerator(const IPDBSession & PDBSession,const PDBSymbolFunc & PDBFunc)31   FunctionArgEnumerator(const IPDBSession &PDBSession,
32                         const PDBSymbolFunc &PDBFunc)
33       : Session(PDBSession), Func(PDBFunc) {
34     // Arguments can appear multiple times if they have live range
35     // information, so we only take the first occurrence.
36     std::unordered_set<std::string> SeenNames;
37     auto DataChildren = Func.findAllChildren<PDBSymbolData>();
38     while (auto Child = DataChildren->getNext()) {
39       if (Child->getDataKind() == PDB_DataKind::Param) {
40         std::string Name = Child->getName();
41         if (SeenNames.find(Name) != SeenNames.end())
42           continue;
43         Args.push_back(std::move(Child));
44         SeenNames.insert(Name);
45       }
46     }
47     reset();
48   }
49 
getChildCount() const50   uint32_t getChildCount() const override { return Args.size(); }
51 
52   std::unique_ptr<PDBSymbolData>
getChildAtIndex(uint32_t Index) const53   getChildAtIndex(uint32_t Index) const override {
54     if (Index >= Args.size())
55       return nullptr;
56 
57     return Session.getConcreteSymbolById<PDBSymbolData>(
58         Args[Index]->getSymIndexId());
59   }
60 
getNext()61   std::unique_ptr<PDBSymbolData> getNext() override {
62     if (CurIter == Args.end())
63       return nullptr;
64     const auto &Result = **CurIter;
65     ++CurIter;
66     return Session.getConcreteSymbolById<PDBSymbolData>(Result.getSymIndexId());
67   }
68 
reset()69   void reset() override { CurIter = Args.empty() ? Args.end() : Args.begin(); }
70 
71 private:
72   typedef std::vector<std::unique_ptr<PDBSymbolData>> ArgListType;
73   const IPDBSession &Session;
74   const PDBSymbolFunc &Func;
75   ArgListType Args;
76   ArgListType::const_iterator CurIter;
77 };
78 }
79 
80 std::unique_ptr<IPDBEnumChildren<PDBSymbolData>>
getArguments() const81 PDBSymbolFunc::getArguments() const {
82   return std::make_unique<FunctionArgEnumerator>(Session, *this);
83 }
84 
dump(PDBSymDumper & Dumper) const85 void PDBSymbolFunc::dump(PDBSymDumper &Dumper) const { Dumper.dump(*this); }
86 
isDestructor() const87 bool PDBSymbolFunc::isDestructor() const {
88   std::string Name = getName();
89   if (Name.empty())
90     return false;
91   if (Name[0] == '~')
92     return true;
93   if (Name == "__vecDelDtor")
94     return true;
95   return false;
96 }
97 
getLineNumbers() const98 std::unique_ptr<IPDBEnumLineNumbers> PDBSymbolFunc::getLineNumbers() const {
99   auto Len = RawSymbol->getLength();
100   return Session.findLineNumbersByAddress(RawSymbol->getVirtualAddress(),
101                                           Len ? Len : 1);
102 }
103 
getCompilandId() const104 uint32_t PDBSymbolFunc::getCompilandId() const {
105   if (auto Lines = getLineNumbers()) {
106     if (auto FirstLine = Lines->getNext()) {
107       return FirstLine->getCompilandId();
108     }
109   }
110   return 0;
111 }
112