LLDB mainline
Mangled.h
Go to the documentation of this file.
1//===-- Mangled.h ---------------------------------------------------------===//
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 <optional>
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 /// The number of mangling schemes.
52 static constexpr size_t kNumManglingSchemes = eManglingSchemeSwift + 1;
53
54 /// What a mangled function name says about the function's declaration.
62
63 /// The parts of a mangled function name used in the symbol table's indices.
65 /// If empty, the symbol is only indexed by its mangled name.
67 /// The enclosing context. The symbol table only uses it to work
68 /// out which contexts are classes: a ConstructorOrDestructor
69 /// shows that its context is one, and an Unknown function in such
70 /// a context is a method.
73 };
74
75 /// Default constructor.
76 ///
77 /// Initialize with both mangled and demangled names empty.
78 Mangled() = default;
79
80 /// Construct with name.
81 ///
82 /// Constructor with an optional string and auto-detect if \a name is
83 /// mangled or not.
84 ///
85 /// \param[in] name
86 /// The already const name to copy into this object.
87 explicit Mangled(ConstString name);
88
89 explicit Mangled(llvm::StringRef name);
90
91 bool operator==(const Mangled &rhs) const {
92 return m_mangled == rhs.m_mangled &&
94 }
95
96 bool operator!=(const Mangled &rhs) const {
97 return !(*this == rhs);
98 }
99
100 /// Convert to bool operator.
101 ///
102 /// This allows code to check any Mangled objects to see if they contain
103 /// anything valid using code such as:
104 ///
105 /// \code
106 /// Mangled mangled(...);
107 /// if (mangled)
108 /// { ...
109 /// \endcode
110 ///
111 /// \return
112 /// Returns \b true if either the mangled or unmangled name is set,
113 /// \b false if the object has an empty mangled and unmangled name.
114 explicit operator bool() const;
115
116 /// Clear the mangled and demangled values.
117 void Clear();
118
119 /// Compare the mangled string values
120 ///
121 /// Compares the Mangled::GetName() string in \a lhs and \a rhs.
122 ///
123 /// \param[in] lhs
124 /// A const reference to the Left Hand Side object to compare.
125 ///
126 /// \param[in] rhs
127 /// A const reference to the Right Hand Side object to compare.
128 ///
129 /// \return
130 /// -1 if \a lhs is less than \a rhs
131 /// 0 if \a lhs is equal to \a rhs
132 /// 1 if \a lhs is greater than \a rhs
133 static int Compare(const Mangled &lhs, const Mangled &rhs);
134
135 /// Dump a description of this object to a Stream \a s.
136 ///
137 /// Dump a Mangled object to stream \a s. We don't force our demangled name
138 /// to be computed currently (we don't use the accessor).
139 ///
140 /// \param[in] s
141 /// The stream to which to dump the object description.
142 void Dump(Stream *s) const;
143
144 /// Dump a debug description of this object to a Stream \a s.
145 ///
146 /// \param[in] s
147 /// The stream to which to dump the object description.
148 void DumpDebug(Stream *s) const;
149
150 /// Demangled name get accessor.
151 ///
152 /// \return
153 /// A const reference to the demangled name string object.
155
156 /// Display demangled name get accessor.
157 ///
158 /// \return
159 /// A const reference to the display demangled name string object.
161
163
164 void SetMangledName(ConstString name) { m_mangled = name; }
165
166 /// Mangled name get accessor.
167 ///
168 /// \return
169 /// The mangled name string object.
171
172 /// Best name get accessor.
173 ///
174 /// \param[in] preference
175 /// Which name would you prefer to get?
176 ///
177 /// \return
178 /// A const reference to the preferred name string object if this
179 /// object has a valid name of that kind, else a const reference to the
180 /// other name is returned.
181 ConstString GetName(NamePreference preference = ePreferDemangled) const;
182
183 /// Check if "name" matches either the mangled or demangled name.
184 ///
185 /// \param[in] name
186 /// A name to match against both strings.
187 ///
188 /// \return
189 /// \b True if \a name matches either name, \b false otherwise.
190 bool NameMatches(ConstString name) const {
191 if (m_mangled == name)
192 return true;
193 return GetDemangledName() == name;
194 }
195 bool NameMatches(const RegularExpression &regex) const;
196
197 /// Set the string value in this object.
198 ///
199 /// This version auto detects if the string is mangled by inspecting the
200 /// string value and looking for common mangling prefixes.
201 ///
202 /// \param[in] name
203 /// The already const version of the name for this object.
204 void SetValue(ConstString name);
205
206 /// Try to guess the language from the mangling.
207 ///
208 /// For a mangled name to have a language it must have both a mangled and a
209 /// demangled name and it can be guessed from the mangling what the language
210 /// is. Note: this will return C++ for any language that uses Itanium ABI
211 /// mangling.
212 ///
213 /// Standard C function names will return eLanguageTypeUnknown because they
214 /// aren't mangled and it isn't clear what language the name represents
215 /// (there will be no mangled name).
216 ///
217 /// \return
218 /// The language for the mangled/demangled name, eLanguageTypeUnknown
219 /// if there is no mangled or demangled counterpart.
221
222 /// Try to identify the mangling scheme used.
223 /// \param[in] name
224 /// The name we are attempting to identify the mangling scheme for.
225 ///
226 /// \return
227 /// eManglingSchemeNone if no known mangling scheme could be identified
228 /// for s, otherwise the enumerator for the mangling scheme detected.
229 static Mangled::ManglingScheme GetManglingScheme(llvm::StringRef name);
230
231 static bool IsMangledName(llvm::StringRef name);
232
233 /// Decode a serialized version of this object from data.
234 ///
235 /// \param data
236 /// The decoder object that references the serialized data.
237 ///
238 /// \param offset_ptr
239 /// A pointer that contains the offset from which the data will be decoded
240 /// from that gets updated as data gets decoded.
241 ///
242 /// \param strtab
243 /// All strings in cache files are put into string tables for efficiency
244 /// and cache file size reduction. Strings are stored as uint32_t string
245 /// table offsets in the cache data.
246 bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
247 const StringTableReader &strtab);
248
249 /// Encode this object into a data encoder object.
250 ///
251 /// This allows this object to be serialized to disk.
252 ///
253 /// \param encoder
254 /// A data encoder object that serialized bytes will be encoded into.
255 ///
256 /// \param strtab
257 /// All strings in cache files are put into string tables for efficiency
258 /// and cache file size reduction. Strings are stored as uint32_t string
259 /// table offsets in the cache data.
260 void Encode(DataEncoder &encoder, ConstStringTable &strtab) const;
261
262 /// Compute the \c DemangledNameInfo of the demangled name.
263 ///
264 /// Note that this always re-runs the demangler. \see DemangledNameInfoCache
265 /// for caching the result.
266 ///
267 /// \return
268 /// std::nullopt if no info could be computed (for example because the
269 /// name is mangled with a scheme that doesn't provide any info).
270 std::optional<DemangledNameInfo> ComputeDemangledInfo() const;
271
272 /// Compute the base name (without namespace/class qualifiers) from the
273 /// demangled name.
274 ///
275 /// For a demangled name like "ns::MyClass<int>::templateFunc", this returns
276 /// just "templateFunc".
277 ///
278 /// \return
279 /// A ConstString containing the basename, or nullptr if computation
280 /// fails.
281 ConstString GetBaseName() const;
282
283private:
284 /// The mangled version of the name.
286
287 /// Mutable so we can get it on demand with
288 /// a const version of this object.
290};
291static_assert(sizeof(Mangled) <= 2 * sizeof(ConstString),
292 "High-volume object, size of object must be increased with care");
293
295
296} // namespace lldb_private
297
298#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:406
bool NameMatches(ConstString name) const
Check if "name" matches either the mangled or demangled name.
Definition Mangled.h:190
static int Compare(const Mangled &lhs, const Mangled &rhs)
Compare the mangled string values.
Definition Mangled.cpp:118
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:162
void DumpDebug(Stream *s) const
Dump a debug description of this object to a Stream s.
Definition Mangled.cpp:290
std::optional< DemangledNameInfo > ComputeDemangledInfo() const
Compute the DemangledNameInfo of the demangled name.
Definition Mangled.cpp:226
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:170
FunctionNameKind
What a mangled function name says about the function's declaration.
Definition Mangled.h:55
ConstString GetDemangledName() const
Demangled name get accessor.
Definition Mangled.cpp:171
ConstString GetBaseName() const
Compute the base name (without namespace/class qualifiers) from the demangled name.
Definition Mangled.cpp:438
lldb::LanguageType GuessLanguage() const
Try to guess the language from the mangling.
Definition Mangled.cpp:304
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:356
void SetMangledName(ConstString name)
Definition Mangled.h:164
bool operator==(const Mangled &rhs) const
Definition Mangled.h:91
void SetValue(ConstString name)
Set the string value in this object.
Definition Mangled.cpp:123
ConstString GetName(NamePreference preference=ePreferDemangled) const
Best name get accessor.
Definition Mangled.cpp:257
static constexpr size_t kNumManglingSchemes
The number of mangling schemes.
Definition Mangled.h:52
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:96
ConstString m_mangled
The mangled version of the name.
Definition Mangled.h:285
ConstString m_demangled
Mutable so we can get it on demand with a const version of this object.
Definition Mangled.h:289
ConstString GetDisplayDemangledName() const
Display demangled name get accessor.
Definition Mangled.cpp:242
void Dump(Stream *s) const
Dump a description of this object to a Stream s.
Definition Mangled.cpp:280
void Clear()
Clear the mangled and demangled values.
Definition Mangled.cpp:112
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:86
LanguageType
Programming language type.
The parts of a mangled function name used in the symbol table's indices.
Definition Mangled.h:64
ConstString basename
If empty, the symbol is only indexed by its mangled name.
Definition Mangled.h:66
ConstString decl_context
The enclosing context.
Definition Mangled.h:71