Skip to content

[Macros] Attached macro expansions return single string #66918

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

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Jun 25, 2023

This is an effort of de-duplicating macro expansion codes between dylib plugins, executable plugins, and macro testing i.e. MacroSystem.

swift-syntax change: swiftlang/swift-syntax#1845

  • Move collapse(expansions:for:attachedTo:) to SwiftSyntaxMacroExpansion
  • SwiftSyntaxMacroExpansion.expandAttachedMacro() now perform collapsing
  • SwiftSyntaxMacroExpansion.expandAttachedMacroWithoutCollapsing() to keep old behavior
  • IPC request getCapability now sends the host protocol version
  • Unified IPC response macroExpansionResult that returns single string for both expandFreestandingMacro and expandAttachedMacro
  • Compiler accepts old expandFreestandingMacroResult and expandAttachedMacroResult to keep compatibility
  • Plugins check the compiler's protocol version to see if it suppports macroExpansionResult, and fall back to old behavior if necessary

Also:

  • Correct PluginMessage.MacroRole definition mismatch freeStandingDeclaration -> declaration

@rintaro
Copy link
Member Author

rintaro commented Jun 25, 2023

swiftlang/swift-syntax#1845
@swift-ci Please test

Comment on lines +54 to +58
case expandMacroResult(
expandedSource: String?,
diagnostics: [PluginMessage.Diagnostic]
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Another idea is, keep expandFreestandingMacroResult as-is, and make expandAttachedMacroResult be

  case expandAttachedMacroResult(
     expandedSource: String?,
     expandedSources: [String]?,
     diagnostics: [PluginMessage.Diagnostic]
  )

then plugins fill either expandedSource or expandedSources depending on host's protocol version.
@bnbarham wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer the new request

@rintaro rintaro changed the title [Macros] Attached macro expasnions return single string [Macros] Attached macro expansions return single string Jun 25, 2023
@rintaro rintaro force-pushed the macros-plugins-collappsed-attached-result branch from 8fd9ccc to e122087 Compare June 25, 2023 20:13
@rintaro
Copy link
Member Author

rintaro commented Jun 25, 2023

swiftlang/swift-syntax#1845
@swift-ci Please smoke test

@rintaro rintaro requested review from DougGregor, bnbarham and ahoppen and removed request for xedin, slavapestov, CodaFi, hborla and zoecarver June 26, 2023 15:47
Comment on lines +54 to +58
case expandMacroResult(
expandedSource: String?,
diagnostics: [PluginMessage.Diagnostic]
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer the new request

@@ -407,6 +407,29 @@ func checkMacroDefinition(
}
}

// Make an expansion result for '@_cdecl' function caller.
func makeExpansionOutputResult(
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't have to be this PR, but could this use BridgedString instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

withBridgedString can't escape the closure argument. Since we need to send the result back to C++ somehow, and currently (afaik) we don't have a way to call C++ lambda from Swift, I don't think it's super simple to use it.

Of course we can create BridgedString using the allocated buffer instead, but I'm not sure we should do it. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

To be clear, I meant the latter + having swift_ASTGen_expand* return it instead of taking it as an output parameter. Is the concern that BridgedString is currently only ever used in the other direction at the moment? IMO having it as the return value + a comment mentioning the caller owns the underlying buffer is enough to me. But like I said, it shouldn't block this PR so we can talk about it later.

Comment on lines 174 to 175
/// Deinitialize and unset athe plugin capability stored in C++
/// 'LoadexExeucutablePlugin'. This should be called inside lock.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// Deinitialize and unset athe plugin capability stored in C++
/// 'LoadexExeucutablePlugin'. This should be called inside lock.
/// Deinitialize and unset the plugin capability stored in C++
/// 'LoadedExecutablePlugin'. This should be called inside lock.

* Move collapse(expansions:for:attachedTo:) to SwiftSyntaxMacroExpansion
* SwiftSyntaxMacroExpansion.expandAttachedMacro() now perform collapsing
* SwiftSyntaxMacroExpansion.expandAttachedMacroWithoutCollapsing()
  to keep old behavior
* IPC request 'getCapability' now sends the host protocol version
* Unified IPC response 'macroExpansionResult' that returns single string
  for both 'expandFreestandingMacro' and 'expandAttachedMacro'
* Compiler accepts old 'expandFreestandingMacroResult' and
  'expandAttachedMacroResult' to keep compatibility
* Plugins check the compiler's protcol version to see if it suppports
  'macroExpansionResult', and fall back to old behavior if necessary
@rintaro rintaro force-pushed the macros-plugins-collappsed-attached-result branch from e122087 to eefe9dc Compare June 26, 2023 18:05
@rintaro
Copy link
Member Author

rintaro commented Jun 26, 2023

swiftlang/swift-syntax#1845
@swift-ci Please smoke test

@rintaro rintaro merged commit d230a1a into swiftlang:main Jun 26, 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.

3 participants