Skip to content

[SourceKit] Format macro decl without crashing #76441

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 2 commits into from
Sep 18, 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
21 changes: 17 additions & 4 deletions lib/IDE/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,12 @@ class FormatWalker : public ASTWalker {
// interpolated string literal.
if (E->getArgs() == Args)
ContextLoc = getContextLocForArgs(SM, E);
} else if (auto *D = Parent.getAsDecl()) {
if (auto *MED = dyn_cast<MacroExpansionDecl>(D)) {
if (MED->getArgs() == Args) {
ContextLoc = MED->getStartLoc();
}
}
}

auto Action = HandlePre(Args, Args->isImplicit());
Expand Down Expand Up @@ -2700,10 +2706,17 @@ class FormatWalker : public ASTWalker {
if (TrailingTarget)
return std::nullopt;

auto *ParentE = Parent.getAsExpr();
assert(ParentE && "Trailing closures can only occur in expr contexts");
return IndentContext{
ContextLoc, !OutdentChecker::hasOutdent(SM, ContextToEnd, ParentE)};
bool hasOutdent;
if (auto *ParentE = Parent.getAsExpr()) {
hasOutdent = OutdentChecker::hasOutdent(SM, ContextToEnd, ParentE);
} else if (auto *ParentD = Parent.getAsDecl()) {
assert(isa<MacroExpansionDecl>(ParentD) && "Trailing closures in decls can only occur in macro expansions");
hasOutdent = OutdentChecker::hasOutdent(SM, ContextToEnd, ParentD);
} else {
assert(false && "Trailing closures can only occur in expr contexts and macro expansions");
return std::nullopt;
}
return IndentContext{ContextLoc, !hasOutdent};
}

std::optional<IndentContext>
Expand Down
9 changes: 9 additions & 0 deletions test/SourceKit/CodeFormat/indent-available-macro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@available(iOS 17, *)
#Preview("") {
UIView()
}

// RUN: %sourcekitd-test -req=format -line=3 -length=1 %s >%t.response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you write the response into %t.response you should also check its contents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the fix this test crashes, and indent-crashes.swift which seems to have a goal similar to this did write into %t.response without checking its content I thought it would be fine like this.
What do you think I should do?

  • Do not write into %t.response?
  • Write into %t.response and check the content? In that case should I change the test to be badly indented on purpose checking it properly gets into the expected shape?

Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would unindent UIView() and then check that the response correctly indents UIView() to one indentation level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahoppen Thanks for the help! I just made the modifications you asked.
I also checked that running this test without my SourceKit changes still makes SourceKit crash and the test fail.

// RUN: %FileCheck --strict-whitespace %s <%t.response

// CHECK: key.sourcetext: " UIView()"