1 //===-- NameToDIE.cpp -----------------------------------------------------===//
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 "DWARFUnit.h"
11 #include "lldb/Symbol/ObjectFile.h"
12 #include "lldb/Utility/ConstString.h"
13 #include "lldb/Utility/RegularExpression.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/Utility/StreamString.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 void NameToDIE::Finalize() {
21   m_map.Sort();
22   m_map.SizeToFit();
23 }
24 
25 void NameToDIE::Insert(ConstString name, const DIERef &die_ref) {
26   m_map.Append(name, die_ref);
27 }
28 
29 bool NameToDIE::Find(ConstString name,
30                      llvm::function_ref<bool(DIERef ref)> callback) const {
31   for (const auto &entry : m_map.equal_range(name))
32     if (!callback(entry.value))
33       return false;
34   return true;
35 }
36 
37 bool NameToDIE::Find(const RegularExpression &regex,
38                      llvm::function_ref<bool(DIERef ref)> callback) const {
39   for (const auto &entry : m_map)
40     if (regex.Execute(entry.cstring.GetCString())) {
41       if (!callback(entry.value))
42         return false;
43     }
44   return true;
45 }
46 
47 void NameToDIE::FindAllEntriesForUnit(
48     DWARFUnit &s_unit, llvm::function_ref<bool(DIERef ref)> callback) const {
49   lldbassert(!s_unit.GetSymbolFileDWARF().GetDwoNum());
50   const DWARFUnit &ns_unit = s_unit.GetNonSkeletonUnit();
51   const uint32_t size = m_map.GetSize();
52   for (uint32_t i = 0; i < size; ++i) {
53     const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);
54     if (ns_unit.GetSymbolFileDWARF().GetDwoNum() == die_ref.dwo_num() &&
55         ns_unit.GetDebugSection() == die_ref.section() &&
56         ns_unit.GetOffset() <= die_ref.die_offset() &&
57         die_ref.die_offset() < ns_unit.GetNextUnitOffset()) {
58       if (!callback(die_ref))
59         return;
60     }
61   }
62 }
63 
64 void NameToDIE::Dump(Stream *s) {
65   const uint32_t size = m_map.GetSize();
66   for (uint32_t i = 0; i < size; ++i) {
67     s->Format("{0} \"{1}\"\n", m_map.GetValueAtIndexUnchecked(i),
68               m_map.GetCStringAtIndexUnchecked(i));
69   }
70 }
71 
72 void NameToDIE::ForEach(
73     std::function<bool(ConstString name, const DIERef &die_ref)> const
74         &callback) const {
75   const uint32_t size = m_map.GetSize();
76   for (uint32_t i = 0; i < size; ++i) {
77     if (!callback(m_map.GetCStringAtIndexUnchecked(i),
78                   m_map.GetValueAtIndexUnchecked(i)))
79       break;
80   }
81 }
82 
83 void NameToDIE::Append(const NameToDIE &other) {
84   const uint32_t size = other.m_map.GetSize();
85   for (uint32_t i = 0; i < size; ++i) {
86     m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),
87                  other.m_map.GetValueAtIndexUnchecked(i));
88   }
89 }
90