Files
swift-mirror/lib/AST/ModuleLoader.cpp
Artem Chikin 12477b7b79 [Dependency Scanning] Refactor the scanner to resolve unqualified module imports
This changes the scanner's behavior to "resolve" a discovered module's dependencies to a set of Module IDs: module name + module kind (swift textual, swift binary, clang, etc.).

The 'ModuleDependencyInfo' objects that are stored in the dependency scanner's cache now carry a set of kind-qualified ModuleIDs for their dependencies, in addition to unqualified imported module names of their dependencies.

Previously, the scanner's internal state would cache a module dependnecy as having its own set of dependencies which were stored as names of imported modules. This led to a design where any time we needed to process the dependency downstream from its discovery (e.g. cycle detection, graph construction), we had to query the ASTContext to resolve this dependency's imports, which shouldn't be necessary. Now, upon discovery, we "resolve" a discovered dependency by executing a lookup for each of its imported module names (this operation happens regardless of this patch) and store a fully-resolved set of dependencies in the dependency module info.

Moreover, looking up a given module dependency by name (via `ASTContext`'s `getModuleDependencies`) would result in iterating over the scanner's module "loaders" and querying each for the module name. The corresponding modules would then check the scanner's cache for a respective discovered module, and if no such module is found the "loader" would search the filesystem.

This meant that in practice, we searched the filesystem on many occasions where we actually had cached the required dependency, as follows:
Suppose we had previously discovered a Clang module "foo" and cached its dependency info.
-> ASTContext.getModuleDependencies("foo")
--> (1) Swift Module "Loader" checks caches for a Swift module "foo" and doesn't find one, so it searches the filesystem for "foo" and fails to find one.
--> (2) Clang Module "Loader" checks caches for a Clang module "foo", finds one and returns it to the client.

This means that we were always searching the filesystem in (1) even if we knew that to be futile.
With this change, queries to `ASTContext`'s `getModuleDependencies` will always check all the caches first, and only delegate to the scanner "loaders" if no cached dependency is found. The loaders are then no longer in the business of checking the cached contents.

To handle cases in the scanner where we must only lookup either a Swift-only module or a Clang-only module, this patch splits 'getModuleDependencies' into an alrady-existing 'getSwiftModuleDependencies' and a newly-added 'getClangModuleDependencies'.
2023-01-05 11:44:06 -08:00

236 lines
8.8 KiB
C++

//===--- ModuleLoader.cpp - Swift Language Module Implementation ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the ModuleLoader class and/or any helpers.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticsCommon.h"
#include "swift/AST/FileUnit.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/ModuleDependencies.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/SourceManager.h"
#include "clang/Frontend/Utils.h"
#include "swift/ClangImporter/ClangImporter.h"
namespace llvm {
class FileCollectorBase;
}
namespace swift {
DependencyTracker::DependencyTracker(
IntermoduleDepTrackingMode Mode,
std::shared_ptr<llvm::FileCollectorBase> FileCollector)
// NB: The ClangImporter believes it's responsible for the construction of
// this instance, and it static_cast<>s the instance pointer to its own
// subclass based on that belief. If you change this to be some other
// instance, you will need to change ClangImporter's code to handle the
// difference.
: clangCollector(
ClangImporter::createDependencyCollector(Mode, FileCollector)) {}
void
DependencyTracker::addDependency(StringRef File, bool IsSystem) {
// DependencyTracker exposes an interface that (intentionally) does not talk
// about clang at all, nor about missing deps. It does expose an IsSystem
// dimension, which we accept and pass along to the clang DependencyCollector.
clangCollector->maybeAddDependency(File, /*FromModule=*/false,
IsSystem, /*IsModuleFile=*/false,
/*IsMissing=*/false);
}
void DependencyTracker::addIncrementalDependency(StringRef File,
Fingerprint FP) {
if (incrementalDepsUniquer.insert(File).second) {
incrementalDeps.emplace_back(File.str(), FP);
}
}
ArrayRef<std::string>
DependencyTracker::getDependencies() const {
return clangCollector->getDependencies();
}
ArrayRef<DependencyTracker::IncrementalDependency>
DependencyTracker::getIncrementalDependencies() const {
return incrementalDeps;
}
std::shared_ptr<clang::DependencyCollector>
DependencyTracker::getClangCollector() {
return clangCollector;
}
static bool findOverlayFilesInDirectory(ASTContext &ctx, StringRef path,
StringRef moduleName,
SourceLoc diagLoc,
llvm::function_ref<void(StringRef)> callback) {
using namespace llvm::sys;
using namespace file_types;
auto fs = ctx.SourceMgr.getFileSystem();
std::error_code error;
for (auto dir = fs->dir_begin(path, error);
!error && dir != llvm::vfs::directory_iterator();
dir.increment(error)) {
StringRef file = dir->path();
if (lookupTypeForExtension(path::extension(file)) != TY_SwiftOverlayFile)
continue;
callback(file);
}
if (error && error != std::errc::no_such_file_or_directory) {
ctx.Diags.diagnose(diagLoc, diag::cannot_list_swiftcrossimport_dir,
moduleName, error.message(), path);
}
return !error;
}
static void findOverlayFilesInternal(ASTContext &ctx, StringRef moduleDefiningPath,
StringRef moduleName,
SourceLoc diagLoc,
llvm::function_ref<void(StringRef)> callback) {
using namespace llvm::sys;
using namespace file_types;
auto &langOpts = ctx.LangOpts;
// This method constructs several paths to directories near the module and
// scans them for .swiftoverlay files. These paths can be in various
// directories and have a few different filenames at the end, but I'll
// illustrate the path transformations by showing examples for a module
// defined by a swiftinterface at:
//
// /usr/lib/swift/FooKit.swiftmodule/x86_64-apple-macos.swiftinterface
// dirPath = /usr/lib/swift/FooKit.swiftmodule
SmallString<64> dirPath{moduleDefiningPath};
// dirPath = /usr/lib/swift/
path::remove_filename(dirPath);
// dirPath = /usr/lib/swift/FooKit.swiftcrossimport
path::append(dirPath, moduleName);
path::replace_extension(dirPath, getExtension(TY_SwiftCrossImportDir));
// Search for swiftoverlays that apply to all platforms.
if (!findOverlayFilesInDirectory(ctx, dirPath, moduleName, diagLoc, callback))
// If we diagnosed an error, or we didn't find the directory at all, don't
// bother trying the target-specific directories.
return;
// dirPath = /usr/lib/swift/FooKit.swiftcrossimport/x86_64-apple-macos
auto moduleTriple = getTargetSpecificModuleTriple(langOpts.Target);
path::append(dirPath, moduleTriple.str());
// Search for swiftoverlays specific to the target triple's platform.
findOverlayFilesInDirectory(ctx, dirPath, moduleName, diagLoc, callback);
// The rest of this handles target variant triples, which are only used for
// certain MacCatalyst builds.
if (!langOpts.TargetVariant)
return;
// dirPath = /usr/lib/swift/FooKit.swiftcrossimport/x86_64-apple-ios-macabi
path::remove_filename(dirPath);
auto moduleVariantTriple =
getTargetSpecificModuleTriple(*langOpts.TargetVariant);
path::append(dirPath, moduleVariantTriple.str());
// Search for swiftoverlays specific to the target variant's platform.
findOverlayFilesInDirectory(ctx, dirPath, moduleName, diagLoc, callback);
}
void ModuleLoader::findOverlayFiles(SourceLoc diagLoc, ModuleDecl *module,
FileUnit *file) {
using namespace llvm::sys;
using namespace file_types;
if (file->getModuleDefiningPath().empty())
return;
findOverlayFilesInternal(module->getASTContext(),
file->getModuleDefiningPath(),
module->getName().str(),
diagLoc,
[&](StringRef file) {
module->addCrossImportOverlayFile(file);
// FIXME: Better to add it only if we load it.
if (dependencyTracker)
dependencyTracker->addDependency(file, module->isSystemModule());
});
}
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>>
ModuleDependencyInfo::collectCrossImportOverlayNames(ASTContext &ctx,
StringRef moduleName) const {
using namespace llvm::sys;
using namespace file_types;
Optional<std::string> modulePath;
// A map from secondary module name to a vector of overlay names.
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>> result;
switch (getKind()) {
case swift::ModuleDependencyKind::SwiftInterface: {
auto *swiftDep = getAsSwiftInterfaceModule();
// Prefer interface path to binary module path if we have it.
modulePath = swiftDep->swiftInterfaceFile;
assert(modulePath.has_value());
StringRef parentDir = llvm::sys::path::parent_path(*modulePath);
if (llvm::sys::path::extension(parentDir) == ".swiftmodule") {
modulePath = parentDir.str();
}
break;
}
case swift::ModuleDependencyKind::SwiftBinary: {
auto *swiftBinaryDep = getAsSwiftBinaryModule();
modulePath = swiftBinaryDep->compiledModulePath;
assert(modulePath.has_value());
StringRef parentDir = llvm::sys::path::parent_path(*modulePath);
if (llvm::sys::path::extension(parentDir) == ".swiftmodule") {
modulePath = parentDir.str();
}
break;
}
case swift::ModuleDependencyKind::Clang: {
auto *clangDep = getAsClangModule();
modulePath = clangDep->moduleMapFile;
assert(modulePath.has_value());
break;
}
case swift::ModuleDependencyKind::SwiftSource: {
auto *swiftSourceDep = getAsSwiftSourceModule();
assert(!swiftSourceDep->sourceFiles.empty());
return result;
}
case swift::ModuleDependencyKind::SwiftPlaceholder: {
return result;
}
case swift::ModuleDependencyKind::LastKind:
llvm_unreachable("Unhandled dependency kind.");
}
// Mimic getModuleDefiningPath() for Swift and Clang module.
findOverlayFilesInternal(ctx, *modulePath, moduleName, SourceLoc(),
[&](StringRef file) {
StringRef bystandingModule;
auto overlayNames =
ModuleDecl::collectCrossImportOverlay(ctx, file, moduleName,
bystandingModule);
result[bystandingModule] = std::move(overlayNames);
});
return result;
}
} // namespace swift