Skip to content

Commit 6a47a82

Browse files
Merge pull request #12011 from adrian-prantl/166574409
[LLDB] Disable implicit modules when importting current CU dependencies
2 parents e3ed56a + 7309a6a commit 6a47a82

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,9 +1859,11 @@ void SwiftASTContext::AddExtraClangArgs(
18591859
swift::ClangImporterOptions &importer_options = GetClangImporterOptions();
18601860
auto defer = llvm::make_scope_exit([&]() {
18611861
// Detect explicitly-built modules.
1862-
m_has_explicit_modules =
1863-
llvm::any_of(importer_options.ExtraArgs, [](const std::string &arg) {
1864-
return StringRef(arg).starts_with("-fmodule-file=");
1862+
m_has_explicit_modules |=
1863+
llvm::any_of(importer_options.ExtraArgs, [](const std::string &s) {
1864+
StringRef arg(s);
1865+
return arg.starts_with("-fno-implicit-module") ||
1866+
arg.starts_with("-fmodule-file=");
18651867
});
18661868
ConfigureModuleValidation(importer_options.ExtraArgs);
18671869
});
@@ -3891,8 +3893,11 @@ ThreadSafeASTContext SwiftASTContext::GetASTContext() {
38913893
*static_cast<swift::ModuleInterfaceCheckerImpl *>(
38923894
m_ast_context_up->getModuleInterfaceChecker()),
38933895
m_dependency_tracker.get(), loading_mode));
3894-
if (module_interface_loader_up)
3896+
if (module_interface_loader_up) {
3897+
m_module_interface_loader = static_cast<swift::ModuleInterfaceLoader *>(
3898+
module_interface_loader_up.get());
38953899
m_ast_context_up->addModuleLoader(std::move(module_interface_loader_up));
3900+
}
38963901
}
38973902

38983903
// 4. Create and install the serialized module loader.
@@ -9413,6 +9418,42 @@ bool SwiftASTContext::GetCompileUnitImportsImpl(
94139418
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
94149419
*modules,
94159420
Status &error) {
9421+
// If EBM is enabled, disable implicit modules during contextual imports.
9422+
// fixme nullptr!
9423+
bool turn_off_implicit = m_has_explicit_modules;
9424+
auto reset = llvm::make_scope_exit([&] {
9425+
if (turn_off_implicit) {
9426+
LOG_PRINTF(GetLog(LLDBLog::Types), "Turning on implicit modules");
9427+
if (m_module_interface_loader) {
9428+
auto &opts = m_module_interface_loader->getOptions();
9429+
opts.disableImplicitSwiftModule = false;
9430+
opts.disableBuildingInterface = false;
9431+
}
9432+
if (m_clangimporter) {
9433+
auto &clang_instance = const_cast<clang::CompilerInstance &>(
9434+
m_clangimporter->getClangInstance());
9435+
clang_instance.getLangOpts().ImplicitModules = true;
9436+
}
9437+
}
9438+
});
9439+
if (turn_off_implicit) {
9440+
LOG_PRINTF(GetLog(LLDBLog::Types), "Turning off implicit modules");
9441+
// Swift.
9442+
if (m_module_interface_loader) {
9443+
auto &opts = m_module_interface_loader->getOptions();
9444+
opts.disableImplicitSwiftModule = true;
9445+
opts.disableBuildingInterface = true;
9446+
}
9447+
// Clang.
9448+
if (m_clangimporter) {
9449+
auto &clang_instance = const_cast<clang::CompilerInstance &>(
9450+
m_clangimporter->getClangInstance());
9451+
// AddExtraArgs is supposed to always turn implicit modules on.
9452+
assert(clang_instance.getLangOpts().ImplicitModules &&
9453+
"ClangImporter implicit module support is off");
9454+
clang_instance.getLangOpts().ImplicitModules = false;
9455+
}
9456+
}
94169457

94179458
CompileUnit *compile_unit = sc.comp_unit;
94189459
if (compile_unit && compile_unit->GetModule())

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class SourceFile;
5252
class CASOptions;
5353
struct PrintOptions;
5454
class MemoryBufferSerializedModuleLoader;
55+
class ModuleInterfaceLoader;
5556
namespace Demangle {
5657
class Demangler;
5758
class Node;
@@ -976,6 +977,7 @@ class SwiftASTContext : public TypeSystemSwift {
976977
swift::MemoryBufferSerializedModuleLoader *m_memory_buffer_module_loader =
977978
nullptr;
978979
swift::ModuleLoader *m_explicit_swift_module_loader = nullptr;
980+
swift::ModuleInterfaceLoader *m_module_interface_loader = nullptr;
979981
swift::ClangImporter *m_clangimporter = nullptr;
980982
/// Wraps the clang::ASTContext owned by ClangImporter.
981983
std::shared_ptr<TypeSystemClang> m_clangimporter_typesystem;

lldb/test/API/lang/swift/explicit_modules/implicit_fallback/TestSwiftExplicitModulesImplicitFallback.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,29 @@ def test_missing_explicit_modules(self):
2727

2828
self.filecheck(f"platform shell cat {log}", __file__)
2929
# CHECK: Nonexistent explicit module file
30+
# CHECK: Explicit modules : false
31+
32+
@swiftTest
33+
def test_sanity(self):
34+
"""Check the normal behavior."""
35+
self.build()
36+
37+
# This test verifies the case where explicit modules are missing.
38+
# Remove explicit modules from their place in the module cache.
39+
mod_cache = self.getBuildArtifact("private-module-cache")
40+
41+
lldbutil.run_to_source_breakpoint(
42+
self, "Set breakpoint here", lldb.SBFileSpec("main.swift")
43+
)
44+
45+
log = self.getBuildArtifact("types.log")
46+
self.runCmd(f"log enable lldb types -f '{log}'")
47+
48+
self.expect("expression c")
49+
self.expect("expression -- import Foundation")
50+
51+
self.filecheck(f"platform shell cat {log}", __file__,
52+
'--check-prefix=CHECK-SANITY')
53+
# CHECK-SANITY: Explicit modules : true
54+
# CHECK-SANITY: Turning off implicit modules
55+
# CHECK-SANITY: Turning on implicit modules

0 commit comments

Comments
 (0)