1 //===-- NameToDIE.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 "NameToDIE.h"
10 #include "lldb/Symbol/ObjectFile.h"
11 #include "lldb/Utility/ConstString.h"
12 #include "lldb/Utility/RegularExpression.h"
13 #include "lldb/Utility/Stream.h"
14 #include "lldb/Utility/StreamString.h"
15 
16 #include "DWARFDebugInfo.h"
17 #include "DWARFDebugInfoEntry.h"
18 #include "SymbolFileDWARF.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 void NameToDIE::Finalize() {
24   m_map.Sort();
25   m_map.SizeToFit();
26 }
27 
28 void NameToDIE::Insert(ConstString name, const DIERef &die_ref) {
29   assert(die_ref.unit_offset().hasValue());
30   m_map.Append(name, die_ref);
31 }
32 
33 size_t NameToDIE::Find(ConstString name, DIEArray &info_array) const {
34   return m_map.GetValues(name, info_array);
35 }
36 
37 size_t NameToDIE::Find(const RegularExpression &regex,
38                        DIEArray &info_array) const {
39   return m_map.GetValues(regex, info_array);
40 }
41 
42 size_t NameToDIE::FindAllEntriesForCompileUnit(dw_offset_t cu_offset,
43                                                DIEArray &info_array) const {
44   const size_t initial_size = info_array.size();
45   const uint32_t size = m_map.GetSize();
46   for (uint32_t i = 0; i < size; ++i) {
47     const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);
48     if (cu_offset == *die_ref.unit_offset())
49       info_array.push_back(die_ref);
50   }
51   return info_array.size() - initial_size;
52 }
53 
54 void NameToDIE::Dump(Stream *s) {
55   const uint32_t size = m_map.GetSize();
56   for (uint32_t i = 0; i < size; ++i) {
57     s->Format("{0} \"{1}\"\n", m_map.GetValueAtIndexUnchecked(i),
58               m_map.GetCStringAtIndexUnchecked(i));
59   }
60 }
61 
62 void NameToDIE::ForEach(
63     std::function<bool(ConstString name, const DIERef &die_ref)> const
64         &callback) const {
65   const uint32_t size = m_map.GetSize();
66   for (uint32_t i = 0; i < size; ++i) {
67     if (!callback(m_map.GetCStringAtIndexUnchecked(i),
68                   m_map.GetValueAtIndexUnchecked(i)))
69       break;
70   }
71 }
72 
73 void NameToDIE::Append(const NameToDIE &other) {
74   const uint32_t size = other.m_map.GetSize();
75   for (uint32_t i = 0; i < size; ++i) {
76     m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),
77                  other.m_map.GetValueAtIndexUnchecked(i));
78   }
79 }
80