Skip to content

Commit 0d59bff

Browse files
author
Nathan Hawes
committed
[SourceKit] Fix placeholder expansion not working inside #if
Update the PlaceholderFinder ASTWalker to walk into the clauses of IfConfigDecls. It wasn't previously, resulting in any placeholders there not being expanded. Also update CallExprFinder (used to determine if expansions should use trailing closure syntax) to walk into inactive if-config clauses. Previously it only walked into active regions, so expansions never used trailing closure syntax in inactive regions. Resolves rdar://problem/51995648
1 parent 35c0fa8 commit 0d59bff

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

test/SourceKit/CodeExpand/code-expand.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,24 @@ singleExprClosureMultiArg(1) {
205205
// CHECK: withtrail {
206206
// CHECK-NEXT: <#code#>
207207
}
208+
209+
func active() {
210+
foo(<#T##value: Foo##Foo#>)
211+
// CHECK: foo(Foo)
212+
}
213+
func activeWithTrailing() {
214+
forEach(<#T##() -> ()#>)
215+
// CHECK: forEach {
216+
// CHECK-NEXT: <#code#>
217+
}
218+
#if false
219+
func inactive() {
220+
foo(<#T##value: Foo##Foo#>)
221+
// CHECK: foo(Foo)
222+
}
223+
func inactiveWithTrailing() {
224+
forEach(<#T##() -> ()#>)
225+
// CHECK: forEach {
226+
// CHECK-NEXT: <#code#>
227+
}
228+
#endif

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,22 @@ class PlaceholderExpansionScanner {
14091409
}
14101410
return { true, E };
14111411
}
1412+
1413+
bool walkToDeclPre(Decl *D) override {
1414+
if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
1415+
// The base walker assumes the content of active IfConfigDecl clauses
1416+
// has been injected into the parent context and will be walked there.
1417+
// This doesn't hold for pre-typechecked ASTs and we need to find
1418+
// placeholders in inactive clauses anyway, so walk them here.
1419+
for (auto Clause: ICD->getClauses()) {
1420+
for (auto Elem: Clause.Elements) {
1421+
Elem.walk(*this);
1422+
}
1423+
}
1424+
return false;
1425+
}
1426+
return true;
1427+
}
14121428
};
14131429

14141430
class ClosureTypeWalker: public ASTWalker {
@@ -1571,6 +1587,8 @@ class PlaceholderExpansionScanner {
15711587
return true;
15721588
}
15731589

1590+
bool shouldWalkInactiveConfigRegion() override { return true; }
1591+
15741592
Expr *findEnclosingCallArg(SourceFile &SF, SourceLoc SL) {
15751593
EnclosingCallAndArg = {nullptr, nullptr};
15761594
OuterExpr = nullptr;

0 commit comments

Comments
 (0)