LLDB mainline
TypeMap.cpp
Go to the documentation of this file.
1//===-- TypeMap.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 <vector>
10
11#include "llvm/Support/FormattedStream.h"
12#include "llvm/Support/raw_ostream.h"
13
16#include "lldb/Symbol/Type.h"
17#include "lldb/Symbol/TypeMap.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22TypeMap::TypeMap() : m_types() {}
23
24// Destructor
25TypeMap::~TypeMap() = default;
26
27void TypeMap::Insert(const TypeSP &type_sp) {
28 // Just push each type on the back for now. We will worry about uniquing
29 // later
30 if (type_sp)
31 m_types.insert(std::make_pair(type_sp->GetID(), type_sp));
32}
33
34bool TypeMap::InsertUnique(const TypeSP &type_sp) {
35 if (type_sp) {
36 user_id_t type_uid = type_sp->GetID();
37 iterator pos, end = m_types.end();
38
39 for (pos = m_types.find(type_uid);
40 pos != end && pos->second->GetID() == type_uid; ++pos) {
41 if (pos->second.get() == type_sp.get())
42 return false;
43 }
44 Insert(type_sp);
45 }
46 return true;
47}
48
49// Find a base type by its unique ID.
50// TypeSP
51// TypeMap::FindType(lldb::user_id_t uid)
52//{
53// iterator pos = m_types.find(uid);
54// if (pos != m_types.end())
55// return pos->second;
56// return TypeSP();
57//}
58
59// Find a type by name.
60// TypeMap
61// TypeMap::FindTypes (ConstString name)
62//{
63// // Do we ever need to make a lookup by name map? Here we are doing
64// // a linear search which isn't going to be fast.
65// TypeMap types(m_ast.getTargetInfo()->getTriple().getTriple().c_str());
66// iterator pos, end;
67// for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
68// if (pos->second->GetName() == name)
69// types.Insert (pos->second);
70// return types;
71//}
72
73void TypeMap::Clear() { m_types.clear(); }
74
75uint32_t TypeMap::GetSize() const { return m_types.size(); }
76
77bool TypeMap::Empty() const { return m_types.empty(); }
78
79// GetTypeAtIndex isn't used a lot for large type lists, currently only for
80// type lists that are returned for "image dump -t TYPENAME" commands and other
81// simple symbol queries that grab the first result...
82
84 iterator pos, end;
85 uint32_t i = idx;
86 for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
87 if (i == 0)
88 return pos->second;
89 --i;
90 }
91 return TypeSP();
92}
93
95 std::function<bool(const lldb::TypeSP &type_sp)> const &callback) const {
96 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
97 if (!callback(pos->second))
98 break;
99 }
100}
101
103 std::function<bool(lldb::TypeSP &type_sp)> const &callback) {
104 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
105 if (!callback(pos->second))
106 break;
107 }
108}
109
110bool TypeMap::Remove(const lldb::TypeSP &type_sp) {
111 if (type_sp) {
112 lldb::user_id_t uid = type_sp->GetID();
113 for (iterator pos = m_types.find(uid), end = m_types.end();
114 pos != end && pos->first == uid; ++pos) {
115 if (pos->second == type_sp) {
116 m_types.erase(pos);
117 return true;
118 }
119 }
120 }
121 return false;
122}
123
124void TypeMap::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) {
125 for (iterator pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
126 pos->second->Dump(s, show_context, level);
127 }
128}
129
130void TypeMap::RemoveMismatchedTypes(llvm::StringRef type_scope,
131 llvm::StringRef type_basename,
132 TypeClass type_class, bool exact_match) {
133 // Our "collection" type currently is a std::map which doesn't have any good
134 // way to iterate and remove items from the map so we currently just make a
135 // new list and add all of the matching types to it, and then swap it into
136 // m_types at the end
137 collection matching_types;
138
139 iterator pos, end = m_types.end();
140
141 for (pos = m_types.begin(); pos != end; ++pos) {
142 Type *the_type = pos->second.get();
143 bool keep_match = false;
144 TypeClass match_type_class = eTypeClassAny;
145
146 if (type_class != eTypeClassAny) {
147 match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
148 if ((match_type_class & type_class) == 0)
149 continue;
150 }
151
152 ConstString match_type_name_const_str(the_type->GetQualifiedName());
153 if (match_type_name_const_str) {
154 const char *match_type_name = match_type_name_const_str.GetCString();
155 llvm::StringRef match_type_scope;
156 llvm::StringRef match_type_basename;
157 if (Type::GetTypeScopeAndBasename(match_type_name, match_type_scope,
158 match_type_basename,
159 match_type_class)) {
160 if (match_type_basename == type_basename) {
161 const size_t type_scope_size = type_scope.size();
162 const size_t match_type_scope_size = match_type_scope.size();
163 if (exact_match || (type_scope_size == match_type_scope_size)) {
164 keep_match = match_type_scope == type_scope;
165 } else {
166 if (match_type_scope_size > type_scope_size) {
167 const size_t type_scope_pos = match_type_scope.rfind(type_scope);
168 if (type_scope_pos == match_type_scope_size - type_scope_size) {
169 if (type_scope_pos >= 2) {
170 // Our match scope ends with the type scope we were looking
171 // for, but we need to make sure what comes before the
172 // matching type scope is a namespace boundary in case we are
173 // trying to match: type_basename = "d" type_scope = "b::c::"
174 // We want to match:
175 // match_type_scope "a::b::c::"
176 // But not:
177 // match_type_scope "a::bb::c::"
178 // So below we make sure what comes before "b::c::" in
179 // match_type_scope is "::", or the namespace boundary
180 if (match_type_scope[type_scope_pos - 1] == ':' &&
181 match_type_scope[type_scope_pos - 2] == ':') {
182 keep_match = true;
183 }
184 }
185 }
186 }
187 }
188 }
189 } else {
190 // The type we are currently looking at doesn't exists in a namespace
191 // or class, so it only matches if there is no type scope...
192 keep_match = type_scope.empty() && type_basename == match_type_name;
193 }
194 }
195
196 if (keep_match) {
197 matching_types.insert(*pos);
198 }
199 }
200 m_types.swap(matching_types);
201}
lldb::TypeClass GetTypeClass() const
A uniqued constant string class.
Definition: ConstString.h:39
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:218
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void Dump(Stream *s, bool show_context, lldb::DescriptionLevel level=lldb::eDescriptionLevelFull)
Definition: TypeMap.cpp:124
uint32_t GetSize() const
Definition: TypeMap.cpp:75
void Insert(const lldb::TypeSP &type)
Definition: TypeMap.cpp:27
bool Empty() const
Definition: TypeMap.cpp:77
bool Remove(const lldb::TypeSP &type_sp)
Definition: TypeMap.cpp:110
bool InsertUnique(const lldb::TypeSP &type)
Definition: TypeMap.cpp:34
void ForEach(std::function< bool(const lldb::TypeSP &type_sp)> const &callback) const
Definition: TypeMap.cpp:94
std::multimap< lldb::user_id_t, lldb::TypeSP > collection
Definition: TypeMap.h:44
collection::iterator iterator
Definition: TypeMap.h:61
void RemoveMismatchedTypes(llvm::StringRef type_scope, llvm::StringRef type_basename, lldb::TypeClass type_class, bool exact_match)
Definition: TypeMap.cpp:130
collection m_types
Definition: TypeMap.h:64
lldb::TypeSP GetTypeAtIndex(uint32_t idx)
Definition: TypeMap.cpp:83
CompilerType GetForwardCompilerType()
Definition: Type.cpp:667
static bool GetTypeScopeAndBasename(llvm::StringRef name, llvm::StringRef &scope, llvm::StringRef &basename, lldb::TypeClass &type_class)
Definition: Type.cpp:676
ConstString GetQualifiedName()
Definition: Type.cpp:672
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
uint64_t user_id_t
Definition: lldb-types.h:80