Skip to content

Commit ae70a22

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 ae70a22

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@ 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(IfConfigDeclSyntax(clauses: [
172+
IfConfigClauseSyntax(poundKeyword: .poundIfToken(), condition: ExprSyntax("true"), elements: .attributes([
173+
.ifConfigDecl(IfConfigDeclSyntax(clauses: [
174+
IfConfigClauseSyntax(poundKeyword: .poundIfToken(), condition: ExprSyntax("true"), elements: .attributes([
175+
.attribute("@frozen")
176+
])),
177+
])),
178+
])),
179+
])),
180+
])
181+
)
182+
}
183+
160184
func testHasAttribute() {
161185
assertParse(
162186
"""

0 commit comments

Comments
 (0)