Skip to content

Commit bfa8c0e

Browse files
committed
[Dependency Scanning] Scan header inputs of binary Swift moduel dependencies
Otherwise they may have module dependencies of their own which will not be detected by the scanner and included in the list of explicit inputs for compilation.
1 parent 918bd3d commit bfa8c0e

16 files changed

+299
-231
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ SWIFTSCAN_PUBLIC swiftscan_string_set_t *
202202
swiftscan_swift_binary_detail_get_swift_overlay_dependencies(
203203
swiftscan_module_details_t details);
204204

205-
SWIFTSCAN_PUBLIC swiftscan_string_set_t *
206-
swiftscan_swift_binary_detail_get_header_dependencies(
205+
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
206+
swiftscan_swift_binary_detail_get_header_dependency(
207207
swiftscan_module_details_t details);
208208

209209
SWIFTSCAN_PUBLIC bool

include/swift/AST/ModuleDependencies.h

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,13 @@ class SwiftBinaryModuleDependencyStorage : public ModuleDependencyInfoStorageBas
326326
const std::string &sourceInfoPath,
327327
const std::vector<std::string> &moduleImports,
328328
const std::vector<std::string> &optionalModuleImports,
329-
const std::vector<std::string> &headerImports,
329+
const std::string &headerImport,
330330
const bool isFramework,
331331
const std::string &moduleCacheKey)
332332
: ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftBinary,
333333
moduleImports, optionalModuleImports, moduleCacheKey),
334334
compiledModulePath(compiledModulePath), moduleDocPath(moduleDocPath),
335-
sourceInfoPath(sourceInfoPath), preCompiledBridgingHeaderPaths(headerImports),
335+
sourceInfoPath(sourceInfoPath), headerImport(headerImport),
336336
isFramework(isFramework) {}
337337

338338
ModuleDependencyInfoStorageBase *clone() const override {
@@ -348,8 +348,14 @@ class SwiftBinaryModuleDependencyStorage : public ModuleDependencyInfoStorageBas
348348
/// The path to the .swiftSourceInfo file.
349349
const std::string sourceInfoPath;
350350

351-
/// The paths of all the .pch dependencies of this module.
352-
const std::vector<std::string> preCompiledBridgingHeaderPaths;
351+
/// The path of the .h dependency of this module.
352+
const std::string headerImport;
353+
354+
/// Source files on which the header inputs depend.
355+
std::vector<std::string> headerSourceFiles;
356+
357+
/// (Clang) modules on which the header inputs depend.
358+
std::vector<std::string> headerModuleDependencies;
353359

354360
/// A flag that indicates this dependency is a framework
355361
const bool isFramework;
@@ -508,13 +514,13 @@ class ModuleDependencyInfo {
508514
const std::string &sourceInfoPath,
509515
const std::vector<std::string> &moduleImports,
510516
const std::vector<std::string> &optionalModuleImports,
511-
const std::vector<std::string> &headerImports,
517+
const std::string &headerImport,
512518
bool isFramework, const std::string &moduleCacheKey) {
513519
return ModuleDependencyInfo(
514520
std::make_unique<SwiftBinaryModuleDependencyStorage>(
515521
compiledModulePath, moduleDocPath, sourceInfoPath,
516522
moduleImports, optionalModuleImports,
517-
headerImports, isFramework, moduleCacheKey));
523+
headerImport, isFramework, moduleCacheKey));
518524
}
519525

520526
/// Describe the main Swift module.
@@ -598,6 +604,26 @@ class ModuleDependencyInfo {
598604
return storage->swiftOverlayDependencies;
599605
}
600606

607+
const ArrayRef<std::string> getHeaderInputSourceFiles() const {
608+
if (auto *detail = getAsSwiftInterfaceModule())
609+
return detail->textualModuleDetails.bridgingSourceFiles;
610+
else if (auto *detail = getAsSwiftSourceModule())
611+
return detail->textualModuleDetails.bridgingSourceFiles;
612+
else if (auto *detail = getAsSwiftBinaryModule())
613+
return detail->headerSourceFiles;
614+
return {};
615+
}
616+
617+
const ArrayRef<std::string> getHeaderDependencies() const {
618+
if (auto *detail = getAsSwiftInterfaceModule())
619+
return detail->textualModuleDetails.bridgingModuleDependencies;
620+
else if (auto *detail = getAsSwiftSourceModule())
621+
return detail->textualModuleDetails.bridgingModuleDependencies;
622+
else if (auto *detail = getAsSwiftBinaryModule())
623+
return detail->headerModuleDependencies;
624+
return {};
625+
}
626+
601627
std::vector<std::string> getCommandline() const {
602628
if (auto *detail = getAsClangModule())
603629
return detail->buildCommandLine;
@@ -751,12 +777,12 @@ class ModuleDependencyInfo {
751777
/// Add source files
752778
void addSourceFile(StringRef sourceFile);
753779

754-
/// Add source files that the bridging header depends on.
755-
void addBridgingSourceFile(StringRef bridgingSourceFile);
780+
/// Add source files that the header input depends on.
781+
void addHeaderSourceFile(StringRef bridgingSourceFile);
756782

757-
/// Add (Clang) module on which the bridging header depends.
758-
void addBridgingModuleDependency(StringRef module,
759-
llvm::StringSet<> &alreadyAddedModules);
783+
/// Add (Clang) modules on which a non-bridging header input depends.
784+
void addHeaderInputModuleDependency(StringRef module,
785+
llvm::StringSet<> &alreadyAddedModules);
760786

761787
/// Add bridging header include tree.
762788
void addBridgingHeaderIncludeTree(StringRef ID);

include/swift/ClangImporter/ClangImporter.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,22 +465,24 @@ class ClangImporter final : public ClangModuleLoader {
465465
ModuleDependencyInfo &MDI,
466466
const clang::tooling::dependencies::TranslationUnitDeps &deps);
467467

468-
/// Add dependency information for the bridging header.
468+
/// Add dependency information for header dependencies
469+
/// of a binary Swift module.
469470
///
470471
/// \param moduleID the name of the Swift module whose dependency
471472
/// information will be augmented with information about the given
472-
/// bridging header.
473+
/// textual header inputs.
473474
///
474475
/// \param clangScanningTool The clang dependency scanner.
475476
///
476477
/// \param cache The module dependencies cache to update, with information
477478
/// about new Clang modules discovered along the way.
478479
///
479480
/// \returns \c true if an error occurred, \c false otherwise
480-
bool addBridgingHeaderDependencies(
481+
bool addHeaderDependencies(
481482
ModuleDependencyID moduleID,
482483
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
483484
ModuleDependenciesCache &cache);
485+
484486
clang::TargetInfo &getModuleAvailabilityTarget() const override;
485487
clang::ASTContext &getClangASTContext() const override;
486488
clang::Preprocessor &getClangPreprocessor() const override;

include/swift/DependencyScan/DependencyScanImpl.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ typedef struct {
128128
/// Clang module dependencies
129129
swiftscan_string_set_t *swift_overlay_module_dependencies;
130130

131-
/// (Clang) header dependencies of this binary module.
132-
/// Typically pre-compiled bridging header.
133-
swiftscan_string_set_t *header_dependencies;
131+
/// (Clang) header (.h) dependency of this binary module.
132+
swiftscan_string_ref_t header_dependency;
133+
134+
/// (Clang) module dependencies of the textual header inputs
135+
swiftscan_string_set_t *header_dependencies_module_dependnecies;
136+
137+
/// Source files included by the header dependencies of this binary module
138+
swiftscan_string_set_t *header_dependencies_source_files;
134139

135140
/// A flag to indicate whether or not this module is a framework.
136141
bool is_framework;

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ class ModuleDependencyScanner {
107107
ModuleDependenciesCache &cache,
108108
ModuleDependencyIDSetVector &directDependencies);
109109

110-
/// If a module has a bridging header, execute a dependency scan
110+
/// If a module has a bridging header or other header inputs, execute a dependency scan
111111
/// on it and record the dependencies.
112-
void resolveBridgingHeaderDependencies(
112+
void resolveHeaderDependencies(
113113
const ModuleDependencyID &moduleID, ModuleDependenciesCache &cache,
114114
std::vector<std::string> &allClangModules,
115115
llvm::StringSet<> &alreadyKnownModules,

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ using SwiftInterfaceModuleDetailsLayout =
141141
FileIDField, // bridgingHeaderFile
142142
FileIDArrayIDField, // sourceFiles
143143
FileIDArrayIDField, // bridgingSourceFiles
144-
FileIDArrayIDField, // bridgingModuleDependencies
144+
IdentifierIDField, // bridgingModuleDependencies
145145
DependencyIDArrayIDField, // swiftOverlayDependencies
146146
IdentifierIDField, // CASFileSystemRootID
147147
IdentifierIDField, // bridgingHeaderIncludeTree
@@ -168,7 +168,9 @@ using SwiftBinaryModuleDetailsLayout =
168168
FileIDField, // moduleDocPath
169169
FileIDField, // moduleSourceInfoPath
170170
DependencyIDArrayIDField, // swiftOverlayDependencies
171-
ImportArrayIDField, // headerImports
171+
FileIDField, // headerImport
172+
IdentifierIDField, // headerModuleDependencies
173+
FileIDArrayIDField, // headerSourceFiles
172174
IsFrameworkField, // isFramework
173175
IdentifierIDField // moduleCacheKey
174176
>;

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
168168

169169
struct BinaryModuleImports {
170170
llvm::StringSet<> moduleImports;
171-
llvm::StringSet<> headerImports;
171+
std::string headerImport;
172172
};
173173

174174
static llvm::ErrorOr<BinaryModuleImports>

lib/AST/ModuleDependencies.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ std::optional<std::string> ModuleDependencyInfo::getBridgingHeader() const {
197197
return swiftSourceStorage->textualModuleDetails.bridgingHeaderFile;
198198
}
199199
default:
200-
llvm_unreachable("Unexpected dependency kind");
200+
return std::nullopt;
201201
}
202202
}
203203

@@ -325,7 +325,7 @@ void ModuleDependencyInfo::addBridgingHeader(StringRef bridgingHeader) {
325325
}
326326

327327
/// Add source files that the bridging header depends on.
328-
void ModuleDependencyInfo::addBridgingSourceFile(StringRef bridgingSourceFile) {
328+
void ModuleDependencyInfo::addHeaderSourceFile(StringRef bridgingSourceFile) {
329329
switch (getKind()) {
330330
case swift::ModuleDependencyKind::SwiftInterface: {
331331
auto swiftInterfaceStorage =
@@ -337,7 +337,14 @@ void ModuleDependencyInfo::addBridgingSourceFile(StringRef bridgingSourceFile) {
337337
case swift::ModuleDependencyKind::SwiftSource: {
338338
auto swiftSourceStorage =
339339
cast<SwiftSourceModuleDependenciesStorage>(storage.get());
340-
swiftSourceStorage->textualModuleDetails.bridgingSourceFiles.push_back(bridgingSourceFile.str());
340+
swiftSourceStorage->textualModuleDetails.bridgingSourceFiles.push_back(
341+
bridgingSourceFile.str());
342+
break;
343+
}
344+
case swift::ModuleDependencyKind::SwiftBinary: {
345+
auto swiftBinaryStorage =
346+
cast<SwiftBinaryModuleDependencyStorage>(storage.get());
347+
swiftBinaryStorage->headerSourceFiles.push_back(bridgingSourceFile.str());
341348
break;
342349
}
343350
default:
@@ -380,21 +387,30 @@ void ModuleDependencyInfo::addSourceFile(StringRef sourceFile) {
380387
}
381388

382389
/// Add (Clang) module on which the bridging header depends.
383-
void ModuleDependencyInfo::addBridgingModuleDependency(
390+
void ModuleDependencyInfo::addHeaderInputModuleDependency(
384391
StringRef module, llvm::StringSet<> &alreadyAddedModules) {
385392
switch (getKind()) {
386393
case swift::ModuleDependencyKind::SwiftInterface: {
387394
auto swiftInterfaceStorage =
388395
cast<SwiftInterfaceModuleDependenciesStorage>(storage.get());
389396
if (alreadyAddedModules.insert(module).second)
390-
swiftInterfaceStorage->textualModuleDetails.bridgingModuleDependencies.push_back(module.str());
397+
swiftInterfaceStorage->textualModuleDetails.bridgingModuleDependencies
398+
.push_back(module.str());
391399
break;
392400
}
393401
case swift::ModuleDependencyKind::SwiftSource: {
394402
auto swiftSourceStorage =
395403
cast<SwiftSourceModuleDependenciesStorage>(storage.get());
396404
if (alreadyAddedModules.insert(module).second)
397-
swiftSourceStorage->textualModuleDetails.bridgingModuleDependencies.push_back(module.str());
405+
swiftSourceStorage->textualModuleDetails.bridgingModuleDependencies
406+
.push_back(module.str());
407+
break;
408+
}
409+
case swift::ModuleDependencyKind::SwiftBinary: {
410+
auto swiftBinaryStorage =
411+
cast<SwiftBinaryModuleDependencyStorage>(storage.get());
412+
if (alreadyAddedModules.insert(module).second)
413+
swiftBinaryStorage->headerModuleDependencies.push_back(module.str());
398414
break;
399415
}
400416
default:

0 commit comments

Comments
 (0)