Files
swift-mirror/lib/DependencyScan/DependencyScanningTool.cpp
Artem Chikin 9429136112 [Dependency Scanning] Split the ModuleDependencyCache into two: current scan cache & global.
This change causes the cache to be layered with a local "cache" that wraps the global cache, which will serve as the source of truth. The local cache persists only for the duration of a given scanning action, and has a store of references to dependencies resolved as a part of the current scanning action only, while the global cache is the one that persists across scanning actions (e.g. in `DependencyScanningTool`) and stores actual module dependency info values.

Only the local cache can answer dependency lookup queries, checking current scanning action results first, before falling back to querying the global cache, with queries disambiguated by the current scannning action's search paths, ensuring we never resolve a dependency lookup query with a module info that could not be found in the current action's search paths.

This change is required because search-path disambiguation can lead to false-negatives: for example, the Clang dependency scanner may find modules relative to the compiler's path that are not on the compiler's direct search paths. While such false-negative query responses should be functionally safe, we rely on the current scanning action's results being always-present-in-the-cache for the scanner's functionality. This layering ensures that the cache use-sites remain unchanged and that we get both: preserved global state which can be queried disambiguated with the search path details, and an always-consistent local (current action) cache state.
2021-08-06 09:13:42 -07:00

162 lines
5.7 KiB
C++

//===------------ DependencyScanningTool.cpp - Swift Compiler -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//
#include "swift/DependencyScan/DependencyScanningTool.h"
#include "swift/DependencyScan/SerializedModuleDependencyCacheFormat.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/LLVMInitialize.h"
#include "swift/DependencyScan/DependencyScanImpl.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include <sstream>
namespace swift {
namespace dependencies {
DependencyScanningTool::DependencyScanningTool()
: SharedCache(std::make_unique<GlobalModuleDependenciesCache>()),
VersionedPCMInstanceCacheCache(
std::make_unique<CompilerArgInstanceCacheMap>()),
PDC(), Alloc(), Saver(Alloc) {}
llvm::ErrorOr<swiftscan_dependency_graph_t>
DependencyScanningTool::getDependencies(
ArrayRef<const char *> Command,
const llvm::StringSet<> &PlaceholderModules) {
// The primary instance used to scan the query Swift source-code
auto InstanceOrErr = initCompilerInstanceForScan(Command);
if (std::error_code EC = InstanceOrErr.getError())
return EC;
auto Instance = std::move(*InstanceOrErr);
// Local scan cache instance, wrapping the shared global cache.
ModuleDependenciesCache cache(*SharedCache);
// Execute the scanning action, retreiving the in-memory result
auto DependenciesOrErr = performModuleScan(*Instance.get(), cache);
if (DependenciesOrErr.getError())
return std::make_error_code(std::errc::not_supported);
auto Dependencies = std::move(*DependenciesOrErr);
return Dependencies;
}
llvm::ErrorOr<swiftscan_import_set_t>
DependencyScanningTool::getImports(ArrayRef<const char *> Command) {
// The primary instance used to scan the query Swift source-code
auto InstanceOrErr = initCompilerInstanceForScan(Command);
if (std::error_code EC = InstanceOrErr.getError())
return EC;
auto Instance = std::move(*InstanceOrErr);
// Execute the scanning action, retreiving the in-memory result
auto DependenciesOrErr = performModulePrescan(*Instance.get());
if (DependenciesOrErr.getError())
return std::make_error_code(std::errc::not_supported);
auto Dependencies = std::move(*DependenciesOrErr);
return Dependencies;
}
std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t>>
DependencyScanningTool::getDependencies(
ArrayRef<const char *> Command,
const std::vector<BatchScanInput> &BatchInput,
const llvm::StringSet<> &PlaceholderModules) {
// The primary instance used to scan Swift modules
auto InstanceOrErr = initCompilerInstanceForScan(Command);
if (std::error_code EC = InstanceOrErr.getError())
return std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t>>(
BatchInput.size(), std::make_error_code(std::errc::invalid_argument));
auto Instance = std::move(*InstanceOrErr);
// Local scan cache instance, wrapping the shared global cache.
ModuleDependenciesCache cache(*SharedCache);
auto BatchScanResults = performBatchModuleScan(
*Instance.get(), cache, VersionedPCMInstanceCacheCache.get(),
Saver, BatchInput);
return BatchScanResults;
}
void DependencyScanningTool::serializeCache(llvm::StringRef path) {
SourceManager SM;
DiagnosticEngine Diags(SM);
Diags.addConsumer(PDC);
module_dependency_cache_serialization::writeInterModuleDependenciesCache(
Diags, path, *SharedCache);
}
bool DependencyScanningTool::loadCache(llvm::StringRef path) {
SourceManager SM;
DiagnosticEngine Diags(SM);
Diags.addConsumer(PDC);
SharedCache = std::make_unique<GlobalModuleDependenciesCache>();
bool readFailed =
module_dependency_cache_serialization::readInterModuleDependenciesCache(
path, *SharedCache);
if (readFailed) {
Diags.diagnose(SourceLoc(), diag::warn_scaner_deserialize_failed, path);
}
return readFailed;
}
void DependencyScanningTool::resetCache() {
SharedCache.reset(new GlobalModuleDependenciesCache());
}
llvm::ErrorOr<std::unique_ptr<CompilerInstance>>
DependencyScanningTool::initCompilerInstanceForScan(
ArrayRef<const char *> Command) {
// State unique to an individual scan
auto Instance = std::make_unique<CompilerInstance>();
Instance->addDiagnosticConsumer(&PDC);
// Basic error checking on the arguments
if (Command.empty()) {
Instance->getDiags().diagnose(SourceLoc(), diag::error_no_frontend_args);
return std::make_error_code(std::errc::invalid_argument);
}
CompilerInvocation Invocation;
SmallString<128> WorkingDirectory;
llvm::sys::fs::current_path(WorkingDirectory);
// We must reset option occurences because we are handling an unrelated
// command-line to those possibly parsed parsed before using the same tool.
// We must do so because LLVM options parsing is done using a managed
// static `GlobalParser`.
llvm::cl::ResetAllOptionOccurrences();
// Parse arguments.
std::string CommandString;
for (const auto *c : Command) {
CommandString.append(c);
CommandString.append(" ");
}
SmallVector<const char *, 4> Args;
llvm::cl::TokenizeGNUCommandLine(CommandString, Saver, Args);
if (Invocation.parseArgs(Args, Instance->getDiags())) {
return std::make_error_code(std::errc::invalid_argument);
}
// Setup the instance
Instance->setup(Invocation);
(void)Instance->getMainModule();
return Instance;
}
} // namespace dependencies
} // namespace swift