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 2 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
2 changes: 2 additions & 0 deletions lldb/.clang-format-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source/Plugins/Language/CPlusPlus/LanguageCPlusPlusProperties.td
source/Plugins/Language/Swift/LanguageSwiftProperties.td
5 changes: 5 additions & 0 deletions lldb/include/lldb/Core/DemangledNameInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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
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
58 changes: 36 additions & 22 deletions lldb/source/Core/Mangled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,41 @@ void Mangled::SetValue(ConstString name) {
}
}

// BEGIN SWIFT
#ifdef LLDB_ENABLE_SWIFT
static ConstString GetSwiftDemangledStr(ConstString m_mangled,
const SymbolContext *sc,
ConstString &m_demangled) {
const char *mangled_name = m_mangled.GetCString();
Log *log = GetLog(LLDBLog::Demangle);
LLDB_LOGF(log, "demangle swift: %s", mangled_name);
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();

// 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,
demangled.c_str());
return ConstString(demangled);
}
if (demangled.empty()) {
LLDB_LOGF(log, "demangle swift: %s -> error: failed to demangle",
mangled_name);
} else {
LLDB_LOGF(log, "demangle swift: %s -> \"%s\"", mangled_name,
demangled.c_str());
m_demangled.SetStringWithMangledCounterpart(demangled, m_mangled);
}
return m_demangled;
}
#endif // LLDB_ENABLE_SWIFT
// END SWIFT

// Local helpers for different demangling implementations.
static char *GetMSVCDemangledStr(llvm::StringRef M) {
char *demangled_cstr = llvm::microsoftDemangle(
Expand Down Expand Up @@ -349,28 +384,7 @@ ConstString Mangled::GetDemangledNameImpl(bool force, // BEGIN SWIFT
// Demangling a swift name requires the swift compiler. This is
// explicitly unsupported on llvm.org.
#ifdef LLDB_ENABLE_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));
// 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,
demangled.c_str());
return ConstString(demangled);
}
if (demangled.empty()) {
LLDB_LOGF(log, "demangle swift: %s -> error: failed to demangle",
mangled_name);
} else {
LLDB_LOGF(log, "demangle swift: %s -> \"%s\"", mangled_name,
demangled.c_str());
m_demangled.SetStringWithMangledCounterpart(demangled, m_mangled);
}
return m_demangled;
}
return GetSwiftDemangledStr(m_mangled, sc, m_demangled);
#endif // LLDB_ENABLE_SWIFT
break;
case eManglingSchemeNone:
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