8000 Merge pull request #3027 from swiftlang/mracek/object-file-format · swiftlang/swift-syntax@9e33a8f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e33a8f

Browse files
authored
Merge pull request #3027 from swiftlang/mracek/object-file-format
[SwiftSyntax] Add #_objectFileFormat compilation conditional
2 parents 4ed73b1 + fc75c9c commit 9e33a8f

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public enum CanImportVersion {
3737
case underlyingVersion(VersionTuple)
3838
}
3939

40+
enum BuildConfigurationError: Error, CustomStringConvertible {
41+
case experimentalFeature(name: String)
42+
43+
var description: String {
44+
switch self {
45+
case .experimentalFeature(let name):
46+
return "'name' is an experimental feature"
47+
}
48+
}
49+
}
50+
4051
/// Captures information about the build configuration that can be
4152
/// queried in a `#if` expression, including OS, compiler version,
4253
/// enabled language features, and available modules.
@@ -214,6 +225,22 @@ public protocol BuildConfiguration {
214225
/// pointer authentication scheme.
215226
func isActiveTargetPointerAuthentication(name: String) throws -> Bool
216227

228+
/// Determine whether the given name is the active target object file format (e.g., ELF).
229+
///
230+
/// The target object file format can only be queried by an experimental
231+
/// syntax `_objectFileFormat(<name>)`, e.g.,
232+
///
233+
/// ```swift
234+
/// #if _objectFileFormat(ELF)
235+
/// // Special logic for ELF object file formats
236+
/// #endif
237+
/// ```
238+
/// - Parameters:
239+
/// - name: The name of the object file format.
240+
/// - Returns: Whether the target object file format matches the given name.
241+
@_spi(ExperimentalLanguageFeatures)
242+
func isActiveTargetObjectFileFormat(name: String) throws -> Bool
243+
217244
/// The bit width of a data pointer for the target architecture.
218245
///
219246
/// The target's pointer bit width (which also corresponds to the number of
@@ -276,3 +303,13 @@ public protocol BuildConfiguration {
276303
/// #endif
277304
var compilerVersion: VersionTuple { get }
278305
}
306+
307+
/// Default implementation of BuildConfiguration, to avoid a revlock with the
308+
/// swift repo, and breaking clients with the new addition to the protocol.
309+
extension BuildConfiguration {
310+
/// FIXME: This should be @_spi(ExperimentalLanguageFeatures) but cannot due
311+
/// to rdar://147943518, https://github.com/swiftlang/swift/issues/80313
312+
public func isActiveTargetObjectFileFormat(name: String) throws -> Bool {
313+
throw BuildConfigurationError.experimentalFeature(name: "_objectFileFormat")
314+
}
315+
}

Sources/SwiftIfConfig/IfConfigDiagnostic.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ extension IfConfigDiagnostic: DiagnosticMessage {
159159
var severity: SwiftDiagnostics.DiagnosticSeverity {
160160
switch self {
161161
case .compilerVersionSecondComponentNotWildcard, .ignoredTrailingComponents,
162-
.likelySimulatorPlatform, .likelyTargetOS, .endiannessDoesNotMatch, .macabiIsMacCatalyst:
162+
.likelySimulatorPlatform, .likelyTargetOS, .endiannessDoesNotMatch,
163+
.macabiIsMacCatalyst:
163164
return .warning
164165
default: return .error
165166
}

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@ func evaluateIfConfig(
307307
case .targetEnvironment:
308308
return doSingleIdentifierArgumentCheck(configuration.isActiveTargetEnvironment, role: "environment")
309309

310+
case ._objectFileFormat:
311+
return doSingleIdentifierArgumentCheck(configuration.isActiveTargetObjectFileFormat, role: "object file format")
312+
310313
case ._runtime:
311314
return doSingleIdentifierArgumentCheck(configuration.isActiveTargetRuntime, role: "runtime")
312315

@@ -818,6 +821,10 @@ private struct CanImportSuppressingBuildConfiguration<Other: BuildConfiguration>
818821
return try other.isActiveTargetPointerAuthentication(name: name)
819822
}
820823

824+
func isActiveTargetObjectFileFormat(name: String) throws -> Bool {
825+
return try other.isActiveTargetObjectFileFormat(name: name)
826+
}
827+
821828
var targetPointerBitWidth: Int { return other.targetPointerBitWidth }
822829

823830
var targetAtomicBitWidths: [Int] { return other.targetAtomicBitWidths }

Sources/SwiftIfConfig/IfConfigFunctions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ enum IfConfigFunctions: String {
4949
/// A check for the target bit width of a pointer (e.g., _64)
5050
case _pointerBitWidth
5151

52+
/// A check for the target object file format (e.g., ELF)
53+
case _objectFileFormat
54+
5255
/// A check for the target runtime paired with the Swift runtime (e.g., _ObjC)
5356
/// via `_runtime(<name>)`.
5457
case _runtime
@@ -69,7 +72,7 @@ enum IfConfigFunctions: String {
6972
return true
7073

7174
case .hasAttribute, .hasFeature, .canImport, .os, .arch, .targetEnvironment,
72-
._hasAtomicBitWidth, ._endian, ._pointerBitWidth, ._runtime, ._ptrauth, .defined:
75+
._hasAtomicBitWidth, ._endian, ._pointerBitWidth, ._objectFileFormat, ._runtime, ._ptrauth, .defined:
7376
return false
7477
}
7578
}

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ public class EvaluateTests: XCTestCase {
208208
assertIfConfig("_pointerBitWidth(_32)", .inactive)
209209
assertIfConfig("_hasAtomicBitWidth(_64)", .active)
210210
assertIfConfig("_hasAtomicBitWidth(_128)", .inactive)
211+
assertIfConfig("_objectFileFormat(ELF)", .active)
212+
assertIfConfig("_objectFileFormat(MachO)", .inactive)
211213

212214
assertIfConfig(
213215
"_endian(mid)",

Tests/SwiftIfConfigTest/TestingBuildConfiguration.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ struct TestingBuildConfiguration: BuildConfiguration {
9999
name == "arm64e"
100100
}
101101

102+
func isActiveTargetObjectFileFormat(name: String) throws -> Bool {
103+
name == "ELF"
104+
}
105+
102106
var targetPointerBitWidth: Int { 64 }
103107

104108
var targetAtomicBitWidths: [Int] { [32, 64] }

0 commit comments

Comments
 (0)
0