-
Notifications
You must be signed in to change notification settings - Fork 341
[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
base: swift/release/6.2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1896,21 +1896,96 @@ SwiftLanguage::GetDemangledFunctionNameWithoutArguments(Mangled mangled) const { | |
|
||
static std::optional<llvm::StringRef> | ||
GetDemangledBasename(const SymbolContext &sc) { | ||
return std::nullopt; | ||
Mangled mangled = sc.GetPossiblyInlinedFunctionName(); | ||
if (!mangled) | ||
return std::nullopt; | ||
|
||
auto demangled_name = mangled.GetDemangledName().GetStringRef(); | ||
if (demangled_name.empty()) | ||
return std::nullopt; | ||
|
||
const std::optional<DemangledNameInfo> &info = mangled.GetDemangledInfo(); | ||
if (!info) | ||
return std::nullopt; | ||
|
||
// Function without a basename is nonsense. | ||
if (!info->hasBasename()) | ||
return std::nullopt; | ||
|
||
return demangled_name.slice(info->BasenameRange.first, | ||
info->BasenameRange.second); | ||
} | ||
|
||
static std::optional<llvm::StringRef> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will port the changes of the following PR once it's merged: |
||
GetDemangledFunctionPrefix(const SymbolContext &sc) { | ||
return std::nullopt; | ||
Mangled mangled = sc.GetPossiblyInlinedFunctionName(); | ||
if (!mangled) | ||
return std::nullopt; | ||
|
||
auto demangled_name = mangled.GetDemangledName().GetStringRef(); | ||
if (demangled_name.empty()) | ||
return std::nullopt; | ||
|
||
const std::optional<DemangledNameInfo> &info = mangled.GetDemangledInfo(); | ||
if (!info) | ||
return std::nullopt; | ||
|
||
// Function without a basename is nonsense. | ||
if (!info->hasBasename()) | ||
return std::nullopt; | ||
|
||
return demangled_name.slice(info->PrefixRange.first, | ||
info->PrefixRange.second); | ||
} | ||
|
||
static std::optional<llvm::StringRef> | ||
GetDemangledFunctionSuffix(const SymbolContext &sc) { | ||
return std::nullopt; | ||
Mangled mangled = sc.GetPossiblyInlinedFunctionName(); | ||
if (!mangled) | ||
return std::nullopt; | ||
|
||
auto demangled_name = mangled.GetDemangledName().GetStringRef(); | ||
if (demangled_name.empty()) | ||
return std::nullopt; | ||
|
||
const std::optional<DemangledNameInfo> &info = mangled.GetDemangledInfo(); | ||
if (!info) | ||
return std::nullopt; | ||
|
||
// Function without a basename is nonsense. | ||
if (!info->hasBasename()) | ||
return std::nullopt; | ||
|
||
return demangled_name.slice(info->SuffixRange.first, | ||
info->SuffixRange.second); | ||
} | ||
|
||
static bool PrintDemangledArgumentList(Stream &s, const SymbolContext &sc) { | ||
return false; | ||
assert(sc.symbol); | ||
|
||
Mangled mangled = sc.GetPossiblyInlinedFunctionName(); | ||
if (!mangled) | ||
return false; | ||
|
||
auto demangled_name = mangled.GetDemangledName().GetStringRef(); | ||
if (demangled_name.empty()) | ||
return false; | ||
|
||
const std::optional<DemangledNameInfo> &info = mangled.GetDemangledInfo(); | ||
if (!info) | ||
return false; | ||
|
||
// Function without a basename is nonsense. | ||
if (!info->hasBasename()) | ||
return false; | ||
|
||
if (info->ArgumentsRange.second < info->ArgumentsRange.first) | ||
return false; | ||
|
||
s << demangled_name.slice(info->ArgumentsRange.first, | ||
info->ArgumentsRange.second); | ||
|
||
return true; | ||
} | ||
|
||
static VariableListSP GetFunctionVariableList(const SymbolContext &sc) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//===-- SwiftMangled.h ------------------------------------------*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef liblldb_SwiftMangled_h_ | ||
#define liblldb_SwiftMangled_h_ | ||
|
||
#include "lldb/Core/DemangledNameInfo.h" | ||
#include "swift/Demangling/Demangle.h" | ||
|
||
using namespace swift::Demangle; | ||
|
||
/// A NodePrinter class with range tracking capabilities. | ||
/// | ||
/// When used instead of a regular NodePrinter, this class will store additional | ||
/// range information of the demangled name in the `info` attribute, such as the | ||
/// range of the name of a method. | ||
class TrackingNodePrinter : public NodePrinter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a oxygen comment here explaining very briefly what this class is used for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed 👍 |
||
public: | ||
TrackingNodePrinter(DemangleOptions options) : NodePrinter(options) {} | ||
|
||
lldb_private::DemangledNameInfo takeInfo() { return std::move(info); } | ||
|
||
private: | ||
lldb_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); | ||
} | ||
}; | ||
|
||
#endif // liblldb_SwiftMangled_h_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,11 +200,33 @@ class SwiftLanguageRuntime : public LanguageRuntime { | |
IsSwiftAsyncAwaitResumePartialFunctionSymbol(llvm::StringRef name); | ||
|
||
enum DemangleMode { eSimplified, eTypeName, eDisplayTypeName }; | ||
|
||
/// Demangle a symbol to a string. | ||
/// | ||
/// \param symbol The mangled symbol to demangle. | ||
/// \param mode The `DemangleMode` to use when demangling. | ||
/// \param sc The associated `SymbolContext`. | ||
/// \param exe_ctx The associated `ExecutionContext`. | ||
/// | ||
/// \return The demangled symbol. | ||
static std::string | ||
DemangleSymbolAsString(llvm::StringRef symbol, DemangleMode mode, | ||
const SymbolContext *sc = nullptr, | ||
const ExecutionContext *exe_ctx = nullptr); | ||
|
||
/// Demangle a symbol to a string with additional range information. | ||
/// | ||
/// \param symbol The mangled symbol to demangle. | ||
/// \param mode The `DemangleMode` to use when demangling. | ||
/// \param sc The associated `SymbolContext`. | ||
/// \param exe_ctx The associated `ExecutionContext`. | ||
/// | ||
/// \return The demangled symbol as well as range tracking information. | ||
static std::pair<std::string, DemangledNameInfo> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doxygen comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed 👍 |
||
TrackedDemangleSymbolAsString(llvm::StringRef symbol, DemangleMode mode, | ||
const SymbolContext *sc = nullptr, | ||
const ExecutionContext *exe_ctx = nullptr); | ||
|
||
static std::string GetParentNameIfClosure(llvm::StringRef mangled_name); | ||
|
||
/// Demangle a symbol to a swift::Demangle node tree. | ||
|
@@ -885,6 +907,11 @@ class SwiftLanguageRuntime : public LanguageRuntime { | |
|
||
/// Swift native NSError isa. | ||
std::optional<lldb::addr_t> m_SwiftNativeNSErrorISA; | ||
|
||
static std::pair<std::string, std::optional<DemangledNameInfo>> | ||
DemangleSymbolAsString(llvm::StringRef symbol, DemangleMode mode, | ||
bool tracking, const SymbolContext *sc, | ||
const ExecutionContext *exe_ctx); | ||
}; | ||
|
||
/// The target specific register numbers used for async unwinding. | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an optional StringRef is redundant unless there is a semantic difference between std::nullopt and an empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just return a StringRef here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I did this in the upstream C++ plugin too. I did this because this way we can differentiate between "failed to extract this demangling info" versus "this part of the demangled string was not present, but nothing failed". In the failure case we return
false
from the frame-format variable handler, which is necessary to trigger the fallback format in case things went wrong.I guess instead of optional I should've probably used
Expected
and logged an error. Might be a good improvement for upstream actually if @charles-zablit has time on his hands :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened a PR upstream to address this: