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