Skip to content

Commit 71e7d6a

Browse files
committed
[Dependency scanning] Add implicit Swift and SwiftOnoneSupport dependencies
1 parent 5f953da commit 71e7d6a

File tree

8 files changed

+72
-33
lines changed

8 files changed

+72
-33
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ class CompilerInvocation {
346346
return ImplicitStdlibKind::Stdlib;
347347
}
348348

349+
/// Whether the Swift -Onone support library should be implicitly imported.
350+
bool shouldImportSwiftONoneSupport() const;
351+
349352
/// Performs input setup common to these tools:
350353
/// sil-opt, sil-func-extractor, sil-llvm-gen, and sil-nm.
351354
/// Return value includes the buffer so caller can keep it alive.

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ bool ArgsToFrontendOptionsConverter::convert(
8383

8484
Opts.TrackSystemDeps |= Args.hasArg(OPT_track_system_dependencies);
8585

86+
// Always track system dependencies when scanning dependencies.
87+
if (const Arg *ModeArg = Args.getLastArg(OPT_modes_Group)) {
88+
if (ModeArg->getOption().matches(OPT_scan_dependencies))
89+
Opts.TrackSystemDeps = true;
90+
}
91+
8692
Opts.SerializeModuleInterfaceDependencyHashes |=
8793
Args.hasArg(OPT_serialize_module_interface_dependency_hashes);
8894

lib/Frontend/Frontend.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,10 @@ std::unique_ptr<SILModule> CompilerInstance::takeSILModule() {
687687
/// builds. This allows for use of popular specialized functions
688688
/// from the standard library, which makes the non-optimized builds
689689
/// execute much faster.
690-
static bool shouldImplicityImportSwiftOnoneSupportModule(
691-
const CompilerInvocation &Invocation) {
692-
if (Invocation.getImplicitStdlibKind() != ImplicitStdlibKind::Stdlib)
690+
bool CompilerInvocation::shouldImportSwiftONoneSupport() const {
691+
if (getImplicitStdlibKind() != ImplicitStdlibKind::Stdlib)
693692
return false;
694-
if (Invocation.getSILOptions().shouldOptimize())
693+
if (getSILOptions().shouldOptimize())
695694
return false;
696695

697696
// If we are not executing an action that has a dependency on
@@ -706,7 +705,7 @@ static bool shouldImplicityImportSwiftOnoneSupportModule(
706705
//
707706
// This optimization is disabled by -track-system-dependencies to preserve
708707
// the explicit dependency.
709-
const auto &options = Invocation.getFrontendOptions();
708+
const auto &options = getFrontendOptions();
710709
return options.TrackSystemDeps
711710
|| FrontendOptions::doesActionGenerateSIL(options.RequestedAction);
712711
}
@@ -720,7 +719,7 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
720719
for (auto &moduleStr : frontendOpts.getImplicitImportModuleNames())
721720
imports.ModuleNames.push_back(Context->getIdentifier(moduleStr));
722721

723-
if (shouldImplicityImportSwiftOnoneSupportModule(Invocation))
722+
if (Invocation.shouldImportSwiftONoneSupport())
724723
imports.ModuleNames.push_back(Context->getIdentifier(SWIFT_ONONE_SUPPORT));
725724

726725
imports.ShouldImportUnderlyingModule = frontendOpts.ImportUnderlyingModule;

lib/Frontend/ModuleDependencyScanner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
104104
unsigned bufferID = Ctx.SourceMgr.addNewSourceBuffer(std::move(interfaceBuf.get()));
105105
auto moduleDecl = ModuleDecl::create(moduleName, Ctx);
106106
auto sourceFile = new (Ctx) SourceFile(
107-
*moduleDecl, SourceFileKind::Interface, bufferID,
108-
SourceFile::ImplicitModuleImportKind::None);
107+
*moduleDecl, SourceFileKind::Interface, bufferID);
109108

110109
// Create a module filename.
111110
// FIXME: Query the module interface loader to determine an appropriate

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,7 @@ static bool performCompile(CompilerInstance &Instance,
12671267
return Context.hadError();
12681268

12691269
if (Action == FrontendOptions::ActionType::ScanDependencies) {
1270-
scanDependencies(Context, Instance.getMainModule(),
1271-
Instance.getDependencyTracker(), opts);
1270+
scanDependencies(Instance);
12721271
}
12731272

12741273
(void)emitMakeDependenciesIfNeeded(Context.Diags,

lib/FrontendTool/ScanDependencies.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include "swift/Basic/Defer.h"
2323
#include "swift/Basic/LLVM.h"
2424
#include "swift/Basic/STLExtras.h"
25+
#include "swift/Frontend/Frontend.h"
2526
#include "swift/Frontend/FrontendOptions.h"
27+
#include "swift/Strings.h"
2628
#include "clang/Basic/Module.h"
2729
#include "llvm/ADT/SetVector.h"
2830
#include "llvm/ADT/StringMap.h"
@@ -316,9 +318,11 @@ static void writeJSON(llvm::raw_ostream &out,
316318
}
317319
}
318320

319-
bool swift::scanDependencies(ASTContext &Context, ModuleDecl *mainModule,
320-
DependencyTracker *depTracker,
321-
const FrontendOptions &opts) {
321+
bool swift::scanDependencies(CompilerInstance &instance) {
322+
ASTContext &Context = instance.getASTContext();
323+
ModuleDecl *mainModule = instance.getMainModule();
324+
const CompilerInvocation &invocation = instance.getInvocation();
325+
const FrontendOptions &opts = invocation.getFrontendOptions();
322326

323327
std::string path = opts.InputsAndOutputs.getSingleOutputFilename();
324328
std::error_code EC;
@@ -349,15 +353,43 @@ bool swift::scanDependencies(ASTContext &Context, ModuleDecl *mainModule,
349353
mainDependencies.addModuleDependencies(*sf, alreadyAddedModules);
350354
}
351355

356+
const auto &importInfo = mainModule->getImplicitImportInfo();
357+
358+
// Swift standard library.
359+
switch (importInfo.StdlibKind) {
360+
case ImplicitStdlibKind::None:
361+
case ImplicitStdlibKind::Builtin:
362+
break;
363+
364+
case ImplicitStdlibKind::Stdlib:
365+
mainDependencies.addModuleDependency("Swift", alreadyAddedModules);
366+
break;
367+
}
368+
369+
// Swift -Onone support library.
370+
if (invocation.shouldImportSwiftONoneSupport()) {
371+
mainDependencies.addModuleDependency(
372+
SWIFT_ONONE_SUPPORT, alreadyAddedModules);
373+
}
374+
375+
// Add any implicit module names.
376+
for (const auto &moduleName : importInfo.ModuleNames) {
377+
mainDependencies.addModuleDependency(moduleName.str(), alreadyAddedModules);
378+
}
379+
380+
// Already-loaded, implicitly imported module names.
381+
for (const auto &module : importInfo.AdditionalModules) {
382+
mainDependencies.addModuleDependency(module.first->getNameStr(), alreadyAddedModules);
383+
}
384+
352385
// Add the bridging header.
353-
StringRef implicitHeaderPath = opts.ImplicitObjCHeaderPath;
354-
if (!implicitHeaderPath.empty()) {
355-
mainDependencies.addBridgingHeader(implicitHeaderPath);
386+
if (!importInfo.BridgingHeaderPath.empty()) {
387+
mainDependencies.addBridgingHeader(importInfo.BridgingHeaderPath);
356388
}
357389

358390
// If we are to import the underlying Clang module of the same name,
359391
// add a dependency with the same name to trigger the search.
360-
if (opts.ImportUnderlyingModule) {
392+
if (importInfo.ShouldImportUnderlyingModule) {
361393
mainDependencies.addModuleDependency(mainModule->getName().str(),
362394
alreadyAddedModules);
363395
}
@@ -389,7 +421,7 @@ bool swift::scanDependencies(ASTContext &Context, ModuleDecl *mainModule,
389421
writeJSON(out, Context, cache, allModules.getArrayRef());
390422

391423
// Update the dependency tracker.
392-
if (depTracker) {
424+
if (auto depTracker = instance.getDependencyTracker()) {
393425
for (auto module : allModules) {
394426
auto deps = cache.findDependencies(module.first, module.second);
395427
if (!deps)

lib/FrontendTool/ScanDependencies.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515

1616
namespace swift {
1717

18-
class ASTContext;
19-
class DependencyTracker;
20-
class FrontendOptions;
21-
class ModuleDecl;
18+
class CompilerInstance;
2219

23-
/// Scans the dependencies of \c mainModule.
24-
bool scanDependencies(ASTContext &Context, ModuleDecl *mainModule,
25-
DependencyTracker *depTracker,
26-
const FrontendOptions &opts);
20+
/// Scans the dependencies of the main module of \c instance.
21+
bool scanDependencies(CompilerInstance &instance);
2722

2823
} // end namespace swift
2924

test/ScanDependencies/module_deps.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ import E
3636
// CHECK-NEXT: "swift": "E"
3737
// CHECK-NEXT: }
3838
// CHECK-NEXT: {
39+
// CHECK-NEXT: "swift": "Swift"
40+
// CHECK-NEXT: }
41+
// CHECK-NEXT: {
42+
// CHECK-NEXT: "swift": "SwiftOnoneSupport"
43+
// CHECK-NEXT: }
44+
// CHECK-NEXT: {
3945
// CHECK-NEXT: "swift": "F"
4046
// CHECK-NEXT: }
4147
// CHECK-NEXT: {
@@ -78,6 +84,13 @@ import E
7884
// CHECK: "moduleInterfacePath"
7985
// CHECK-SAME: E.swiftinterface
8086

87+
/// --------Swift module Swift
88+
// CHECK-LABEL: "modulePath": "Swift.swiftmodule",
89+
90+
// CHECK: directDependencies
91+
// CHECK-NEXT: {
92+
// CHECK-NEXT: "clang": "SwiftShims"
93+
8194
/// --------Swift module A
8295
// CHECK-LABEL: "modulePath": "A.swiftmodule",
8396

@@ -98,13 +111,6 @@ import E
98111
// CHECK-NEXT: "clang": "A"
99112
// CHECK-NEXT: }
100113

101-
/// --------Swift module Swift
102-
// CHECK-LABEL: "modulePath": "Swift.swiftmodule",
103-
104-
// CHECK: directDependencies
105-
// CHECK-NEXT: {
106-
// CHECK-NEXT: "clang": "SwiftShims"
107-
108114
/// --------Clang module SwiftShims
109115
// CHECK-LABEL: "modulePath": "SwiftShims.pcm",
110116

0 commit comments

Comments
 (0)