Commit Graph

21 Commits

Author SHA1 Message Date
Steven Wu
0a18baa510 Merge pull request #76732 from cachemeifyoucan/eng/PR-136682810
[Macro] Precise macro plugin dependency during scanning
2024-11-04 09:21:33 -08:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
Steven Wu
47b3efdb6e [Macro] Add a new macro loading option that do not involve searching
Add flag `-load-resolved-plugin` to load macro plugin, which provides a
pre-resolved entry into PluginLoader so the plugins can be loaded based
on module name without searching the file system. The option is mainly
intended to be used by explicitly module build and the flag is supplied
by dependency scanner.
2024-10-31 10:56:21 -07:00
Rintaro Ishizaki
be419a3428 [Macros] Use 'LibraryPluginProvider' in swift-plugin-server
Move 'PluginProvider' logic to 'SwiftLibraryPluginProvider' module
so the compiler can reuse that logic for the actual in-process plugins.
2024-05-07 09:47:04 -07:00
Rintaro Ishizaki
9a38072856 Revert "[Macros] Use 'LibraryPluginProvider' in swift-plugin-server" 2024-05-06 15:57:32 -07:00
Rintaro Ishizaki
26dbe2f8a1 [Macros] Use 'LibraryPluginProvider' in swift-plugin-server
Move 'PluginProvider' logic to 'SwiftLibraryPluginProvider' module
so the compiler can reuse that logic for the actual in-process plugins.
2024-05-01 16:41:14 -07:00
Doug Gregor
e7be8f32f1 Provide the lexical context to macro expansions 2024-02-02 16:03:29 -08:00
Steven Wu
407674ea52 [Test] Fix the wrong subsitution of swift-frontend-target in tests
There is no `%swift-frontend-target` subsitution in test, which is
actually `%target-swift-frontend`. The wrong spelling is actually
interpreted by lit as `%swift`-frontend-target, and surprising didn't
break any tests as the last argument from subsitution is
`-define-availability` so it just leads to an very akward availability
definition.
2023-12-18 16:07:48 -08:00
Rintaro Ishizaki
28aad9d126 [Macros] Return plugin loading error as result
Instead of emitting an warning to the diagnostic engine, return the
plugin loading error as the result of the request. So that the user
can decide to emit it as a warning or an error.
2023-10-12 16:08:54 -07:00
Rintaro Ishizaki
00f9935297 [Windows] Enable macros in Windows
After FetchContent changes, macros in Windows were disabled.
2023-09-30 14:50:07 -07:00
Rintaro Ishizaki
e9a183c502 [Macros] Fix plugin-server test for non-Darwin platforms
`.dylib` was hardcoded. Accept other platforms' shared library filenames
2023-09-29 17:06:20 -07:00
Rintaro Ishizaki
d0dfd75341 [Tests] Mark macro test 'REQUIRES: swift_swift_parser'
rdar://116239522
2023-09-29 17:06:18 -07:00
Doug Gregor
339d31fabb [Macros] Add a frontend flag -Rmacro-loading to remark on macro resolution
Macro implementations can come from various locations associated with
different search paths. Add a frontend flag `-Rmacro-loading` to emit
a remark when each macro implementation module is resolved, providing
the kind of macro (shared library, executable, shared library loaded
via the plugin server) and appropriate paths. This allows one to tell
from the build load which macros are used.

Addresses rdar://110780311.
2023-08-25 15:09:49 -07:00
Rintaro Ishizaki
eefe9dc7ef [Macros] Attached macro expansions return single string
* 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
2023-06-26 11:04:59 -07:00
Doug Gregor
3c04cff8dd [Macros] Provide the freestanding macro role for expansion operations.
The compiler knows (from a macro declaration) what freestanding macro
role a macro implementation is expected to implement. Pass that through
to the macro expansion code itself, rather than guessing based on the
protocol conformances of the implementation type. We already use this
approach with attached macros, so this is more of the same.

Eliminates a crash and improves diagnostics when the freestanding macro
role and its implementation are out of sync, fixing rdar://110418969.
2023-06-09 12:59:56 -07:00
Rintaro Ishizaki
e181a4d39c [Macros] Improve error handling for plugin errors 2023-05-04 14:40:28 -07:00
Ben Barham
e5a28caa9a [Test] Fix swift_swift_parser feature and use in tests
The macro tests were all using "REQUIRES: OS=macosx" as a proxy for
"have the Swift Swift parser". There was an existing feature for this,
but it was just checking whether the path was passed through. Fix that
to use the same variable as in CMake.

Also remove all extraneous `-I` and `-L` to the host libs in the target
invocations.
2023-04-03 09:25:03 -07:00
Rintaro Ishizaki
1d2fd4223f [Test] Add %host_triple and %host_sdkroot substitutions
Macro tests need to build host libraries/tools. We can't use %target-*
substitutions for that.

rdar://107398734
2023-03-31 07:41:41 -07:00
Rintaro Ishizaki
d5b8c84d5b [Macros] Use SWIFT_DUMP_PLUGIN_MESSAGING to test exectuable plugins
And stop using '-dump-macro-expansions'
2023-03-24 16:39:30 -07:00
Rintaro Ishizaki
a49ab25ae8 [Macros] Recovery after executable plugin crash
When executable plugins crashed or somehow decided to exit, the compiler
should relaunch the plugin executable before sending another message.
2023-03-23 22:26:42 -07:00
Rintaro Ishizaki
c4b3edd6df [Macros] Add swift-plugin-server executable
This executable is intended to be installed in the toolchain and act as
an executable compiler plugin just like other 'macro' plugins.

This plugin server has an optional method 'loadPluginLibrary' that
dynamically loads dylib plugins.
The compiler has a newly added option '-external-plugin-path'. This
option receives a pair of the plugin library search path (just like
'-plugin-path') and the corresponding "plugin server" path, separated
by '#'. i.e.

  -external-plugin-path
    <plugin library search path>#<plugin server executable path>

For exmaple, when there's a macro decl:

  @freestanding(expression)
  macro stringify<T>(T) -> (T, String) =
      #externalMacro(module: "BasicMacro", type: "StringifyMacro")

The compiler look for 'libBasicMacro.dylib' in '-plugin-path' paths,
if not found, it falls back to '-external-plugin-path' and tries to find
'libBasicMacro.dylib' in them. If it's found, the "plugin server" path
is launched just like an executable plugin, then 'loadPluginLibrary'
method is invoked via IPC, which 'dlopen' the library path in the plugin
server. At the actual macro expansion, the mangled name for
'BasicMacro.StringifyMacro' is used to resolve the macro  just like
dylib plugins in the compiler.

This is useful for
 * Isolating the plugin process, so the plugin crashes doesn't result
   the compiler crash
 * Being able to use library plugins linked with other `swift-syntax`
   versions

rdar://105104850
2023-03-16 14:00:45 -07:00