Skip to content

Commit 79b95ce

Browse files
committed
[Parser] Include all AST nodes from every #if region in ParserUnit
'ParserUnit' is used for analyzing syntax structures _mainly_ in SourceKit. Since we removed IfConfigDecl from AST, ParserUnit didn't inclue any AST in #if ... #endif regions even for active region because it used to consider all inactive. Instead, consider every region "active" and include all the AST nodes. rdar://117387631 (cherry picked from commit e3fef8e) (cherry picked from commit fa6c027)
1 parent 1fbbebe commit 79b95ce

File tree

10 files changed

+590
-8
lines changed

10 files changed

+590
-8
lines changed

include/swift/AST/SourceFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class SourceFile final : public FileUnit {
9797

9898
/// Validate the new SwiftSyntax parser diagnostics.
9999
ValidateNewParserDiagnostics = 1 << 6,
100+
101+
/// Consider every #if ... #endif region active.
102+
PoundIfAllActive = 1 << 7,
100103
};
101104
using ParsingOptions = OptionSet<ParsingFlags>;
102105

lib/Parse/ParseIfConfig.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,11 @@ Result Parser::parseIfConfigRaw(
834834
// determined solely by which block has the completion token.
835835
!ideInspectionClauseLoc.isValid();
836836

837+
// For constructing syntactic structures, we need AST nodes even for
838+
// non-active regions.
839+
bool allActive = SF.getParsingOptions().contains(
840+
SourceFile::ParsingFlags::PoundIfAllActive);
841+
837842
bool foundActive = false;
838843
bool isVersionCondition = false;
839844
CharSourceRange activeBodyRange;
@@ -893,6 +898,9 @@ Result Parser::parseIfConfigRaw(
893898
if (ideInspectionClauseLoc.isValid() && !foundActive)
894899
isActive = (ClauseLoc == ideInspectionClauseLoc);
895900

901+
if (allActive)
902+
isActive = true;
903+
896904
foundActive |= isActive;
897905

898906
if (!Tok.isAtStartOfLine() && Tok.isNot(tok::eof)) {

lib/Parse/Parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,7 @@ struct ParserUnit::Implementation {
11451145
auto parsingOpts = SourceFile::getDefaultParsingOptions(LangOpts);
11461146
parsingOpts |= ParsingFlags::DisableDelayedBodies;
11471147
parsingOpts |= ParsingFlags::DisablePoundIfEvaluation;
1148+
parsingOpts |= ParsingFlags::PoundIfAllActive;
11481149

11491150
auto *M = ModuleDecl::create(Ctx.getIdentifier(ModuleName), Ctx);
11501151
SF = new (Ctx) SourceFile(*M, SFKind, BufferID, parsingOpts);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s
2+
// RUN: %target-swift-ide-test -syntax-coloring -typecheck -source-filename %s | %FileCheck %s
3+
4+
// CHECK: <#kw>#if</#kw> d
5+
// CHECK-NEXT: <kw>func</kw> bar() {
6+
// CHECK-NEXT: <#kw>#if</#kw> d
7+
// CHECK-NEXT: }
8+
// CHECK-NEXT: <kw>func</kw> foo() {}
9+
10+
#if d
11+
func bar() {
12+
#if d
13+
}
14+
func foo() {}

test/SourceKit/CodeExpand/code-expand.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,13 @@ braced2(x: {<#T##() -> Void#>}, y: Int)
147147
// CHECK-NEXT: }, y: Int)
148148

149149
braced3({
150+
#if true
150151
<#T##() -> Int#>
152+
#endif
151153
})
152154
// CHECK: braced3 {
153-
// CHECK-NEXT: <#code#>
154-
// CHECK-NEXT: }
155+
// CHECK-NEXT: <#code#>
156+
// CHECK-NEXT: }
155157

156158
func returnTrailing() -> Int {
157159
return withtrail(<#T##() -> ()#>)
@@ -254,6 +256,17 @@ func activeWithTrailing() {
254256
// CHECK: forEach {
255257
// CHECK-NEXT: <#code#>
256258
}
259+
#if false
260+
func inactive() {
261+
foo(<#T##value: Foo##Foo#>)
262+
// CHECK: foo(Foo)
263+
}
264+
func inactiveWithTrailing() {
265+
forEach(<#T##() -> ()#>)
266+
// CHECK: forEach {
267+
// CHECK-NEXT: <#code#>
268+
}
269+
#endif
257270

258271
expandClosureWithInternalParameterNames {
259272
withtrail(<#T##callback: (Int, Int) -> Bool##(_ a: Int, _ b: Int) -> Bool#>)

test/SourceKit/CodeFormat/indent-pound-if.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ print(false)
3535
// RUN: %FileCheck --strict-whitespace %s <%t.response
3636

3737
// CHECK: key.sourcetext: "#else"
38-
// CHECK: key.sourcetext: "let i = 3"
38+
// CHECK: key.sourcetext: " let i = 3"
3939
// CHECK: key.sourcetext: "func b () {"
4040
// CHECK: key.sourcetext: "#elseif os(OSX)"
41-
// CHECK: key.sourcetext: "func b () {"
41+
// CHECK: key.sourcetext: " func b () {"

test/SourceKit/DocumentStructure/structure.swift.empty.response

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,106 @@
14281428
}
14291429
]
14301430
},
1431+
{
1432+
key.kind: source.lang.swift.decl.extension,
1433+
key.name: "Result",
1434+
key.offset: 2496,
1435+
key.length: 36,
1436+
key.nameoffset: 2506,
1437+
key.namelength: 6,
1438+
key.bodyoffset: 2514,
1439+
key.bodylength: 17,
1440+
key.substructure: [
1441+
{
1442+
key.kind: source.lang.swift.decl.function.method.instance,
1443+
key.name: "foo()",
1444+
key.offset: 2517,
1445+
key.length: 13,
1446+
key.nameoffset: 2522,
1447+
key.namelength: 5,
1448+
key.bodyoffset: 2529,
1449+
key.bodylength: 0
1450+
}
1451+
]
1452+
},
1453+
{
1454+
key.kind: source.lang.swift.decl.extension,
1455+
key.name: "Outer",
1456+
key.offset: 2534,
1457+
key.length: 53,
1458+
key.nameoffset: 2544,
1459+
key.namelength: 5,
1460+
key.bodyoffset: 2551,
1461+
key.bodylength: 35,
1462+
key.substructure: [
1463+
{
1464+
key.kind: source.lang.swift.decl.class,
1465+
key.name: "Inner",
1466+
key.offset: 2554,
1467+
key.length: 31,
1468+
key.nameoffset: 2560,
1469+
key.namelength: 5,
1470+
key.bodyoffset: 2567,
1471+
key.bodylength: 17,
1472+
key.substructure: [
1473+
{
1474+
key.kind: source.lang.swift.decl.function.method.instance,
1475+
key.name: "deinit",
1476+
key.offset: 2572,
1477+
key.length: 9,
1478+
key.nameoffset: 2572,
1479+
key.namelength: 6,
1480+
key.bodyoffset: 2580,
1481+
key.bodylength: 0
1482+
}
1483+
]
1484+
}
1485+
]
1486+
},
1487+
{
1488+
key.kind: source.lang.swift.decl.extension,
1489+
key.accessibility: source.lang.swift.accessibility.public,
1490+
key.name: "Outer2",
1491+
key.offset: 2596,
1492+
key.length: 55,
1493+
key.nameoffset: 2606,
1494+
key.namelength: 6,
1495+
key.bodyoffset: 2614,
1496+
key.bodylength: 36,
1497+
key.attributes: [
1498+
{
1499+
key.offset: 2589,
1500+
key.length: 6,
1501+
key.attribute: source.decl.attribute.public
1502+
}
1503+
],
1504+
key.substructure: [
1505+
{
1506+
key.kind: source.lang.swift.decl.class,
1507+
key.accessibility: source.lang.swift.accessibility.public,
1508+
key.name: "Inner2",
1509+
key.offset: 2617,
1510+
key.length: 32,
1511+
key.nameoffset: 2623,
1512+
key.namelength: 6,
1513+
key.bodyoffset: 2631,
1514+
key.bodylength: 17,
1515+
key.substructure: [
1516+
{
1517+
key.kind: source.lang.swift.decl.function.method.instance,
1518+
key.accessibility: source.lang.swift.accessibility.public,
1519+
key.name: "deinit",
1520+
key.offset: 2636,
1521+
key.length: 9,
1522+
key.nameoffset: 2636,
1523+
key.namelength: 6,
1524+
key.bodyoffset: 2644,
1525+
key.bodylength: 0
1526+
}
1527+
]
1528+
}
1529+
]
1530+
},
14311531
{
14321532
key.kind: source.lang.swift.decl.protocol,
14331533
key.accessibility: source.lang.swift.accessibility.internal,
@@ -1478,6 +1578,50 @@
14781578
}
14791579
]
14801580
},
1581+
{
1582+
key.kind: source.lang.swift.decl.protocol,
1583+
key.accessibility: source.lang.swift.accessibility.internal,
1584+
key.name: "MyProtocol",
1585+
key.offset: 2780,
1586+
key.length: 71,
1587+
key.nameoffset: 2789,
1588+
key.namelength: 10,
1589+
key.bodyoffset: 2819,
1590+
key.bodylength: 31,
1591+
key.inheritedtypes: [
1592+
{
1593+
key.name: "NSObjectProtocol"
1594+
}
1595+
],
1596+
key.attributes: [
1597+
{
1598+
key.offset: 2774,
1599+
key.length: 5,
1600+
key.attribute: source.decl.attribute.objc
1601+
}
1602+
],
1603+
key.elements: [
1604+
{
1605+
key.kind: source.lang.swift.structure.elem.typeref,
1606+
key.offset: 2801,
1607+
key.length: 16
1608+
}
1609+
],
1610+
key.substructure: [
1611+
{
1612+
key.kind: source.lang.swift.decl.var.instance,
1613+
key.accessibility: source.lang.swift.accessibility.internal,
1614+
key.name: "thing",
1615+
key.offset: 2824,
1616+
key.length: 25,
1617+
key.typename: "NSObject",
1618+
key.nameoffset: 2828,
1619+
key.namelength: 5,
1620+
key.bodyoffset: 2845,
1621+
key.bodylength: 3
1622+
}
1623+
]
1624+
},
14811625
{
14821626
key.kind: source.lang.swift.decl.class,
14831627
key.accessibility: source.lang.swift.accessibility.internal,
@@ -1487,7 +1631,44 @@
14871631
key.nameoffset: 2866,
14881632
key.namelength: 1,
14891633
key.bodyoffset: 2869,
1490-
key.bodylength: 59
1634+
key.bodylength: 59,
1635+
key.substructure: [
1636+
{
1637+
key.kind: source.lang.swift.decl.function.method.instance,
1638+
key.accessibility: source.lang.swift.accessibility.internal,
1639+
key.name: "foo(a:)",
1640+
key.offset: 2899,
1641+
key.length: 19,
1642+
key.selector_name: "fooWithA:",
1643+
key.nameoffset: 2904,
1644+
key.namelength: 11,
1645+
key.bodyoffset: 2917,
1646+
key.bodylength: 0,
1647+
key.attributes: [
1648+
{
1649+
key.offset: 2893,
1650+
key.length: 5,
1651+
key.attribute: source.decl.attribute.objc.name
1652+
},
1653+
{
1654+
key.offset: 2883,
1655+
key.length: 9,
1656+
key.attribute: source.decl.attribute.ibaction
1657+
}
1658+
],
1659+
key.substructure: [
1660+
{
1661+
key.kind: source.lang.swift.decl.var.parameter,
1662+
key.name: "a",
1663+
key.offset: 2908,
1664+
key.length: 6,
1665+
key.typename: "Int",
1666+
key.nameoffset: 2908,
1667+
key.namelength: 1
1668+
}
1669+
]
1670+
}
1671+
]
14911672
}
14921673
],
14931674
key.diagnostics: [

0 commit comments

Comments
 (0)