Skip to content

Commit 025b60c

Browse files
committed
[SwiftSyntax] Add #_objectFileFormat compilation conditional
1 parent af7c0c6 commit 025b60c

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

Sources/SwiftIfConfig/BuildConfiguration.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ public protocol BuildConfiguration {
214214
/// pointer authentication scheme.
215215
func isActiveTargetPointerAuthentication(name: String) throws -> Bool
216216

217+
/// Determine whether the given name is the active target object file format (e.g., ELF).
218+
///
219+
/// The target object file format can only be queried by an experimental
220+
/// syntax `_objectFileFormat(<name>)`, e.g.,
221+
///
222+
/// ```swift
223+
/// #if _objectFileFormat(ELF)
224+
/// // Special logic for ELF object file formats
225+
/// #endif
226+
/// ```
227+
/// - Parameters:
228+
/// - name: The name of the object file format.
229+
/// - Returns: Whether the target object file format matches the given name.
230+
func isActiveTargetObjectFileFormat(name: String) throws -> Bool
231+
217232
/// The bit width of a data pointer for the target architecture.
218233
///
219234
/// The target's pointer bit width (which also corresponds to the number of

Sources/SwiftIfConfig/IfConfigDiagnostic.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum IfConfigDiagnostic: Error, CustomStringConvertible {
3636
case likelySimulatorPlatform(syntax: ExprSyntax)
3737
case likelyTargetOS(syntax: ExprSyntax, replacement: ExprSyntax?)
3838
case endiannessDoesNotMatch(syntax: ExprSyntax, argument: String)
39+
case objectFileFormatDoesNotMatch(syntax: ExprSyntax, argument: String)
3940
case macabiIsMacCatalyst(syntax: ExprSyntax)
4041
case expectedModuleName(syntax: ExprSyntax)
4142
case badInfixOperator(syntax: ExprSyntax)
@@ -102,6 +103,9 @@ enum IfConfigDiagnostic: Error, CustomStringConvertible {
102103
case .endiannessDoesNotMatch:
103104
return "unknown endianness for build configuration '_endian' (must be 'big' or 'little')"
104105

106+
case .objectFileFormatDoesNotMatch:
107+
return "unknown object file format for build configuration '_objectFileFormat'"
108+
105109
case .expectedModuleName:
106110
return "expected module name"
107111

@@ -136,6 +140,7 @@ enum IfConfigDiagnostic: Error, CustomStringConvertible {
136140
.likelySimulatorPlatform(syntax: let syntax),
137141
.likelyTargetOS(syntax: let syntax, replacement: _),
138142
.endiannessDoesNotMatch(syntax: let syntax, argument: _),
143+
.objectFileFormatDoesNotMatch(syntax: let syntax, argument: _),
139144
.macabiIsMacCatalyst(syntax: let syntax),
140145
.expectedModuleName(syntax: let syntax),
141146
.badInfixOperator(syntax: let syntax),
@@ -159,7 +164,7 @@ extension IfConfigDiagnostic: DiagnosticMessage {
159164
var severity: SwiftDiagnostics.DiagnosticSeverity {
160165
switch self {
161166
case .compilerVersionSecondComponentNotWildcard, .ignoredTrailingComponents,
162-
.likelySimulatorPlatform, .likelyTargetOS, .endiannessDoesNotMatch, .macabiIsMacCatalyst:
167+
.likelySimulatorPlatform, .likelyTargetOS, .endiannessDoesNotMatch, .objectFileFormatDoesNotMatch, .macabiIsMacCatalyst:
163168
return .warning
164169
default: return .error
165170
}

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)