Skip to content

Commit c82775b

Browse files
committed
Address code review feedback
1 parent 523a7b2 commit c82775b

File tree

6 files changed

+94
-39
lines changed

6 files changed

+94
-39
lines changed

Sources/SwiftIfConfig/StaticBuildConfiguration.swift

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public struct StaticBuildConfiguration: Codable {
2929
customConditions: Set<String> = [],
3030
features: Set<String> = [],
3131
attributes: Set<String> = [],
32-
targetOSNames: Set<String> = [],
32+
targetOSs: Set<String> = [],
3333
targetArchitectures: Set<String> = [],
3434
targetEnvironments: Set<String> = [],
3535
targetRuntimes: Set<String> = [],
@@ -44,7 +44,7 @@ public struct StaticBuildConfiguration: Codable {
4444
self.customConditions = customConditions
4545
self.features = features
4646
self.attributes = attributes
47-
self.targetOSNames = targetOSNames
47+
self.targetOSs = targetOSs
4848
self.targetArchitectures = targetArchitectures
4949
self.targetEnvironments = targetEnvironments
5050
self.targetRuntimes = targetRuntimes
@@ -53,7 +53,7 @@ public struct StaticBuildConfiguration: Codable {
5353
self.targetPointerBitWidth = targetPointerBitWidth
5454
self.targetAtomicBitWidths = targetAtomicBitWidths
5555
self.endianness = endianness
56-
self.languageVersion = languageVersion
56+
self.languageMode = languageVersion
5757
self.compilerVersion = compilerVersion
5858
}
5959

@@ -95,22 +95,76 @@ public struct StaticBuildConfiguration: Codable {
9595
/// ```
9696
public var attributes: Set<String> = []
9797

98-
/// The active target OS names, e.g., "Windows", "iOS".
99-
public var targetOSNames: Set<String> = []
98+
/// The active target OS, e.g., "Windows", "iOS".
99+
///
100+
/// The target operating system can be queried with `os(<name>)`, e.g.,
101+
///
102+
/// ```swift
103+
/// #if os(Linux)
104+
/// // Linux-specific implementation
105+
/// #endif
106+
/// ```
107+
public var targetOSs: Set<String> = []
100108

101109
/// The active target architectures, e.g., "x64_64".
110+
///
111+
/// The target processor architecture can be queried with `arch(<name>)`, e.g.,
112+
///
113+
/// ```swift
114+
/// #if arch(x86_64)
115+
/// // 64-bit x86 Intel-specific code
116+
/// #endif
117+
/// ```
102118
public var targetArchitectures: Set<String> = []
103119

104120
/// The active target environments, e.g., "simulator".
121+
///
122+
/// The target environment can be queried with `targetEnvironment(<name>)`,
123+
/// e.g.,
124+
///
125+
/// ```swift
126+
/// #if targetEnvironment(simulator)
127+
/// // Simulator-specific code
128+
/// #endif
129+
/// ```
105130
public var targetEnvironments: Set<String> = []
106131

107132
/// The active target runtimes, e.g., _ObjC.
133+
///
134+
/// The target runtime can only be queried by an experimental syntax
135+
/// `_runtime(<name>)`, e.g.,
136+
///
137+
/// ```swift
138+
/// #if _runtime(_ObjC)
139+
/// // Code that depends on Swift being built for use with the Objective-C
140+
/// // runtime, e.g., on Apple platforms.
141+
/// #endif
142+
/// ```
108143
public var targetRuntimes: Set<String> = []
109144

110145
/// The active target's pointer authentication schemes, e.g., "arm64e".
146+
///
147+
/// The target pointer authentication scheme describes how pointers are
148+
/// signed, as a security mitigation. This scheme can only be queried by
149+
/// an experimental syntax `_ptrath(<name>)`, e.g.,
150+
///
151+
/// ```swift
152+
/// #if _ptrauth(arm64e)
153+
/// // Special logic for arm64e pointer signing
154+
/// #endif
155+
/// ```
111156
public var targetPointerAuthenticationSchemes: Set<String> = []
112157

113158
/// The active target's object file formats, e.g., "COFF"
159+
///
160+
/// The target object file format can only be queried by an experimental
161+
/// syntax `_objectFileFormat(<name>)`, e.g.,
162+
///
163+
/// ```swift
164+
/// #if _objectFileFormat(ELF)
165+
/// // Special logic for ELF object file formats
166+
/// #endif
167+
/// ```
114168
public var targetObjectFileFormats: Set<String> = []
115169

116170
/// The bit width of a data pointer for the target architecture.
@@ -151,17 +205,17 @@ public struct StaticBuildConfiguration: Codable {
151205
/// ```
152206
public var endianness: Endianness = .little
153207

154-
/// The effective language version, which can be set by the user (e.g., 5.0).
208+
/// The effective language mode, which can be set by the user (e.g., 5.0).
155209
///
156210
/// The language version can be queried with the `swift` directive that checks
157211
/// how the supported language version compares, as described by
158212
/// [SE-0212](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
159213
///
160214
/// ```swift
161-
/// #if swift(>=5.5)
215+
/// #if swift(>=6.0)
162216
/// // Hooray, we can use tasks!
163217
/// ```
164-
public var languageVersion: VersionTuple
218+
public var languageMode: VersionTuple
165219

166220
/// The version of the compiler (e.g., 5.9).
167221
///
@@ -264,7 +318,7 @@ extension StaticBuildConfiguration: BuildConfiguration {
264318
/// - Returns: Whether the given operating system name is the target operating
265319
/// system, i.e., the operating system for which code is being generated.
266320
public func isActiveTargetOS(name: String) -> Bool {
267-
targetOSNames.contains(name)
321+
targetOSs.contains(name)
268322
}
269323

270324
/// Determine whether the given name is the active target architecture
@@ -363,6 +417,12 @@ extension StaticBuildConfiguration: BuildConfiguration {
363417
public func isActiveTargetObjectFileFormat(name: String) -> Bool {
364418
targetObjectFileFormats.contains(name)
365419
}
420+
421+
/// Equivalent to `languageMode`, but required for conformance to the
422+
/// `BuildConfiguration` protocol.
423+
public var languageVersion: VersionTuple {
424+
languageMode
425+
}
366426
}
367427

368428
extension StaticBuildConfiguration {
@@ -376,11 +436,11 @@ extension StaticBuildConfiguration {
376436
extension StaticBuildConfiguration {
377437
/// The Swift version that can be set for the parser.
378438
public var parserSwiftVersion: Parser.SwiftVersion {
379-
if languageVersion < VersionTuple(5) {
439+
if languageMode < VersionTuple(5) {
380440
return .v4
381-
} else if languageVersion < VersionTuple(6) {
441+
} else if languageMode < VersionTuple(6) {
382442
return .v5
383-
} else if languageVersion < VersionTuple(7) {
443+
} else if languageMode < VersionTuple(7) {
384444
return .v6
385445
} else {
386446
return Parser.defaultSwiftVersion

Sources/SwiftSyntaxMacros/MacroExpansionContext.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public protocol MacroExpansionContext: AnyObject {
7777
/// source code. Macro implementations can use this information to determine
7878
/// the context into which they are generating code.
7979
///
80-
/// When the build configuration is not known, this returns nil.
80+
/// When the build configuration is not known, for example because the
81+
/// compiler has not provided this information to the macro implementation,
82+
/// this returns nil.
8183
var buildConfiguration: (any BuildConfiguration)? { get }
8284
}
8385

SwiftParserCLI/Sources/swift-parser-cli/Commands/Print.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ struct Print: ParsableCommand, ParseCommand {
2323
@OptionGroup
2424
var arguments: ParseArguments
2525

26-
@Flag(name: .long, help: "Include trivia in the output")
27-
var includeTrivia: Bool = false
28-
2926
func run() throws {
30-
try withParsedSourceFile(wantDiagnostics: false) { (tree, _) in
31-
print(tree.description)
32-
}
27+
let (tree, _) = try parsedSourceFile(wantDiagnostics: false)
28+
print(tree.description)
3329
}
3430
}

SwiftParserCLI/Sources/swift-parser-cli/Commands/PrintDiags.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,18 @@ struct PrintDiags: ParsableCommand, ParseCommand {
2828
var colorize: Bool = false
2929

3030
func run() throws {
31-
try withParsedSourceFile { (tree, diags) in
32-
var group = GroupedDiagnostics()
33-
group.addSourceFile(tree: tree, displayName: sourceFileName, diagnostics: diags)
34-
let annotatedSource = DiagnosticsFormatter.annotateSources(
35-
in: group,
36-
colorize: colorize || TerminalHelper.isConnectedToTerminal
37-
)
31+
let (tree, diags) = try parsedSourceFile()
32+
var group = GroupedDiagnostics()
33+
group.addSourceFile(tree: tree, displayName: sourceFileName, diagnostics: diags)
34+
let annotatedSource = DiagnosticsFormatter.annotateSources(
35+
in: group,
36+
colorize: colorize || TerminalHelper.isConnectedToTerminal
37+
)
3838

39-
print(annotatedSource)
39+
print(annotatedSource)
4040

41-
if diags.isEmpty {
42-
print("No diagnostics produced")
43-
}
41+
if diags.isEmpty {
42+
print("No diagnostics produced")
4443
}
4544
}
4645
}

SwiftParserCLI/Sources/swift-parser-cli/Commands/PrintTree.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ struct PrintTree: ParsableCommand, ParseCommand {
2727
var includeTrivia: Bool = false
2828

2929
func run() throws {
30-
try withParsedSourceFile(wantDiagnostics: false) { (tree, _) in
31-
print(tree.debugDescription(includeTrivia: includeTrivia))
32-
}
30+
let (tree, _) = try parsedSourceFile(wantDiagnostics: false)
31+
print(tree.debugDescription(includeTrivia: includeTrivia))
3332
}
3433
}

SwiftParserCLI/Sources/swift-parser-cli/ParseCommand.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ extension ParseCommand {
6161
var foldSequences: Bool { arguments.foldSequences }
6262

6363
/// Parse the source file, applying any additional configuration options
64-
/// such as sequence folding, and provide it to the given closure.
65-
func withParsedSourceFile<R>(
66-
wantDiagnostics: Bool = true,
67-
body: (SourceFileSyntax, [Diagnostic]) throws -> R
68-
) throws -> R {
64+
/// such as sequence folding, and return it with diagnostics.
65+
func parsedSourceFile(
66+
wantDiagnostics: Bool = true
67+
) throws -> (SourceFileSyntax, [Diagnostic]) {
6968
return try sourceFileContents.withUnsafeBufferPointer { sourceBuffer in
7069
// Parse the sources
7170
var tree = Parser.parse(source: sourceBuffer)
@@ -104,7 +103,7 @@ extension ParseCommand {
104103
}
105104
}
106105

107-
return try body(tree, diags)
106+
return (tree, diags)
108107
}
109108
}
110109
}

0 commit comments

Comments
 (0)