Skip to content

Commit 19ed1e8

Browse files
committed
Improve documentation for BuildConfiguration
1 parent 14b6718 commit 19ed1e8

File tree

1 file changed

+180
-5
lines changed

1 file changed

+180
-5
lines changed

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 180 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@
1111
//===----------------------------------------------------------------------===//
1212
import SwiftSyntax
1313

14+
/// Describes the ordering of a sequence of bytes that make up a word of
15+
/// storage for a particular architecture.
1416
public enum Endianness: String {
17+
/// Little endian, meaning that the least significant byte of a word is
18+
/// stored at the lowest address.
1519
case little
20+
21+
/// Big endian, meaning that the most significant byte of a word is stored
22+
/// at the lowest address.
1623
case big
1724
}
1825

19-
/// Describes the requested version of a module
26+
/// Describes the requested version of a module.
2027
public enum CanImportVersion {
2128
/// Any version of the module will suffice.
2229
case unversioned
@@ -45,48 +52,216 @@ public protocol BuildConfiguration {
4552
///
4653
/// Custom build conditions can be set by the `-D` command line option to
4754
/// the Swift compiler. For example, `-DDEBUG` sets the custom condition
48-
/// named `DEBUG`.
55+
/// named `DEBUG`, which could be checked with, e.g.,
56+
///
57+
/// ```
58+
/// #if DEBUG
59+
/// // ...
60+
/// #endif
61+
/// ```
62+
///
63+
/// - Parameters:
64+
/// - name: The name of the custom build condition being checked (e.g.,
65+
/// `DEBUG`.
66+
/// - syntax: The syntax node for the name of the custom build
67+
/// configuration.
68+
/// - Returns: Whether the custom condition is set.
4969
func isCustomConditionSet(name: String, syntax: ExprSyntax) -> Bool?
5070

5171
/// Determine whether the given feature is enabled.
5272
///
5373
/// Features are determined by the Swift compiler, language mode, and other
54-
/// options such as `--enable-upcoming-feature`.
74+
/// options such as `--enable-upcoming-feature`, and can be checked with
75+
/// the `hasFeature` syntax, e.g.,
76+
///
77+
/// ```
78+
/// #if hasFeature(VariadicGenerics)
79+
/// // ...
80+
/// #endif
81+
/// ```
82+
///
83+
/// - Parameters:
84+
/// - name: The name of the feature being checked.
85+
/// - syntax: The syntax node for the `hasFeature(<name>)`.
86+
/// - Returns: Whether the requested feature is available.
5587
func hasFeature(name: String, syntax: ExprSyntax) -> Bool?
5688

5789
/// Determine whether the given attribute is available.
5890
///
59-
/// Attributes are determined by the Swift compiler.
91+
/// Attributes are determined by the Swift compiler. They can be checked
92+
/// with `hasAttribute` syntax, e.g.,
93+
///
94+
/// ```
95+
/// #if hasAttribute(available)
96+
/// // ...
97+
/// #endif
98+
/// ```
99+
///
100+
/// - Parameters:
101+
/// - name: The name of the attribute being queried.
102+
/// - syntax: The syntax node for the `hasAttribute(<name>)`.
103+
/// - Returns: Whether the requested attribute is supported.
60104
func hasAttribute(name: String, syntax: ExprSyntax) -> Bool?
61105

62106
/// Determine whether a module with the given import path can be imported,
63107
/// with additional version information.
108+
///
109+
/// The availability of a module for import can be checked with `canImport`,
110+
/// e.g.,
111+
///
112+
/// ```
113+
/// #if canImport(UIKit)
114+
/// // ...
115+
/// #endif
116+
/// ```
117+
///
118+
/// There is an experimental syntax for providing required module version
119+
/// information, which will translate into the `version` argument.
120+
///
121+
/// - Parameters:
122+
/// - importPath: A nonempty sequence of identifiers describing the
123+
/// imported module, which was written in source as a dotted sequence,
124+
/// e.g., `UIKit.UIViewController` will be passed in as the import path
125+
/// array `["UIKit", "UIViewController"]`.
126+
/// - version: The version restriction on the imported module. For the
127+
/// normal `canImport(<import-path>)` syntax, this will always be
128+
/// `CanImportVersion.unversioned`.
129+
/// - syntax: The syntax node for the `canImport` expression.
130+
/// - Returns: Whether the module can be imported.
64131
func canImport(importPath: [String], version: CanImportVersion, syntax: ExprSyntax) -> Bool?
65132

66133
/// Determine whether the given name is the active target OS (e.g., Linux, iOS).
134+
///
135+
/// The target operating system can be queried with `os(<name>)`, e.g.,
136+
///
137+
/// ```
138+
/// #if os(Linux)
139+
/// // Linux-specific implementation
140+
/// #endif
141+
///
142+
/// - Parameters:
143+
/// - name: The name of the operating system being queried, such as `Linux`,
144+
/// `Windows`, `macOS`, etc.
145+
/// - syntax: The syntax node for the `os(<name>)` expression.
146+
/// - Returns: Whether the given operating system name is the target operating
147+
/// system, i.e., the operating system for which code is being generated.
67148
func isActiveTargetOS(name: String, syntax: ExprSyntax) -> Bool?
68149

69-
/// Determine whether the given name is the active target architecture (e.g., x86_64, arm64)
150+
/// Determine whether the given name is the active target architecture
151+
/// (e.g., x86_64, arm64).
152+
///
153+
/// The target processor architecture can be queried with `arch(<name>)`, e.g.,
154+
///
155+
/// ```
156+
/// #if arch(x86_64)
157+
/// // 64-bit x86 Intel-specific code
158+
/// #endif
159+
///
160+
/// - Parameters:
161+
/// - name: The name of the target architecture to check.
162+
/// - syntax: The syntax node for the `arch(<name>)` expression.
163+
/// - Returns: Whether the given processor architecture is the target
164+
/// architecture.
70165
func isActiveTargetArchitecture(name: String, syntax: ExprSyntax) -> Bool?
71166

72167
/// Determine whether the given name is the active target environment (e.g., simulator)
168+
///
169+
/// The target environment can be queried with `targetEnvironment(<name>)`,
170+
/// e.g.,
171+
///
172+
/// ```
173+
/// #if targetEnvironment(simulator)
174+
/// // Simulator-specific code
175+
/// #endif
176+
///
177+
/// - Parameters:
178+
/// - name: The name of the target environment to check.
179+
/// - syntax: The syntax node for the `targetEnvironment(<name>)`
180+
/// expression.
181+
/// - Returns: Whether the target platform is for a specific environment,
182+
/// such as a simulator or emulator.
73183
func isActiveTargetEnvironment(name: String, syntax: ExprSyntax) -> Bool?
74184

75185
/// Determine whether the given name is the active target runtime (e.g., _ObjC vs. _Native)
186+
///
187+
/// The target runtime can only be queried by an experimental syntax
188+
/// `_runtime(<name>)`, e.g.,
189+
///
190+
/// #if _runtime(_ObjC)
191+
/// // Code that depends on Swift being built for use with the Objective-C
192+
/// // runtime, e.g., on Apple platforms.
193+
/// #endif
194+
///
195+
/// The only other runtime is "none", when Swift isn't tying into any other
196+
/// specific runtime.
197+
///
198+
/// - Parameters:
199+
/// - name: The name of the runtime.
200+
/// - syntax: The syntax node for the `_runtime(<name>)` expression.
201+
/// - Returns: Whether the target runtime matches the given name.
76202
func isActiveTargetRuntime(name: String, syntax: ExprSyntax) -> Bool?
77203

78204
/// Determine whether the given name is the active target pointer authentication scheme (e.g., arm64e).
205+
///
206+
/// The target pointer authentication scheme describes how pointers are
207+
/// signed, as a security mitigation. This scheme can only be queried by
208+
/// an experimental syntax `_ptrath(<name>)`, e.g.,
209+
///
210+
/// ```
211+
/// #if _ptrauth(arm64e)
212+
/// // Special logic for arm64e pointer signing
213+
/// #endif
214+
///
215+
/// - Parameters:
216+
/// - name: The name of the pointer authentication scheme to check.
217+
/// - syntax: The syntax node for the `_ptrauth(<name>)` expression.
218+
/// - Returns: Whether the code generated for the target will use the given
219+
/// pointer authentication scheme.
79220
func isActiveTargetPointerAuthentication(name: String, syntax: ExprSyntax) -> Bool?
80221

81222
/// The bit width of a data pointer for the target architecture.
223+
///
224+
/// The target's pointer bit with (which also corresponds to the number of
225+
/// bits in `Int`/`UInt`) can only be queried with the experimental syntax
226+
/// `_pointerBitWidth(_<bitwidth>)`, e.g.,
227+
///
228+
/// ```
229+
/// #if _pointerBitWidth(32)
230+
/// // 32-bit system
231+
/// #endif
82232
var targetPointerBitWidth: Int? { get }
83233

84234
/// The endianness of the target architecture.
235+
///
236+
/// The target's endianness can onyl be queried with the experimental syntax
237+
/// `_endian(<name>)`, where `<name>` can be either "big" or "little", e.g.,
238+
///
239+
/// #if _endian(little)
240+
/// // Swap some bytes around for network byte order
241+
/// #endif
85242
var endianness: Endianness? { get }
86243

87244
/// The effective language version, which can be set by the user (e.g., 5.0).
245+
///
246+
/// The language version can be queried with the `swift` directive that checks
247+
/// how the supported language version compares, as described by
248+
/// [SE-0212](https://github.com/apple/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
249+
///
250+
/// ```
251+
/// #if swift(>=5.5)
252+
/// // Hooray, we can use tasks!
253+
/// ```
88254
var languageVersion: VersionTuple? { get }
89255

90256
/// The version of the compiler (e.g., 5.9).
257+
///
258+
/// The compiler version can be queried with the `compiler` directive that
259+
/// checks the specific version of the compiler being used to process the
260+
/// code, e.g.,
261+
///
262+
/// ```
263+
/// #if compiler(>=5.7)
264+
/// // Hoorway, we can implicitly open existentials!
265+
/// #endif
91266
var compilerVersion: VersionTuple? { get }
92267
}

0 commit comments

Comments
 (0)