xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/PDB/Native/NativeEnumGlobals.cpp (revision 2dfa4b66b3d0caaaae6ce2df476b5615f8415a19)
1 //==- NativeEnumGlobals.cpp - Native Global Enumerator impl ------*- 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/Native/NativeEnumGlobals.h"
10 
11 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
12 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
13 #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
14 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
15 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
16 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
18 
19 using namespace llvm;
20 using namespace llvm::codeview;
21 using namespace llvm::pdb;
22 
23 NativeEnumGlobals::NativeEnumGlobals(NativeSession &PDBSession,
24                                      std::vector<codeview::SymbolKind> Kinds)
25     : Index(0), Session(PDBSession) {
26   GlobalsStream &GS = cantFail(Session.getPDBFile().getPDBGlobalsStream());
27   SymbolStream &SS = cantFail(Session.getPDBFile().getPDBSymbolStream());
28   for (uint32_t Off : GS.getGlobalsTable()) {
29     CVSymbol S = SS.readRecord(Off);
30     if (!llvm::is_contained(Kinds, S.kind()))
31       continue;
32     MatchOffsets.push_back(Off);
33   }
34 }
35 
36 uint32_t NativeEnumGlobals::getChildCount() const {
37   return static_cast<uint32_t>(MatchOffsets.size());
38 }
39 
40 std::unique_ptr<PDBSymbol>
41 NativeEnumGlobals::getChildAtIndex(uint32_t N) const {
42   if (N >= MatchOffsets.size())
43     return nullptr;
44 
45   SymIndexId Id =
46       Session.getSymbolCache().getOrCreateGlobalSymbolByOffset(MatchOffsets[N]);
47   return Session.getSymbolCache().getSymbolById(Id);
48 }
49 
50 std::unique_ptr<PDBSymbol> NativeEnumGlobals::getNext() {
51   return getChildAtIndex(Index++);
52 }
53 
54 void NativeEnumGlobals::reset() { Index = 0; }
55