Skip to content

Commit 477ce84

Browse files
committed
[lldb] Make leading $-prefix optional in swift demangle (#4054)
Change `language swift demangle` to accept swift symbols that don't have a leading `$`. This matches the behavior of the command line `swift-demangle` tool (see [swift-demangle.cpp#L394-L396](https://github.com/apple/swift/blob/09cdd36c0ad480e68575db54e4b16fc47add46dc/tools/swift-demangle/swift-demangle.cpp#L394-L396)). This makes it more convenient to copy and paste symbols, because a double click on text doesn't select the `$`. While updating this command, I also replaced lldb's custom node tree printing function with Swift's `getNodeTreeAsString`. (cherry picked from commit 4facf96)
1 parent 7e84832 commit 477ce84

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "swift/AST/ASTMangler.h"
4040
#include "swift/AST/Decl.h"
4141
#include "swift/AST/Module.h"
42+
#include "swift/Demangling/Demangle.h"
4243
#include "swift/Reflection/ReflectionContext.h"
4344
#include "swift/RemoteAST/RemoteAST.h"
4445

@@ -1945,19 +1946,6 @@ SwiftLanguageRuntime::CreateExceptionResolver(const lldb::BreakpointSP &bkpt, bo
19451946
return ::CreateExceptionResolver(bkpt, catch_bp, throw_bp);
19461947
}
19471948

1948-
static const char *
1949-
SwiftDemangleNodeKindToCString(const swift::Demangle::Node::Kind node_kind) {
1950-
#define NODE(e) \
1951-
case swift::Demangle::Node::Kind::e: \
1952-
return #e;
1953-
1954-
switch (node_kind) {
1955-
#include "swift/Demangling/DemangleNodes.def"
1956-
}
1957-
return "swift::Demangle::Node::Kind::???";
1958-
#undef NODE
1959-
}
1960-
19611949
static OptionDefinition g_swift_demangle_options[] = {
19621950
// clang-format off
19631951
{LLDB_OPT_SET_1, false, "expand", 'e', OptionParser::eNoArgument, nullptr, {}, 0, eArgTypeNone, "Whether LLDB should print the demangled tree"},
@@ -2016,43 +2004,25 @@ class CommandObjectSwift_Demangle : public CommandObjectParsed {
20162004
};
20172005

20182006
protected:
2019-
void PrintNode(swift::Demangle::NodePointer node_ptr, Stream &stream,
2020-
int depth = 0) {
2021-
if (!node_ptr)
2022-
return;
2023-
2024-
std::string indent(2 * depth, ' ');
2025-
2026-
stream.Printf("%s", indent.c_str());
2027-
2028-
stream.Printf("kind=%s",
2029-
SwiftDemangleNodeKindToCString(node_ptr->getKind()));
2030-
if (node_ptr->hasText()) {
2031-
std::string Text = node_ptr->getText().str();
2032-
stream.Printf(", text=\"%s\"", Text.c_str());
2033-
}
2034-
if (node_ptr->hasIndex())
2035-
stream.Printf(", index=%" PRIu64, node_ptr->getIndex());
2036-
2037-
stream.Printf("\n");
2038-
2039-
for (auto &&child : *node_ptr) {
2040-
PrintNode(child, stream, depth + 1);
2041-
}
2042-
}
2043-
20442007
bool DoExecute(Args &command, CommandReturnObject &result) override {
20452008
for (size_t i = 0; i < command.GetArgumentCount(); i++) {
2046-
const char *arg = command.GetArgumentAtIndex(i);
2047-
if (arg && *arg) {
2009+
StringRef name = command.GetArgumentAtIndex(i);
2010+
if (!name.empty()) {
20482011
swift::Demangle::Context demangle_ctx;
2049-
auto node_ptr = demangle_ctx.demangleSymbolAsNode(llvm::StringRef(arg));
2012+
NodePointer node_ptr = nullptr;
2013+
// Match the behavior of swift-demangle and accept Swift symbols without
2014+
// the leading `$`. This makes symbol copy & paste more convenient.
2015+
if (name.startswith("S") || name.startswith("s")) {
2016+
std::string correctedName = std::string("$") + name.str();
2017+
node_ptr = demangle_ctx.demangleSymbolAsNode(correctedName);
2018+
} else {
2019+
node_ptr = demangle_ctx.demangleSymbolAsNode(name);
2020+
}
20502021
if (node_ptr) {
2051-
if (m_options.m_expand) {
2052-
PrintNode(node_ptr, result.GetOutputStream());
2053-
}
2022+
if (m_options.m_expand)
2023+
result.GetOutputStream().PutCString(getNodeTreeAsString(node_ptr));
20542024
result.GetOutputStream().Printf(
2055-
"%s ---> %s\n", arg,
2025+
"%s ---> %s\n", name.data(),
20562026
swift::Demangle::nodeToString(node_ptr).c_str());
20572027
}
20582028
}

0 commit comments

Comments
 (0)