Skip to content

Implement precise compiler invocations for SwiftASTContext #7758

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
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
1 change: 1 addition & 0 deletions lldb/include/lldb/Core/ModuleList.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ModuleListProperties : public Properties {
SwiftModuleLoadingMode GetSwiftModuleLoadingMode() const;
bool SetSwiftModuleLoadingMode(SwiftModuleLoadingMode);

bool GetUseSwiftPreciseCompilerInvocation() const;
bool GetEnableSwiftMetadataCache() const;
uint64_t GetSwiftMetadataCacheMaxByteSize();
uint64_t GetSwiftMetadataCacheExpirationDays();
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Core/CoreProperties.td
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ let Definition = "modulelist" in {
def SwiftValidateTypeSystem: Property<"swift-validate-typesystem", "Boolean">,
DefaultFalse,
Desc<"Validate all Swift typesystem queries. Used for testing an asserts-enabled LLDB only.">;
def UseSwiftPreciseCompilerInvocation: Property<"swift-precise-compiler-invocation", "Boolean">,
DefaultFalse,
Desc<"In the Swift expression evaluator, create many Swift compiler instances with the precise invocation for the current context, instead of a single compiler instance that merges all flags from the entire project.">;
def SwiftModuleLoadingMode: Property<"swift-module-loading-mode", "Enum">,
DefaultEnumValue<"eSwiftModuleLoadingModePreferSerialized">,
EnumValues<"OptionEnumValues(g_swift_module_loading_mode_enums)">,
Expand Down
6 changes: 6 additions & 0 deletions lldb/source/Core/ModuleList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ bool ModuleListProperties::GetSwiftValidateTypeSystem() const {
idx, g_modulelist_properties[idx].default_uint_value != 0);
}

bool ModuleListProperties::GetUseSwiftPreciseCompilerInvocation() const {
const uint32_t idx = ePropertyUseSwiftPreciseCompilerInvocation;
return GetPropertyAtIndexAs<bool>(
idx, g_modulelist_properties[idx].default_uint_value != 0);
}

bool ModuleListProperties::SetUseSwiftTypeRefTypeSystem(bool new_value) {
const uint32_t idx = ePropertyUseSwiftTypeRefTypeSystem;
return SetPropertyAtIndex(idx, new_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ swift::BraceStmt *SwiftASTManipulatorBase::GetUserBody() {

SwiftASTManipulator::SwiftASTManipulator(
SwiftASTContextForExpressions &swift_ast_ctx,
swift::SourceFile &source_file, bool repl,
swift::SourceFile &source_file, const SymbolContext &sc, bool repl,
lldb::BindGenericTypes bind_generic_types)
: SwiftASTManipulatorBase(source_file, repl, bind_generic_types),
: SwiftASTManipulatorBase(source_file, sc, repl, bind_generic_types),
m_swift_ast_ctx(swift_ast_ctx) {}

void SwiftASTManipulator::FindSpecialNames(
Expand Down Expand Up @@ -889,7 +889,7 @@ llvm::Optional<swift::Type> SwiftASTManipulator::GetSwiftTypeForVariable(
// When injecting a value pack or pack count into the outer
// lldb_expr function, treat it as an opaque raw pointer.
if (m_bind_generic_types == lldb::eDontBind && variable.IsUnboundPack()) {
auto swift_ast_ctx = type_system_swift->GetSwiftASTContext();
auto swift_ast_ctx = type_system_swift->GetSwiftASTContext(&m_sc);
if (swift_ast_ctx) {
auto it = m_type_aliases.find("$__lldb_builtin_ptr_t");
if (it == m_type_aliases.end())
Expand Down
11 changes: 6 additions & 5 deletions lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ class SwiftASTManipulatorBase {
VariableMetadataSP m_metadata;
};

SwiftASTManipulatorBase(swift::SourceFile &source_file, bool repl,
SwiftASTManipulatorBase(swift::SourceFile &source_file,
const SymbolContext &sc, bool repl,
lldb::BindGenericTypes bind_generic_types)
: m_source_file(source_file), m_variables(), m_repl(repl),
: m_source_file(source_file), m_sc(sc), m_repl(repl),
m_bind_generic_types(bind_generic_types) {
DoInitialization();
}
Expand All @@ -168,7 +169,7 @@ class SwiftASTManipulatorBase {
protected:
swift::SourceFile &m_source_file;
llvm::SmallVector<VariableInfo, 1> m_variables;

const SymbolContext &m_sc;
bool m_repl = false;

lldb::BindGenericTypes m_bind_generic_types = lldb::eBindAuto;
Expand Down Expand Up @@ -196,8 +197,8 @@ class SwiftASTManipulatorBase {
class SwiftASTManipulator : public SwiftASTManipulatorBase {
public:
SwiftASTManipulator(SwiftASTContextForExpressions &swift_ast_ctx,
swift::SourceFile &source_file, bool repl,
lldb::BindGenericTypes bind_generic_types);
swift::SourceFile &source_file, const SymbolContext &sc,
bool repl, lldb::BindGenericTypes bind_generic_types);
SwiftASTContextForExpressions &GetScratchContext() { return m_swift_ast_ctx; }

void FindSpecialNames(llvm::SmallVectorImpl<swift::Identifier> &names,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
// If we are extending a generic class it's going to be a metatype,
// and we have to grab the instance type:
imported_self_type = swift_type_system->GetInstanceType(
imported_self_type.GetOpaqueQualType());
imported_self_type.GetOpaqueQualType(), stack_frame_sp.get());
if (!imported_self_type)
return Status(
"Unable to add the aliases the expression needs because the Swift "
Expand Down Expand Up @@ -1416,7 +1416,8 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
std::unique_ptr<SwiftASTManipulator> code_manipulator;
if (repl || !playground) {
code_manipulator = std::make_unique<SwiftASTManipulator>(
swift_ast_context, *source_file, repl, options.GetBindGenericTypes());
swift_ast_context, *source_file, sc, repl,
options.GetBindGenericTypes());

if (!playground) {
code_manipulator->RewriteResult();
Expand Down
3 changes: 1 addition & 2 deletions lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,12 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
llvm::consumeError(type_system_or_err.takeError());
return;
}

auto *swift_ts =
llvm::dyn_cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
type_system_or_err->get());
auto *target_swift_ast =
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
swift_ts->GetSwiftASTContext());
swift_ts->GetSwiftASTContext(nullptr));
m_swift_ast = target_swift_ast;
}
SwiftASTContextForExpressions *swift_ast = m_swift_ast;
Expand Down
17 changes: 11 additions & 6 deletions lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ findSwiftSelf(StackFrame &frame, lldb::VariableSP self_var_sp) {
// 4) If `self` is a metatype, get its instance type.
if (Flags(info.type.GetTypeInfo())
.AllSet(lldb::eTypeIsSwift | lldb::eTypeIsMetatype)) {
info.type = TypeSystemSwift::GetInstanceType(info.type);
info.type = TypeSystemSwift::GetInstanceType(info.type, &frame);
info.is_metatype = true;
}

Expand Down Expand Up @@ -686,17 +686,22 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
if (!frame)
return error("couldn't start parsing - no stack frame");

auto *exe_scope = exe_ctx.GetBestExecutionContextScope();
if (!exe_scope)
return error( "no execution context scope");
ExecutionContextScope *exe_scope =
m_options.GetREPLEnabled() ? static_cast<ExecutionContextScope *>(target)
: static_cast<ExecutionContextScope *>(frame);

exe_scope = exe_ctx.GetBestExecutionContextScope();

m_swift_scratch_ctx = target->GetSwiftScratchContext(m_err, *exe_scope);
if (!m_swift_scratch_ctx)
return error("could not create a Swift scratch context: ",
m_err.AsCString());

m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
m_swift_scratch_ctx->get()->GetSwiftASTContext());
const SymbolContext *sc =
&frame->GetSymbolContext(lldb::eSymbolContextFunction);
auto *swift_ast_ctx = m_swift_scratch_ctx->get()->GetSwiftASTContext(sc);
m_swift_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(swift_ast_ctx);

if (!m_swift_ast_ctx)
return error("could not create a Swift AST context");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ static std::string TranslateObjCNameToSwiftName(std::string className,
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(type_system_or_err->get());
if (!ts)
return "";
auto *ctx = ts->GetSwiftASTContext();
const SymbolContext *sc = nullptr;
if (swiftFrame)
sc = &swiftFrame->GetSymbolContext(eSymbolContextFunction);
auto *ctx = ts->GetSwiftASTContext(sc);
if (!ctx)
return "";
swift::ClangImporter *imp = ctx->GetClangImporter();
Expand Down
148 changes: 81 additions & 67 deletions lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,10 +1326,13 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
target->GetSwiftScratchContext(error, *exe_scope,
create_on_demand);
const SymbolContext *sc = nullptr;
if (auto frame_sp = exe_scope->CalculateStackFrame())
sc = &frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
if (maybe_scratch_ctx)
if (auto scratch_ctx = maybe_scratch_ctx->get())
if (SwiftASTContext *ast_ctx =
scratch_ctx->GetSwiftASTContext()) {
scratch_ctx->GetSwiftASTContext(sc)) {
ConstString cs_input{input};
Mangled mangled(cs_input);
if (mangled.GuessLanguage() == eLanguageTypeSwift) {
Expand Down Expand Up @@ -1366,8 +1369,16 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
if (result_sp && result_sp->GetCompilerType().IsValid()) {
CompilerType result_type(result_sp->GetCompilerType());
if (Flags(result_type.GetTypeInfo())
.AllSet(eTypeIsSwift | eTypeIsMetatype))
result_type = TypeSystemSwift::GetInstanceType(result_type);
.AllSet(eTypeIsSwift | eTypeIsMetatype)) {
result_type = TypeSystemSwift::GetInstanceType(result_type,
exe_scope);
if (auto swift_ast_ctx =
result_type.GetTypeSystem()
.dyn_cast_or_null<SwiftASTContext>())
result_type = swift_ast_ctx->GetTypeSystemSwiftTypeRef()
.GetTypeFromMangledTypename(
result_type.GetMangledTypeName());
}
results.insert(TypeOrDecl(result_type));
}
}
Expand All @@ -1388,73 +1399,76 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
target->GetSwiftScratchContext(error, *exe_scope,
create_on_demand);
if (maybe_scratch_ctx)
if (auto scratch_ctx = maybe_scratch_ctx->get())
if (SwiftASTContext *ast_ctx =
scratch_ctx->GetSwiftASTContext()) {
auto iter = ast_ctx->GetModuleCache().begin(),
end = ast_ctx->GetModuleCache().end();

std::vector<llvm::StringRef> name_parts;
SplitDottedName(input, name_parts);

std::function<void(swift::ModuleDecl *)> lookup_func =
[&ast_ctx, input, name_parts,
&results](swift::ModuleDecl *module) -> void {
for (auto imported_module :
swift::namelookup::getAllImports(module)) {
auto module = imported_module.importedModule;
TypesOrDecls local_results;
ast_ctx->FindTypesOrDecls(input, module, local_results,
false);
llvm::Optional<TypeOrDecl> candidate;
if (local_results.empty() && name_parts.size() > 1) {
size_t idx_of_deeper = 1;
// if you're looking for Swift.Int in module Swift,
// try looking for Int
if (name_parts.front() == module->getName().str()) {
candidate = ast_ctx->FindTypeOrDecl(
name_parts[1].str().c_str(), module);
idx_of_deeper = 2;
}
// this is probably the top-level name of a nested
// type String.UTF8View
else {
candidate = ast_ctx->FindTypeOrDecl(
name_parts[0].str().c_str(), module);
}
if (candidate.has_value()) {
TypesOrDecls candidates{candidate.value()};
for (; idx_of_deeper < name_parts.size();
idx_of_deeper++) {
TypesOrDecls new_candidates;
for (auto candidate : candidates) {
ast_ctx->FindContainedTypeOrDecl(
name_parts[idx_of_deeper], candidate,
new_candidates);
}
candidates = new_candidates;
}
const SymbolContext *sc = nullptr;
if (auto frame_sp = exe_scope->CalculateStackFrame())
sc = &frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);

if (maybe_scratch_ctx)
if (auto scratch_ctx = maybe_scratch_ctx->get())
if (SwiftASTContext *ast_ctx =
scratch_ctx->GetSwiftASTContext(sc)) {
auto iter = ast_ctx->GetModuleCache().begin(),
end = ast_ctx->GetModuleCache().end();

std::vector<llvm::StringRef> name_parts;
SplitDottedName(input, name_parts);

std::function<void(swift::ModuleDecl *)> lookup_func =
[&ast_ctx, input, name_parts,
&results](swift::ModuleDecl *module) -> void {
for (auto imported_module :
swift::namelookup::getAllImports(module)) {
auto module = imported_module.importedModule;
TypesOrDecls local_results;
ast_ctx->FindTypesOrDecls(input, module, local_results,
false);
llvm::Optional<TypeOrDecl> candidate;
if (local_results.empty() && name_parts.size() > 1) {
size_t idx_of_deeper = 1;
// if you're looking for Swift.Int in module Swift,
// try looking for Int
if (name_parts.front() == module->getName().str()) {
candidate = ast_ctx->FindTypeOrDecl(
name_parts[1].str().c_str(), module);
idx_of_deeper = 2;
}
// this is probably the top-level name of a nested
// type String.UTF8View
else {
candidate = ast_ctx->FindTypeOrDecl(
name_parts[0].str().c_str(), module);
}
if (candidate.has_value()) {
TypesOrDecls candidates{candidate.value()};
for (; idx_of_deeper < name_parts.size();
idx_of_deeper++) {
TypesOrDecls new_candidates;
for (auto candidate : candidates) {
if (candidate)
results.insert(candidate);
ast_ctx->FindContainedTypeOrDecl(
name_parts[idx_of_deeper], candidate,
new_candidates);
}
candidates = new_candidates;
}
} else if (local_results.size() > 0) {
for (const auto &result : local_results)
results.insert(result);
} else if (local_results.empty() && module &&
name_parts.size() == 1 &&
name_parts.front() ==
module->getName().str())
results.insert(
ToCompilerType(swift::ModuleType::get(module)));
}
};

for (; iter != end; iter++)
lookup_func(iter->second);
}
for (auto candidate : candidates) {
if (candidate)
results.insert(candidate);
}
}
} else if (local_results.size() > 0) {
for (const auto &result : local_results)
results.insert(result);
} else if (local_results.empty() && module &&
name_parts.size() == 1 &&
name_parts.front() == module->getName().str())
results.insert(
ToCompilerType(swift::ModuleType::get(module)));
}
};

for (; iter != end; iter++)
lookup_func(iter->second);
}
}

return (results.size() - before);
Expand Down
5 changes: 3 additions & 2 deletions lldb/source/Plugins/Language/Swift/SwiftMetatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ bool lldb_private::formatters::swift::SwiftMetatype_SummaryProvider(
lldb::addr_t metadata_ptr = valobj.GetPointerValue();
if (metadata_ptr == LLDB_INVALID_ADDRESS || metadata_ptr == 0) {
CompilerType compiler_metatype_type(valobj.GetCompilerType());
CompilerType instancetype =
TypeSystemSwift::GetInstanceType(compiler_metatype_type);
ExecutionContext exe_ctx = valobj.GetExecutionContextRef();
CompilerType instancetype = TypeSystemSwift::GetInstanceType(
compiler_metatype_type, exe_ctx.GetBestExecutionContextScope());
name = instancetype.GetDisplayTypeName();
} else if (CompilerType meta_type = valobj.GetCompilerType()) {
name = meta_type.GetDisplayTypeName();
Expand Down
Loading