Skip to content

Commit ada0ceb

Browse files
authored
Merge pull request #76332 from DougGregor/sourcekit-drop-ifconfigdecl
Ignore inactive IfConfigDecls in SourceKit's syntax model, formatting, and placeholder expansion
2 parents 7aaed1e + a4852f0 commit ada0ceb

17 files changed

+44
-996
lines changed

CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
66
## Swift (next)
77

8+
* Syntactic SourceKit queries no longer attempt to provide information
9+
within the inactive `#if` regions. For example, given:
10+
11+
```swift
12+
#if DEBUG
13+
extension MyType: CustomDebugStringConvertible {
14+
var debugDescription: String { ... }
15+
}
16+
#endif
17+
```
18+
19+
If `DEBUG` is not set, SourceKit results will not involve the
20+
inactive code. Clients should use either SourceKit-LSP or
21+
swift-syntax for syntactic queries that are independent of the
22+
specific build configuration.
23+
824
* [SE-0442][]:
925
TaskGroups can now be created without explicitly specifying their child task's result types:
1026

lib/IDE/Formatting.cpp

-33
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,6 @@ class RangeWalker: protected ASTWalker {
487487
if (D->isImplicit())
488488
return Action::Continue();
489489

490-
// Walk into inactive config regions.
491-
if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
492-
for (auto Clause : ICD->getClauses()) {
493-
for (auto Member : Clause.Elements)
494-
Member.walk(*this);
495-
}
496-
return Action::SkipNode();
497-
}
498-
499490
SourceLoc ContextLoc = D->getStartLoc();
500491

501492
if (auto *GC = D->getAsGenericContext()) {
@@ -1396,17 +1387,6 @@ class FormatWalker : public ASTWalker {
13961387
}
13971388
}
13981389

1399-
// Walk into inactive config regions.
1400-
if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
1401-
if (Action.shouldVisitChildren()) {
1402-
for (auto Clause : ICD->getClauses()) {
1403-
for (auto Member : Clause.Elements)
1404-
Member.walk(*this);
1405-
}
1406-
}
1407-
return Action::SkipNode();
1408-
}
1409-
14101390
// FIXME: We ought to be able to use Action::VisitChildrenIf here, but we'd
14111391
// need to ensure the AST is walked in source order (currently not the case
14121392
// for things like postfix operators).
@@ -1933,19 +1913,6 @@ class FormatWalker : public ASTWalker {
19331913
return IndentContext {ContextLoc, !OutdentChecker::hasOutdent(SM, D)};
19341914
}
19351915

1936-
if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
1937-
for (auto &Clause: ICD->getClauses()) {
1938-
if (Clause.Loc == TargetLocation)
1939-
break;
1940-
if (auto *Cond = Clause.Cond) {
1941-
SourceRange CondRange = Cond->getSourceRange();
1942-
if (CondRange.isValid() && overlapsTarget(CondRange))
1943-
return IndentContext {Clause.Loc, true};
1944-
}
1945-
}
1946-
return IndentContext { ICD->getStartLoc(), false };
1947-
}
1948-
19491916
switch (D->getKind()) {
19501917
case DeclKind::InfixOperator:
19511918
case DeclKind::PostfixOperator:

lib/IDE/SyntaxModel.cpp

+2-30
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@ class ModelASTWalker : public ASTWalker {
416416
private:
417417
static bool findUrlStartingLoc(StringRef Text, unsigned &Start,
418418
std::regex& Regex);
419-
bool annotateIfConfigConditionIdentifiers(Expr *Cond);
420419
bool handleAttrs(const ParsedDeclAttributes &Attrs);
421420
bool handleAttrs(ArrayRef<TypeOrCustomAttr> Attrs);
422421

@@ -1003,24 +1002,8 @@ ASTWalker::PreWalkAction ModelASTWalker::walkToDeclPre(Decl *D) {
10031002
}
10041003
pushStructureNode(SN, VD);
10051004

1006-
} else if (auto *ConfigD = dyn_cast<IfConfigDecl>(D)) {
1007-
for (auto &Clause : ConfigD->getClauses()) {
1008-
if (Clause.Cond && !annotateIfConfigConditionIdentifiers(Clause.Cond))
1009-
return Action::SkipNode();
1010-
1011-
InactiveClauseRAII inactiveClauseRAII(inInactiveClause, !Clause.isActive);
1012-
for (auto &Element : Clause.Elements) {
1013-
if (auto *E = Element.dyn_cast<Expr*>()) {
1014-
E->walk(*this);
1015-
} else if (auto *S = Element.dyn_cast<Stmt*>()) {
1016-
S->walk(*this);
1017-
} else {
1018-
Element.get<Decl*>()->walk(*this);
1019-
}
1020-
NodesVisitedBefore.insert(Element);
1021-
}
1022-
}
1023-
1005+
} else if (isa<IfConfigDecl>(D)) {
1006+
// Note: nothing to do.
10241007
} else if (auto *EnumCaseD = dyn_cast<EnumCaseDecl>(D)) {
10251008
SyntaxStructureNode SN;
10261009
setDecl(SN, D);
@@ -1172,17 +1155,6 @@ class IdRefWalker : public ASTWalker {
11721155
};
11731156
} // end anonymous namespace
11741157

1175-
bool ModelASTWalker::annotateIfConfigConditionIdentifiers(Expr *Cond) {
1176-
if (!Cond)
1177-
return true;
1178-
auto passNode = [&](CharSourceRange R) {
1179-
return passNonTokenNode({ SyntaxNodeKind::BuildConfigId, R });
1180-
};
1181-
1182-
IdRefWalker<decltype(passNode)> Walker(passNode);
1183-
return Cond->walk(Walker);
1184-
}
1185-
11861158
bool ModelASTWalker::handleSpecialDeclAttribute(const DeclAttribute *D,
11871159
ArrayRef<Token> Toks) {
11881160
if (!D)

0 commit comments

Comments
 (0)