Skip to content

Commit 74dc3f8

Browse files
committed
Add diagnostic if #elseif and #else is followed by any declarations
1 parent 6e02c94 commit 74dc3f8

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

Sources/SwiftParser/Directives.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,13 @@ extension Parser {
125125
var unexpectedBeforePoundKeyword: RawUnexpectedNodesSyntax?
126126
var poundKeyword: RawTokenSyntax
127127
let condition: RawExprSyntax?
128+
let unexpectedBetweenConditionAndElements: RawUnexpectedNodesSyntax?
128129

129130
switch match {
130131
case .poundElseifKeyword:
131132
(unexpectedBeforePoundKeyword, poundKeyword) = self.eat(handle)
132133
condition = RawExprSyntax(self.parseSequenceExpression(.basic, forDirective: true))
134+
unexpectedBetweenConditionAndElements = self.consumeRemainingTokenOnLine()
133135
case .poundElseKeyword:
134136
(unexpectedBeforePoundKeyword, poundKeyword) = self.eat(handle)
135137
if let ifToken = self.consume(if: .init(.if, allowAtStartOfLine: false)) {
@@ -139,6 +141,7 @@ extension Parser {
139141
} else {
140142
condition = nil
141143
}
144+
unexpectedBetweenConditionAndElements = self.consumeRemainingTokenOnLine()
142145
case .pound:
143146
if self.atElifTypo() {
144147
(unexpectedBeforePoundKeyword, poundKeyword) = self.eat(handle)
@@ -148,6 +151,7 @@ extension Parser {
148151
unexpectedBeforePoundKeyword = RawUnexpectedNodesSyntax(combining: unexpectedBeforePoundKeyword, poundKeyword, elif, arena: self.arena)
149152
poundKeyword = self.missingToken(.poundElseifKeyword)
150153
condition = RawExprSyntax(self.parseSequenceExpression(.basic, forDirective: true))
154+
unexpectedBetweenConditionAndElements = self.consumeRemainingTokenOnLine()
151155
} else {
152156
break LOOP
153157
}
@@ -158,6 +162,7 @@ extension Parser {
158162
unexpectedBeforePoundKeyword,
159163
poundKeyword: poundKeyword,
160164
condition: condition,
165+
unexpectedBetweenConditionAndElements,
161166
elements: syntax(&self, parseIfConfigClauseElements(parseElement, addSemicolonIfNeeded: addSemicolonIfNeeded)),
162167
arena: self.arena
163168
)

Tests/SwiftParserTest/DirectiveTests.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,61 @@ final class DirectiveTests: XCTestCase {
246246
)
247247
}
248248

249+
func testElseIfFollowByDeclarations() {
250+
assertParse(
251+
"""
252+
struct Foo {
253+
#if DEBUG
254+
var x: Int = 1
255+
#elseif UAT1️⃣; var x = 1
256+
var x: Int = 2
257+
#endif
258+
}
259+
""",
260+
diagnostics: [
261+
DiagnosticSpec(
262+
message: "extra tokens following conditional compilation directive"
263+
)
264+
]
265+
)
266+
267+
assertParse(
268+
"""
269+
struct Foo {
270+
#if DEBUG
271+
var x: Int = 1
272+
#elseif UAT || UAT1️⃣; var x = 1
273+
var x: Int = 2
274+
#endif
275+
}
276+
""",
277+
diagnostics: [
278+
DiagnosticSpec(
279+
message: "extra tokens following conditional compilation directive"
280+
)
281+
]
282+
)
283+
}
284+
285+
func testElseFollowByDeclarations() {
286+
assertParse(
287+
"""
288+
struct Foo {
289+
#if DEBUG
290+
var x: Int = 1
291+
#else1️⃣; var x = 1
292+
var x: Int = 2
293+
#endif
294+
}
295+
""",
296+
diagnostics: [
297+
DiagnosticSpec(
298+
message: "extra tokens following conditional compilation directive"
299+
)
300+
]
301+
)
302+
}
303+
249304
func testSourcelocationDirectiveFollowedByDeclarations() {
250305
assertParse(
251306
"""

0 commit comments

Comments
 (0)