|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +public import SWBUtil |
| 14 | +import SWBMacro |
| 15 | + |
| 16 | +public struct ModuleDependency: Hashable, Sendable, SerializableCodable { |
| 17 | + public let name: String |
| 18 | + public let accessLevel: AccessLevel |
| 19 | + |
| 20 | + public enum AccessLevel: String, Hashable, Sendable, CaseIterable, Codable, Serializable { |
| 21 | + case Private = "private" |
| 22 | + case Package = "package" |
| 23 | + case Public = "public" |
| 24 | + |
| 25 | + public init(_ string: String) throws { |
| 26 | + guard let accessLevel = AccessLevel(rawValue: string) else { |
| 27 | + throw StubError.error("unexpected access modifier '\(string)', expected one of: \(AccessLevel.allCases.map { $0.rawValue }.joined(separator: ", "))") |
| 28 | + } |
| 29 | + |
| 30 | + self = accessLevel |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public init(name: String, accessLevel: AccessLevel) { |
| 35 | + self.name = name |
| 36 | + self.accessLevel = accessLevel |
| 37 | + } |
| 38 | + |
| 39 | + public init(entry: String) throws { |
| 40 | + var it = entry.split(separator: " ").makeIterator() |
| 41 | + switch (it.next(), it.next(), it.next()) { |
| 42 | + case (let .some(name), nil, nil): |
| 43 | + self.name = String(name) |
| 44 | + self.accessLevel = .Private |
| 45 | + |
| 46 | + case (let .some(accessLevel), let .some(name), nil): |
| 47 | + self.name = String(name) |
| 48 | + self.accessLevel = try AccessLevel(String(accessLevel)) |
| 49 | + |
| 50 | + default: |
| 51 | + throw StubError.error("expected 1 or 2 space-separated components in: \(entry)") |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public var asBuildSettingEntry: String { |
| 56 | + "\(accessLevel == .Private ? "" : "\(accessLevel.rawValue) ")\(name)" |
| 57 | + } |
| 58 | + |
| 59 | + public var asBuildSettingEntryQuotedIfNeeded: String { |
| 60 | + let e = asBuildSettingEntry |
| 61 | + return e.contains(" ") ? "\"\(e)\"" : e |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +public struct ModuleDependenciesContext: Sendable, SerializableCodable { |
| 66 | + var validate: BooleanWarningLevel |
| 67 | + var moduleDependencies: [ModuleDependency] |
| 68 | + var fixItContext: FixItContext? |
| 69 | + |
| 70 | + init(validate: BooleanWarningLevel, moduleDependencies: [ModuleDependency], fixItContext: FixItContext? = nil) { |
| 71 | + self.validate = validate |
| 72 | + self.moduleDependencies = moduleDependencies |
| 73 | + self.fixItContext = fixItContext |
| 74 | + } |
| 75 | + |
| 76 | + public init?(settings: Settings) { |
| 77 | + let validate = settings.globalScope.evaluate(BuiltinMacros.VALIDATE_MODULE_DEPENDENCIES) |
| 78 | + guard validate != .no else { return nil } |
| 79 | + let fixItContext = ModuleDependenciesContext.FixItContext(settings: settings) |
| 80 | + self.init(validate: validate, moduleDependencies: settings.moduleDependencies, fixItContext: fixItContext) |
| 81 | + } |
| 82 | + |
| 83 | + /// Nil `imports` means the current toolchain doesn't have the features to gather imports. This is temporarily required to support running against older toolchains. |
| 84 | + public func makeDiagnostics(imports: [(ModuleDependency, importLocations: [Diagnostic.Location])]?) -> [Diagnostic] { |
| 85 | + guard validate != .no else { return [] } |
| 86 | + guard let imports else { |
| 87 | + return [Diagnostic( |
| 88 | + behavior: .error, |
| 89 | + location: .unknown, |
| 90 | + data: DiagnosticData("The current toolchain does not support \(BuiltinMacros.VALIDATE_MODULE_DEPENDENCIES.name)"))] |
| 91 | + } |
| 92 | + |
| 93 | + let missingDeps = imports.filter { |
| 94 | + // ignore module deps without source locations, these are inserted by swift / swift-build and we should treat them as implementation details which we can track without needing the user to declare them |
| 95 | + if $0.importLocations.isEmpty { return false } |
| 96 | + |
| 97 | + // TODO: if the difference is just the access modifier, we emit a new entry, but ultimately our fixit should update the existing entry or emit an error about a conflict |
| 98 | + if moduleDependencies.contains($0.0) { return false } |
| 99 | + return true |
| 100 | + } |
| 101 | + |
| 102 | + guard !missingDeps.isEmpty else { return [] } |
| 103 | + |
| 104 | + let behavior: Diagnostic.Behavior = validate == .yesError ? .error : .warning |
| 105 | + |
| 106 | + let fixIt = fixItContext?.makeFixIt(newModules: missingDeps.map { $0.0 }) |
| 107 | + let fixIts = fixIt.map { [$0] } ?? [] |
| 108 | + |
| 109 | + let importDiags: [Diagnostic] = missingDeps |
| 110 | + .flatMap { dep in |
| 111 | + dep.1.map { |
| 112 | + return Diagnostic( |
| 113 | + behavior: behavior, |
| 114 | + location: $0, |
| 115 | + data: DiagnosticData("Missing entry in \(BuiltinMacros.MODULE_DEPENDENCIES.name): \(dep.0.asBuildSettingEntryQuotedIfNeeded)"), |
| 116 | + fixIts: fixIts) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + let message = "Missing entries in \(BuiltinMacros.MODULE_DEPENDENCIES.name): \(missingDeps.map { $0.0.asBuildSettingEntryQuotedIfNeeded }.sorted().joined(separator: " "))" |
| 121 | + |
| 122 | + let location: Diagnostic.Location = fixIt.map { |
| 123 | + Diagnostic.Location.path($0.sourceRange.path, line: $0.sourceRange.endLine, column: $0.sourceRange.endColumn) |
| 124 | + } ?? Diagnostic.Location.buildSetting(BuiltinMacros.MODULE_DEPENDENCIES) |
| 125 | + |
| 126 | + return [Diagnostic( |
| 127 | + behavior: behavior, |
| 128 | + location: location, |
| 129 | + data: DiagnosticData(message), |
| 130 | + fixIts: fixIts, |
| 131 | + childDiagnostics: importDiags)] |
| 132 | + } |
| 133 | + |
| 134 | + struct FixItContext: Sendable, SerializableCodable { |
| 135 | + var sourceRange: Diagnostic.SourceRange |
| 136 | + var modificationStyle: ModificationStyle |
| 137 | + |
| 138 | + init(sourceRange: Diagnostic.SourceRange, modificationStyle: ModificationStyle) { |
| 139 | + self.sourceRange = sourceRange |
| 140 | + self.modificationStyle = modificationStyle |
| 141 | + } |
| 142 | + |
| 143 | + init?(settings: Settings) { |
| 144 | + guard let target = settings.target else { return nil } |
| 145 | + let thisTargetCondition = MacroCondition(parameter: BuiltinMacros.targetNameCondition, valuePattern: target.name) |
| 146 | + |
| 147 | + if let assignment = (settings.globalScope.table.lookupMacro(BuiltinMacros.MODULE_DEPENDENCIES)?.sequence.first { |
| 148 | + $0.location != nil && ($0.conditions?.conditions == [thisTargetCondition] || ($0.conditions?.conditions.isEmpty ?? true)) |
| 149 | + }), |
| 150 | + let location = assignment.location |
| 151 | + { |
| 152 | + self.init(sourceRange: .init(path: location.path, startLine: location.endLine, startColumn: location.endColumn, endLine: location.endLine, endColumn: location.endColumn), modificationStyle: .appendToExistingAssignment) |
| 153 | + } |
| 154 | + else if let path = settings.constructionComponents.targetXcconfigPath { |
| 155 | + self.init(sourceRange: .init(path: path, startLine: 0, startColumn: 0, endLine: 0, endColumn: 0), modificationStyle: .insertNewAssignment(targetNameCondition: nil)) |
| 156 | + } |
| 157 | + else if let path = settings.constructionComponents.projectXcconfigPath { |
| 158 | + self.init(sourceRange: .init(path: path, startLine: 0, startColumn: 0, endLine: 0, endColumn: 0), modificationStyle: .insertNewAssignment(targetNameCondition: target.name)) |
| 159 | + } |
| 160 | + else { |
| 161 | + return nil |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + enum ModificationStyle: Sendable, SerializableCodable, Hashable { |
| 166 | + case appendToExistingAssignment |
| 167 | + case insertNewAssignment(targetNameCondition: String?) |
| 168 | + } |
| 169 | + |
| 170 | + func makeFixIt(newModules: [ModuleDependency]) -> Diagnostic.FixIt { |
| 171 | + let stringValue = newModules.map { $0.asBuildSettingEntryQuotedIfNeeded }.sorted().joined(separator: " ") |
| 172 | + let newText: String |
| 173 | + switch modificationStyle { |
| 174 | + case .appendToExistingAssignment: |
| 175 | + newText = " \(stringValue)" |
| 176 | + case .insertNewAssignment(let targetNameCondition): |
| 177 | + let targetCondition = targetNameCondition.map { "[target=\($0)]" } ?? "" |
| 178 | + newText = "\n\(BuiltinMacros.MODULE_DEPENDENCIES.name)\(targetCondition) = $(inherited) \(stringValue)\n" |
| 179 | + } |
| 180 | + |
| 181 | + return Diagnostic.FixIt(sourceRange: sourceRange, newText: newText) |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments