Skip to content

Commit b8997c0

Browse files
[Demangling] Refactor Demangler range tracking (#140762)
This PR is a subset of the commits made in swiftlang#10710. The most notable change is the addition of `PrefixRange` and `SuffixRange` which are a catch-all to track anything after or before a function's demangled name. In the case of Swift, this allows to add support for name highlighting without having to track the range of the scope and specifiers of a function (this will come in another PR).
1 parent 9aebf4c commit b8997c0

File tree

7 files changed

+47
-4
lines changed

7 files changed

+47
-4
lines changed

lldb/docs/use/formatting.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ A complete list of currently supported format string variables is listed below:
9191
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
9292
| ``function.basename`` | The basename of the current function depending on the frame's language. E.g., for C++ the basename for ``void ns::foo<float>::bar<int>(int) const`` is ``bar``. |
9393
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
94-
| ``function.scope`` | The scope qualifiers of the current function depending on the frame's language. E.g., for C++ the scope for ``void ns::foo<float>::bar<int>(int) const`` is ``ns::foo<float>``. |
94+
| ``function.prefix`` | Any prefix added to the demangled function name of the current function. This depends on the frame's language. E.g., for C++ the prefix will always be empty. |
95+
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
96+
| ``function.scope`` | The scope qualifiers of the current function depending on the frame's language. E.g., for C++ the scope for ``void ns::foo<float>::bar<int>(int) const`` is ``ns::foo<float>``. |
9597
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
9698
| ``function.template-arguments`` | The template arguments of the current function depending on the frame's language. E.g., for C++ the template arguments for ``void ns::foo<float>::bar<int>(int) const`` are ``<float>``. |
9799
+---------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
@@ -325,6 +327,7 @@ _____________________
325327
The function names displayed in backtraces/``frame info``/``thread info`` are the demangled names of functions. On some platforms (like ones using Itanium the mangling scheme), LLDB supports decomposing these names into fine-grained components. These are currently:
326328

327329
- ``${function.return-left}``
330+
- ``${function.prefix}``
328331
- ``${function.scope}``
329332
- ``${function.basename}``
330333
- ``${function.template-arguments}``

lldb/include/lldb/Core/DemangledNameInfo.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct DemangledNameInfo {
3939
/// \endcode
4040
std::pair<size_t, size_t> ScopeRange;
4141

42-
/// Indicates the [start, end) of the function argument lits.
42+
/// Indicates the [start, end) of the function argument list.
4343
/// E.g.,
4444
/// \code{.cpp}
4545
/// int (*getFunc<float>(float, double))(int, int)
@@ -59,6 +59,16 @@ struct DemangledNameInfo {
5959
/// \endcode
6060
std::pair<size_t, size_t> QualifiersRange;
6161

62+
/// Indicates the [start, end) of the function's prefix. This is a
63+
/// catch-all range for anything that is not tracked by the rest of
64+
/// the pairs.
65+
std::pair<size_t, size_t> PrefixRange;
66+
67+
/// Indicates the [start, end) of the function's suffix. This is a
68+
/// catch-all range for anything that is not tracked by the rest of
69+
/// the pairs.
70+
std::pair<size_t, size_t> SuffixRange;
71+
6272
/// Returns \c true if this object holds a valid basename range.
6373
bool hasBasename() const {
6474
return BasenameRange.second > BasenameRange.first &&

lldb/include/lldb/Core/FormatEntity.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct Entry {
8888
FunctionNameWithArgs,
8989
FunctionNameNoArgs,
9090
FunctionMangledName,
91+
FunctionPrefix,
9192
FunctionScope,
9293
FunctionBasename,
9394
FunctionTemplateArguments,

lldb/source/Core/FormatEntity.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ constexpr Definition g_function_child_entries[] = {
124124
Definition("initial-function", EntryType::FunctionInitial),
125125
Definition("changed", EntryType::FunctionChanged),
126126
Definition("is-optimized", EntryType::FunctionIsOptimized),
127+
Definition("prefix", EntryType::FunctionPrefix),
127128
Definition("scope", EntryType::FunctionScope),
128129
Definition("basename", EntryType::FunctionBasename),
129130
Definition("template-arguments", EntryType::FunctionTemplateArguments),
@@ -385,6 +386,7 @@ const char *FormatEntity::Entry::TypeToCString(Type t) {
385386
ENUM_TO_CSTR(FunctionNameWithArgs);
386387
ENUM_TO_CSTR(FunctionNameNoArgs);
387388
ENUM_TO_CSTR(FunctionMangledName);
389+
ENUM_TO_CSTR(FunctionPrefix);
388390
ENUM_TO_CSTR(FunctionScope);
389391
ENUM_TO_CSTR(FunctionBasename);
390392
ENUM_TO_CSTR(FunctionTemplateArguments);
@@ -1835,6 +1837,7 @@ bool FormatEntity::Format(const Entry &entry, Stream &s,
18351837
return true;
18361838
}
18371839

1840+
case Entry::Type::FunctionPrefix:
18381841
case Entry::Type::FunctionScope:
18391842
case Entry::Type::FunctionBasename:
18401843
case Entry::Type::FunctionTemplateArguments:

lldb/source/Core/Mangled.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ GetItaniumDemangledStr(const char *M) {
172172

173173
TrackingOutputBuffer OB(demangled_cstr, demangled_size);
174174
demangled_cstr = ipd.finishDemangle(&OB);
175+
OB.NameInfo.SuffixRange.first = OB.NameInfo.QualifiersRange.second;
176+
OB.NameInfo.SuffixRange.second = std::string_view(OB).size();
175177
info = std::move(OB.NameInfo);
176178

177179
assert(demangled_cstr &&

lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ GetDemangledFunctionSuffix(const SymbolContext &sc) {
401401
if (!info->hasBasename())
402402
return std::nullopt;
403403

404-
return demangled_name.slice(info->QualifiersRange.second,
405-
llvm::StringRef::npos);
404+
return demangled_name.slice(info->SuffixRange.first,
405+
info->SuffixRange.second);
406406
}
407407

408408
static bool PrintDemangledArgumentList(Stream &s, const SymbolContext &sc) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Check that we have an appropriate fallback for ${function.prefix} in languages that
2+
# don't implement this frame format variable (in this case Objective-C).
3+
#
4+
# RUN: split-file %s %t
5+
# RUN: %clang_host -g -gdwarf %t/main.m -o %t.objc.out
6+
# RUN: %lldb -x -b -s %t/commands.input %t.objc.out -o exit 2>&1 \
7+
# RUN: | FileCheck %s
8+
9+
#--- main.m
10+
11+
int func() {}
12+
int bar() { func(); }
13+
14+
int main() { return bar(); }
15+
16+
#--- commands.input
17+
settings set -f frame-format "custom-frame '${function.prefix}'\n"
18+
break set -n bar
19+
20+
run
21+
bt
22+
23+
# CHECK: bt
24+
# CHECK-NOT: custom-frame

0 commit comments

Comments
 (0)