Skip to content

[Syntax Highlighting] Add name and parameters syntax highlighting in Swift backtraces #10710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: swift/release/6.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lldb/include/lldb/Core/DemangledNameInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLDB_CORE_DEMANGLEDNAMEINFO_H
#define LLDB_CORE_DEMANGLEDNAMEINFO_H

#include "swift/Demangling/Demangle.h"
#include "llvm/Demangle/ItaniumDemangle.h"
#include "llvm/Demangle/Utility.h"

Expand Down Expand Up @@ -73,6 +74,11 @@ struct DemangledNameInfo {
bool hasBasename() const {
return BasenameRange.second > BasenameRange.first;
}

/// Returns \c true if this object holds a valid arguments range.
bool hasArguments() const {
return ArgumentsRange.second > ArgumentsRange.first;
}
};

/// An OutputBuffer which keeps a record of where certain parts of a
Expand Down
69 changes: 69 additions & 0 deletions lldb/include/lldb/Core/Mangled.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,75 @@ class Mangled {

Stream &operator<<(Stream &s, const Mangled &obj);

using namespace swift::Demangle;
class TrackingNodePrinter : public NodePrinter {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a doxygen comment explaining the purpose of this?
It's a layering violation to have a Swift-specific declaration inside of Core/Mangled.h
This declaration should probably live somewhere under Plugins/Language(Runtime)/Swift/

public:
TrackingNodePrinter(DemangleOptions options) : NodePrinter(options) {}

DemangledNameInfo takeInfo() { return std::move(info); }

private:
DemangledNameInfo info;
std::optional<unsigned> parametersDepth;

void startName() {
if (!info.hasBasename())
info.BasenameRange.first = getStreamLength();
}

void endName() {
if (!info.hasBasename())
info.BasenameRange.second = getStreamLength();
}

void startParameters(unsigned depth) {
if (parametersDepth || !info.hasBasename() || info.hasArguments()) {
return;
}
info.ArgumentsRange.first = getStreamLength();
parametersDepth = depth;
}

void endParameters(unsigned depth) {
if (!parametersDepth || *parametersDepth != depth || info.hasArguments()) {
return;
}
info.ArgumentsRange.second = getStreamLength();
}

bool shouldTrackNameRange(NodePointer Node) const {
switch (Node->getKind()) {
case Node::Kind::Function:
case Node::Kind::Constructor:
case Node::Kind::Allocator:
case Node::Kind::ExplicitClosure:
return true;
default:
return false;
}
}

void printFunctionName(bool hasName, llvm::StringRef &OverwriteName,
llvm::StringRef &ExtraName, bool MultiWordName,
int &ExtraIndex, NodePointer Entity,
unsigned int depth) override {
if (shouldTrackNameRange(Entity))
startName();
NodePrinter::printFunctionName(hasName, OverwriteName, ExtraName,
MultiWordName, ExtraIndex, Entity, depth);
if (shouldTrackNameRange(Entity))
endName();
}

void printFunctionParameters(NodePointer LabelList, NodePointer ParameterType,
unsigned depth, bool showTypes) override {
startParameters(depth);
NodePrinter::printFunctionParameters(LabelList, ParameterType, depth,
showTypes);
endParameters(depth);
}
};

} // namespace lldb_private

#endif // LLDB_CORE_MANGLED_H
8 changes: 8 additions & 0 deletions lldb/include/lldb/Core/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,14 @@ class PluginManager {
static bool CreateSettingForCPlusPlusLanguagePlugin(
Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
llvm::StringRef description, bool is_global_property);

static lldb::OptionValuePropertiesSP
GetSettingForSwiftLanguagePlugin(Debugger &debugger,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it would be nice if this could live in Plugin/ (but maybe it's harder to pull off here?)

llvm::StringRef setting_name);

static bool CreateSettingForSwiftLanguagePlugin(
Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
llvm::StringRef description, bool is_global_property);
};

} // namespace lldb_private
Expand Down
11 changes: 9 additions & 2 deletions lldb/source/Core/Mangled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,15 @@ ConstString Mangled::GetDemangledNameImpl(bool force, // BEGIN SWIFT
const char *mangled_name = m_mangled.GetCString();
Log *log = GetLog(LLDBLog::Demangle);
LLDB_LOGF(log, "demangle swift: %s", mangled_name);
std::string demangled(SwiftLanguageRuntime::DemangleSymbolAsString(
mangled_name, SwiftLanguageRuntime::eTypeName, sc));
auto [demangled, info] = SwiftLanguageRuntime::TrackedDemangleSymbolAsString(
mangled_name, SwiftLanguageRuntime::eSimplified, sc);
info.PrefixRange.second =
std::min(info.BasenameRange.first, info.ArgumentsRange.first);
info.SuffixRange.first =
std::max(info.BasenameRange.second, info.ArgumentsRange.second);
info.SuffixRange.second = demangled.length();
m_demangled_info.emplace(info);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a function that does all of this, like in the C++ case?


// Don't cache the demangled name the function isn't available yet.
if (!sc || !sc->function) {
LLDB_LOGF(log, "demangle swift: %s -> \"%s\" (not cached)", mangled_name,
Expand Down
15 changes: 15 additions & 0 deletions lldb/source/Core/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,7 @@ static constexpr llvm::StringLiteral kJITLoaderPluginName("jit-loader");
static constexpr llvm::StringLiteral
kStructuredDataPluginName("structured-data");
static constexpr llvm::StringLiteral kCPlusPlusLanguagePlugin("cplusplus");
static constexpr llvm::StringLiteral kSwiftLanguagePlugin("swift");

lldb::OptionValuePropertiesSP
PluginManager::GetSettingForDynamicLoaderPlugin(Debugger &debugger,
Expand Down Expand Up @@ -1937,3 +1938,17 @@ bool PluginManager::CreateSettingForCPlusPlusLanguagePlugin(
"Settings for CPlusPlus language plug-ins",
properties_sp, description, is_global_property);
}

lldb::OptionValuePropertiesSP
PluginManager::GetSettingForSwiftLanguagePlugin(
Debugger &debugger, llvm::StringRef setting_name) {
return GetSettingForPlugin(debugger, setting_name, kSwiftLanguagePlugin);
}

bool PluginManager::CreateSettingForSwiftLanguagePlugin(
Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
llvm::StringRef description, bool is_global_property) {
return CreateSettingForPlugin(debugger, kSwiftLanguagePlugin,
"Settings for Swift language plug-ins",
properties_sp, description, is_global_property);
}
12 changes: 12 additions & 0 deletions lldb/source/Plugins/Language/Swift/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
lldb_tablegen(LanguageSwiftProperties.inc -gen-lldb-property-defs
SOURCE LanguageSwiftProperties.td
TARGET LLDBPluginLanguageSwiftPropertiesGen)

lldb_tablegen(LanguageSwiftPropertiesEnum.inc -gen-lldb-property-enum-defs
SOURCE LanguageSwiftProperties.td
TARGET LLDBPluginLanguageSwiftPropertiesEnumGen)

set(LLVM_NO_RTTI 1)

add_lldb_library(lldbPluginSwiftLanguage PLUGIN
Expand Down Expand Up @@ -36,3 +44,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL Clang AND NOT SWIFT_COMPILER_MSVC_LIKE)
target_compile_options(lldbPluginSwiftLanguage PRIVATE
-Wno-dollar-in-identifier-extension)
endif()

add_dependencies(lldbPluginSwiftLanguage
LLDBPluginLanguageSwiftPropertiesGen
LLDBPluginLanguageSwiftPropertiesEnumGen)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include "../../../../include/lldb/Core/PropertiesBase.td"

let Definition = "language_swift" in {
def FunctionNameFormat: Property<"function-name-format", "FormatEntity">,
Global,
DefaultStringValue<"${function.prefix}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.formatted-arguments}${function.suffix}">,
Desc<"Swift specific frame format string to use when displaying stack frame information for threads.">;
}
Loading