Commit Graph

127 Commits

Author SHA1 Message Date
Doug Gregor
acc8a081ca Merge pull request #65670 from DougGregor/freestanding-member-lookup
[Macros] Fix lazy expansion of freestanding macros within types/extensions
2023-05-04 16:02:31 -07:00
Doug Gregor
a690fc4abb [Macros] Fix lazy expansion of freestanding macros within types/extensions
An incorrectly-specified starting context for name lookup meant that
macro lookup itself was failing, and therefore did not trigger
expansion of the freestanding macro or introduction of the
macro-expanded declarations into the context.

Fixes rdar://108824784.
2023-05-04 12:26:12 -07:00
Hamish Knight
100244a0c3 Avoid computing generated source notes on child notes
This could cause us to double up on the same note
for nested macro expansions, as we'd generate the
note, then when recursing back through the function
we'd compute the same note again. Also remove the
seemingly unused `lastBufferID` param.
2023-05-02 16:21:44 +01:00
Rintaro Ishizaki
987e5698ae Merge pull request #65519 from rintaro/macros-nestedexpand-rdar108622244
[Macros] Don't expand macros inside macro expession arguments
2023-05-01 09:06:21 -07:00
Ben Barham
38f2e6c2b9 Merge pull request #65450 from bnbarham/format-macro-expansions
[Macros] Automatically format expanded macros
2023-04-28 16:21:33 -07:00
Rintaro Ishizaki
5c2faf3228 [Macros] Don't expand macros inside macro expession arguments
Macro expressions nested in macro arguments should be type checked, but
should not be expanded.

rdar://108622244
2023-04-28 15:58:05 -07:00
Ben Barham
fe2104ca99 [Macros] Automatically format expanded macros
Rather than requiring macro implementations to add required whitespace
and indentation, basic format all macro expansions. Right now this uses
the default four space indentation, we can consider having that inferred
later. Macros can opt-out of automatic formatting by implementing
`formatMode` and setting it to `.disabled`.

Also moves the extra newlines before/after expansions to a new "Inline
Macro" refactoring.

Resolves rdar://107731047.
2023-04-27 21:24:46 -07:00
Adrian Prantl
debb455e0a Merge pull request #65402 from adrian-prantl/108323748
Add support for nested ASTScopes inside of macro expansions.  … @adrian-prantl
2023-04-25 16:01:35 -07:00
Adrian Prantl
3901219eeb Add support for nested ASTScopes inside of macro expansions.
Before this patch the parents of SILDebugScopes representing macro expansions
were missing the inlinedAt field, which resulted in incorrent LLVM IR being
produced. This is fixed by first computing the inlined call site for a macro
expansion and then computing the nested SILDebugScope for the ASTScope of the
expanded nodes; adding the inlinedAt field to all of levels of parent scopes.

rdar://108323748
2023-04-24 14:11:19 -07:00
Alex Hoppen
3b0dc8d3a9 [Macros] Remove usages of deprecated methods in SwiftSyntaxMacros 2023-04-22 09:52:10 -07:00
Doug Gregor
c38ef86401 Disable lexing #assert unless experimental static assertions are enabled
`#assert` should be usable as a macro name, but the experimental feature
was getting in the way.

Fixes rdar://107894668.
2023-04-19 13:33:29 -07:00
swift-ci
703222583c Merge pull request #65047 from adrian-prantl/107764966
Ignore profile counter instructions in the dihole verifier.
2023-04-12 21:36:02 -07:00
Adrian Prantl
e51937f29e Don't emit freestanding macros in inlined scopes 2023-04-12 16:48:57 -07:00
Doug Gregor
a23d39bdfb [Macros] Mangle attached macro expansions based only on syntactic information
The mangling of attached macro expansions based on the declaration to
which they are attached requires semantic information (specifically,
the interface type of that declaration) that caused cyclic
dependencies during type checking. Replace the mangling with a
less-complete mangling that only requires syntactic information from
the declaration, i.e., the name of the declaration to which the macro
was attached.

This eliminates reference cycles that occur with attached macros that
produce arbitrary names.
2023-04-11 23:40:28 -04:00
Doug Gregor
527a4cdd42 [Macros] Suppress lookups into macro expansions in direct lookup in types/extensions
Eliminate a source of cyclic dependencies by not expanding macros when
we are resolving macro arguments within a type or extension context.
This extends the scheme introduced for module-scope lookup to also
apply to lookup within types.
2023-04-06 16:04:43 -07:00
swift-ci
caf6e359e5 Merge pull request #64952 from adrian-prantl/105997826
Add a test for nested macro debug info
2023-04-05 19:14:47 -07:00
Adrian Prantl
475f7798cd Add a test for nested macro debug info
rdar://105997826
2023-04-05 13:58:32 -07:00
Rintaro Ishizaki
02a604e7e7 [Tests/Macros] Add %target-codesign before %target-run
rdar://107658689
2023-04-05 12:48:09 -07:00
Adrian Prantl
829491b230 Merge pull request #64551 from adrian-prantl/88274783
Rebase SILScope generation on top of ASTScope
2023-04-05 08:30:13 -07:00
Adrian Prantl
158772c2ab Rebase SILScope generation on top of ASTScope.
This patch replaces the stateful generation of SILScope information in
SILGenFunction with data derived from the ASTScope hierarchy, which should be
100% in sync with the scopes needed for local variables. The goal is to
eliminate the surprising effects that the stack of cleanup operations can have
on the current state of SILBuilder leading to a fully deterministic (in the
sense of: predictible by a human) association of SILDebugScopes with
SILInstructions. The patch also eliminates the need to many workarounds. There
are still some accomodations for several Sema transformation passes such as
ResultBuilders, which don't correctly update the source locations when moving
around nodes. If these were implemented as macros, this problem would disappear.

This necessary rewrite of the macro scope handling included in this patch also
adds proper support nested macro expansions.

This fixes

rdar://88274783

and either fixes or at least partially addresses the following:

rdar://89252827
rdar://105186946
rdar://105757810
rdar://105997826
rdar://105102288
2023-04-04 15:20:11 -07:00
Richard Wei
01e6fe2936 [Macros] Code item macros
Add support for declaring and expanding code item macros.  Add experimental feature flag `CodeItemMacros`.
2023-04-04 09:54:57 -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
Doug Gregor
7dca692c01 Merge pull request #64840 from DougGregor/macro-lookup-in-types 2023-04-02 07:23:58 -07:00
Doug Gregor
e4365535e3 [Macros] Make sure to avoid full macro resolution in name lookup.
When populating the name lookup tables with macro-expanded declarations,
we were careful to avoid full macro resolution for attached macros, but
freestanding macros were will doing full resolution. Switch
freestanding macros over to the same partial resolution mechanism to
avoid reference cycles.
2023-04-01 00:36:42 -07:00
Richard Wei
eb8e984b97 [Macros] Private discriminators for outermost-private MacroExpansionDecl (#64813)
Add a private discriminator to the mangling of an outermost-private `MacroExpansionDecl` so that declaration macros in different files won't have colliding macro expansion buffer names.

rdar://107462515
2023-03-31 20:36:29 -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
Doug Gregor
82de47c494 [Macros] Fix name lookup in macro expansion declaration arguments.
Teach ASTScope to create child nodes for each of the arguments of a
macro expansion declaration, so that we can perform name lookup into
them. Otherwise, we cannot have nontrivial closures in arguments to a
macro expansion declaration.

Fixes rdar://107388151.
2023-03-30 00:10:20 -07:00
Doug Gregor
49277f7e89 [Macros] Move most macro definition checking into ASTGen
In preparation for supporting macros that are defined in terms of other
macros, adopt macro definition checking provided by the
`MacroDeclSyntax.checkDefinition` operation (implemented in
swift-syntax). Use this in lieu of the checking on the C++ side.

This required me to finally fix an issue with the source ranges for
Fix-Its, where we were replacing the leading/trailing trivia of nodes
along with the node itself, even though that's incorrect: we should
only replce the node itself, and there are other Fix-It kinds for
replacing leading or trailing trivia.
2023-03-29 16:30:42 -07:00
Doug Gregor
613ec8827c Merge pull request #64132 from hborla/validate-freestanding-macro-expansions
[Macros] Validate freestanding macro expansions.
2023-03-06 08:17:39 -08:00
Richard Wei
833338f9ce [Macros] Top-level freestanding macros (#63553)
Allow freestanding macros to be used at top-level.
- Parse top-level `#…` as `MacroExpansionDecl` when we are not in scripting mode.
- Add macro expansion decls to the source lookup cache with name-driven lazy expansion. Not supporting arbitrary name yet.
- Experimental support for script mode and brace-level declaration macro expansions: When type-checking a `MacroExpansionExpr`, assign it a substitute `MacroExpansionDecl` if the macro reference resolves to a declaration macro. This doesn’t work quite fully yet and will be enabled in a future fix.
2023-03-06 07:15:20 -08:00
Richard Wei
c6139f2f78 [Macros] Emit loaded module trace for plugins loaded from search paths
When loading plugins from `-plugin-path`, use the global `PluginRegistry` to keep a record of what's loaded. Emit these dependencies to the loaded module trace.
2023-03-06 02:19:56 -08:00
Holly Borla
ed4ce5cd8f [Macros] Validate freestanding macro expansions. 2023-03-05 22:55:28 -08:00
Holly Borla
9b722b9bd2 Merge pull request #64097 from hborla/macro-expansion-validation
[Macros] Diagnose macro expansions containing invalid declarations.
2023-03-05 13:22:38 -08:00
Doug Gregor
558390ea52 Merge pull request #64115 from DougGregor/macro-discardable-result 2023-03-05 07:42:04 -08:00
Doug Gregor
1c37ee5a1d [Macros] Support @discardableResult on macro declarations.
The result of an expansion of such macros can be silently ignored.
2023-03-04 22:21:02 -08:00
Doug Gregor
ba3504ad96 Add a test for a macro introducing members to an enum
There was a short time where this was broken. Add a test to make sure
we don't regress it later. rdar://105228228
2023-03-04 21:53:55 -08:00
Holly Borla
4a319b2a4e [Macros] Diagnose default literal type overrides in macro expansions. 2023-03-04 15:31:25 -08:00
Holly Borla
cd62c6d513 [Macros] Fix a few issues with invalid macro expansion diagnostics, and add tests. 2023-03-04 15:31:25 -08:00
Doug Gregor
f88d2c638f Clean up feature flags for macros.
Enable expression macros by default, and add separate feature flags for
attached and freestanding macros.
2023-03-02 14:34:59 -08:00
Doug Gregor
200f2340d9 [Macros] Be deliberate about walking macro arguments vs. expansions
Provide ASTWalker with a customization point to specify whether to
check macro arguments (which are type checked but never emitted), the
macro expansion (which is the result of applying the macro and is
actually emitted into the source), or both. Provide answers for the
~115 different ASTWalker visitors throughout the code base.

Fixes rdar://104042945, which concerns checking of effects in
macro arguments---which we shouldn't do.
2023-02-28 17:48:23 -08:00
Doug Gregor
4705183149 [Macros] Add plugin search paths to load plugin modules by name.
Introduce `-plugin-path <path>` to add a search path where we will look
for compiler plugins. When resolving an external macro definition, look
for libraries in these search paths whose names match the module name
of the macro.

Implements rdar://105095761.
2023-02-09 13:47:26 -08:00
Alex Hoppen
75771c1579 Merge pull request #63387 from ahoppen/ahoppen/macros-swift-5
[Macros] Compile Macros tests with swift-version 5 instead of swift-version 4
2023-02-07 09:16:44 +01:00
Richard Wei
01e4c8df26 [Macros] Use name lookup for lazy declaration macro expansion (#63411)
- Use the name lookup table instead of adding members from a macro expansion to the parent decl context.
- Require declaration macros to specify introduced names and used the declared names to guide macro expansions lazily.
2023-02-07 11:24:42 +08:00
Alex Hoppen
ba5a727983 [Macros] Compile Macros tests with swift-version 5 instead of swift-version 4 2023-02-04 11:10:08 +01:00
Adrian Prantl
5ff139dd6e Wire up mangled macro names in debug info.
rdar://104894694
2023-02-03 10:30:48 -08:00
Doug Gregor
8ad7601ade [Macros] Create macro expansion contexts based on the SourceManager
Rather than trying to patch up the "basic" macro expansion context
that comes from the swift-syntax package, implement our own based
on the new SourceManager. Fixes the `location(of:)` operation.
2023-02-02 23:24:56 -08:00
Doug Gregor
cbdb18097e [Macros] Fix emission of thrown errors.
Errors that were thrown out of a macro implementation were being
emitted and then lost. The result of this is a SILGen assertion later
on, if there were no other errors in the code.

Make sure we properly emit these diagnostics through the source
manager, which required fixing an issue in the offset computation used
for diagnostics.
2023-02-01 15:01:44 -08:00
Holly Borla
e41cdfbdd4 Merge pull request #63341 from hborla/generalize-macro-expansion-expr
[Macros] Generalize `MacroExpansionExpr` and use it for both freestanding and attached macros.
2023-02-01 11:04:39 -08:00
Holly Borla
26786c1e09 [Macros] Make macro expansions always have an argument list. If an explicit
argument list isn't written, an implicit, empty list is created.
2023-01-31 17:04:35 -08:00
Doug Gregor
ac4aa41d0f [Macros] Use macro expansion mangling for unique names in macros
Use the name mangling scheme we've devised for macro expansions to
back the implementation of the macro expansion context's
`getUniqueName` operation. This way, we guarantee that the names
provided by macro expansions don't conflict, as well as making them
demangleable so we can determine what introduced the names.
2023-01-31 09:40:48 -08:00