Skip to content

[lldb] Fallback to implicit modules when explicit modules are missing #8356

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
43 changes: 43 additions & 0 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@
#include "clang/Driver/Driver.h"

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetSelect.h"
Expand Down Expand Up @@ -1594,9 +1596,50 @@ void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string> &source,
}
}

namespace {

bool HasNonexistentExplicitModule(const std::vector<std::string> &args) {
for (const std::string &arg : args) {
StringRef value = arg;
if (!value.consume_front("-fmodule-file="))
continue;
StringRef path = value;
size_t eq = value.find('=');
// The value that follows is in one of two formats:
// 1. ModuleName=ModulePath
// 2. ModulePath
if (eq != std::string::npos)
// The value appears to be in ModuleName=ModulePath forat.
path = value.drop_front(eq + 1);
// Check both path and value. This is to handle paths containing '='.
if (!llvm::sys::fs::exists(path) && !llvm::sys::fs::exists(value)) {
std::string m_description;
HEALTH_LOG_PRINTF("Nonexistent explicit module file %s", arg.data());
return true;
}
}
return false;
}

void RemoveExplicitModules(std::vector<std::string> &args) {
llvm::erase_if(args, [](const std::string &arg) {
if (arg == "-fno-implicit-modules" || arg == "-fno-implicit-module-maps")
return true;
StringRef s = arg;
if (s.starts_with("-fmodule-file=") || s.starts_with("-fmodule-map-file="))
return true;

return false;
});
}

} // namespace

void SwiftASTContext::AddExtraClangArgs(const std::vector<std::string> &ExtraArgs) {
swift::ClangImporterOptions &importer_options = GetClangImporterOptions();
AddExtraClangArgs(ExtraArgs, importer_options.ExtraArgs);
if (HasNonexistentExplicitModule(importer_options.ExtraArgs))
RemoveExplicitModules(importer_options.ExtraArgs);
}

void SwiftASTContext::AddUserClangArgs(TargetProperties &props) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SWIFT_SOURCES := main.swift
SWIFT_ENABLE_EXPLICIT_MODULES := YES
USE_PRIVATE_MODULE_CACHE := YES
include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import shutil
import lldb
from lldbsuite.test.decorators import *
import lldbsuite.test.lldbtest as lldbtest
import lldbsuite.test.lldbutil as lldbutil


class TestCase(lldbtest.TestBase):
@swiftTest
@skipIf(oslist=["linux"], bugnumber="rdar://124691219")
def test_missing_explicit_modules(self):
"""Test missing explicit Swift modules and fallback to implicit modules."""
self.build()

# This test verifies the case where explicit modules are missing.
# Remove explicit modules from their place in the module cache.
mod_cache = self.getBuildArtifact("private-module-cache")
shutil.rmtree(mod_cache)

lldbutil.run_to_source_breakpoint(
self, "Set breakpoint here", lldb.SBFileSpec("main.swift")
)

log = self.getBuildArtifact("types.log")
self.runCmd(f"log enable lldb types -f '{log}'")

self.expect("expression c", substrs=["hello implicit fallback"])

self.filecheck(f"platform shell cat {log}", __file__)
# CHECK: Nonexistent explicit module file
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Class {
var msg : String = "hello implicit fallback"
}

func main() {
let c = Class()
print(c) // Set breakpoint here
}

main()