Skip to content

Commit 06dd06e

Browse files
committed
Handle backticked identifiers properly in #if evaluation
1 parent a328c63 commit 06dd06e

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func evaluateIfConfig(
9797

9898
// Declaration references are for custom compilation flags.
9999
if let identExpr = condition.as(DeclReferenceExprSyntax.self),
100-
let ident = identExpr.simpleIdentifier
100+
let ident = identExpr.simpleIdentifier?.name
101101
{
102102
// Evaluate the custom condition. If the build configuration cannot answer this query, fail.
103103
return checkConfiguration(at: identExpr) {
@@ -214,7 +214,7 @@ func evaluateIfConfig(
214214

215215
// Call syntax is for operations.
216216
if let call = condition.as(FunctionCallExprSyntax.self),
217-
let fnName = call.calledExpression.simpleIdentifierExpr,
217+
let fnName = call.calledExpression.simpleIdentifierExpr?.name,
218218
let fn = IfConfigFunctions(rawValue: fnName)
219219
{
220220
/// Perform a check for an operation that takes a single identifier argument.
@@ -224,7 +224,7 @@ func evaluateIfConfig(
224224
) -> (active: Bool, syntaxErrorsAllowed: Bool, diagnostics: [Diagnostic]) {
225225
// Ensure that we have a single argument that is a simple identifier.
226226
guard let argExpr = call.arguments.singleUnlabeledExpression,
227-
var arg = argExpr.simpleIdentifierExpr
227+
var arg = argExpr.simpleIdentifierExpr?.name
228228
else {
229229
return recordError(
230230
.requiresUnlabeledArgument(name: fnName, role: role, syntax: ExprSyntax(call))
@@ -316,7 +316,7 @@ func evaluateIfConfig(
316316
case ._endian:
317317
// Ensure that we have a single argument that is a simple identifier.
318318
guard let argExpr = call.arguments.singleUnlabeledExpression,
319-
let arg = argExpr.simpleIdentifierExpr
319+
let arg = argExpr.simpleIdentifierExpr?.name
320320
else {
321321
return recordError(
322322
.requiresUnlabeledArgument(
@@ -352,7 +352,7 @@ func evaluateIfConfig(
352352
// Ensure that we have a single argument that is a simple identifier, which
353353
// is an underscore followed by an integer.
354354
guard let argExpr = call.arguments.singleUnlabeledExpression,
355-
let arg = argExpr.simpleIdentifierExpr,
355+
let arg = argExpr.simpleIdentifierExpr?.name,
356356
let argFirst = arg.first,
357357
argFirst == "_",
358358
let expectedBitWidth = Int(arg.dropFirst())
@@ -530,14 +530,14 @@ private func extractImportPath(_ expression: some ExprSyntaxProtocol) throws ->
530530
// Member access.
531531
if let memberAccess = expression.as(MemberAccessExprSyntax.self),
532532
let base = memberAccess.base,
533-
let memberName = memberAccess.declName.simpleIdentifier
533+
let memberName = memberAccess.declName.simpleIdentifier?.name
534534
{
535535
return try extractImportPath(base) + [memberName]
536536
}
537537

538538
// Declaration reference.
539539
if let declRef = expression.as(DeclReferenceExprSyntax.self),
540-
let name = declRef.simpleIdentifier
540+
let name = declRef.simpleIdentifier?.name
541541
{
542542
return [name]
543543
}
@@ -574,11 +574,11 @@ private func isConditionDisjunction(
574574
// If we have a call to this function, check whether the argument is one of
575575
// the acceptable values.
576576
if let call = condition.as(FunctionCallExprSyntax.self),
577-
let fnName = call.calledExpression.simpleIdentifierExpr,
577+
let fnName = call.calledExpression.simpleIdentifierExpr?.name,
578578
let callFn = IfConfigFunctions(rawValue: fnName),
579579
callFn == function,
580580
let argExpr = call.arguments.singleUnlabeledExpression,
581-
let arg = argExpr.simpleIdentifierExpr
581+
let arg = argExpr.simpleIdentifierExpr?.name
582582
{
583583
return values.contains(arg)
584584
}
@@ -705,7 +705,7 @@ extension ExprSyntaxProtocol {
705705

706706
// Call syntax is for operations.
707707
if let call = self.as(FunctionCallExprSyntax.self),
708-
let fnName = call.calledExpression.simpleIdentifierExpr,
708+
let fnName = call.calledExpression.simpleIdentifierExpr?.name,
709709
let fn = IfConfigFunctions(rawValue: fnName)
710710
{
711711
return fn.syntaxErrorsAllowed

Sources/SwiftIfConfig/SyntaxLiteralUtils.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extension LabeledExprListSyntax {
3535

3636
extension ExprSyntax {
3737
/// Whether this is a simple identifier expression and, if so, what the identifier string is.
38-
var simpleIdentifierExpr: String? {
38+
var simpleIdentifierExpr: Identifier? {
3939
guard let identExpr = self.as(DeclReferenceExprSyntax.self) else {
4040
return nil
4141
}
@@ -47,12 +47,11 @@ extension ExprSyntax {
4747
extension DeclReferenceExprSyntax {
4848
/// If this declaration reference is a simple identifier, return that
4949
/// string.
50-
var simpleIdentifier: String? {
50+
var simpleIdentifier: Identifier? {
5151
guard argumentNames == nil else {
5252
return nil
5353
}
5454

55-
/// FIXME: Make this an Identifier so we handle escaping properly.
56-
return baseName.text
55+
return baseName.identifier
5756
}
5857
}

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class EvaluateTests: XCTestCase {
7171
}
7272

7373
func testCustomConfigs() throws {
74-
let buildConfig = TestingBuildConfiguration(customConditions: ["DEBUG", "ASSERTS"])
74+
let buildConfig = TestingBuildConfiguration(customConditions: ["DEBUG", "ASSERTS", "try"])
7575

7676
assertIfConfig("DEBUG", .active, configuration: buildConfig)
7777
assertIfConfig("NODEBUG", .inactive, configuration: buildConfig)
@@ -80,6 +80,8 @@ public class EvaluateTests: XCTestCase {
8080
assertIfConfig("DEBUG && ASSERTS", .active, configuration: buildConfig)
8181
assertIfConfig("DEBUG && nope", .inactive, configuration: buildConfig)
8282
assertIfConfig("nope && DEBUG", .inactive, configuration: buildConfig)
83+
assertIfConfig("`try`", .active, configuration: buildConfig)
84+
assertIfConfig("`return`", .inactive, configuration: buildConfig)
8385
assertIfConfig(
8486
"nope && 3.14159",
8587
.unparsed,

0 commit comments

Comments
 (0)