Skip to content

[5.9][SourceKit] Add request to expand macros syntactically #66383

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

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Jun 6, 2023

Cherry-pick #66295 into release/5.9

  • Explanation: Add a new SourceKit request to expand macros syntactically without any module imports or type checking.
  • Scope: SourceKit
  • Risk: Low. This is a new SourceKit request. Almost NFC for exiting compiler functionalities.
  • Testing: Added regression test cases
  • Issue: rdar://109118982
  • Reviewer: Alex Hoppen (@ahoppen) Ben Barham (@bnbarham)

@rintaro
Copy link
Member Author

rintaro commented Jun 6, 2023

@swift-ci Please test

@rintaro rintaro force-pushed the 5.9-sourcekit-syntactic-macroexpansion branch from 2a3e515 to b8e9cf9 Compare June 7, 2023 18:45
@rintaro
Copy link
Member Author

rintaro commented Jun 7, 2023

@swift-ci Please test

rintaro added 9 commits June 7, 2023 13:54
Introduce 'createMacroSourceFile()' that creates macro expanded
'SourceFile' object. Use it from various macro expansion functions.

(cherry picked from commit 78b2229)
'MacroExpansionDecl' and 'MacroExpansionExpr' have many common methods.
Introduce a common base class 'FreestandingMacroExpansion' that holds
'MacroExpansionInfo'.

Factor out common expansion logic to 'evaluateFreestandingMacro'
function that resembles 'evaluateAttachedMacro'.

(cherry picked from commit 86d405b)
Expand macros in the specified source file syntactically (without any
module imports, nor typechecking).

Request would look like:
```
{
  key.compilerargs: [...]
  key.sourcefile: <file name>
  key.sourcetext: <source text> (optional)
  key.expansions: [<expansion specifier>...]
}
```
`key.compilerargs` are used for getting plugins search paths. If
`key.sourcetext` is not specified, it's loaded from the file system.
Each `<expansion sepecifier>` is
```
{
  key.offset: <offset>
  key.modulename: <plugin module name>
  key.typename: <macro typename>
  key.macro_roles: [<macro role UID>...]
}
```
Clients have to provide the module and type names because that's
semantic.

Response is a `CategorizedEdits` just like (semantic) "ExpandMacro"
refactoring. Each edit object has `key.buffer_name` that can be used for
recursive expansion. If the client founds nested macro expansion in the
expanded source, it can send another request with the same compiler
arguments using the buffer name and the source to get the nested
expansions.

(cherry picked from commit 43908fa)
(cherry picked from commit 0c931e0)
(cherry picked from commit 6456241)
(cherry picked from commit 1a4a72a)
This doesn't support nested expansions for now. So there is not much to
reuse.

(cherry picked from commit 4243599)
(cherry picked from commit de41f45)
@rintaro rintaro force-pushed the 5.9-sourcekit-syntactic-macroexpansion branch from b8e9cf9 to 5e61e92 Compare June 7, 2023 20:55
@rintaro
Copy link
Member Author

rintaro commented Jun 7, 2023

@swift-ci Please test

@rintaro rintaro closed this Jun 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant