Skip to content

Commit 544cc09

Browse files
[lldb] add syntax highlighting infrastructure to Swift plugin
1 parent 84cd05a commit 544cc09

File tree

7 files changed

+192
-1
lines changed

7 files changed

+192
-1
lines changed

lldb/.clang-format-ignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source/Plugins/Language/CPlusPlus/LanguageCPlusPlusProperties.td
2+
source/Plugins/Language/Swift/LanguageSwiftProperties.td

lldb/include/lldb/Core/PluginManager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,13 @@ class PluginManager {
787787

788788
static std::vector<RegisteredPluginInfo> GetUnwindAssemblyPluginInfo();
789789
static bool SetUnwindAssemblyPluginEnabled(llvm::StringRef name, bool enable);
790+
static lldb::OptionValuePropertiesSP
791+
GetSettingForSwiftLanguagePlugin(Debugger &debugger,
792+
llvm::StringRef setting_name);
793+
794+
static bool CreateSettingForSwiftLanguagePlugin(
795+
Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
796+
llvm::StringRef description, bool is_global_property);
790797
};
791798

792799
} // namespace lldb_private

lldb/source/Core/PluginManager.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,7 @@ static constexpr llvm::StringLiteral kJITLoaderPluginName("jit-loader");
20722072
static constexpr llvm::StringLiteral
20732073
kStructuredDataPluginName("structured-data");
20742074
static constexpr llvm::StringLiteral kCPlusPlusLanguagePlugin("cplusplus");
2075+
static constexpr llvm::StringLiteral kSwiftLanguagePlugin("swift");
20752076

20762077
lldb::OptionValuePropertiesSP
20772078
PluginManager::GetSettingForDynamicLoaderPlugin(Debugger &debugger,
@@ -2474,3 +2475,16 @@ bool PluginManager::SetUnwindAssemblyPluginEnabled(llvm::StringRef name,
24742475
bool enable) {
24752476
return GetUnwindAssemblyInstances().SetInstanceEnabled(name, enable);
24762477
}
2478+
lldb::OptionValuePropertiesSP
2479+
PluginManager::GetSettingForSwiftLanguagePlugin(Debugger &debugger,
2480+
llvm::StringRef setting_name) {
2481+
return GetSettingForPlugin(debugger, setting_name, kSwiftLanguagePlugin);
2482+
}
2483+
2484+
bool PluginManager::CreateSettingForSwiftLanguagePlugin(
2485+
Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
2486+
llvm::StringRef description, bool is_global_property) {
2487+
return CreateSettingForPlugin(debugger, kSwiftLanguagePlugin,
2488+
"Settings for Swift language plug-ins",
2489+
properties_sp, description, is_global_property);
2490+
}

lldb/source/Plugins/Language/Swift/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
lldb_tablegen(LanguageSwiftProperties.inc -gen-lldb-property-defs
2+
SOURCE LanguageSwiftProperties.td
3+
TARGET LLDBPluginLanguageSwiftPropertiesGen)
4+
5+
lldb_tablegen(LanguageSwiftPropertiesEnum.inc -gen-lldb-property-enum-defs
6+
SOURCE LanguageSwiftProperties.td
7+
TARGET LLDBPluginLanguageSwiftPropertiesEnumGen)
8+
19
set(LLVM_NO_RTTI 1)
210

311
add_lldb_library(lldbPluginSwiftLanguage PLUGIN
@@ -36,3 +44,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL Clang AND NOT SWIFT_COMPILER_MSVC_LIKE)
3644
target_compile_options(lldbPluginSwiftLanguage PRIVATE
3745
-Wno-dollar-in-identifier-extension)
3846
endif()
47+
48+
add_dependencies(lldbPluginSwiftLanguage
49+
LLDBPluginLanguageSwiftPropertiesGen
50+
LLDBPluginLanguageSwiftPropertiesEnumGen)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include "../../../../include/lldb/Core/PropertiesBase.td"
2+
3+
let Definition = "language_swift" in {
4+
def FunctionNameFormat: Property<"function-name-format", "FormatEntity">,
5+
Global,
6+
DefaultStringValue<"${function.prefix}${ansi.fg.yellow}${function.basename}${ansi.normal}${function.formatted-arguments}${function.suffix}">,
7+
Desc<"Swift specific frame format string to use when displaying stack frame information for threads.">;
8+
}

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

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void SwiftLanguage::Initialize() {
6868
static ConstString g_SwiftStringStorageClass("_TtCs15__StringStorage");
6969
static ConstString g_NSArrayClass1("_TtCs22__SwiftDeferredNSArray");
7070
PluginManager::RegisterPlugin(GetPluginNameStatic(), "Swift Language",
71-
CreateInstance);
71+
CreateInstance, &DebuggerInitialize);
7272

7373
lldb_private::formatters::NSString_Additionals::GetAdditionalSummaries()
7474
.emplace(
@@ -1900,6 +1900,144 @@ SwiftLanguage::GetDemangledFunctionNameWithoutArguments(Mangled mangled) const {
19001900
return mangled_name;
19011901
}
19021902

1903+
static std::optional<llvm::StringRef>
1904+
GetDemangledBasename(const SymbolContext &sc) {
1905+
return std::nullopt;
1906+
}
1907+
1908+
static std::optional<llvm::StringRef>
1909+
GetDemangledFunctionPrefix(const SymbolContext &sc) {
1910+
return std::nullopt;
1911+
}
1912+
1913+
static std::optional<llvm::StringRef>
1914+
GetDemangledFunctionSuffix(const SymbolContext &sc) {
1915+
return std::nullopt;
1916+
}
1917+
1918+
static bool PrintDemangledArgumentList(Stream &s, const SymbolContext &sc) {
1919+
return false;
1920+
}
1921+
1922+
static VariableListSP GetFunctionVariableList(const SymbolContext &sc) {
1923+
assert(sc.function);
1924+
1925+
if (sc.block)
1926+
if (Block *inline_block = sc.block->GetContainingInlinedBlock())
1927+
return inline_block->GetBlockVariableList(true);
1928+
1929+
return sc.function->GetBlock(true).GetBlockVariableList(true);
1930+
}
1931+
1932+
bool SwiftLanguage::HandleFrameFormatVariable(const SymbolContext &sc,
1933+
const ExecutionContext *exe_ctx,
1934+
FormatEntity::Entry::Type type,
1935+
Stream &s) {
1936+
switch (type) {
1937+
case FormatEntity::Entry::Type::FunctionBasename: {
1938+
std::optional<llvm::StringRef> name = GetDemangledBasename(sc);
1939+
if (!name)
1940+
return false;
1941+
1942+
s << *name;
1943+
1944+
return true;
1945+
}
1946+
case FormatEntity::Entry::Type::FunctionFormattedArguments: {
1947+
// This ensures we print the arguments even when no debug-info is available.
1948+
//
1949+
// FIXME: we should have a Entry::Type::FunctionArguments and
1950+
// use it in the plugin.cplusplus.display.function-name-format
1951+
// once we have a "fallback operator" in the frame-format language.
1952+
if (!sc.function && sc.symbol)
1953+
return PrintDemangledArgumentList(s, sc);
1954+
std::string display_name = SwiftLanguageRuntime::DemangleSymbolAsString(
1955+
sc.function->GetMangled().GetMangledName().GetStringRef(),
1956+
SwiftLanguageRuntime::eSimplified, &sc, exe_ctx);
1957+
if (display_name.empty())
1958+
return false;
1959+
1960+
VariableList args;
1961+
if (auto variable_list_sp = GetFunctionVariableList(sc))
1962+
variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
1963+
args);
1964+
1965+
s << GetFunctionDisplayArgs(sc, args, exe_ctx);
1966+
return true;
1967+
}
1968+
case FormatEntity::Entry::Type::FunctionPrefix: {
1969+
std::optional<llvm::StringRef> prefix = GetDemangledFunctionPrefix(sc);
1970+
if (!prefix)
1971+
return false;
1972+
1973+
s << *prefix;
1974+
1975+
return true;
1976+
}
1977+
case FormatEntity::Entry::Type::FunctionSuffix: {
1978+
std::optional<llvm::StringRef> suffix = GetDemangledFunctionSuffix(sc);
1979+
if (!suffix)
1980+
return false;
1981+
1982+
s << *suffix;
1983+
1984+
return true;
1985+
}
1986+
1987+
case FormatEntity::Entry::Type::FunctionScope:
1988+
case FormatEntity::Entry::Type::FunctionTemplateArguments:
1989+
case FormatEntity::Entry::Type::FunctionReturnRight:
1990+
case FormatEntity::Entry::Type::FunctionReturnLeft:
1991+
case FormatEntity::Entry::Type::FunctionQualifiers:
1992+
default:
1993+
return true;
1994+
}
1995+
}
1996+
1997+
#define LLDB_PROPERTIES_language_swift
1998+
#include "LanguageSwiftProperties.inc"
1999+
2000+
enum {
2001+
#define LLDB_PROPERTIES_language_swift
2002+
#include "LanguageSwiftPropertiesEnum.inc"
2003+
};
2004+
2005+
namespace {
2006+
class PluginProperties : public Properties {
2007+
public:
2008+
static llvm::StringRef GetSettingName() { return "display"; }
2009+
2010+
PluginProperties() {
2011+
m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
2012+
m_collection_sp->Initialize(g_language_swift_properties);
2013+
}
2014+
2015+
FormatEntity::Entry GetFunctionNameFormat() const {
2016+
return GetPropertyAtIndexAs<const FormatEntity::Entry>(
2017+
ePropertyFunctionNameFormat, {});
2018+
}
2019+
};
2020+
} // namespace
2021+
2022+
static PluginProperties &GetGlobalPluginProperties() {
2023+
static PluginProperties g_settings;
2024+
return g_settings;
2025+
}
2026+
2027+
FormatEntity::Entry SwiftLanguage::GetFunctionNameFormat() const {
2028+
return GetGlobalPluginProperties().GetFunctionNameFormat();
2029+
}
2030+
2031+
void SwiftLanguage::DebuggerInitialize(Debugger &debugger) {
2032+
if (!PluginManager::GetSettingForSwiftLanguagePlugin(
2033+
debugger, PluginProperties::GetSettingName())) {
2034+
PluginManager::CreateSettingForSwiftLanguagePlugin(
2035+
debugger, GetGlobalPluginProperties().GetValueProperties(),
2036+
"Properties for the Swift language plug-in.",
2037+
/*is_global_property=*/true);
2038+
}
2039+
}
2040+
19032041
namespace {
19042042
using namespace swift::Demangle;
19052043
struct AsyncInfo {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ class SwiftLanguage : public Language {
133133

134134
llvm::StringRef GetInstanceVariableName() override { return "self"; }
135135

136+
bool HandleFrameFormatVariable(const SymbolContext &sc,
137+
const ExecutionContext *exe_ctx,
138+
FormatEntity::Entry::Type type,
139+
Stream &s) override;
140+
141+
FormatEntity::Entry GetFunctionNameFormat() const override;
142+
136143
/// Override that skips breakpoints inside await resume ("Q") async funclets.
137144
void FilterForLineBreakpoints(
138145
llvm::SmallVectorImpl<SymbolContext> &) const override;
@@ -141,6 +148,9 @@ class SwiftLanguage : public Language {
141148
// PluginInterface protocol
142149
//------------------------------------------------------------------
143150
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
151+
152+
private:
153+
static void DebuggerInitialize(Debugger &);
144154
};
145155

146156
} // namespace lldb_private

0 commit comments

Comments
 (0)