Skip to content

Commit 5c27465

Browse files
authored
Merge pull request swiftlang#32601 from hamishknight/not-yet-over-and-out
2 parents 4f27eee + 553dadd commit 5c27465

File tree

13 files changed

+173
-141
lines changed

13 files changed

+173
-141
lines changed

include/swift/AST/ModuleLoader.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,22 @@ enum class Bridgeability : unsigned {
6060
Full
6161
};
6262

63+
/// Specifies which dependencies the intermodule dependency tracker records.
64+
enum class IntermoduleDepTrackingMode {
65+
/// Records both system and non-system dependencies.
66+
IncludeSystem,
67+
68+
/// Records only non-system dependencies.
69+
ExcludeSystem,
70+
};
71+
6372
/// Records dependencies on files outside of the current module;
6473
/// implemented in terms of a wrapped clang::DependencyCollector.
6574
class DependencyTracker {
6675
std::shared_ptr<clang::DependencyCollector> clangCollector;
6776
public:
6877
explicit DependencyTracker(
69-
bool TrackSystemDeps,
78+
IntermoduleDepTrackingMode Mode,
7079
std::shared_ptr<llvm::FileCollector> FileCollector = {});
7180

7281
/// Adds a file as a dependency.

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class ClangImporter final : public ClangModuleLoader {
175175
/// Create a new clang::DependencyCollector customized to
176176
/// ClangImporter's specific uses.
177177
static std::shared_ptr<clang::DependencyCollector>
178-
createDependencyCollector(bool TrackSystemDeps,
178+
createDependencyCollector(IntermoduleDepTrackingMode Mode,
179179
std::shared_ptr<llvm::FileCollector> FileCollector);
180180

181181
/// Append visible module names to \p names. Note that names are possibly

include/swift/Frontend/Frontend.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,6 @@ class CompilerInstance {
498498
Diagnostics.removeConsumer(*DC);
499499
}
500500

501-
void createDependencyTracker(bool TrackSystemDeps) {
502-
assert(!Context && "must be called before setup()");
503-
DepTracker = std::make_unique<DependencyTracker>(TrackSystemDeps);
504-
}
505501
DependencyTracker *getDependencyTracker() { return DepTracker.get(); }
506502
const DependencyTracker *getDependencyTracker() const { return DepTracker.get(); }
507503

@@ -582,6 +578,7 @@ class CompilerInstance {
582578
bool setUpASTContextIfNeeded();
583579
void setupStatsReporter();
584580
void setupDiagnosticVerifierIfNeeded();
581+
void setupDependencyTrackerIfNeeded();
585582
Optional<unsigned> setUpCodeCompletionBuffer();
586583

587584
/// Set up all state in the CompilerInstance to process the given input file.

include/swift/Frontend/FrontendOptions.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace llvm {
2727
}
2828

2929
namespace swift {
30-
30+
enum class IntermoduleDepTrackingMode;
3131

3232
/// Options for controlling the behavior of the frontend.
3333
class FrontendOptions {
@@ -237,9 +237,10 @@ class FrontendOptions {
237237
/// See the \ref SILOptions.EmitSortedSIL flag.
238238
bool EmitSortedSIL = false;
239239

240-
/// Indicates whether the dependency tracker should track system
241-
/// dependencies as well.
242-
bool TrackSystemDeps = false;
240+
/// Specifies the collection mode for the intermodule dependency tracker.
241+
/// Note that if set, the dependency tracker will be enabled even if no
242+
/// output path is configured.
243+
Optional<IntermoduleDepTrackingMode> IntermoduleDependencyTracking;
243244

244245
/// Should we serialize the hashes of dependencies (vs. the modification
245246
/// times) when compiling a module interface?
@@ -315,6 +316,9 @@ class FrontendOptions {
315316
return ImplicitImportModuleNames;
316317
}
317318

319+
/// Whether we're configured to track system intermodule dependencies.
320+
bool shouldTrackSystemDependencies() const;
321+
318322
private:
319323
static bool canActionEmitDependencies(ActionType);
320324
static bool canActionEmitReferenceDependencies(ActionType);

lib/AST/ModuleLoader.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ class FileCollector;
3030
namespace swift {
3131

3232
DependencyTracker::DependencyTracker(
33-
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector)
33+
IntermoduleDepTrackingMode Mode,
34+
std::shared_ptr<llvm::FileCollector> FileCollector)
3435
// NB: The ClangImporter believes it's responsible for the construction of
3536
// this instance, and it static_cast<>s the instance pointer to its own
3637
// subclass based on that belief. If you change this to be some other
3738
// instance, you will need to change ClangImporter's code to handle the
3839
// difference.
39-
: clangCollector(ClangImporter::createDependencyCollector(TrackSystemDeps,
40-
FileCollector)) {}
40+
: clangCollector(
41+
ClangImporter::createDependencyCollector(Mode, FileCollector)) {}
4142

4243
void
4344
DependencyTracker::addDependency(StringRef File, bool IsSystem) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,13 @@ class ClangImporterDependencyCollector : public clang::DependencyCollector
328328
/// The FileCollector is used by LLDB to generate reproducers. It's not used
329329
/// by Swift to track dependencies.
330330
std::shared_ptr<llvm::FileCollector> FileCollector;
331-
const bool TrackSystemDeps;
331+
const IntermoduleDepTrackingMode Mode;
332332

333333
public:
334334
ClangImporterDependencyCollector(
335-
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector)
336-
: FileCollector(FileCollector), TrackSystemDeps(TrackSystemDeps) {}
335+
IntermoduleDepTrackingMode Mode,
336+
std::shared_ptr<llvm::FileCollector> FileCollector)
337+
: FileCollector(FileCollector), Mode(Mode) {}
337338

338339
void excludePath(StringRef filename) {
339340
ExcludedPaths.insert(filename);
@@ -345,7 +346,9 @@ class ClangImporterDependencyCollector : public clang::DependencyCollector
345346
|| Filename == ImporterImpl::bridgingHeaderBufferName);
346347
}
347348

348-
bool needSystemDependencies() override { return TrackSystemDeps; }
349+
bool needSystemDependencies() override {
350+
return Mode == IntermoduleDepTrackingMode::IncludeSystem;
351+
}
349352

350353
bool sawDependency(StringRef Filename, bool FromClangModule,
351354
bool IsSystem, bool IsClangModuleFile,
@@ -375,8 +378,9 @@ class ClangImporterDependencyCollector : public clang::DependencyCollector
375378

376379
std::shared_ptr<clang::DependencyCollector>
377380
ClangImporter::createDependencyCollector(
378-
bool TrackSystemDeps, std::shared_ptr<llvm::FileCollector> FileCollector) {
379-
return std::make_shared<ClangImporterDependencyCollector>(TrackSystemDeps,
381+
IntermoduleDepTrackingMode Mode,
382+
std::shared_ptr<llvm::FileCollector> FileCollector) {
383+
return std::make_shared<ClangImporterDependencyCollector>(Mode,
380384
FileCollector);
381385
}
382386

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,19 @@ bool ArgsToFrontendOptionsConverter::convert(
8181

8282
Opts.EnableImplicitDynamic |= Args.hasArg(OPT_enable_implicit_dynamic);
8383

84-
Opts.TrackSystemDeps |= Args.hasArg(OPT_track_system_dependencies);
84+
if (Args.hasArg(OPT_track_system_dependencies)) {
85+
Opts.IntermoduleDependencyTracking =
86+
IntermoduleDepTrackingMode::IncludeSystem;
87+
}
8588

8689
Opts.DisableImplicitModules |= Args.hasArg(OPT_disable_implicit_swift_modules);
8790

8891
// Always track system dependencies when scanning dependencies.
8992
if (const Arg *ModeArg = Args.getLastArg(OPT_modes_Group)) {
90-
if (ModeArg->getOption().matches(OPT_scan_dependencies))
91-
Opts.TrackSystemDeps = true;
93+
if (ModeArg->getOption().matches(OPT_scan_dependencies)) {
94+
Opts.IntermoduleDependencyTracking =
95+
IntermoduleDepTrackingMode::IncludeSystem;
96+
}
9297
}
9398

9499
Opts.SerializeModuleInterfaceDependencyHashes |=

lib/Frontend/Frontend.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,36 @@ void CompilerInstance::setupDiagnosticVerifierIfNeeded() {
288288
}
289289
}
290290

291+
void CompilerInstance::setupDependencyTrackerIfNeeded() {
292+
assert(!Context && "Must be called before the ASTContext is created");
293+
294+
const auto &Invocation = getInvocation();
295+
const auto &opts = Invocation.getFrontendOptions();
296+
297+
// Note that we may track dependencies even when we don't need to write them
298+
// directly; in particular, -track-system-dependencies affects how module
299+
// interfaces get loaded, and so we need to be consistently tracking system
300+
// dependencies throughout the compiler.
301+
auto collectionMode = opts.IntermoduleDependencyTracking;
302+
if (!collectionMode) {
303+
// If we have an output path specified, but no other tracking options,
304+
// default to non-system dependency tracking.
305+
if (opts.InputsAndOutputs.hasDependencyTrackerPath() ||
306+
!opts.IndexStorePath.empty()) {
307+
collectionMode = IntermoduleDepTrackingMode::ExcludeSystem;
308+
}
309+
}
310+
if (!collectionMode)
311+
return;
312+
313+
DepTracker = std::make_unique<DependencyTracker>(*collectionMode);
314+
}
315+
291316
bool CompilerInstance::setup(const CompilerInvocation &Invok) {
292317
Invocation = Invok;
293318

319+
setupDependencyTrackerIfNeeded();
320+
294321
// If initializing the overlay file system fails there's no sense in
295322
// continuing because the compiler will read the wrong files.
296323
if (setUpVirtualFileSystemOverlays())
@@ -685,8 +712,8 @@ bool CompilerInvocation::shouldImportSwiftONoneSupport() const {
685712
// This optimization is disabled by -track-system-dependencies to preserve
686713
// the explicit dependency.
687714
const auto &options = getFrontendOptions();
688-
return options.TrackSystemDeps
689-
|| FrontendOptions::doesActionGenerateSIL(options.RequestedAction);
715+
return options.shouldTrackSystemDependencies() ||
716+
FrontendOptions::doesActionGenerateSIL(options.RequestedAction);
690717
}
691718

692719
ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {

lib/Frontend/FrontendOptions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/Frontend/FrontendOptions.h"
1414

1515
#include "swift/AST/DiagnosticsFrontend.h"
16+
#include "swift/AST/ModuleLoader.h"
1617
#include "swift/Option/Options.h"
1718
#include "swift/Parse/Lexer.h"
1819
#include "swift/Strings.h"
@@ -592,3 +593,8 @@ const PrimarySpecificPaths &
592593
FrontendOptions::getPrimarySpecificPathsForPrimary(StringRef filename) const {
593594
return InputsAndOutputs.getPrimarySpecificPathsForPrimary(filename);
594595
}
596+
597+
bool FrontendOptions::shouldTrackSystemDependencies() const {
598+
return IntermoduleDependencyTracking ==
599+
IntermoduleDepTrackingMode::IncludeSystem;
600+
}

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,9 +1126,14 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
11261126
GenericArgs.push_back("-prebuilt-module-cache-path");
11271127
GenericArgs.push_back(prebuiltCachePath);
11281128
}
1129-
subInvocation.getFrontendOptions().TrackSystemDeps = trackSystemDependencies;
11301129
if (trackSystemDependencies) {
1130+
subInvocation.getFrontendOptions().IntermoduleDependencyTracking =
1131+
IntermoduleDepTrackingMode::IncludeSystem;
11311132
GenericArgs.push_back("-track-system-dependencies");
1133+
} else {
1134+
// Always track at least the non-system dependencies for interface building.
1135+
subInvocation.getFrontendOptions().IntermoduleDependencyTracking =
1136+
IntermoduleDepTrackingMode::ExcludeSystem;
11321137
}
11331138
if (LoaderOpts.disableImplicitSwiftModule) {
11341139
subInvocation.getFrontendOptions().DisableImplicitModules = true;
@@ -1252,7 +1257,7 @@ InterfaceSubContextDelegateImpl::getCacheHash(StringRef useInterfacePath) {
12521257

12531258
// Whether or not we're tracking system dependencies affects the
12541259
// invalidation behavior of this cache item.
1255-
subInvocation.getFrontendOptions().TrackSystemDeps);
1260+
subInvocation.getFrontendOptions().shouldTrackSystemDependencies());
12561261

12571262
return llvm::APInt(64, H).toString(36, /*Signed=*/false);
12581263
}
@@ -1330,8 +1335,6 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
13301335

13311336
ForwardingDiagnosticConsumer FDC(Diags);
13321337
subInstance.addDiagnosticConsumer(&FDC);
1333-
subInstance.createDependencyTracker(subInvocation.getFrontendOptions()
1334-
.TrackSystemDeps);
13351338
if (subInstance.setup(subInvocation)) {
13361339
return true;
13371340
}

0 commit comments

Comments
 (0)