LLDB mainline
Mangled.h
Go to the documentation of this file.
1//===-- Mangled.h -----------------------------------------------*- 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#ifndef LLDB_CORE_MANGLED_H
10#define LLDB_CORE_MANGLED_H
11
15#include "lldb/lldb-forward.h"
16#include "lldb/lldb-types.h"
17#include "llvm/ADT/StringRef.h"
18
19#include <cstddef>
20#include <memory>
21
22namespace lldb_private {
23
24/// \class Mangled Mangled.h "lldb/Core/Mangled.h"
25/// A class that handles mangled names.
26///
27/// Designed to handle mangled names. The demangled version of any names will
28/// be computed when the demangled name is accessed through the Demangled()
29/// accessor. This class can also tokenize the demangled version of the name
30/// for powerful searches. Functions and symbols could make instances of this
31/// class for their mangled names. Uniqued string pools are used for the
32/// mangled, demangled, and token string values to allow for faster
33/// comparisons and for efficient memory use.
34class Mangled {
35public:
41
50
51 /// Default constructor.
52 ///
53 /// Initialize with both mangled and demangled names empty.
54 Mangled() = default;
55
56 Mangled(const Mangled &other)
60 ? std::make_unique<DemangledNameInfo>(*other.m_demangled_info)
61 : nullptr) {}
62
63 Mangled &operator=(const Mangled &other) {
64 if (this != &other) {
65 m_mangled = other.m_mangled;
69 ? std::make_unique<DemangledNameInfo>(*other.m_demangled_info)
70 : nullptr;
71 }
72 return *this;
73 }
74
75 Mangled(Mangled &&) = default;
76 Mangled &operator=(Mangled &&) = default;
77
78 /// Construct with name.
79 ///
80 /// Constructor with an optional string and auto-detect if \a name is
81 /// mangled or not.
82 ///
83 /// \param[in] name
84 /// The already const name to copy into this object.
85 explicit Mangled(ConstString name);
86
87 explicit Mangled(llvm::StringRef name);
88
89 bool operator==(const Mangled &rhs) const {
90 return m_mangled == rhs.m_mangled &&
92 }
93
94 bool operator!=(const Mangled &rhs) const {
95 return !(*this == rhs);
96 }
97
98 /// Convert to bool operator.
99 ///
100 /// This allows code to check any Mangled objects to see if they contain
101 /// anything valid using code such as:
102 ///
103 /// \code
104 /// Mangled mangled(...);
105 /// if (mangled)
106 /// { ...
107 /// \endcode
108 ///
109 /// \return
110 /// Returns \b true if either the mangled or unmangled name is set,
111 /// \b false if the object has an empty mangled and unmangled name.
112 explicit operator bool() const;
113
114 /// Clear the mangled and demangled values.
115 void Clear();
116
117 /// Compare the mangled string values
118 ///
119 /// Compares the Mangled::GetName() string in \a lhs and \a rhs.
120 ///
121 /// \param[in] lhs
122 /// A const reference to the Left Hand Side object to compare.
123 ///
124 /// \param[in] rhs
125 /// A const reference to the Right Hand Side object to compare.
126 ///
127 /// \return
128 /// -1 if \a lhs is less than \a rhs
129 /// 0 if \a lhs is equal to \a rhs
130 /// 1 if \a lhs is greater than \a rhs
131 static int Compare(const Mangled &lhs, const Mangled &rhs);
132
133 /// Dump a description of this object to a Stream \a s.
134 ///
135 /// Dump a Mangled object to stream \a s. We don't force our demangled name
136 /// to be computed currently (we don't use the accessor).
137 ///
138 /// \param[in] s
139 /// The stream to which to dump the object description.
140 void Dump(Stream *s) const;
141
142 /// Dump a debug description of this object to a Stream \a s.
143 ///
144 /// \param[in] s
145 /// The stream to which to dump the object description.
146 void DumpDebug(Stream *s) const;
147
148 /// Demangled name get accessor.
149 ///
150 /// \return
151 /// A const reference to the demangled name string object.
153
154 /// Display demangled name get accessor.
155 ///
156 /// \return
157 /// A const reference to the display demangled name string object.
159
161 m_demangled = name;
162 m_demangled_info.reset();
163 }
164
166 m_mangled = name;
167 m_demangled_info.reset();
168 }
169
170 /// Mangled name get accessor.
171 ///
172 /// \return
173 /// The mangled name string object.
175
176 /// Best name get accessor.
177 ///
178 /// \param[in] preference
179 /// Which name would you prefer to get?
180 ///
181 /// \return
182 /// A const reference to the preferred name string object if this
183 /// object has a valid name of that kind, else a const reference to the
184 /// other name is returned.
185 ConstString GetName(NamePreference preference = ePreferDemangled) const;
186
187 /// Check if "name" matches either the mangled or demangled name.
188 ///
189 /// \param[in] name
190 /// A name to match against both strings.
191 ///
192 /// \return
193 /// \b True if \a name matches either name, \b false otherwise.
194 bool NameMatches(ConstString name) const {
195 if (m_mangled == name)
196 return true;
197 return GetDemangledName() == name;
198 }
199 bool NameMatches(const RegularExpression &regex) const;
200
201 /// Get the memory cost of this object.
202 ///
203 /// Return the size in bytes that this object takes in memory. This returns
204 /// the size in bytes of this object, not any shared string values it may
205 /// refer to.
206 ///
207 /// \return
208 /// The number of bytes that this object occupies in memory.
209 size_t MemorySize() const;
210
211 /// Set the string value in this object.
212 ///
213 /// This version auto detects if the string is mangled by inspecting the
214 /// string value and looking for common mangling prefixes.
215 ///
216 /// \param[in] name
217 /// The already const version of the name for this object.
218 void SetValue(ConstString name);
219
220 /// Try to guess the language from the mangling.
221 ///
222 /// For a mangled name to have a language it must have both a mangled and a
223 /// demangled name and it can be guessed from the mangling what the language
224 /// is. Note: this will return C++ for any language that uses Itanium ABI
225 /// mangling.
226 ///
227 /// Standard C function names will return eLanguageTypeUnknown because they
228 /// aren't mangled and it isn't clear what language the name represents
229 /// (there will be no mangled name).
230 ///
231 /// \return
232 /// The language for the mangled/demangled name, eLanguageTypeUnknown
233 /// if there is no mangled or demangled counterpart.
235
236 /// Function signature for filtering mangled names.
237 using SkipMangledNameFn = bool(llvm::StringRef, ManglingScheme);
238
239 /// Get rich mangling information. This is optimized for batch processing
240 /// while populating a name index. To get the pure demangled name string for
241 /// a single entity, use GetDemangledName() instead.
242 ///
243 /// For names that match the Itanium mangling scheme, this uses LLVM's
244 /// ItaniumPartialDemangler. All other names fall back to LLDB's builtin
245 /// parser currently.
246 ///
247 /// This function is thread-safe when used with different \a context
248 /// instances in different threads.
249 ///
250 /// \param[in] context
251 /// The context for this function. A single instance can be stack-
252 /// allocated in the caller's frame and used for multiple calls.
253 ///
254 /// \param[in] skip_mangled_name
255 /// A filtering function for skipping entities based on name and mangling
256 /// scheme. This can be null if unused.
257 ///
258 /// \return
259 /// True on success, false otherwise.
261 SkipMangledNameFn *skip_mangled_name);
262
263 /// Try to identify the mangling scheme used.
264 /// \param[in] name
265 /// The name we are attempting to identify the mangling scheme for.
266 ///
267 /// \return
268 /// eManglingSchemeNone if no known mangling scheme could be identified
269 /// for s, otherwise the enumerator for the mangling scheme detected.
270 static Mangled::ManglingScheme GetManglingScheme(llvm::StringRef name);
271
272 static bool IsMangledName(llvm::StringRef name);
273
274 /// Decode a serialized version of this object from data.
275 ///
276 /// \param data
277 /// The decoder object that references the serialized data.
278 ///
279 /// \param offset_ptr
280 /// A pointer that contains the offset from which the data will be decoded
281 /// from that gets updated as data gets decoded.
282 ///
283 /// \param strtab
284 /// All strings in cache files are put into string tables for efficiency
285 /// and cache file size reduction. Strings are stored as uint32_t string
286 /// table offsets in the cache data.
287 bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
288 const StringTableReader &strtab);
289
290 /// Encode this object into a data encoder object.
291 ///
292 /// This allows this object to be serialized to disk.
293 ///
294 /// \param encoder
295 /// A data encoder object that serialized bytes will be encoded into.
296 ///
297 /// \param strtab
298 /// All strings in cache files are put into string tables for efficiency
299 /// and cache file size reduction. Strings are stored as uint32_t string
300 /// table offsets in the cache data.
301 void Encode(DataEncoder &encoder, ConstStringTable &strtab) const;
302
303 /// Retrieve \c DemangledNameInfo of the demangled name held by this object.
304 const DemangledNameInfo *GetDemangledInfo() const;
305
306 /// Compute the base name (without namespace/class qualifiers) from the
307 /// demangled name.
308 ///
309 /// For a demangled name like "ns::MyClass<int>::templateFunc", this returns
310 /// just "templateFunc".
311 ///
312 /// \return
313 /// A ConstString containing the basename, or nullptr if computation
314 /// fails.
315 ConstString GetBaseName() const;
316
317private:
318 /// If \c force is \c false, this function will re-use the previously
319 /// demangled name (if any). If \c force is \c true (or the mangled name
320 /// on this object was not previously demangled), demangle and cache the
321 /// name.
322 ConstString GetDemangledNameImpl(bool force) const;
323
324 /// The mangled version of the name.
326
327 /// Mutable so we can get it on demand with
328 /// a const version of this object.
330
331 /// If available, holds information about where in \c m_demangled certain
332 /// parts of the name (e.g., basename, arguments, etc.) begin and end.
333 mutable std::unique_ptr<DemangledNameInfo> m_demangled_info;
334};
335
337
338} // namespace lldb_private
339
340#endif // LLDB_CORE_MANGLED_H
Many cache files require string tables to store data efficiently.
A uniqued constant string class.
Definition ConstString.h:40
An binary data encoding class.
Definition DataEncoder.h:42
An data extractor class.
A class that handles mangled names.
Definition Mangled.h:34
void Encode(DataEncoder &encoder, ConstStringTable &strtab) const
Encode this object into a data encoder object.
Definition Mangled.cpp:526
bool NameMatches(ConstString name) const
Check if "name" matches either the mangled or demangled name.
Definition Mangled.h:194
Mangled & operator=(Mangled &&)=default
static int Compare(const Mangled &lhs, const Mangled &rhs)
Compare the mangled string values.
Definition Mangled.cpp:119
static bool IsMangledName(llvm::StringRef name)
Definition Mangled.cpp:39
@ ePreferDemangledWithoutArguments
Definition Mangled.h:39
Mangled()=default
Default constructor.
void SetDemangledName(ConstString name)
Definition Mangled.h:160
Mangled(Mangled &&)=default
void DumpDebug(Stream *s) const
Dump a debug description of this object to a Stream s.
Definition Mangled.cpp:402
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:174
size_t MemorySize() const
Get the memory cost of this object.
Definition Mangled.cpp:413
bool GetRichManglingInfo(RichManglingContext &context, SkipMangledNameFn *skip_mangled_name)
Get rich mangling information.
Definition Mangled.cpp:228
ConstString GetDemangledName() const
Demangled name get accessor.
Definition Mangled.cpp:284
ConstString GetBaseName() const
Compute the base name (without namespace/class qualifiers) from the demangled name.
Definition Mangled.cpp:558
std::unique_ptr< DemangledNameInfo > m_demangled_info
If available, holds information about where in m_demangled certain parts of the name (e....
Definition Mangled.h:333
lldb::LanguageType GuessLanguage() const
Try to guess the language from the mangling.
Definition Mangled.cpp:423
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr, const StringTableReader &strtab)
Decode a serialized version of this object from data.
Definition Mangled.cpp:475
void SetMangledName(ConstString name)
Definition Mangled.h:165
bool(llvm::StringRef, ManglingScheme) SkipMangledNameFn
Function signature for filtering mangled names.
Definition Mangled.h:237
bool operator==(const Mangled &rhs) const
Definition Mangled.h:89
void SetValue(ConstString name)
Set the string value in this object.
Definition Mangled.cpp:124
const DemangledNameInfo * GetDemangledInfo() const
Retrieve DemangledNameInfo of the demangled name held by this object.
Definition Mangled.cpp:288
ConstString GetName(NamePreference preference=ePreferDemangled) const
Best name get accessor.
Definition Mangled.cpp:369
static Mangled::ManglingScheme GetManglingScheme(llvm::StringRef name)
Try to identify the mangling scheme used.
Definition Mangled.cpp:43
bool operator!=(const Mangled &rhs) const
Definition Mangled.h:94
Mangled(const Mangled &other)
Definition Mangled.h:56
ConstString GetDemangledNameImpl(bool force) const
If force is false, this function will re-use the previously demangled name (if any).
Definition Mangled.cpp:298
ConstString m_mangled
The mangled version of the name.
Definition Mangled.h:325
ConstString m_demangled
Mutable so we can get it on demand with a const version of this object.
Definition Mangled.h:329
ConstString GetDisplayDemangledName() const
Display demangled name get accessor.
Definition Mangled.cpp:354
void Dump(Stream *s) const
Dump a description of this object to a Stream s.
Definition Mangled.cpp:392
Mangled & operator=(const Mangled &other)
Definition Mangled.h:63
void Clear()
Clear the mangled and demangled values.
Definition Mangled.cpp:112
Uniform wrapper for access to rich mangling information from different providers.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Many cache files require string tables to store data efficiently.
A class that represents a running process on the host machine.
Stream & operator<<(Stream &s, const Mangled &obj)
uint64_t offset_t
Definition lldb-types.h:85
LanguageType
Programming language type.
Stores information about where certain portions of a demangled function name begin and end.