How to programmatically inline ALL macros in a file?

it is possible to manually inline a macro in VSCode by right-clicking the macro token and selecting Refactor > Inline macro.

a library can thus be de-macro-ized by doing the following:

  1. inline each macro token in VSCode with the editor tool
  2. delete the import statements for the macros
  3. rename the changed files
  4. git checkout the original files containing the macro tokens
  5. exclude the original files from the Package.swift manifest

i am interested in removing macros from libraries in order to enable cross-compilation for projects that use libraries with macros.

this however is bad for the maintainability of the library. is there a way to automate this process?

1 Like

The cross-compilation issue should be fixed by Revert "Revert "Support macros when cross-compiling (#7118)" (#7352)" by MaxDesiatov · Pull Request #7353 · apple/swift-package-manager · GitHub. Once we have another Swift development snapshot that contains the changes from that RR, the issue should be fixed. It would be great if you could try that.

If you do really want to go ahead and inline all macros, the best idea I have, is to parse the source file using swift-syntax and for every MacroExpansionDeclSyntax, MacroExpansionExprSyntax and AttributeSyntax node and try expanding those, either by launching sourcekit-lsp and running the Inline macro refactoring action or by launching sourcekitd and running the refactoring from there. Inlining AttributeSyntax might fail because not all attributes are macros. It’s not an easy or clean solution though and I would recommend waiting for the fix to land in SwiftPM if that’s an option for you.