Skip to content

Commit 487436d

Browse files
Merge pull request #7758 from adrian-prantl/precise-compiler-invocations
Implement precise compiler invocations for SwiftASTContext
2 parents 0bc383a + f1a7160 commit 487436d

39 files changed

+938
-362
lines changed

lldb/include/lldb/Core/ModuleList.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class ModuleListProperties : public Properties {
6666
SwiftModuleLoadingMode GetSwiftModuleLoadingMode() const;
6767
bool SetSwiftModuleLoadingMode(SwiftModuleLoadingMode);
6868

69+
bool GetUseSwiftPreciseCompilerInvocation() const;
6970
bool GetEnableSwiftMetadataCache() const;
7071
uint64_t GetSwiftMetadataCacheMaxByteSize();
7172
uint64_t GetSwiftMetadataCacheExpirationDays();

lldb/source/Core/CoreProperties.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ let Definition = "modulelist" in {
2626
def SwiftValidateTypeSystem: Property<"swift-validate-typesystem", "Boolean">,
2727
DefaultFalse,
2828
Desc<"Validate all Swift typesystem queries. Used for testing an asserts-enabled LLDB only.">;
29+
def UseSwiftPreciseCompilerInvocation: Property<"swift-precise-compiler-invocation", "Boolean">,
30+
DefaultFalse,
31+
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.">;
2932
def SwiftModuleLoadingMode: Property<"swift-module-loading-mode", "Enum">,
3033
DefaultEnumValue<"eSwiftModuleLoadingModePreferSerialized">,
3134
EnumValues<"OptionEnumValues(g_swift_module_loading_mode_enums)">,

lldb/source/Core/ModuleList.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ bool ModuleListProperties::GetSwiftValidateTypeSystem() const {
183183
idx, g_modulelist_properties[idx].default_uint_value != 0);
184184
}
185185

186+
bool ModuleListProperties::GetUseSwiftPreciseCompilerInvocation() const {
187+
const uint32_t idx = ePropertyUseSwiftPreciseCompilerInvocation;
188+
return GetPropertyAtIndexAs<bool>(
189+
idx, g_modulelist_properties[idx].default_uint_value != 0);
190+
}
191+
186192
bool ModuleListProperties::SetUseSwiftTypeRefTypeSystem(bool new_value) {
187193
const uint32_t idx = ePropertyUseSwiftTypeRefTypeSystem;
188194
return SetPropertyAtIndex(idx, new_value);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ swift::BraceStmt *SwiftASTManipulatorBase::GetUserBody() {
191191

192192
SwiftASTManipulator::SwiftASTManipulator(
193193
SwiftASTContextForExpressions &swift_ast_ctx,
194-
swift::SourceFile &source_file, bool repl,
194+
swift::SourceFile &source_file, const SymbolContext &sc, bool repl,
195195
lldb::BindGenericTypes bind_generic_types)
196-
: SwiftASTManipulatorBase(source_file, repl, bind_generic_types),
196+
: SwiftASTManipulatorBase(source_file, sc, repl, bind_generic_types),
197197
m_swift_ast_ctx(swift_ast_ctx) {}
198198

199199
void SwiftASTManipulator::FindSpecialNames(
@@ -889,7 +889,7 @@ llvm::Optional<swift::Type> SwiftASTManipulator::GetSwiftTypeForVariable(
889889
// When injecting a value pack or pack count into the outer
890890
// lldb_expr function, treat it as an opaque raw pointer.
891891
if (m_bind_generic_types == lldb::eDontBind && variable.IsUnboundPack()) {
892-
auto swift_ast_ctx = type_system_swift->GetSwiftASTContext();
892+
auto swift_ast_ctx = type_system_swift->GetSwiftASTContext(&m_sc);
893893
if (swift_ast_ctx) {
894894
auto it = m_type_aliases.find("$__lldb_builtin_ptr_t");
895895
if (it == m_type_aliases.end())

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ class SwiftASTManipulatorBase {
146146
VariableMetadataSP m_metadata;
147147
};
148148

149-
SwiftASTManipulatorBase(swift::SourceFile &source_file, bool repl,
149+
SwiftASTManipulatorBase(swift::SourceFile &source_file,
150+
const SymbolContext &sc, bool repl,
150151
lldb::BindGenericTypes bind_generic_types)
151-
: m_source_file(source_file), m_variables(), m_repl(repl),
152+
: m_source_file(source_file), m_sc(sc), m_repl(repl),
152153
m_bind_generic_types(bind_generic_types) {
153154
DoInitialization();
154155
}
@@ -168,7 +169,7 @@ class SwiftASTManipulatorBase {
168169
protected:
169170
swift::SourceFile &m_source_file;
170171
llvm::SmallVector<VariableInfo, 1> m_variables;
171-
172+
const SymbolContext &m_sc;
172173
bool m_repl = false;
173174

174175
lldb::BindGenericTypes m_bind_generic_types = lldb::eBindAuto;
@@ -196,8 +197,8 @@ class SwiftASTManipulatorBase {
196197
class SwiftASTManipulator : public SwiftASTManipulatorBase {
197198
public:
198199
SwiftASTManipulator(SwiftASTContextForExpressions &swift_ast_ctx,
199-
swift::SourceFile &source_file, bool repl,
200-
lldb::BindGenericTypes bind_generic_types);
200+
swift::SourceFile &source_file, const SymbolContext &sc,
201+
bool repl, lldb::BindGenericTypes bind_generic_types);
201202
SwiftASTContextForExpressions &GetScratchContext() { return m_swift_ast_ctx; }
202203

203204
void FindSpecialNames(llvm::SmallVectorImpl<swift::Identifier> &names,

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

Lines changed: 3 additions & 2 deletions
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 "
@@ -1416,7 +1416,8 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
14161416
std::unique_ptr<SwiftASTManipulator> code_manipulator;
14171417
if (repl || !playground) {
14181418
code_manipulator = std::make_unique<SwiftASTManipulator>(
1419-
swift_ast_context, *source_file, repl, options.GetBindGenericTypes());
1419+
swift_ast_context, *source_file, sc, repl,
1420+
options.GetBindGenericTypes());
14201421

14211422
if (!playground) {
14221423
code_manipulator->RewriteResult();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,13 +563,12 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
563563
llvm::consumeError(type_system_or_err.takeError());
564564
return;
565565
}
566-
567566
auto *swift_ts =
568567
llvm::dyn_cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
569568
type_system_or_err->get());
570569
auto *target_swift_ast =
571570
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
572-
swift_ts->GetSwiftASTContext());
571+
swift_ts->GetSwiftASTContext(nullptr));
573572
m_swift_ast = target_swift_ast;
574573
}
575574
SwiftASTContextForExpressions *swift_ast = m_swift_ast;

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

Lines changed: 11 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,17 +686,22 @@ 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)
695697
return error("could not create a Swift scratch context: ",
696698
m_err.AsCString());
697699

698-
m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
699-
m_swift_scratch_ctx->get()->GetSwiftASTContext());
700+
const SymbolContext *sc =
701+
&frame->GetSymbolContext(lldb::eSymbolContextFunction);
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);
700705

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

lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ static std::string TranslateObjCNameToSwiftName(std::string className,
9999
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(type_system_or_err->get());
100100
if (!ts)
101101
return "";
102-
auto *ctx = ts->GetSwiftASTContext();
102+
const SymbolContext *sc = nullptr;
103+
if (swiftFrame)
104+
sc = &swiftFrame->GetSymbolContext(eSymbolContextFunction);
105+
auto *ctx = ts->GetSwiftASTContext(sc);
103106
if (!ctx)
104107
return "";
105108
swift::ClangImporter *imp = ctx->GetClangImporter();

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

Lines changed: 81 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,13 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
13261326
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
13271327
target->GetSwiftScratchContext(error, *exe_scope,
13281328
create_on_demand);
1329+
const SymbolContext *sc = nullptr;
1330+
if (auto frame_sp = exe_scope->CalculateStackFrame())
1331+
sc = &frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
13291332
if (maybe_scratch_ctx)
13301333
if (auto scratch_ctx = maybe_scratch_ctx->get())
13311334
if (SwiftASTContext *ast_ctx =
1332-
scratch_ctx->GetSwiftASTContext()) {
1335+
scratch_ctx->GetSwiftASTContext(sc)) {
13331336
ConstString cs_input{input};
13341337
Mangled mangled(cs_input);
13351338
if (mangled.GuessLanguage() == eLanguageTypeSwift) {
@@ -1366,8 +1369,16 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
13661369
if (result_sp && result_sp->GetCompilerType().IsValid()) {
13671370
CompilerType result_type(result_sp->GetCompilerType());
13681371
if (Flags(result_type.GetTypeInfo())
1369-
.AllSet(eTypeIsSwift | eTypeIsMetatype))
1370-
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+
}
13711382
results.insert(TypeOrDecl(result_type));
13721383
}
13731384
}
@@ -1388,73 +1399,76 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
13881399
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
13891400
target->GetSwiftScratchContext(error, *exe_scope,
13901401
create_on_demand);
1391-
if (maybe_scratch_ctx)
1392-
if (auto scratch_ctx = maybe_scratch_ctx->get())
1393-
if (SwiftASTContext *ast_ctx =
1394-
scratch_ctx->GetSwiftASTContext()) {
1395-
auto iter = ast_ctx->GetModuleCache().begin(),
1396-
end = ast_ctx->GetModuleCache().end();
1397-
1398-
std::vector<llvm::StringRef> name_parts;
1399-
SplitDottedName(input, name_parts);
1400-
1401-
std::function<void(swift::ModuleDecl *)> lookup_func =
1402-
[&ast_ctx, input, name_parts,
1403-
&results](swift::ModuleDecl *module) -> void {
1404-
for (auto imported_module :
1405-
swift::namelookup::getAllImports(module)) {
1406-
auto module = imported_module.importedModule;
1407-
TypesOrDecls local_results;
1408-
ast_ctx->FindTypesOrDecls(input, module, local_results,
1409-
false);
1410-
llvm::Optional<TypeOrDecl> candidate;
1411-
if (local_results.empty() && name_parts.size() > 1) {
1412-
size_t idx_of_deeper = 1;
1413-
// if you're looking for Swift.Int in module Swift,
1414-
// try looking for Int
1415-
if (name_parts.front() == module->getName().str()) {
1416-
candidate = ast_ctx->FindTypeOrDecl(
1417-
name_parts[1].str().c_str(), module);
1418-
idx_of_deeper = 2;
1419-
}
1420-
// this is probably the top-level name of a nested
1421-
// type String.UTF8View
1422-
else {
1423-
candidate = ast_ctx->FindTypeOrDecl(
1424-
name_parts[0].str().c_str(), module);
1425-
}
1426-
if (candidate.has_value()) {
1427-
TypesOrDecls candidates{candidate.value()};
1428-
for (; idx_of_deeper < name_parts.size();
1429-
idx_of_deeper++) {
1430-
TypesOrDecls new_candidates;
1431-
for (auto candidate : candidates) {
1432-
ast_ctx->FindContainedTypeOrDecl(
1433-
name_parts[idx_of_deeper], candidate,
1434-
new_candidates);
1435-
}
1436-
candidates = new_candidates;
1437-
}
1402+
const SymbolContext *sc = nullptr;
1403+
if (auto frame_sp = exe_scope->CalculateStackFrame())
1404+
sc = &frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
1405+
1406+
if (maybe_scratch_ctx)
1407+
if (auto scratch_ctx = maybe_scratch_ctx->get())
1408+
if (SwiftASTContext *ast_ctx =
1409+
scratch_ctx->GetSwiftASTContext(sc)) {
1410+
auto iter = ast_ctx->GetModuleCache().begin(),
1411+
end = ast_ctx->GetModuleCache().end();
1412+
1413+
std::vector<llvm::StringRef> name_parts;
1414+
SplitDottedName(input, name_parts);
1415+
1416+
std::function<void(swift::ModuleDecl *)> lookup_func =
1417+
[&ast_ctx, input, name_parts,
1418+
&results](swift::ModuleDecl *module) -> void {
1419+
for (auto imported_module :
1420+
swift::namelookup::getAllImports(module)) {
1421+
auto module = imported_module.importedModule;
1422+
TypesOrDecls local_results;
1423+
ast_ctx->FindTypesOrDecls(input, module, local_results,
1424+
false);
1425+
llvm::Optional<TypeOrDecl> candidate;
1426+
if (local_results.empty() && name_parts.size() > 1) {
1427+
size_t idx_of_deeper = 1;
1428+
// if you're looking for Swift.Int in module Swift,
1429+
// try looking for Int
1430+
if (name_parts.front() == module->getName().str()) {
1431+
candidate = ast_ctx->FindTypeOrDecl(
1432+
name_parts[1].str().c_str(), module);
1433+
idx_of_deeper = 2;
1434+
}
1435+
// this is probably the top-level name of a nested
1436+
// type String.UTF8View
1437+
else {
1438+
candidate = ast_ctx->FindTypeOrDecl(
1439+
name_parts[0].str().c_str(), module);
1440+
}
1441+
if (candidate.has_value()) {
1442+
TypesOrDecls candidates{candidate.value()};
1443+
for (; idx_of_deeper < name_parts.size();
1444+
idx_of_deeper++) {
1445+
TypesOrDecls new_candidates;
14381446
for (auto candidate : candidates) {
1439-
if (candidate)
1440-
results.insert(candidate);
1447+
ast_ctx->FindContainedTypeOrDecl(
1448+
name_parts[idx_of_deeper], candidate,
1449+
new_candidates);
14411450
}
1451+
candidates = new_candidates;
14421452
}
1443-
} else if (local_results.size() > 0) {
1444-
for (const auto &result : local_results)
1445-
results.insert(result);
1446-
} else if (local_results.empty() && module &&
1447-
name_parts.size() == 1 &&
1448-
name_parts.front() ==
1449-
module->getName().str())
1450-
results.insert(
1451-
ToCompilerType(swift::ModuleType::get(module)));
1452-
}
1453-
};
1454-
1455-
for (; iter != end; iter++)
1456-
lookup_func(iter->second);
1457-
}
1453+
for (auto candidate : candidates) {
1454+
if (candidate)
1455+
results.insert(candidate);
1456+
}
1457+
}
1458+
} else if (local_results.size() > 0) {
1459+
for (const auto &result : local_results)
1460+
results.insert(result);
1461+
} else if (local_results.empty() && module &&
1462+
name_parts.size() == 1 &&
1463+
name_parts.front() == module->getName().str())
1464+
results.insert(
1465+
ToCompilerType(swift::ModuleType::get(module)));
1466+
}
1467+
};
1468+
1469+
for (; iter != end; iter++)
1470+
lookup_func(iter->second);
1471+
}
14581472
}
14591473

14601474
return (results.size() - before);

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();

0 commit comments

Comments
 (0)