Skip to content

Commit 9f0083c

Browse files
committed
[Dependency Scanning] Refactor Clang dependency bridging into a 'ModuleDependencyScanner' utility
This moves the functionality of 'bridgeClangModuleDependency' into a utility in the main scanner class because it relies on various objects whose lifetime is already tied to the scanner itself.
1 parent 5015ba6 commit 9f0083c

File tree

5 files changed

+301
-299
lines changed

5 files changed

+301
-299
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "swift/Basic/Assertions.h"
2424
#include "swift/Basic/CXXStdlibKind.h"
2525
#include "swift/Basic/LLVM.h"
26-
#include "swift/ClangImporter/ClangImporter.h"
2726
#include "swift/Serialization/Validation.h"
2827
#include "clang/CAS/CASOptions.h"
2928
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
@@ -176,18 +175,6 @@ void registerCxxInteropLibraries(
176175
const llvm::Triple &Target, StringRef mainModuleName, bool hasStaticCxx,
177176
bool hasStaticCxxStdlib, CXXStdlibKind cxxStdlibKind,
178177
std::function<void(const LinkLibrary &)> RegistrationCallback);
179-
180-
using RemapPathCallback = llvm::function_ref<std::string(StringRef)>;
181-
using LookupModuleOutputCallback =
182-
llvm::function_ref<std::string(const clang::tooling::dependencies::ModuleDeps &,
183-
clang::tooling::dependencies::ModuleOutputKind)>;
184-
185-
ModuleDependencyInfo
186-
bridgeClangModuleDependency(
187-
const ASTContext &ctx,
188-
const clang::tooling::dependencies::ModuleDeps &clangDependency,
189-
LookupModuleOutputCallback LookupModuleOutput,
190-
RemapPathCallback remapPath = nullptr);
191178
} // namespace dependencies
192179

193180
struct ScannerImportStatementInfo {
@@ -1042,6 +1029,8 @@ using ModuleNameToDependencyMap = llvm::StringMap<ModuleDependencyInfo>;
10421029
using ModuleDependenciesKindMap =
10431030
std::unordered_map<ModuleDependencyKind, ModuleNameToDependencyMap,
10441031
ModuleDependencyKindHash>;
1032+
using BridgeClangDependencyCallback = llvm::function_ref<ModuleDependencyInfo(
1033+
const clang::tooling::dependencies::ModuleDeps &clangModuleDep)>;
10451034

10461035
// MARK: SwiftDependencyScanningService
10471036
/// A carrier of state shared among possibly multiple invocations of the
@@ -1194,9 +1183,8 @@ class ModuleDependenciesCache {
11941183
/// Record dependencies for the given collection of Clang modules.
11951184
void recordClangDependencies(
11961185
const clang::tooling::dependencies::ModuleDepsGraph &dependencies,
1197-
const ASTContext &ctx,
1198-
dependencies::LookupModuleOutputCallback LookupModuleOutput,
1199-
dependencies::RemapPathCallback remapPath = nullptr);
1186+
DiagnosticEngine &diags,
1187+
BridgeClangDependencyCallback bridgeClangModule);
12001188

12011189
/// Update stored dependencies for the given module.
12021190
void updateDependency(ModuleDependencyID moduleID,

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ class DependencyTracker;
2525

2626
namespace swift {
2727

28+
/// A callback to lookup module outputs for "-fmodule-file=", "-o" etc.
29+
using LookupModuleOutputCallback = llvm::function_ref<std::string(
30+
const clang::tooling::dependencies::ModuleDeps &,
31+
clang::tooling::dependencies::ModuleOutputKind)>;
32+
using RemapPathCallback = llvm::function_ref<std::string(StringRef)>;
33+
2834
/// A dependency scanning worker which performs filesystem lookup
2935
/// of a named module dependency.
3036
class ModuleDependencyScanningWorker {
@@ -39,43 +45,60 @@ class ModuleDependencyScanningWorker {
3945
llvm::PrefixMapper *mapper, DiagnosticEngine &diags);
4046

4147
private:
42-
/// Retrieve the module dependencies for the Clang module with the given name.
48+
/// Query dependency information for a named Clang module
49+
///
50+
/// \param moduleName moduel identifier for the query
51+
///
52+
/// \param lookupModuleCallback a callback to compute a client-specific
53+
/// module-cache-relative output path for discovered Clang module dependencies.
54+
///
55+
/// \param alreadySeenModules a set of module dependencies previously seen
56+
/// by the scanner, as to avoid processing them all over again
57+
///
58+
/// \returns Clang dependency scanner's \c TranslationUnitDeps result
4359
std::optional<clang::tooling::dependencies::TranslationUnitDeps>
4460
scanFilesystemForClangModuleDependency(
4561
Identifier moduleName,
62+
LookupModuleOutputCallback lookupModuleCallback,
4663
const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
4764
&alreadySeenModules);
4865

49-
/// Retrieve the module dependencies for the Swift module with the given name.
50-
SwiftModuleScannerQueryResult scanFilesystemForSwiftModuleDependency(
51-
Identifier moduleName, bool isTestableImport = false);
52-
5366
/// Query dependency information for header dependencies
5467
/// of a binary Swift module.
5568
///
5669
/// \param moduleID the name of the Swift module whose dependency
5770
/// information will be augmented with information about the given
5871
/// textual header inputs.
5972
///
60-
/// \param headerPath the path to the header to be scanned.
73+
/// \param headerPath optional path to the header to be scanned.
74+
///
75+
/// \param sourceBuffer optional in-memory buffer of a header to be scanned.
6176
///
62-
/// \param clangScanningTool The clang dependency scanner.
77+
/// \param lookupModuleCallback a callback to compute a client-specific
78+
/// module-cache-relative output path for discovered Clang module dependencies.
6379
///
64-
/// \param cache The module dependencies cache to update, with information
65-
/// about new Clang modules discovered along the way.
80+
/// \param alreadySeenModules a set of module dependencies previously seen
81+
/// by the scanner, as to avoid processing them all over again
6682
///
67-
/// \returns \c true if an error occurred, \c false otherwise
68-
bool scanHeaderDependenciesOfSwiftModule(
69-
const ASTContext &ctx,
83+
/// \returns Clang dependency scanner's \c TranslationUnitDeps result
84+
std::optional<clang::tooling::dependencies::TranslationUnitDeps>
85+
scanHeaderDependenciesOfSwiftModule(
7086
ModuleDependencyID moduleID, std::optional<StringRef> headerPath,
7187
std::optional<llvm::MemoryBufferRef> sourceBuffer,
72-
ModuleDependenciesCache &cache,
73-
ModuleDependencyIDSetVector &headerClangModuleDependencies,
74-
std::vector<std::string> &headerFileInputs,
75-
std::vector<std::string> &bridgingHeaderCommandLine,
76-
std::vector<std::string> &visibleClangModules,
77-
std::optional<std::string> &includeTreeID);
88+
LookupModuleOutputCallback lookupModuleCallback,
89+
const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
90+
&alreadySeenModules);
7891

92+
/// Query dependency information for a named Swift module
93+
///
94+
/// \param moduleName moduel identifier for the query
95+
///
96+
/// \param isTestableImport a boolean flag which indicates whether
97+
/// this is an @testable dependency
98+
///
99+
/// \returns a struct containing query results
100+
SwiftModuleScannerQueryResult scanFilesystemForSwiftModuleDependency(
101+
Identifier moduleName, bool isTestableImport = false);
79102

80103
/// Store cache entry for include tree.
81104
llvm::Error
@@ -93,16 +116,9 @@ class ModuleDependencyScanningWorker {
93116
// Swift and Clang module loaders acting as scanners.
94117
std::unique_ptr<SwiftModuleScanner> swiftModuleScannerLoader;
95118

96-
/// The location of where the explicitly-built modules will be output to
97-
std::string moduleOutputPath;
98-
/// The location of where the explicitly-built SDK modules will be output to
99-
std::string sdkModuleOutputPath;
100-
101119
// CAS instance.
102120
std::shared_ptr<llvm::cas::ObjectStore> CAS;
103121
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
104-
/// File prefix mapper.
105-
llvm::PrefixMapper *PrefixMapper;
106122

107123
// Base command line invocation for clang scanner queries (both module and header)
108124
std::vector<std::string> clangScanningBaseCommandLineArgs;
@@ -223,11 +239,7 @@ class ModuleDependencyScanner {
223239
return PrefixMapper && !PrefixMapper->getMappings().empty();
224240
}
225241
llvm::PrefixMapper *getPrefixMapper() const { return PrefixMapper.get(); }
226-
std::string remapPath(StringRef Path) const {
227-
if (!PrefixMapper)
228-
return Path.str();
229-
return PrefixMapper->mapToString(Path);
230-
}
242+
std::string remapPath(StringRef Path) const;
231243

232244
/// CAS options.
233245
llvm::cas::ObjectStore &getCAS() const {
@@ -294,11 +306,22 @@ class ModuleDependencyScanner {
294306
ModuleDependenciesCache &cache,
295307
ModuleDependencyIDSetVector &allModules);
296308

309+
/// Bridge Clang dependency scanner's dependency node
310+
/// to the Swift scanner's `ModuleDependencyInfo`.
311+
ModuleDependencyInfo
312+
bridgeClangModuleDependency(
313+
const clang::tooling::dependencies::ModuleDeps &clangDependency);
314+
297315
/// Perform an operation utilizing one of the Scanning workers
298316
/// available to this scanner.
299317
template <typename Function, typename... Args>
300318
auto withDependencyScanningWorker(Function &&F, Args &&...ArgList);
301319

320+
/// Determine cache-relative output path for a given Clang module
321+
std::string clangModuleOutputPathLookup(
322+
const clang::tooling::dependencies::ModuleDeps &clangDep,
323+
clang::tooling::dependencies::ModuleOutputKind moduleOutputKind) const;
324+
302325
/// Use the scanner's ASTContext to construct an `Identifier`
303326
/// for a given module name.
304327
Identifier getModuleImportIdentifier(StringRef moduleName);
@@ -318,6 +341,11 @@ class ModuleDependencyScanner {
318341
ASTContext &ScanASTContext;
319342
ModuleDependencyIssueReporter IssueReporter;
320343

344+
/// The location of where the explicitly-built modules will be output to
345+
std::string ModuleOutputPath;
346+
/// The location of where the explicitly-built SDK modules will be output to
347+
std::string SDKModuleOutputPath;
348+
321349
/// The available pool of workers for filesystem module search
322350
unsigned NumThreads;
323351
std::list<std::unique_ptr<ModuleDependencyScanningWorker>> Workers;

include/swift/Serialization/ScanningLoaders.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
7777

7878
/// AST delegate to be used for textual interface scanning
7979
InterfaceSubContextDelegate &astDelegate;
80-
/// Location where pre-built modules are to be built into.
81-
std::string moduleOutputPath;
82-
/// Location where pre-built SDK modules are to be built into.
83-
std::string sdkModuleOutputPath;
8480
/// Clang-specific (-Xcc) command-line flags to include on
8581
/// Swift module compilation commands
8682
std::vector<std::string> swiftModuleClangCC1CommandLineArgs;
@@ -96,14 +92,12 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
9692
public:
9793
SwiftModuleScanner(
9894
ASTContext &ctx, ModuleLoadingMode LoadMode,
99-
InterfaceSubContextDelegate &astDelegate, StringRef moduleOutputPath,
100-
StringRef sdkModuleOutputPath,
95+
InterfaceSubContextDelegate &astDelegate,
10196
std::vector<std::string> swiftModuleClangCC1CommandLineArgs,
10297
llvm::StringMap<std::string> &explicitSwiftModuleInputs)
10398
: SerializedModuleLoaderBase(ctx, nullptr, LoadMode,
10499
/*IgnoreSwiftSourceInfoFile=*/true),
105-
astDelegate(astDelegate), moduleOutputPath(moduleOutputPath),
106-
sdkModuleOutputPath(sdkModuleOutputPath),
100+
astDelegate(astDelegate),
107101
swiftModuleClangCC1CommandLineArgs(swiftModuleClangCC1CommandLineArgs),
108102
explicitSwiftModuleInputs(explicitSwiftModuleInputs) {
109103
}

0 commit comments

Comments
 (0)