|
39 | 39 | #include "swift/AST/ASTMangler.h"
|
40 | 40 | #include "swift/AST/Decl.h"
|
41 | 41 | #include "swift/AST/Module.h"
|
| 42 | +#include "swift/Demangling/Demangle.h" |
42 | 43 | #include "swift/Reflection/ReflectionContext.h"
|
43 | 44 | #include "swift/RemoteAST/RemoteAST.h"
|
44 | 45 |
|
@@ -1945,19 +1946,6 @@ SwiftLanguageRuntime::CreateExceptionResolver(const lldb::BreakpointSP &bkpt, bo
|
1945 | 1946 | return ::CreateExceptionResolver(bkpt, catch_bp, throw_bp);
|
1946 | 1947 | }
|
1947 | 1948 |
|
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 |
| - |
1961 | 1949 | static OptionDefinition g_swift_demangle_options[] = {
|
1962 | 1950 | // clang-format off
|
1963 | 1951 | {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 {
|
2016 | 2004 | };
|
2017 | 2005 |
|
2018 | 2006 | 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 |
| - |
2044 | 2007 | bool DoExecute(Args &command, CommandReturnObject &result) override {
|
2045 | 2008 | 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()) { |
2048 | 2011 | 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 | + } |
2050 | 2021 | 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)); |
2054 | 2024 | result.GetOutputStream().Printf(
|
2055 |
| - "%s ---> %s\n", arg, |
| 2025 | + "%s ---> %s\n", name.data(), |
2056 | 2026 | swift::Demangle::nodeToString(node_ptr).c_str());
|
2057 | 2027 | }
|
2058 | 2028 | }
|
|
0 commit comments