Skip to content

Commit cefbd1d

Browse files
author
David Ungar
committed
Clean it up
1 parent 60e12b8 commit cefbd1d

File tree

3 files changed

+59
-27
lines changed

3 files changed

+59
-27
lines changed

Sources/SwiftDriver/IncrementalCompilation/DependencyKey.swift

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,21 +370,61 @@ extension DependencyKey.Designator: Comparable {
370370

371371
// MARK: - InvalidationReason
372372
extension ExternalDependency {
373-
public enum InvalidationReason: String, CustomStringConvertible {
374-
case added, changed, testing
375-
init?(_ graph: ModuleDependencyGraph,
376-
isNewToTheGraph: Bool ,
377-
_ externalDependency: ExternalDependency) {
378-
if isNewToTheGraph {
373+
/// When explaining incremental decisions, it helps to know why a particular external dependency
374+
/// was investigated.
375+
public enum Why: String, CustomStringConvertible {
376+
/// An `Import` of this file was added to the source code.
377+
case added
378+
379+
/// The imported file has changed.
380+
case changed
381+
382+
/// Used when testing invalidation
383+
case testing
384+
385+
/// Figure out the reason to invalidate or process a dependency.
386+
/// Even if invalidation won't be reported to the caller, a new or added
387+
/// incremental external dependency may require integration in order to
388+
/// transitively close them, (e.g. if an imported module imports a module).
389+
init?(should fed: FingerprintedExternalDependency,
390+
whichIsNewToTheGraph isNewToTheGraph: Bool,
391+
closeOverSwiftModulesIn graph: ModuleDependencyGraph) {
392+
guard graph.info.isCrossModuleIncrementalBuildEnabled
393+
else { return nil }
394+
self.init(should: fed,
395+
whichIsSignificantlyNew: isNewToTheGraph,
396+
beInvestigatedIn: graph)
397+
}
398+
399+
400+
init?(shouldUsesOf fed: FingerprintedExternalDependency,
401+
whichIsNewToTheGraph isNewToTheGraph: Bool,
402+
beInvalidatedIn graph: ModuleDependencyGraph) {
403+
if graph.phase.isCompilingAllInputsNoMatterWhat {
404+
// going to compile every input anyway, less work for callers
405+
return nil
406+
}
407+
let isSignificantlyNew = graph.phase.shouldNewExternalDependenciesTriggerInvalidation && isNewToTheGraph
408+
self.init(should: fed,
409+
whichIsSignificantlyNew: isSignificantlyNew,
410+
beInvestigatedIn: graph)
411+
}
412+
413+
/// Figure out the reason to invalidate or process a dependency
414+
private init?(should fed: FingerprintedExternalDependency,
415+
whichIsSignificantlyNew isSignificantlyNew: Bool,
416+
beInvestigatedIn graph: ModuleDependencyGraph) {
417+
if isSignificantlyNew {
379418
self = .added
380419
return
381420
}
382-
if graph.hasFileChanged(of: externalDependency) {
421+
if graph.hasFileChanged(of: fed.externalDependency) {
383422
self = .changed
384423
return
385424
}
386425
return nil
387426
}
427+
388428
public var description: String { rawValue }
389429
}
390430
}

Sources/SwiftDriver/IncrementalCompilation/IncrementalCompilationState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ extension IncrementalCompilationState {
432432
func reportInvalidated<Nodes: Sequence>(
433433
_ nodes: Nodes,
434434
by externalDependency: ExternalDependency,
435-
_ why: ExternalDependency.InvalidationReason
435+
_ why: ExternalDependency.Why
436436
)
437437
where Nodes.Element == ModuleDependencyGraph.Node
438438
{

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ extension ModuleDependencyGraph {
265265
/// As an optimization, only return the nodes that have not been already traced, because the traced nodes
266266
/// will have already been used to schedule jobs to run.
267267
/*@_spi(Testing)*/ public func collectUntracedNodesUsing(
268-
_ why: ExternalDependency.InvalidationReason,
268+
_ why: ExternalDependency.Why,
269269
_ fingerprintedExternalDependency: FingerprintedExternalDependency
270270
) -> DirectlyInvalidatedNodeSet {
271271
// These nodes will depend on the *interface* of the external Decl.
@@ -336,30 +336,22 @@ extension ModuleDependencyGraph {
336336
isPresentInTheGraph: Bool?)
337337
-> DirectlyInvalidatedNodeSet {
338338

339+
/// Compute this up front as an optimization.
339340
let isNewToTheGraph = isPresentInTheGraph != true && fingerprintedExternalDependencies.insert(fed).inserted
340341

341-
// Even if invalidation won't be reported to the caller, a new or added
342-
// incremental external dependency may require integration in order to
343-
// transitively close them, (e.g. if an imported module imports a module).
344-
let whyIntegrateForClosure = info.isCrossModuleIncrementalBuildEnabled
345-
? ExternalDependency.InvalidationReason(self,
346-
isNewToTheGraph: isNewToTheGraph,
347-
fed.externalDependency)
348-
: nil
342+
let whyIntegrateForClosure = ExternalDependency.Why(
343+
should: fed,
344+
whichIsNewToTheGraph: isNewToTheGraph,
345+
closeOverSwiftModulesIn: self)
349346

350347
let invalidatedNodesFromIncrementalExternal = whyIntegrateForClosure.flatMap { why in
351348
collectNodesInvalidatedByAttemptingToProcess(why, fed)
352349
}
353350

354-
if phase.isCompilingAllInputsNoMatterWhat {
355-
// going to compile every input anyway, less work for callers
356-
return DirectlyInvalidatedNodeSet()
357-
}
358-
359-
guard let whyInvalidate = ExternalDependency.InvalidationReason(
360-
self,
361-
isNewToTheGraph: phase.shouldNewExternalDependenciesTriggerInvalidation && isNewToTheGraph,
362-
fed.externalDependency)
351+
guard let whyInvalidate = ExternalDependency.Why(
352+
shouldUsesOf: fed,
353+
whichIsNewToTheGraph: isNewToTheGraph,
354+
beInvalidatedIn: self)
363355
else {
364356
return DirectlyInvalidatedNodeSet()
365357
}
@@ -386,7 +378,7 @@ extension ModuleDependencyGraph {
386378
/// Try to read and integrate an external dependency.
387379
/// Return nil if it's not incremental, or if an error occurs.
388380
private func collectNodesInvalidatedByAttemptingToProcess(
389-
_ why: ExternalDependency.InvalidationReason,
381+
_ why: ExternalDependency.Why,
390382
_ fed: FingerprintedExternalDependency
391383
) -> DirectlyInvalidatedNodeSet? {
392384
guard let source = fed.incrementalDependencySource,

0 commit comments

Comments
 (0)