Skip to content

Display generic function type parameters in backtraces. #8703

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

Merged
merged 3 commits into from
May 7, 2024
Merged
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: 1 addition & 1 deletion lldb/include/lldb/Symbol/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ class Function : public UserID, public SymbolContextScope {

ConstString GetNameNoArguments(const SymbolContext *sc = nullptr) const;

ConstString GetDisplayName(const SymbolContext *sc = nullptr) const;
ConstString GetDisplayName() const;

const Mangled &GetMangled() const { return m_mangled; }

Expand Down
53 changes: 6 additions & 47 deletions lldb/source/Core/Mangled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,6 @@ static inline bool cstring_is_mangled(llvm::StringRef s) {
return Mangled::GetManglingScheme(s) != Mangled::eManglingSchemeNone;
}

#ifdef LLDB_ENABLE_SWIFT
#pragma mark DisplayDemangledNamesCache

// make the key type be a const char* because that gives us usable
// DenseMapInfo for free making DenseMap work for ConstString requires
// us to provide two "invalid" values: the empty key and the tombstone
// key; but for ConstString, we really don't have any well-known
// invalid value other than ConstString(nullptr) so, just use const
// char* as the key as LLVM knows how to do proper DenseMapInfo for
// pointers
static ThreadSafeDenseMap<const char *, ConstString>&
GetDisplayDemangledNamesCache() {
static ThreadSafeDenseMap<const char *, ConstString> g_cache;
return g_cache;
}
#endif // LLDB_ENABLE_SWIFT

#pragma mark Mangled

Mangled::ManglingScheme Mangled::GetManglingScheme(llvm::StringRef const name) {
Expand Down Expand Up @@ -346,39 +329,15 @@ ConstString Mangled::GetDemangledName(// BEGIN SWIFT

ConstString Mangled::GetDisplayDemangledName(
// BEGIN SWIFT
const SymbolContext *sc
) const {
ConstString demangled;
const SymbolContext *sc) const {
#ifdef LLDB_ENABLE_SWIFT
if (m_mangled) {
do {
const char *mangled = m_mangled.GetCString();

if (mangled) {
if (SwiftLanguageRuntime::IsSwiftMangledName(m_mangled.GetStringRef())) {
auto& display_cache = ::GetDisplayDemangledNamesCache();
if (display_cache.Lookup(mangled, demangled) &&
demangled)
break;

std::string demangled_std =
SwiftLanguageRuntime::DemangleSymbolAsString(
m_mangled.GetStringRef(), SwiftLanguageRuntime::eSimplified,
sc);
if (!demangled_std.empty()) {
demangled.SetCString(demangled_std.c_str());
display_cache.Insert(mangled, demangled);
break;
}
}
}
} while (0);
}
if (m_mangled &&
SwiftLanguageRuntime::IsSwiftMangledName(m_mangled.GetStringRef()))
return ConstString(SwiftLanguageRuntime::DemangleSymbolAsString(
m_mangled.GetStringRef(), SwiftLanguageRuntime::eSimplified, sc));
#endif // LLDB_ENABLE_SWIFT
if (!demangled)
demangled = GetDemangledName();
return demangled ? demangled : m_mangled;
// END SWIFT
return GetDemangledName();
}

bool Mangled::NameMatches(const RegularExpression &regex) const {
Expand Down
277 changes: 140 additions & 137 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,8 @@ bool SwiftLanguage::IsSourceFile(llvm::StringRef file_path) const {
return file_path.endswith(".swift");
}

std::vector<FormattersMatchCandidate> SwiftLanguage::GetPossibleFormattersMatches(
std::vector<FormattersMatchCandidate>
SwiftLanguage::GetPossibleFormattersMatches(
ValueObject &valobj, lldb::DynamicValueType use_dynamic) {
std::vector<FormattersMatchCandidate> result;

Expand Down Expand Up @@ -1636,152 +1637,154 @@ bool SwiftLanguage::GetFunctionDisplayName(
SwiftScratchContextLock scratch_ctx_lock(exe_ctx);
switch (representation) {
case Language::FunctionNameRepresentation::eName:
break; // no need to customize this
// No need to customize this.
return false;
case Language::FunctionNameRepresentation::eNameWithNoArgs: {
if (sc->function) {
if (sc->function->GetLanguage() == eLanguageTypeSwift) {
if (ConstString cs = sc->function->GetDisplayName(sc)) {
s.Printf("%s", cs.AsCString());
return true;
}
}
}
break;
if (!sc->function)
return false;
if (sc->function->GetLanguage() != eLanguageTypeSwift)
return false;
std::string display_name = SwiftLanguageRuntime::DemangleSymbolAsString(
sc->function->GetMangled().GetMangledName().GetStringRef(),
SwiftLanguageRuntime::eSimplified, sc, exe_ctx);
if (display_name.empty())
return false;
s << display_name;
return true;
}
case Language::FunctionNameRepresentation::eNameWithArgs: {
if (sc->function) {
if (sc->function->GetLanguage() == eLanguageTypeSwift) {
if (const char *cstr = sc->function->GetDisplayName(sc).AsCString()) {
ExecutionContextScope *exe_scope =
exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
const InlineFunctionInfo *inline_info = NULL;
VariableListSP variable_list_sp;
bool get_function_vars = true;
if (sc->block) {
Block *inline_block = sc->block->GetContainingInlinedBlock();

if (inline_block) {
get_function_vars = false;
inline_info = sc->block->GetInlinedFunctionInfo();
if (inline_info)
variable_list_sp = inline_block->GetBlockVariableList(true);
}
}

if (get_function_vars) {
variable_list_sp =
sc->function->GetBlock(true).GetBlockVariableList(true);
}

if (inline_info) {
s.PutCString(cstr);
s.PutCString(" [inlined] ");
cstr = inline_info->GetName().GetCString();
}
if (!sc->function)
return false;
if (sc->function->GetLanguage() != eLanguageTypeSwift)
return false;
std::string display_name = SwiftLanguageRuntime::DemangleSymbolAsString(
sc->function->GetMangled().GetMangledName().GetStringRef(),
SwiftLanguageRuntime::eSimplified, sc, exe_ctx);
if (display_name.empty())
return false;
ExecutionContextScope *exe_scope =
exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL;
const InlineFunctionInfo *inline_info = NULL;
VariableListSP variable_list_sp;
bool get_function_vars = true;
if (sc->block) {
Block *inline_block = sc->block->GetContainingInlinedBlock();

if (inline_block) {
get_function_vars = false;
inline_info = sc->block->GetInlinedFunctionInfo();
if (inline_info)
variable_list_sp = inline_block->GetBlockVariableList(true);
}
}

VariableList args;
if (variable_list_sp)
variable_list_sp->AppendVariablesWithScope(
eValueTypeVariableArgument, args);
if (args.GetSize() > 0) {
const char *open_paren = strchr(cstr, '(');
const char *close_paren = nullptr;
const char *generic = strchr(cstr, '<');
// if before the arguments list begins there is a template sign
// then scan to the end of the generic args before you try to find
// the arguments list
if (generic && open_paren && generic < open_paren) {
int generic_depth = 1;
++generic;
for (; *generic && generic_depth > 0; generic++) {
if (*generic == '<')
generic_depth++;
if (*generic == '>')
generic_depth--;
}
if (*generic)
open_paren = strchr(generic, '(');
else
open_paren = nullptr;
}
if (open_paren) {
close_paren = strchr(open_paren, ')');
}
if (get_function_vars) {
variable_list_sp =
sc->function->GetBlock(true).GetBlockVariableList(true);
}

if (open_paren)
s.Write(cstr, open_paren - cstr + 1);
else {
s.PutCString(cstr);
s.PutChar('(');
}
const size_t num_args = args.GetSize();
for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx) {
std::string buffer;

VariableSP var_sp(args.GetVariableAtIndex(arg_idx));
ValueObjectSP var_value_sp(
ValueObjectVariable::Create(exe_scope, var_sp));
StreamString ss;
const char *var_representation = nullptr;
const char *var_name = var_value_sp->GetName().GetCString();
if (var_value_sp->GetCompilerType().IsValid()) {
if (var_value_sp && exe_scope->CalculateTarget())
var_value_sp =
var_value_sp->GetQualifiedRepresentationIfAvailable(
exe_scope->CalculateTarget()
->TargetProperties::GetPreferDynamicValue(),
exe_scope->CalculateTarget()
->TargetProperties::GetEnableSyntheticValue());
if (var_value_sp->GetCompilerType().IsAggregateType() &&
DataVisualization::ShouldPrintAsOneLiner(
*var_value_sp.get())) {
static StringSummaryFormat format(
TypeSummaryImpl::Flags()
.SetHideItemNames(false)
.SetShowMembersOneLiner(true),
"");
format.FormatObject(var_value_sp.get(), buffer,
TypeSummaryOptions());
var_representation = buffer.c_str();
} else
var_value_sp->DumpPrintableRepresentation(
ss,
ValueObject::ValueObjectRepresentationStyle::
eValueObjectRepresentationStyleSummary,
eFormatDefault,
ValueObject::PrintableRepresentationSpecialCases::eAllow,
false);
}
if (ss.GetData() && ss.GetSize())
var_representation = ss.GetData();
if (arg_idx > 0)
s.PutCString(", ");
if (var_value_sp->GetError().Success()) {
if (var_representation)
s.Printf("%s=%s", var_name, var_representation);
else
s.Printf("%s=%s at %s", var_name,
var_value_sp->GetTypeName().GetCString(),
var_value_sp->GetLocationAsCString());
} else
s.Printf("%s=<unavailable>", var_name);
}
if (inline_info) {
s << display_name;
s.PutCString(" [inlined] ");
display_name = inline_info->GetName();
}

if (close_paren)
s.PutCString(close_paren);
else
s.PutChar(')');
VariableList args;
if (variable_list_sp)
variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
args);
if (args.GetSize() == 0) {
s << display_name;
return true;
}
const char *cstr = display_name.data();
const char *open_paren = strchr(cstr, '(');
const char *close_paren = nullptr;
const char *generic = strchr(cstr, '<');
// If before the arguments list begins there is a template sign
// then scan to the end of the generic args before you try to find
// the arguments list.
if (generic && open_paren && generic < open_paren) {
int generic_depth = 1;
++generic;
for (; *generic && generic_depth > 0; generic++) {
if (*generic == '<')
generic_depth++;
if (*generic == '>')
generic_depth--;
}
if (*generic)
open_paren = strchr(generic, '(');
else
open_paren = nullptr;
}
if (open_paren) {
close_paren = strchr(open_paren, ')');
}

} else {
s.PutCString(cstr);
}
return true;
}
if (open_paren)
s.Write(cstr, open_paren - cstr + 1);
else {
s << display_name;
s.PutChar('(');
}
const size_t num_args = args.GetSize();
for (size_t arg_idx = 0; arg_idx < num_args; ++arg_idx) {
std::string buffer;

VariableSP var_sp(args.GetVariableAtIndex(arg_idx));
ValueObjectSP var_value_sp(
ValueObjectVariable::Create(exe_scope, var_sp));
if (!var_sp || !var_value_sp || var_sp->IsArtificial())

Choose a reason for hiding this comment

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

I assume this is the self bit you mentioned in the commit message?

Copy link
Author

Choose a reason for hiding this comment

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

Yes.

Choose a reason for hiding this comment

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

can you explain a little more, where is self being omitted? Is there a test for it that I've overlooked?

continue;
StreamString ss;
const char *var_representation = nullptr;
const char *var_name = var_value_sp->GetName().GetCString();
if (var_value_sp->GetCompilerType().IsValid()) {
if (var_value_sp && exe_scope->CalculateTarget())
var_value_sp = var_value_sp->GetQualifiedRepresentationIfAvailable(
exe_scope->CalculateTarget()
->TargetProperties::GetPreferDynamicValue(),
exe_scope->CalculateTarget()
->TargetProperties::GetEnableSyntheticValue());
if (var_value_sp->GetCompilerType().IsAggregateType() &&
DataVisualization::ShouldPrintAsOneLiner(*var_value_sp.get())) {
static StringSummaryFormat format(TypeSummaryImpl::Flags()
.SetHideItemNames(false)
.SetShowMembersOneLiner(true),
"");
format.FormatObject(var_value_sp.get(), buffer, TypeSummaryOptions());
var_representation = buffer.c_str();
} else
var_value_sp->DumpPrintableRepresentation(
ss,
ValueObject::ValueObjectRepresentationStyle::
eValueObjectRepresentationStyleSummary,
eFormatDefault,
ValueObject::PrintableRepresentationSpecialCases::eAllow, false);
}
if (ss.GetData() && ss.GetSize())
var_representation = ss.GetData();
if (arg_idx > 0)
s.PutCString(", ");
if (var_value_sp->GetError().Success()) {
if (var_representation)
s.Printf("%s=%s", var_name, var_representation);
else
s.Printf("%s=%s at %s", var_name,
var_value_sp->GetTypeName().GetCString(),
var_value_sp->GetLocationAsCString());
} else
s.Printf("%s=<unavailable>", var_name);
}
}
}

if (close_paren)
s.PutCString(close_paren);
else
s.PutChar(')');
}
return true;
}
return false;
}

Expand Down
Loading