NameToDIE.cpp revision 360660
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 "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
17using namespace lldb;
18using namespace lldb_private;
19
20void NameToDIE::Finalize() {
21  m_map.Sort();
22  m_map.SizeToFit();
23}
24
25void NameToDIE::Insert(ConstString name, const DIERef &die_ref) {
26  m_map.Append(name, die_ref);
27}
28
29size_t NameToDIE::Find(ConstString name, DIEArray &info_array) const {
30  return m_map.GetValues(name, info_array);
31}
32
33size_t NameToDIE::Find(const RegularExpression &regex,
34                       DIEArray &info_array) const {
35  return m_map.GetValues(regex, info_array);
36}
37
38size_t NameToDIE::FindAllEntriesForUnit(const DWARFUnit &unit,
39                                        DIEArray &info_array) const {
40  const size_t initial_size = info_array.size();
41  const uint32_t size = m_map.GetSize();
42  for (uint32_t i = 0; i < size; ++i) {
43    const DIERef &die_ref = m_map.GetValueAtIndexUnchecked(i);
44    if (unit.GetSymbolFileDWARF().GetDwoNum() == die_ref.dwo_num() &&
45        unit.GetDebugSection() == die_ref.section() &&
46        unit.GetOffset() <= die_ref.die_offset() &&
47        die_ref.die_offset() < unit.GetNextUnitOffset())
48      info_array.push_back(die_ref);
49  }
50  return info_array.size() - initial_size;
51}
52
53void NameToDIE::Dump(Stream *s) {
54  const uint32_t size = m_map.GetSize();
55  for (uint32_t i = 0; i < size; ++i) {
56    s->Format("{0} \"{1}\"\n", m_map.GetValueAtIndexUnchecked(i),
57              m_map.GetCStringAtIndexUnchecked(i));
58  }
59}
60
61void NameToDIE::ForEach(
62    std::function<bool(ConstString name, const DIERef &die_ref)> const
63        &callback) const {
64  const uint32_t size = m_map.GetSize();
65  for (uint32_t i = 0; i < size; ++i) {
66    if (!callback(m_map.GetCStringAtIndexUnchecked(i),
67                  m_map.GetValueAtIndexUnchecked(i)))
68      break;
69  }
70}
71
72void NameToDIE::Append(const NameToDIE &other) {
73  const uint32_t size = other.m_map.GetSize();
74  for (uint32_t i = 0; i < size; ++i) {
75    m_map.Append(other.m_map.GetCStringAtIndexUnchecked(i),
76                 other.m_map.GetValueAtIndexUnchecked(i));
77  }
78}
79