Skip to content

Commit 84c0060

Browse files
committed
[Dependency Scanning] Model main module as separate dependency kind: SwiftSource
These kinds of modules differ from `SwiftTextual` modules in that they do not have an interface and have source-files. It is cleaner to enforce this distinction with types, instead of checking for interface optionality everywhere.
1 parent 5e0d32b commit 84c0060

16 files changed

+473
-158
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ typedef struct {
4949
} swiftscan_string_set_t;
5050

5151
typedef enum {
52+
// This dependency info encodes two ModuleDependencyKind types:
53+
// SwiftInterface and SwiftSource.
5254
SWIFTSCAN_DEPENDENCY_INFO_SWIFT_TEXTUAL = 0,
5355
SWIFTSCAN_DEPENDENCY_INFO_SWIFT_BINARY = 1,
5456
SWIFTSCAN_DEPENDENCY_INFO_SWIFT_PLACEHOLDER = 2,

include/swift/AST/ModuleDependencies.h

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class Identifier;
3737
/// Which kind of module dependencies we are looking for.
3838
enum class ModuleDependenciesKind : int8_t {
3939
FirstKind,
40-
SwiftTextual = FirstKind,
40+
SwiftInterface = FirstKind,
41+
SwiftSource,
4142
SwiftBinary,
4243
// Placeholder dependencies are a kind of dependencies used only by the
4344
// dependency scanner. They are swift modules that the scanner will not be
@@ -97,14 +98,14 @@ class ModuleDependenciesStorageBase {
9798
std::vector<std::string> moduleDependencies;
9899
};
99100

100-
/// Describes the dependencies of a Swift module.
101+
/// Describes the dependencies of a Swift module described by an Swift interface file.
101102
///
102103
/// This class is mostly an implementation detail for \c ModuleDependencies.
103-
class SwiftTextualModuleDependenciesStorage :
104+
class SwiftInterfaceModuleDependenciesStorage :
104105
public ModuleDependenciesStorageBase {
105106
public:
106-
/// The Swift interface file, if it can be used to generate the module file.
107-
const Optional<std::string> swiftInterfaceFile;
107+
/// The Swift interface file to be used to generate the module file.
108+
const std::string swiftInterfaceFile;
108109

109110
/// Potentially ready-to-use compiled modules for the interface file.
110111
const std::vector<std::string> compiledModuleCandidates;
@@ -136,14 +137,14 @@ class SwiftTextualModuleDependenciesStorage :
136137
/// (Clang) modules on which the bridging header depends.
137138
std::vector<std::string> bridgingModuleDependencies;
138139

139-
SwiftTextualModuleDependenciesStorage(
140-
const Optional<std::string> &swiftInterfaceFile,
140+
SwiftInterfaceModuleDependenciesStorage(
141+
const std::string swiftInterfaceFile,
141142
ArrayRef<std::string> compiledModuleCandidates,
142143
ArrayRef<StringRef> buildCommandLine,
143144
ArrayRef<StringRef> extraPCMArgs,
144145
StringRef contextHash,
145146
bool isFramework
146-
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftTextual),
147+
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftInterface),
147148
swiftInterfaceFile(swiftInterfaceFile),
148149
compiledModuleCandidates(compiledModuleCandidates.begin(),
149150
compiledModuleCandidates.end()),
@@ -152,14 +153,51 @@ class SwiftTextualModuleDependenciesStorage :
152153
contextHash(contextHash), isFramework(isFramework) { }
153154

154155
ModuleDependenciesStorageBase *clone() const override {
155-
return new SwiftTextualModuleDependenciesStorage(*this);
156+
return new SwiftInterfaceModuleDependenciesStorage(*this);
156157
}
157158

158159
static bool classof(const ModuleDependenciesStorageBase *base) {
159-
return base->dependencyKind == ModuleDependenciesKind::SwiftTextual;
160+
return base->dependencyKind == ModuleDependenciesKind::SwiftInterface;
160161
}
161162
};
162163

164+
/// Describes the dependencies of a Swift module
165+
///
166+
/// This class is mostly an implementation detail for \c ModuleDependencies.
167+
class SwiftSourceModuleDependenciesStorage :
168+
public ModuleDependenciesStorageBase {
169+
public:
170+
/// To build a PCM to be used by this Swift module, we need to append these
171+
/// arguments to the generic PCM build arguments reported from the dependency
172+
/// graph.
173+
const std::vector<std::string> extraPCMArgs;
174+
175+
/// Bridging header file, if there is one.
176+
Optional<std::string> bridgingHeaderFile;
177+
178+
/// Swift source files that are part of the Swift module, when known.
179+
std::vector<std::string> sourceFiles;
180+
181+
/// Source files on which the bridging header depends.
182+
std::vector<std::string> bridgingSourceFiles;
183+
184+
/// (Clang) modules on which the bridging header depends.
185+
std::vector<std::string> bridgingModuleDependencies;
186+
187+
SwiftSourceModuleDependenciesStorage(
188+
ArrayRef<StringRef> extraPCMArgs
189+
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftSource),
190+
extraPCMArgs(extraPCMArgs.begin(), extraPCMArgs.end()) {}
191+
192+
ModuleDependenciesStorageBase *clone() const override {
193+
return new SwiftSourceModuleDependenciesStorage(*this);
194+
}
195+
196+
static bool classof(const ModuleDependenciesStorageBase *base) {
197+
return base->dependencyKind == ModuleDependenciesKind::SwiftSource;
198+
}
199+
};
200+
163201
/// Describes the dependencies of a pre-built Swift module (with no .swiftinterface).
164202
///
165203
/// This class is mostly an implementation detail for \c ModuleDependencies.
@@ -291,15 +329,15 @@ class ModuleDependencies {
291329

292330
/// Describe the module dependencies for a Swift module that can be
293331
/// built from a Swift interface file (\c .swiftinterface).
294-
static ModuleDependencies forSwiftTextualModule(
295-
const Optional<std::string> &swiftInterfaceFile,
332+
static ModuleDependencies forSwiftInterfaceModule(
333+
std::string swiftInterfaceFile,
296334
ArrayRef<std::string> compiledCandidates,
297335
ArrayRef<StringRef> buildCommands,
298336
ArrayRef<StringRef> extraPCMArgs,
299337
StringRef contextHash,
300338
bool isFramework) {
301339
return ModuleDependencies(
302-
std::make_unique<SwiftTextualModuleDependenciesStorage>(
340+
std::make_unique<SwiftInterfaceModuleDependenciesStorage>(
303341
swiftInterfaceFile, compiledCandidates, buildCommands,
304342
extraPCMArgs, contextHash, isFramework));
305343
}
@@ -316,11 +354,9 @@ class ModuleDependencies {
316354
}
317355

318356
/// Describe the main Swift module.
319-
static ModuleDependencies forMainSwiftModule(ArrayRef<StringRef> extraPCMArgs) {
357+
static ModuleDependencies forSwiftSourceModule(ArrayRef<StringRef> extraPCMArgs) {
320358
return ModuleDependencies(
321-
std::make_unique<SwiftTextualModuleDependenciesStorage>(
322-
None, ArrayRef<std::string>(), ArrayRef<StringRef>(),
323-
extraPCMArgs, StringRef(), false));
359+
std::make_unique<SwiftSourceModuleDependenciesStorage>(extraPCMArgs));
324360
}
325361

326362
/// Describe the module dependencies for a Clang module that can be
@@ -350,11 +386,14 @@ class ModuleDependencies {
350386
return storage->moduleDependencies;
351387
}
352388

353-
/// Whether the dependencies are for a Swift module: either Textual, Binary, or Placeholder.
389+
/// Whether the dependencies are for a Swift module: either Textual, Source, Binary, or Placeholder.
354390
bool isSwiftModule() const;
355391

356392
/// Whether the dependencies are for a textual Swift module.
357-
bool isSwiftTextualModule() const;
393+
bool isSwiftInterfaceModule() const;
394+
395+
/// Whether the dependencies are for a textual Swift module.
396+
bool isSwiftSourceModule() const;
358397

359398
/// Whether the dependencies are for a binary Swift module.
360399
bool isSwiftBinaryModule() const;
@@ -368,8 +407,11 @@ class ModuleDependencies {
368407
ModuleDependenciesKind getKind() const {
369408
return storage->dependencyKind;
370409
}
410+
/// Retrieve the dependencies for a Swift textual-interface module.
411+
const SwiftInterfaceModuleDependenciesStorage *getAsSwiftInterfaceModule() const;
412+
371413
/// Retrieve the dependencies for a Swift module.
372-
const SwiftTextualModuleDependenciesStorage *getAsSwiftTextualModule() const;
414+
const SwiftSourceModuleDependenciesStorage *getAsSwiftSourceModule() const;
373415

374416
/// Retrieve the dependencies for a binary Swift module.
375417
const SwiftBinaryModuleDependencyStorage *getAsSwiftBinaryModule() const;

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ class ClangImporter final : public ClangModuleLoader {
409409
///
410410
/// \returns \c true if an error occurred, \c false otherwise
411411
bool addBridgingHeaderDependencies(
412-
StringRef moduleName, ModuleDependenciesCache &cache);
412+
StringRef moduleName,
413+
ModuleDependenciesKind moduleKind,
414+
ModuleDependenciesCache &cache);
413415

414416
clang::TargetInfo &getTargetInfo() const override;
415417
clang::ASTContext &getClangASTContext() const override;

include/swift/DependencyScan/DependencyScanImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ struct swiftscan_dependency_info_s {
3737
/// The format is:
3838
/// `<module-kind>:<module-name>`
3939
/// where `module-kind` is one of:
40-
/// "swiftTextual"
40+
/// "swiftInterface"
41+
/// "swiftSource"
4142
/// "swiftBinary"
4243
/// "swiftPlaceholder"
4344
/// "clang""

include/swift/DependencyScan/DependencyScanningTool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class DependencyScanningTool {
6666
void resetCache();
6767

6868
private:
69+
/// Using the specified invocation command, initialize the scanner instance
70+
/// for this scan. Returns the `CompilerInstance` that will be used.
71+
llvm::ErrorOr<std::unique_ptr<CompilerInstance>>
72+
initScannerForAction(ArrayRef<const char *> Command);
73+
6974
/// Using the specified invocation command, instantiate a CompilerInstance
7075
/// that will be used for this scan.
7176
llvm::ErrorOr<std::unique_ptr<CompilerInstance>>

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ using llvm::BCVBR;
3737
/// Every .moddepcache file begins with these 4 bytes, for easy identification.
3838
const unsigned char MODULE_DEPENDENCY_CACHE_FORMAT_SIGNATURE[] = {'I', 'M', 'D',
3939
'C'};
40-
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 1;
40+
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR = 2;
4141
/// Increment this on every change.
4242
const unsigned MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR = 0;
4343

@@ -72,7 +72,8 @@ namespace graph_block {
7272
enum {
7373
METADATA = 1,
7474
MODULE_NODE,
75-
SWIFT_TEXTUAL_MODULE_DETAILS_NODE,
75+
SWIFT_INTERFACE_MODULE_DETAILS_NODE,
76+
SWIFT_SOURCE_MODULE_DETAILS_NODE,
7677
SWIFT_PLACEHOLDER_MODULE_DETAILS_NODE,
7778
SWIFT_BINARY_MODULE_DETAILS_NODE,
7879
CLANG_MODULE_DETAILS_NODE,
@@ -109,7 +110,8 @@ using IdentifierArrayLayout =
109110

110111
// After the array records, we have a sequence of Module info
111112
// records, each of which is followed by one of:
112-
// - SwiftTextualModuleDetails
113+
// - SwiftInterfaceModuleDetails
114+
// - SwiftSourceModuleDetails
113115
// - SwiftBinaryModuleDetails
114116
// - SwiftPlaceholderModuleDetails
115117
// - ClangModuleDetails
@@ -119,8 +121,8 @@ using ModuleInfoLayout =
119121
DependencyIDArrayIDField // directDependencies
120122
>;
121123

122-
using SwiftTextualModuleDetailsLayout =
123-
BCRecordLayout<SWIFT_TEXTUAL_MODULE_DETAILS_NODE, // ID
124+
using SwiftInterfaceModuleDetailsLayout =
125+
BCRecordLayout<SWIFT_INTERFACE_MODULE_DETAILS_NODE, // ID
124126
FileIDField, // swiftInterfaceFile
125127
FileIDArrayIDField, // compiledModuleCandidates
126128
FlagIDArrayIDField, // buildCommandLine
@@ -130,7 +132,16 @@ using SwiftTextualModuleDetailsLayout =
130132
FileIDField, // bridgingHeaderFile
131133
FileIDArrayIDField, // sourceFiles
132134
FileIDArrayIDField, // bridgingSourceFiles
133-
IdentifierIDField // bridgingModuleDependencies
135+
FileIDArrayIDField // bridgingModuleDependencies
136+
>;
137+
138+
using SwiftSourceModuleDetailsLayout =
139+
BCRecordLayout<SWIFT_SOURCE_MODULE_DETAILS_NODE, // ID
140+
FlagIDArrayIDField, // extraPCMArgs
141+
FileIDField, // bridgingHeaderFile
142+
FileIDArrayIDField, // sourceFiles
143+
FileIDArrayIDField, // bridgingSourceFiles
144+
FileIDArrayIDField // bridgingModuleDependencies
134145
>;
135146

136147
using SwiftBinaryModuleDetailsLayout =

include/swift/DependencyScan/StringUtils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ swiftscan_string_set_t *create_set(const std::vector<std::string> &strings);
3535
/// create_clone routine.
3636
swiftscan_string_set_t *create_set(int count, const char **strings);
3737

38+
/// Create an empty array of swiftscan_string_ref_t objects
39+
swiftscan_string_set_t *create_empty_set();
40+
3841
/// Retrieve the character data associated with the given string.
3942
const char *get_C_string(swiftscan_string_ref_t string);
4043
}

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,11 @@ Optional<ModuleDependencies> ASTContext::getModuleDependencies(
15791579
if (!isUnderlyingClangModule) {
15801580
if (auto found = cache.findDependencies(
15811581
moduleName,
1582-
{ModuleDependenciesKind::SwiftTextual, searchPathSet}))
1582+
{ModuleDependenciesKind::SwiftSource, searchPathSet}))
1583+
return found;
1584+
if (auto found = cache.findDependencies(
1585+
moduleName,
1586+
{ModuleDependenciesKind::SwiftInterface, searchPathSet}))
15831587
return found;
15841588
if (auto found = cache.findDependencies(
15851589
moduleName, {ModuleDependenciesKind::SwiftBinary, searchPathSet}))

0 commit comments

Comments
 (0)