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     const DWARFUnit &unit,
49     llvm::function_ref<bool(DIERef ref)> callback) const {
50   const uint32_t size = m_map.GetSize();
51   for (uint32_t i = 0; i < size; ++i) {
52     const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);
53     if (unit.GetSymbolFileDWARF().GetDwoNum() == die_ref.dwo_num() &&
54         unit.GetDebugSection() == die_ref.section() &&
55         unit.GetOffset() <= die_ref.die_offset() &&
56         die_ref.die_offset() < unit.GetNextUnitOffset()) {
57       if (!callback(die_ref))
58         return;
59     }
60   }
61 }
62 
63 void NameToDIE::Dump(Stream *s) {
64   const uint32_t size = m_map.GetSize();
65   for (uint32_t i = 0; i < size; ++i) {
66     s->Format("{0} \"{1}\"\n", m_map.GetValueAtIndexUnchecked(i),
67               m_map.GetCStringAtIndexUnchecked(i));
68   }
69 }
70 
71 void NameToDIE::ForEach(
72     std::function<bool(ConstString name, const DIERef &die_ref)> const
73         &callback) const {
74   const uint32_t size = m_map.GetSize();
75   for (uint32_t i = 0; i < size; ++i) {
76     if (!callback(m_map.GetCStringAtIndexUnchecked(i),
77                   m_map.GetValueAtIndexUnchecked(i)))
78       break;
79   }
80 }
81 
82 void NameToDIE::Append(const NameToDIE &other) {
83   const uint32_t size = other.m_map.GetSize();
84   for (uint32_t i = 0; i < size; ++i) {
85     m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),
86                  other.m_map.GetValueAtIndexUnchecked(i));
87   }
88 }
89