Skip to content

Commit dc2166c

Browse files
committed
[Dependency Scanning] Keep track of which direct dependencies are '@testable' for a Source dependency
1 parent ed488eb commit dc2166c

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,10 @@ class ImportDecl final : public Decl,
14221422
return getAttrs().hasAttribute<ExportedAttr>();
14231423
}
14241424

1425+
bool isTestable() const {
1426+
return getAttrs().hasAttribute<TestableAttr>();
1427+
}
1428+
14251429
ModuleDecl *getModule() const { return Mod; }
14261430
void setModule(ModuleDecl *M) { Mod = M; }
14271431

include/swift/AST/ModuleDependencies.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,14 @@ class SwiftSourceModuleDependenciesStorage :
211211
/// Details common to Swift textual (interface or source) modules
212212
CommonSwiftTextualModuleDependencyDetails textualModuleDetails;
213213

214+
/// Collection of module imports that were detected to be `@Testable`
215+
llvm::StringSet<> testableImports;
216+
214217
SwiftSourceModuleDependenciesStorage(
215218
ArrayRef<StringRef> extraPCMArgs
216219
) : ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftSource),
217-
textualModuleDetails(extraPCMArgs) {}
220+
textualModuleDetails(extraPCMArgs),
221+
testableImports(llvm::StringSet<>()) {}
218222

219223
ModuleDependencyInfoStorageBase *clone() const override {
220224
return new SwiftSourceModuleDependenciesStorage(*this);
@@ -223,6 +227,10 @@ class SwiftSourceModuleDependenciesStorage :
223227
static bool classof(const ModuleDependencyInfoStorageBase *base) {
224228
return base->dependencyKind == ModuleDependencyKind::SwiftSource;
225229
}
230+
231+
void addTestableImport(ImportPath::Module module) {
232+
testableImports.insert(module.front().Item.str());
233+
}
226234
};
227235

228236
/// Describes the dependencies of a pre-built Swift module (with no .swiftinterface).
@@ -460,6 +468,13 @@ class ModuleDependencyInfo {
460468
storage->resolved = isResolved;
461469
}
462470

471+
/// For a Source dependency, register a `Testable` import
472+
void addTestableImport(ImportPath::Module module);
473+
474+
/// Whether or not a queried module name is a `@Testable` import dependency
475+
/// of this module. Can only return `true` for Swift source modules.
476+
bool isTestableImport(StringRef moduleName) const;
477+
463478
/// Whether the dependencies are for a Swift module: either Textual, Source, Binary, or Placeholder.
464479
bool isSwiftModule() const;
465480

lib/AST/ModuleDependencies.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ ModuleDependencyInfo::getAsPlaceholderDependencyModule() const {
8585
return dyn_cast<SwiftPlaceholderModuleDependencyStorage>(storage.get());
8686
}
8787

88+
void ModuleDependencyInfo::addTestableImport(ImportPath::Module module) {
89+
assert(getAsSwiftSourceModule() && "Expected source module for addTestableImport.");
90+
dyn_cast<SwiftSourceModuleDependenciesStorage>(storage.get())->addTestableImport(module);
91+
}
92+
93+
bool ModuleDependencyInfo::isTestableImport(StringRef moduleName) const {
94+
if (auto swiftSourceDepStorage = getAsSwiftSourceModule())
95+
return swiftSourceDepStorage->testableImports.contains(moduleName);
96+
else
97+
return false;
98+
}
99+
88100
void ModuleDependencyInfo::addModuleDependency(ModuleDependencyID dependencyID) {
89101
storage->resolvedModuleDependencies.push_back(dependencyID);
90102
}
@@ -114,6 +126,10 @@ void ModuleDependencyInfo::addModuleImport(
114126
ImportPath::Builder scratch;
115127
auto realPath = importDecl->getRealModulePath(scratch);
116128
addModuleImport(realPath, &alreadyAddedModules);
129+
130+
if (getKind() == swift::ModuleDependencyKind::SwiftSource &&
131+
importDecl->isTestable())
132+
addTestableImport(realPath);
117133
}
118134

119135
auto fileName = sf.getFilename();

0 commit comments

Comments
 (0)