Skip to content

Commit fc8389e

Browse files
[lldb] add support TemplateArguments and NameQualifiers to Swift format Entities
1 parent 3c0ab62 commit fc8389e

File tree

7 files changed

+512
-420
lines changed

7 files changed

+512
-420
lines changed

lldb/source/Core/Mangled.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,21 @@ GetSwiftDemangledStr(ConstString m_mangled, const SymbolContext *sc,
168168
}
169169
auto [demangled, info] = SwiftLanguageRuntime::TrackedDemangleSymbolAsString(
170170
mangled_name, demangle_mode, sc);
171+
// TODO: The logic below should be in the NodePrinter.
171172
info.PrefixRange.second =
172173
std::min(info.BasenameRange.first, info.ArgumentsRange.first);
173174
info.SuffixRange.first =
174175
std::max(info.BasenameRange.second, info.ArgumentsRange.second);
175176
info.SuffixRange.second = demangled.length();
177+
if (info.hasBasename() && info.hasArguments()) {
178+
if (info.hasTemplateArguments() && info.TemplateArgumentsRange.first > 0) {
179+
info.NameQualifiersRange.second = std::min(
180+
info.ArgumentsRange.first, info.TemplateArgumentsRange.first);
181+
} else {
182+
info.NameQualifiersRange.second = info.ArgumentsRange.first;
183+
}
184+
info.NameQualifiersRange.first = info.BasenameRange.second;
185+
}
176186

177187
// Don't cache the demangled name if the function isn't available yet.
178188
// Only cache eFullName demangled functions to keep the cache consistent.

lldb/source/Plugins/Language/Swift/LanguageSwiftProperties.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ include "../../../../include/lldb/Core/PropertiesBase.td"
33
let Definition = "language_swift" in {
44
def FunctionNameFormat: Property<"function-name-format", "FormatEntity">,
55
Global,
6-
DefaultStringValue<"${function.prefix}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.formatted-arguments}${function.suffix}">,
6+
DefaultStringValue<"${function.prefix}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.name-qualifiers}${function.template-arguments}${function.formatted-arguments}${function.suffix}">,
77
Desc<"Swift specific frame format string to use when displaying stack frame information for threads.">;
88
}

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 84 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,13 +1722,7 @@ bool SwiftLanguage::GetFunctionDisplayName(
17221722
// No need to customize this.
17231723
return false;
17241724
case Language::FunctionNameRepresentation::eNameWithNoArgs: {
1725-
if (!sc.function)
1726-
return false;
1727-
if (sc.function->GetLanguage() != eLanguageTypeSwift)
1728-
return false;
1729-
std::string display_name = SwiftLanguageRuntime::DemangleSymbolAsString(
1730-
sc.function->GetMangled().GetMangledName().GetStringRef(),
1731-
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
1725+
std::string display_name = GetDemangledFunctionName(sc, exe_ctx);
17321726
if (display_name.empty())
17331727
return false;
17341728
s << display_name;
@@ -1763,69 +1757,82 @@ bool SwiftLanguage::GetFunctionDisplayName(
17631757
variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
17641758
args);
17651759

1760+
s << GetFunctionTemplateArguments(sc, exe_ctx);
17661761
s << GetFunctionDisplayArgs(sc, args, exe_ctx);
17671762
return true;
17681763
}
17691764
}
17701765
return false;
17711766
}
17721767

1773-
std::string SwiftLanguage::GetFunctionName(const SymbolContext &sc,
1774-
const ExecutionContext *exe_ctx) {
1768+
std::string
1769+
SwiftLanguage::GetDemangledFunctionName(const SymbolContext &sc,
1770+
const ExecutionContext *exe_ctx) {
17751771
if (!sc.function)
17761772
return {};
17771773
if (sc.function->GetLanguage() != eLanguageTypeSwift)
17781774
return {};
1779-
std::string name = SwiftLanguageRuntime::DemangleSymbolAsString(
1775+
return SwiftLanguageRuntime::DemangleSymbolAsString(
17801776
sc.GetPossiblyInlinedFunctionName().GetMangledName(),
17811777
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
1782-
if (name.empty())
1778+
}
1779+
1780+
std::string SwiftLanguage::GetFunctionName(const SymbolContext &sc,
1781+
const ExecutionContext *exe_ctx) {
1782+
std::string demangled_name = GetDemangledFunctionName(sc, exe_ctx);
1783+
if (demangled_name.empty())
17831784
return {};
1784-
size_t open_paren = name.find('(');
1785-
size_t generic = name.find('<');
1785+
size_t open_paren = demangled_name.find('(');
1786+
size_t generic = demangled_name.find('<');
17861787
size_t name_end = std::min(open_paren, generic);
17871788
if (name_end == std::string::npos)
1788-
return name;
1789-
return name.substr(0, name_end);
1789+
return demangled_name;
1790+
return demangled_name.substr(0, name_end);
1791+
}
1792+
1793+
std::string
1794+
SwiftLanguage::GetFunctionTemplateArguments(const SymbolContext &sc,
1795+
const ExecutionContext *exe_ctx) {
1796+
std::string demangled_name = GetDemangledFunctionName(sc, exe_ctx);
1797+
if (demangled_name.empty())
1798+
return {};
1799+
size_t open_paren = demangled_name.find('(');
1800+
size_t generic_start = demangled_name.find('<');
1801+
if (generic_start == std::string::npos || generic_start > open_paren)
1802+
return {};
1803+
1804+
int generic_depth = 1;
1805+
size_t generic_end = generic_start + 1;
1806+
1807+
while (generic_end < demangled_name.size() && generic_depth > 0) {
1808+
if (demangled_name[generic_end] == '<') {
1809+
generic_depth++;
1810+
} else if (demangled_name[generic_end] == '>') {
1811+
generic_depth--;
1812+
}
1813+
generic_end++;
1814+
}
1815+
1816+
if (generic_depth != 0)
1817+
return {};
1818+
1819+
return demangled_name.substr(generic_start, generic_end - generic_start);
17901820
}
17911821

17921822
std::string SwiftLanguage::GetFunctionDisplayArgs(
17931823
const SymbolContext &sc, VariableList &args,
17941824
const lldb_private::ExecutionContext *exe_ctx) {
17951825
ExecutionContextScope *exe_scope =
17961826
exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
1797-
std::string name = SwiftLanguageRuntime::DemangleSymbolAsString(
1798-
sc.function->GetMangled().GetMangledName().GetStringRef(),
1799-
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
1827+
std::string name = GetDemangledFunctionName(sc, exe_ctx);
18001828
lldb_private::StreamString s;
18011829
const char *cstr = name.data();
18021830
const char *open_paren = strchr(cstr, '(');
18031831
const char *close_paren = nullptr;
1804-
const char *generic = strchr(cstr, '<');
1805-
// If before the arguments list begins there is a template sign
1806-
// then scan to the end of the generic args before you try to find
1807-
// the arguments list.
1808-
const char *generic_start = generic;
1809-
if (generic && open_paren && generic < open_paren) {
1810-
int generic_depth = 1;
1811-
++generic;
1812-
for (; *generic && generic_depth > 0; generic++) {
1813-
if (*generic == '<')
1814-
generic_depth++;
1815-
if (*generic == '>')
1816-
generic_depth--;
1817-
}
1818-
if (*generic)
1819-
open_paren = strchr(generic, '(');
1820-
else
1821-
open_paren = nullptr;
1822-
}
18231832
if (open_paren) {
18241833
close_paren = strchr(open_paren, ')');
18251834
}
18261835

1827-
if (generic_start && generic_start < open_paren)
1828-
s.Write(generic_start, open_paren - generic_start);
18291836
s.PutChar('(');
18301837

18311838
const size_t num_args = args.GetSize();
@@ -1949,6 +1956,23 @@ GetDemangledBasename(const SymbolContext &sc) {
19491956
info.BasenameRange.second);
19501957
}
19511958

1959+
static llvm::Expected<llvm::StringRef>
1960+
GetDemangledNameQualifiers(const SymbolContext &sc) {
1961+
auto info_or_err = GetAndValidateInfo(sc);
1962+
if (!info_or_err)
1963+
return info_or_err.takeError();
1964+
1965+
auto [demangled_name, info] = *info_or_err;
1966+
1967+
if (!info.hasPrefix())
1968+
return llvm::createStringError(
1969+
"DemangledInfo for '%s does not have a name qualifiers range.",
1970+
demangled_name.data());
1971+
1972+
return demangled_name.slice(info.NameQualifiersRange.first,
1973+
info.NameQualifiersRange.second);
1974+
}
1975+
19521976
static llvm::Expected<llvm::StringRef>
19531977
GetDemangledFunctionPrefix(const SymbolContext &sc) {
19541978
auto info_or_err = GetAndValidateInfo(sc);
@@ -1959,7 +1983,7 @@ GetDemangledFunctionPrefix(const SymbolContext &sc) {
19591983

19601984
if (!info.hasPrefix())
19611985
return llvm::createStringError(
1962-
"DemangledInfo for '%s does not have suffix range.",
1986+
"DemangledInfo for '%s does not have a prefix range.",
19631987
demangled_name.data());
19641988

19651989
return demangled_name.slice(info.PrefixRange.first, info.PrefixRange.second);
@@ -1975,7 +1999,7 @@ GetDemangledFunctionSuffix(const SymbolContext &sc) {
19751999

19762000
if (!info.hasSuffix())
19772001
return llvm::createStringError(
1978-
"DemangledInfo for '%s does not have suffix range.",
2002+
"DemangledInfo for '%s does not have a suffix range.",
19792003
demangled_name.data());
19802004

19812005
return demangled_name.slice(info.SuffixRange.first, info.SuffixRange.second);
@@ -2030,6 +2054,24 @@ bool SwiftLanguage::HandleFrameFormatVariable(const SymbolContext &sc,
20302054

20312055
return true;
20322056
}
2057+
case FormatEntity::Entry::Type::FunctionNameQualifiers: {
2058+
auto qualifiers_or_err = GetDemangledNameQualifiers(sc);
2059+
if (!qualifiers_or_err) {
2060+
LLDB_LOG_ERROR(GetLog(LLDBLog::Language), qualifiers_or_err.takeError(),
2061+
"Failed to handle ${{function.name-qualifiers}} "
2062+
"frame-format variable: {0}");
2063+
return false;
2064+
}
2065+
2066+
s << *qualifiers_or_err;
2067+
2068+
return true;
2069+
}
2070+
case FormatEntity::Entry::Type::FunctionTemplateArguments: {
2071+
s << GetFunctionTemplateArguments(sc, exe_ctx);
2072+
2073+
return true;
2074+
}
20332075
case FormatEntity::Entry::Type::FunctionFormattedArguments: {
20342076
// This ensures we print the arguments even when no debug-info is available.
20352077
//
@@ -2038,9 +2080,7 @@ bool SwiftLanguage::HandleFrameFormatVariable(const SymbolContext &sc,
20382080
// once we have a "fallback operator" in the frame-format language.
20392081
if (!sc.function && sc.symbol)
20402082
return PrintDemangledArgumentList(s, sc);
2041-
std::string display_name = SwiftLanguageRuntime::DemangleSymbolAsString(
2042-
sc.function->GetMangled().GetMangledName().GetStringRef(),
2043-
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
2083+
std::string display_name = GetDemangledFunctionName(sc, exe_ctx);
20442084
if (display_name.empty())
20452085
return false;
20462086

@@ -2080,7 +2120,6 @@ bool SwiftLanguage::HandleFrameFormatVariable(const SymbolContext &sc,
20802120
}
20812121

20822122
case FormatEntity::Entry::Type::FunctionScope:
2083-
case FormatEntity::Entry::Type::FunctionTemplateArguments:
20842123
case FormatEntity::Entry::Type::FunctionReturnRight:
20852124
case FormatEntity::Entry::Type::FunctionReturnLeft:
20862125
case FormatEntity::Entry::Type::FunctionQualifiers:

lldb/source/Plugins/Language/Swift/SwiftLanguage.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class SwiftLanguage : public Language {
6969
FunctionNameRepresentation representation,
7070
Stream &s) override;
7171

72+
std::string GetDemangledFunctionName(const SymbolContext &sc,
73+
const ExecutionContext *exe_ctx);
74+
7275
/// Returns the name of function up to the first generic or opening
7376
/// parenthesis.
7477
///
@@ -83,6 +86,9 @@ class SwiftLanguage : public Language {
8386
std::string GetFunctionName(const SymbolContext &sc,
8487
const ExecutionContext *exe_ctx);
8588

89+
std::string GetFunctionTemplateArguments(const SymbolContext &sc,
90+
const ExecutionContext *exe_ctx);
91+
8692
/// Returns the arguments of a function call with its generics if any.
8793
///
8894
/// Calling GetFunctionDisplayArgs on the following function call will return

lldb/source/Plugins/Language/Swift/SwiftMangled.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class TrackingNodePrinter : public NodePrinter {
3232
private:
3333
lldb_private::DemangledNameInfo info;
3434
std::optional<unsigned> parametersDepth;
35+
std::optional<unsigned> genericsSignatureDepth;
3536

3637
void startName() {
3738
if (!info.hasBasename())
@@ -43,6 +44,25 @@ class TrackingNodePrinter : public NodePrinter {
4344
info.BasenameRange.second = getStreamLength();
4445
}
4546

47+
void startGenericSignature(unsigned depth) {
48+
if (genericsSignatureDepth || !info.hasBasename() ||
49+
info.TemplateArgumentsRange.first <
50+
info.TemplateArgumentsRange.second) {
51+
return;
52+
}
53+
info.TemplateArgumentsRange.first = getStreamLength();
54+
genericsSignatureDepth = depth;
55+
}
56+
57+
void endGenericSignature(unsigned depth) {
58+
if (!genericsSignatureDepth || *genericsSignatureDepth != depth ||
59+
info.TemplateArgumentsRange.first <
60+
info.TemplateArgumentsRange.second) {
61+
return;
62+
}
63+
info.TemplateArgumentsRange.second = getStreamLength();
64+
}
65+
4666
void startParameters(unsigned depth) {
4767
if (parametersDepth || !info.hasBasename() ||
4868
info.ArgumentsRange.first < info.ArgumentsRange.second) {
@@ -85,6 +105,12 @@ class TrackingNodePrinter : public NodePrinter {
85105
endName();
86106
}
87107

108+
void printGenericSignature(NodePointer Node, unsigned depth) override {
109+
startGenericSignature(depth);
110+
NodePrinter::printGenericSignature(Node, depth);
111+
endGenericSignature(depth);
112+
}
113+
88114
void printFunctionParameters(NodePointer LabelList, NodePointer ParameterType,
89115
unsigned depth, bool showTypes) override {
90116
startParameters(depth);

lldb/test/API/lang/swift/async/unwind/hidden_frames/TestSwiftAsyncHiddenFrames.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import lldbsuite.test.lldbutil as lldbutil
55

66

7-
@skipIf(bugnumber="rdar://156178892")
87
class TestSwiftAsyncHiddenFrames(lldbtest.TestBase):
98

109
NO_DEBUG_INFO_TESTCASE = True

0 commit comments

Comments
 (0)