Commit Graph

127 Commits

Author SHA1 Message Date
Doug Gregor
1deb72dd93 Enable emission of macro expansion buffers for diagnostics by default
This feature is too handy to leave off-by-default. Enable it by
default, but allow `-emit-macro-expansion-files no-diagnostics` to
disable it.
2023-01-31 09:40:48 -08:00
Doug Gregor
027ce8d21c [ASTGen/Macros] Introduce a Swift-side SourceManager into ASTGen.
Add SourceManager that can keep track of multiple source file syntax
nodes along with their external representations. The source manager can
emit diagnostics into any of those files, including tracking any
explicitly "detached" syntax nodes used for macro expansion.

Make sure we detach syntax nodes before passing them to macro
implementations, so they cannot see more of the source file than they
are permitted. We hadn't been doing this before (by accident), and
doing so motivated the introduction of the SourceManager.

Additionally, perform operator folding on macro arguments as part of
detaching them. Macro clients shouldn't have to do this, and moreover,
when clients do this, they lose the ability to easily emit diagnostics
on the now-folded nodes.
2023-01-28 22:23:52 -08:00
Doug Gregor
4e50e29f3d Merge pull request #63233 from DougGregor/macros-mangle-buffer-names
[Macros] Mangle the names of macro expansion buffers
2023-01-26 06:57:15 -08:00
Doug Gregor
3b6a9c75e3 [Source manager] Use macro buffer names when writing files to disk.
Use the uniquenes of mangled names to keep macro expansion buffers
around.
2023-01-25 23:27:42 -08:00
Doug Gregor
2e96587810 Use the mangling prefix @__swiftmacro_ for macro expansion buffers 2023-01-25 23:11:51 -08:00
Doug Gregor
af7ce9e945 [Macros] Remove the @declaration attribute.
We have @freestanding working appropriately now.
2023-01-25 17:07:38 -08:00
Doug Gregor
5a9a654adb Adopt @freestanding(expression) for all @expression macros 2023-01-25 17:07:38 -08:00
Doug Gregor
ad49d2e7a2 [Test] We don't need an IR test here 2023-01-20 23:29:38 -08:00
Adrian Prantl
6335f1dee5 Debug Info: Represent macro expansions as inlined functions.
This allows the debugger to choose whether to display the expanded macro
(inlined) or the original source code (parent frame).

rdar://102916513
2023-01-20 21:43:20 -08:00
Doug Gregor
6def6006f7 [Macros] Ensure that we inherit imports from enclosing file for macro expansion files 2023-01-20 21:41:56 -08:00
Doug Gregor
456fbf84f5 [Debug info] Dump macro expansion buffers to disk for debug info to reference
When emitting debug info that references into a macro expansion, dump
the macro expansion buffer into a file on disk (in the temporary
directory) so that one can see the macro expansion buffers in the
source files themselves.

The test here validates that the generated LLVM IR looks correct, but
I'm having a problem getting LLDB to actually locate the source code
correctly.
2023-01-19 21:30:25 -08:00
Richard Wei
f17b7c48bf [Macros] Freestanding declaration macros
Add support for freestanding declaration macros.

- Parse `@declaration` attribute.
- Type check and expand `MacroExpansionDecl`.

Known issues:
- Generic macros are not yet handled.
- Expansion does not work when the parent decl context is `BraceStmt`. Need to parse freestanding declaration macro expansions in `BraceStmt` as `MacroExpansionDecl`, and add expanded decls to name lookup.
2023-01-10 19:09:11 -08:00
Doug Gregor
32a918a320 [Macros] Ensure that we check the macro definition in the primary file.
Otherwise, we won't diagnose issues with a macro until the point of use.
2023-01-08 21:58:25 -08:00
Doug Gregor
dfa3ed910d Teach DeclContext::getParentSourceFile() to return the innermost source file
With macro expansions, and really any code generation that produces
proper source locations, the parent source file of a declaration
context will be a generated source file rather than the source file
near the top of the declaration-context stack. Adjust the
implementation of `getParentSourceFile()` to return that innermost
source file by doing location-based lookup.
2023-01-08 11:40:02 -08:00
Doug Gregor
c4312216b5 [Diagnostics] Emit generated code buffers into diagnostic files.
Extend the serialized diagnostics file format to also support providing
the contents of source files, which can include a reference to the
"original source range" whose text is conceptually replaced with the
contents of the serialized diagnostics file, e.g., due to macro
expansion.
2023-01-04 21:28:09 -08:00
Doug Gregor
7000969f14 Introduce and use #externalMacro for externally-defined macros.
Align the grammar of macro declarations with SE-0382, so that macro
definitions are parsed as an expression. External macro definitions
are referenced via a referenced to the macro `#externalMacro`. Define
that macro in the standard library, and recognize uses of it as the
definition of other macros to use externally-defined macros. For
example, this means that the "stringify" macro used in a lot of
examples is now defined as something like this:

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

We still parse the old "A.B" syntax for two reasons. First, it's
helpful to anyone who has existing code using the prior syntax, so they
get a warning + Fix-It to rewrite to the new syntax. Second, we use it
to define builtin macros like `externalMacro` itself, which looks like this:

    @expression
    public macro externalMacro<T>(module: String, type: String) -> T =
        Builtin.ExternalMacro

This uses the same virtual `Builtin` module as other library builtins,
and we can expand it to handle other builtin macro implementations
(such as #line) over time.
2023-01-02 21:22:05 -08:00
Doug Gregor
222bc03c0a [Macros] Prevent recursive expansion of macros. 2023-01-02 21:22:04 -08:00
Doug Gregor
0407610162 [Macros] Don't parse the macro expansion buffer twice.
Each macro expansion buffer was getting parsed twice: once by
ParseSourceFileRequest (which is used by unqualified name lookup) and
once to parse the expression when type-checking the expanded macro.
This meant that the same code had two ASTs. Hilarity ensures.

Stop directly invoking the parser on macro-expanded code. Instead, go
through ParseSourceFileRequest *as is always the right way*, and dig
out the expression we want.
2023-01-02 21:22:04 -08:00
Doug Gregor
0e691c45d2 Merge pull request #62747 from DougGregor/expression-macro-attr
[Macro] Add the expression attribute for macro declarations
2023-01-02 21:19:07 -08:00
Doug Gregor
d3540e6b59 [ASTScope] Remove a now-incorrect and not-actually-useful assertion.
This assertion is incorrect now that we have macro expansion buffers, which
don't meet the narrow definition of "inside" from this check.
2022-12-22 10:06:40 -08:00
Doug Gregor
f78f5729c3 Start requiring expression macros to be marked with @expression 2022-12-22 09:18:05 -08:00
Doug Gregor
4ef35c5f14 Embrace the swift-syntax notion of Fix-Its as separate notes.
The swift-syntax diagnostic system always treats Fix-Its as separate
notes, which are never attached to the primary diagnostic. Embrace this
module in the mapping over to the existing C++ diagnostic engine.
2022-12-15 21:24:44 -08:00
Doug Gregor
921e61b2fa Emit "in expansion of macro" notes for diagnostics within macro expansion buffers 2022-12-15 13:37:29 -08:00
Doug Gregor
cb3a24edda [Macros] Note when diagnostics came from the implementation of a macro 2022-12-14 16:18:58 -08:00
Doug Gregor
387a79c47b [Macro test] Handle source locations in macro diagnostics better.
"Relink" the folded syntax node back into the primary syntax node. When
we do this, we get consistent source locations that do not require any
adjustment.

Test this by adding a Fix-It to the silly AddBlocker macro, replacing
the `+` with a `-`.
2022-12-14 16:13:49 -08:00
Doug Gregor
a2dfd90516 [Macros] Fix test case after rename 2022-12-05 18:18:02 -08:00
Doug Gregor
1253bdd900 Rename test to something more sensible 2022-12-05 12:55:45 -08:00