Skip to content

Commit 8d7bd10

Browse files
committed
Implement precise compiler invocations for SwiftASTContext.
To deal with the growing number of incompatible Swift language flags, LLDB's expression evaluator can now precisely match the compiler flags from the current breakpoint instead of attempting to build a single compiler invocation for the entire project. This is now possible for the first time thanks to dwim-print and the generic expression evaluator. The feature can be controlled via a new "symbols.swift-precise-compiler-invocation" setting. rdar://115188886
1 parent 71e3bfa commit 8d7bd10

29 files changed

+732
-205
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
625625
// If we are extending a generic class it's going to be a metatype,
626626
// and we have to grab the instance type:
627627
imported_self_type = swift_type_system->GetInstanceType(
628-
imported_self_type.GetOpaqueQualType());
628+
imported_self_type.GetOpaqueQualType(), stack_frame_sp.get());
629629
if (!imported_self_type)
630630
return Status(
631631
"Unable to add the aliases the expression needs because the Swift "

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ findSwiftSelf(StackFrame &frame, lldb::VariableSP self_var_sp) {
145145
// 4) If `self` is a metatype, get its instance type.
146146
if (Flags(info.type.GetTypeInfo())
147147
.AllSet(lldb::eTypeIsSwift | lldb::eTypeIsMetatype)) {
148-
info.type = TypeSystemSwift::GetInstanceType(info.type);
148+
info.type = TypeSystemSwift::GetInstanceType(info.type, &frame);
149149
info.is_metatype = true;
150150
}
151151

@@ -686,9 +686,11 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
686686
if (!frame)
687687
return error("couldn't start parsing - no stack frame");
688688

689-
auto *exe_scope = exe_ctx.GetBestExecutionContextScope();
690-
if (!exe_scope)
691-
return error( "no execution context scope");
689+
ExecutionContextScope *exe_scope =
690+
m_options.GetREPLEnabled() ? static_cast<ExecutionContextScope *>(target)
691+
: static_cast<ExecutionContextScope *>(frame);
692+
693+
exe_scope = exe_ctx.GetBestExecutionContextScope();
692694

693695
m_swift_scratch_ctx = target->GetSwiftScratchContext(m_err, *exe_scope);
694696
if (!m_swift_scratch_ctx)
@@ -697,8 +699,9 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
697699

698700
const SymbolContext *sc =
699701
&frame->GetSymbolContext(lldb::eSymbolContextFunction);
700-
m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
701-
m_swift_scratch_ctx->get()->GetSwiftASTContext(sc));
702+
auto *swift_ast_ctx = m_swift_scratch_ctx->get()->GetSwiftASTContext(sc);
703+
m_swift_ast_ctx =
704+
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(swift_ast_ctx);
702705

703706
if (!m_swift_ast_ctx)
704707
return error("could not create a Swift AST context");

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,16 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
13691369
if (result_sp && result_sp->GetCompilerType().IsValid()) {
13701370
CompilerType result_type(result_sp->GetCompilerType());
13711371
if (Flags(result_type.GetTypeInfo())
1372-
.AllSet(eTypeIsSwift | eTypeIsMetatype))
1373-
result_type = TypeSystemSwift::GetInstanceType(result_type);
1372+
.AllSet(eTypeIsSwift | eTypeIsMetatype)) {
1373+
result_type = TypeSystemSwift::GetInstanceType(result_type,
1374+
exe_scope);
1375+
if (auto swift_ast_ctx =
1376+
result_type.GetTypeSystem()
1377+
.dyn_cast_or_null<SwiftASTContext>())
1378+
result_type = swift_ast_ctx->GetTypeSystemSwiftTypeRef()
1379+
.GetTypeFromMangledTypename(
1380+
result_type.GetMangledTypeName());
1381+
}
13741382
results.insert(TypeOrDecl(result_type));
13751383
}
13761384
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ bool lldb_private::formatters::swift::SwiftMetatype_SummaryProvider(
2525
lldb::addr_t metadata_ptr = valobj.GetPointerValue();
2626
if (metadata_ptr == LLDB_INVALID_ADDRESS || metadata_ptr == 0) {
2727
CompilerType compiler_metatype_type(valobj.GetCompilerType());
28-
CompilerType instancetype =
29-
TypeSystemSwift::GetInstanceType(compiler_metatype_type);
28+
ExecutionContext exe_ctx = valobj.GetExecutionContextRef();
29+
CompilerType instancetype = TypeSystemSwift::GetInstanceType(
30+
compiler_metatype_type, exe_ctx.GetBestExecutionContextScope());
3031
name = instancetype.GetDisplayTypeName();
3132
} else if (CompilerType meta_type = valobj.GetCompilerType()) {
3233
name = meta_type.GetDisplayTypeName();

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
7272
m_type(clang_type), m_cases() {}
7373

7474
void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
75-
FillCasesIfNeeded() {
75+
FillCasesIfNeeded(const ExecutionContext *exe_ctx) {
7676
if (m_cases.has_value())
7777
return;
7878

@@ -87,18 +87,18 @@ void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
8787
// standalone and provided with a callback to read the APINote
8888
// information.
8989
auto ts = m_type.GetTypeSystem();
90+
std::shared_ptr<SwiftASTContext> swift_ast_ctx;
9091
if (auto trts =
9192
ts.dyn_cast_or_null<TypeSystemSwiftTypeRef>()) {
92-
m_type = trts->ReconstructType(m_type);
93-
ts = m_type.GetTypeSystem();
93+
m_type = trts->ReconstructType(m_type, exe_ctx);
94+
swift_ast_ctx = m_type.GetTypeSystem().dyn_cast_or_null<SwiftASTContext>();
95+
if (!swift_ast_ctx)
96+
return;
9497
decl_ts = GetAsEnumDecl(m_type);
9598
enum_decl = decl_ts.first;
9699
if (!enum_decl)
97100
return;
98101
}
99-
auto swift_ts = ts.dyn_cast_or_null<SwiftASTContext>();
100-
if (!swift_ts)
101-
return;
102102

103103
auto iter = enum_decl->enumerator_begin(), end = enum_decl->enumerator_end();
104104
for (; iter != end; ++iter) {
@@ -112,7 +112,8 @@ void lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
112112
case_init_val = case_init_val.zext(64);
113113
if (case_init_val.getBitWidth() > 64)
114114
continue;
115-
ConstString case_name(swift_ts->GetSwiftName(case_decl, *decl_ts.second));
115+
ConstString case_name(
116+
swift_ast_ctx->GetSwiftName(case_decl, *decl_ts.second));
116117
m_cases->push_back({case_init_val, case_name});
117118
}
118119
}
@@ -162,7 +163,10 @@ bool lldb_private::formatters::swift::SwiftOptionSetSummaryProvider::
162163
if (!ReadValueIfAny(*rawValue_sp, value))
163164
return false;
164165

165-
FillCasesIfNeeded();
166+
{
167+
ExecutionContext exe_ctx = valobj->GetExecutionContextRef().Lock(false);
168+
FillCasesIfNeeded(&exe_ctx);
169+
}
166170

167171
StreamString ss;
168172
bool first_match = true;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct SwiftOptionSetSummaryProvider : public TypeSummaryImpl {
4141
const SwiftOptionSetSummaryProvider &
4242
operator=(const SwiftOptionSetSummaryProvider &) = delete;
4343

44-
void FillCasesIfNeeded();
44+
void FillCasesIfNeeded(const ExecutionContext *);
4545

4646
CompilerType m_type;
4747

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,10 @@ llvm::Optional<uint64_t> SwiftLanguageRuntimeImpl::GetMemberVariableOffset(
506506
.GetSwiftValidateTypeSystem()) {
507507
// Convert to an AST type, if necessary.
508508
if (auto ts = instance_type.GetTypeSystem()
509-
.dyn_cast_or_null<TypeSystemSwiftTypeRef>())
510-
instance_type = ts->ReconstructType(instance_type);
509+
.dyn_cast_or_null<TypeSystemSwiftTypeRef>()) {
510+
ExecutionContext exe_ctx = instance->GetExecutionContextRef().Lock(false);
511+
instance_type = ts->ReconstructType(instance_type, &exe_ctx);
512+
}
511513
auto reference = GetMemberVariableOffsetRemoteAST(instance_type, instance,
512514
member_name);
513515
if (reference.has_value() && offset != reference) {

0 commit comments

Comments
 (0)