Skip to content

[lldb] Implement check for potential Swift interop types in DWARF #8009

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@ void ClangASTMetadata::Dump(Stream *s) {
if (m_is_dynamic_cxx) {
s->Printf("is_dynamic_cxx=%i ", m_is_dynamic_cxx);
}

// BEGIN SWIFT
if (m_is_potentially_swift_interop_type) {
s->Printf("is_swift_interop_type=%i ", m_is_potentially_swift_interop_type);
}
// END SWIFT

s->EOL();
}
27 changes: 25 additions & 2 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangASTMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ class ClangASTMetadata {
ClangASTMetadata()
: m_user_id(0), m_union_is_user_id(false), m_union_is_isa_ptr(false),
m_has_object_ptr(false), m_is_self(false), m_is_dynamic_cxx(true),
m_is_forcefully_completed(false) {}
m_is_forcefully_completed(false)
// BEGIN SWIFT
// This is initialized to true because in the off-chance we don't parse
// this type in debug info we should stil take the regular, expensive
// path to figure out if the type is a Swift interop type or not.
,
m_is_potentially_swift_interop_type(true)
// END SWIFT
{}

bool GetIsDynamicCXXType() const { return m_is_dynamic_cxx; }

Expand Down Expand Up @@ -93,6 +101,16 @@ class ClangASTMetadata {
m_is_forcefully_completed = true;
}

// BEGIN SWIFT
bool GetIsPotentiallySwiftInteropType() {
return m_is_potentially_swift_interop_type;
}

void SetIsPotentiallySwiftInteropType(bool is_swift_interop_type) {
m_is_potentially_swift_interop_type = is_swift_interop_type;
}
// END SWIFT

void Dump(Stream *s);

private:
Expand All @@ -102,7 +120,12 @@ class ClangASTMetadata {
};

bool m_union_is_user_id : 1, m_union_is_isa_ptr : 1, m_has_object_ptr : 1,
m_is_self : 1, m_is_dynamic_cxx : 1, m_is_forcefully_completed : 1;
m_is_self : 1, m_is_dynamic_cxx : 1,
m_is_forcefully_completed : 1
// BEGIN SWIFT
, m_is_potentially_swift_interop_type : 1
// END SWIFT
;
};

} // namespace lldb_private
Expand Down
5 changes: 5 additions & 0 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/DataFormatters/StringPrinter.h"

#include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/Symbol/CompileUnit.h"
Expand Down Expand Up @@ -768,6 +769,10 @@ ExtractSwiftTypeNameFromCxxInteropType(CompilerType type) {
}

const clang::RecordDecl *record_decl = record_type->getDecl();
auto *metadata = tsc->GetMetadata(record_decl);
if (metadata && !metadata->GetIsPotentiallySwiftInteropType())
return {};

for (auto *child_decl : record_decl->decls()) {
auto *var_decl = llvm::dyn_cast<clang::VarDecl>(child_decl);
if (!var_decl)
Expand Down
13 changes: 13 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,16 @@ DWARFASTParserClang::GetCPlusPlusQualifiedName(const DWARFDIE &die) {
return qualified_name;
}

// BEGIN SWIFT
bool DWARFASTParserClang::IsSwiftInteropType(const DWARFDIE &die) {
for (DWARFDIE die : die.children())
if (die.Tag() == llvm::dwarf::DW_TAG_member &&
llvm::StringRef(die.GetName()) == "__swift_mangled_name")

Choose a reason for hiding this comment

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

Regarding a conversation @felipepiovezan and I had offline about query APIs: I'm curious if this if (die.GetName() == string) also happens often enough to warrant a faster die.IsName(string) API.

Choose a reason for hiding this comment

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

Probably worth having a die.CompareName(other) method too, as it is always faster to compare it that way in the negative case.

return true;
return false;
}
// END SWIFT

TypeSP
DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
const DWARFDIE &die,
Expand Down Expand Up @@ -1774,6 +1784,9 @@ DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
ClangASTMetadata metadata;
metadata.SetUserID(die.GetID());
metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die));
// BEGIN SWIFT
metadata.SetIsPotentiallySwiftInteropType(IsSwiftInteropType(die));
// END SWIFT

TypeSystemClang::TemplateParameterInfos template_param_infos;
if (ParseTemplateParameterInfos(die, template_param_infos)) {
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ class DWARFASTParserClang : public lldb_private::plugin::dwarf::DWARFASTParser {
lldb_private::CompilerType &class_clang_type,
const lldb::AccessType default_accesibility,
lldb_private::ClangASTImporter::LayoutInfo &layout_info);

// BEGIN SWIFT
/// Returns true if the C++ type is a compiler-generated wrapper around a
/// Swift type.
bool IsSwiftInteropType(const lldb_private::plugin::dwarf::DWARFDIE &die);
// END SWIFT

};

/// Parsed form of all attributes that are relevant for type reconstruction.
Expand Down