Skip to content

Commit 757d2b3

Browse files
committed
Ensure that we diagnose compound names where they shouldn't be
1 parent 150e569 commit 757d2b3

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

Sources/SwiftIfConfig/IfConfigEvaluation.swift

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ func evaluateIfConfig(
9696
}
9797

9898
// Declaration references are for custom compilation flags.
99-
if let identExpr = condition.as(DeclReferenceExprSyntax.self) {
100-
// FIXME: Need a real notion of an identifier.
101-
let ident = identExpr.baseName.text
102-
99+
if let identExpr = condition.as(DeclReferenceExprSyntax.self),
100+
let ident = identExpr.simpleIdentifier
101+
{
103102
// Evaluate the custom condition. If the build configuration cannot answer this query, fail.
104103
return checkConfiguration(at: identExpr) {
105104
(active: try configuration.isCustomConditionSet(name: ident), syntaxErrorsAllowed: false)
@@ -499,18 +498,6 @@ private func extractImportPath(_ expression: some ExprSyntaxProtocol) throws ->
499498
throw IfConfigError.expectedModuleName(syntax: ExprSyntax(expression))
500499
}
501500

502-
extension DeclReferenceExprSyntax {
503-
/// If this declaration reference is a simple identifier, return that
504-
/// string.
505-
fileprivate var simpleIdentifier: String? {
506-
guard argumentNames == nil else {
507-
return nil
508-
}
509-
510-
return baseName.text
511-
}
512-
}
513-
514501
/// Determine whether the given condition only involves disjunctions that
515502
/// check the given config function against one of the provided values.
516503
///

Sources/SwiftIfConfig/SyntaxLiteralUtils.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,23 @@ extension LabeledExprListSyntax {
3636
extension ExprSyntax {
3737
/// Whether this is a simple identifier expression and, if so, what the identifier string is.
3838
var simpleIdentifierExpr: String? {
39-
guard let identExpr = self.as(DeclReferenceExprSyntax.self),
40-
identExpr.argumentNames == nil
41-
else {
39+
guard let identExpr = self.as(DeclReferenceExprSyntax.self) else {
4240
return nil
4341
}
4442

45-
// FIXME: Handle escaping here.
46-
return identExpr.baseName.text
43+
return identExpr.simpleIdentifier
44+
}
45+
}
46+
47+
extension DeclReferenceExprSyntax {
48+
/// If this declaration reference is a simple identifier, return that
49+
/// string.
50+
var simpleIdentifier: String? {
51+
guard argumentNames == nil else {
52+
return nil
53+
}
54+
55+
/// FIXME: Make this an Identifier so we handle escaping properly.
56+
return baseName.text
4757
}
4858
}

Tests/SwiftIfConfigTest/EvaluateTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ public class EvaluateTests: XCTestCase {
120120
)
121121
]
122122
)
123+
assertIfConfig(
124+
"BAR(_:)",
125+
.unparsed,
126+
diagnostics: [
127+
DiagnosticSpec(
128+
message: "invalid conditional compilation expression",
129+
line: 1,
130+
column: 1
131+
)
132+
]
133+
)
123134
}
124135

125136
func testBadExpressions() throws {

0 commit comments

Comments
 (0)