Skip to content

Ignore inactive IfConfigDecls in SourceKit's syntax model, formatting, and placeholder expansion #76332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@

## Swift (next)

* Syntactic SourceKit queries no longer attempt to provide information
within the inactive `#if` regions. For example, given:

```swift
#if DEBUG
extension MyType: CustomDebugStringConvertible {
var debugDescription: String { ... }
}
#endif
```

If `DEBUG` is not set, SourceKit results will not involve the
inactive code. Clients should use either SourceKit-LSP or
swift-syntax for syntactic queries that are independent of the
specific build configuration.

* [SE-0442][]:
TaskGroups can now be created without explicitly specifying their child task's result types:

Expand Down
33 changes: 0 additions & 33 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,6 @@ class RangeWalker: protected ASTWalker {
if (D->isImplicit())
return Action::Continue();

// Walk into inactive config regions.
if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
for (auto Clause : ICD->getClauses()) {
for (auto Member : Clause.Elements)
Member.walk(*this);
}
return Action::SkipNode();
}

SourceLoc ContextLoc = D->getStartLoc();

if (auto *GC = D->getAsGenericContext()) {
Expand Down Expand Up @@ -1396,17 +1387,6 @@ class FormatWalker : public ASTWalker {
}
}

// Walk into inactive config regions.
if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
if (Action.shouldVisitChildren()) {
for (auto Clause : ICD->getClauses()) {
for (auto Member : Clause.Elements)
Member.walk(*this);
}
}
return Action::SkipNode();
}

// FIXME: We ought to be able to use Action::VisitChildrenIf here, but we'd
// need to ensure the AST is walked in source order (currently not the case
// for things like postfix operators).
Expand Down Expand Up @@ -1933,19 +1913,6 @@ class FormatWalker : public ASTWalker {
return IndentContext {ContextLoc, !OutdentChecker::hasOutdent(SM, D)};
}

if (auto *ICD = dyn_cast<IfConfigDecl>(D)) {
for (auto &Clause: ICD->getClauses()) {
if (Clause.Loc == TargetLocation)
break;
if (auto *Cond = Clause.Cond) {
SourceRange CondRange = Cond->getSourceRange();
if (CondRange.isValid() && overlapsTarget(CondRange))
return IndentContext {Clause.Loc, true};
}
}
return IndentContext { ICD->getStartLoc(), false };
}

switch (D->getKind()) {
case DeclKind::InfixOperator:
case DeclKind::PostfixOperator:
Expand Down
32 changes: 2 additions & 30 deletions lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ class ModelASTWalker : public ASTWalker {
private:
static bool findUrlStartingLoc(StringRef Text, unsigned &Start,
std::regex& Regex);
bool annotateIfConfigConditionIdentifiers(Expr *Cond);
bool handleAttrs(const ParsedDeclAttributes &Attrs);
bool handleAttrs(ArrayRef<TypeOrCustomAttr> Attrs);

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

} else if (auto *ConfigD = dyn_cast<IfConfigDecl>(D)) {
for (auto &Clause : ConfigD->getClauses()) {
if (Clause.Cond && !annotateIfConfigConditionIdentifiers(Clause.Cond))
return Action::SkipNode();

InactiveClauseRAII inactiveClauseRAII(inInactiveClause, !Clause.isActive);
for (auto &Element : Clause.Elements) {
if (auto *E = Element.dyn_cast<Expr*>()) {
E->walk(*this);
} else if (auto *S = Element.dyn_cast<Stmt*>()) {
S->walk(*this);
} else {
Element.get<Decl*>()->walk(*this);
}
NodesVisitedBefore.insert(Element);
}
}

} else if (isa<IfConfigDecl>(D)) {
// Note: nothing to do.
} else if (auto *EnumCaseD = dyn_cast<EnumCaseDecl>(D)) {
SyntaxStructureNode SN;
setDecl(SN, D);
Expand Down Expand Up @@ -1172,17 +1155,6 @@ class IdRefWalker : public ASTWalker {
};
} // end anonymous namespace

bool ModelASTWalker::annotateIfConfigConditionIdentifiers(Expr *Cond) {
if (!Cond)
return true;
auto passNode = [&](CharSourceRange R) {
return passNonTokenNode({ SyntaxNodeKind::BuildConfigId, R });
};

IdRefWalker<decltype(passNode)> Walker(passNode);
return Cond->walk(Walker);
}

bool ModelASTWalker::handleSpecialDeclAttribute(const DeclAttribute *D,
ArrayRef<Token> Toks) {
if (!D)
Expand Down
Loading