Skip to content

Commit 1425e5a

Browse files
committed
Downgrade error about bad "_endian" platform condition to a warning
The compiler issues a warning, so do the same here.
1 parent 2f24394 commit 1425e5a

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Sources/SwiftIfConfig/IfConfigError.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum IfConfigError: Error, CustomStringConvertible {
3131
case ignoredTrailingComponents(version: VersionTuple, syntax: ExprSyntax)
3232
case integerLiteralCondition(syntax: ExprSyntax, replacement: Bool)
3333
case likelySimulatorPlatform(syntax: ExprSyntax)
34+
case endiannessDoesNotMatch(syntax: ExprSyntax, argument: String)
3435

3536
var description: String {
3637
switch self {
@@ -81,6 +82,9 @@ enum IfConfigError: Error, CustomStringConvertible {
8182
case .likelySimulatorPlatform:
8283
return
8384
"platform condition appears to be testing for simulator environment; use 'targetEnvironment(simulator)' instead"
85+
86+
case .endiannessDoesNotMatch:
87+
return "unknown endianness for build configuration '_endian' (must be 'big' or 'little')"
8488
}
8589
}
8690

@@ -100,7 +104,8 @@ enum IfConfigError: Error, CustomStringConvertible {
100104
.canImportTwoParameters(syntax: let syntax),
101105
.ignoredTrailingComponents(version: _, syntax: let syntax),
102106
.integerLiteralCondition(syntax: let syntax, replacement: _),
103-
.likelySimulatorPlatform(syntax: let syntax):
107+
.likelySimulatorPlatform(syntax: let syntax),
108+
.endiannessDoesNotMatch(syntax: let syntax, argument: _):
104109
return Syntax(syntax)
105110

106111
case .unsupportedVersionOperator(name: _, operator: let op):
@@ -118,7 +123,8 @@ extension IfConfigError: DiagnosticMessage {
118123

119124
var severity: SwiftDiagnostics.DiagnosticSeverity {
120125
switch self {
121-
case .compilerVersionSecondComponentNotWildcard, .ignoredTrailingComponents, .likelySimulatorPlatform:
126+
case .compilerVersionSecondComponentNotWildcard, .ignoredTrailingComponents,
127+
.likelySimulatorPlatform, .endiannessDoesNotMatch:
122128
return .warning
123129
default: return .error
124130
}

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,23 +283,36 @@ func evaluateIfConfig(
283283
)
284284

285285
case ._endian:
286-
// Ensure that we have a single argument that is a simple identifier,
287-
// either "little" or "big".
286+
// Ensure that we have a single argument that is a simple identifier.
288287
guard let argExpr = call.arguments.singleUnlabeledExpression,
289-
let arg = argExpr.simpleIdentifierExpr,
290-
let expectedEndianness = Endianness(rawValue: arg)
288+
let arg = argExpr.simpleIdentifierExpr
291289
else {
292290
return recordError(
293291
.requiresUnlabeledArgument(
294292
name: fnName,
295-
role: "endiannes ('big' or 'little')",
293+
role: "endianness ('big' or 'little')",
296294
syntax: ExprSyntax(call)
297295
)
298296
)
299297
}
300298

299+
// The argument needs to be either "little" or "big". Otherwise, we assume
300+
// it fails.
301+
let isActive: Bool
302+
if let expectedEndianness = Endianness(rawValue: arg) {
303+
isActive = configuration.endianness == expectedEndianness
304+
} else {
305+
// Complain about unknown endianness
306+
extraDiagnostics.append(
307+
contentsOf: IfConfigError.endiannessDoesNotMatch(syntax: argExpr, argument: arg)
308+
.asDiagnostics(at: argExpr)
309+
)
310+
311+
isActive = false
312+
}
313+
301314
return (
302-
active: configuration.endianness == expectedEndianness,
315+
active: isActive,
303316
syntaxErrorsAllowed: fn.syntaxErrorsAllowed,
304317
diagnostics: extraDiagnostics
305318
)

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,19 @@ public class EvaluateTests: XCTestCase {
170170
assertIfConfig("_pointerBitWidth(_32)", .inactive)
171171
assertIfConfig("_hasAtomicBitWidth(_64)", .active)
172172
assertIfConfig("_hasAtomicBitWidth(_128)", .inactive)
173+
174+
assertIfConfig(
175+
"_endian(mid)",
176+
.inactive,
177+
diagnostics: [
178+
DiagnosticSpec(
179+
message: "unknown endianness for build configuration '_endian' (must be 'big' or 'little')",
180+
line: 1,
181+
column: 9,
182+
severity: .warning
183+
)
184+
]
185+
)
173186
}
174187

175188
func testVersions() throws {

0 commit comments

Comments
 (0)