Skip to content

Commit 191392b

Browse files
committed
[SwiftParser] Handle nested '#if' in attribute list correctly
Parse the following '#if' directive as an attribute list for the func decl. ``` #if condition #if condition @attr #endif #endif func foo() ```
1 parent 7b1fcd6 commit 191392b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Sources/SwiftParser/Lookahead.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ extension Parser.Lookahead {
254254
didSeeAnyAttributes = true
255255
_ = self.consumeAttributeList()
256256
case .poundIf:
257-
_ = self.consumeIfConfigOfAttributes()
257+
if self.consumeIfConfigOfAttributes() {
258+
didSeeAnyAttributes = true
259+
}
258260
default:
259261
break ATTRIBUTE_LOOP
260262
}

Tests/SwiftParserTest/DirectiveTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,42 @@ final class DirectiveTests: ParserTestCase {
157157
)
158158
}
159159

160+
func testPoundIfNestedStructure() {
161+
assertParse(
162+
"""
163+
#if true
164+
#if true
165+
@frozen
166+
#endif
167+
#endif
168+
public struct S {}
169+
""",
170+
substructure: AttributeListSyntax([
171+
.ifConfigDecl(
172+
IfConfigDeclSyntax(clauses: [
173+
IfConfigClauseSyntax(
174+
poundKeyword: .poundIfToken(),
175+
condition: ExprSyntax("true"),
176+
elements: .attributes([
177+
.ifConfigDecl(
178+
IfConfigDeclSyntax(clauses: [
179+
IfConfigClauseSyntax(
180+
poundKeyword: .poundIfToken(),
181+
condition: ExprSyntax("true"),
182+
elements: .attributes([
183+
.attribute("@frozen")
184+
])
185+
)
186+
])
187+
)
188+
])
189+
)
190+
])
191+
)
192+
])
193+
)
194+
}
195+
160196
func testHasAttribute() {
161197
assertParse(
162198
"""

0 commit comments

Comments
 (0)