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