Skip to content

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Sep 26, 2025

Introduce StaticBuildConfiguration, a data structure that describes all of the aspects of a build configuration that are needed for #if evaluation. It is Codable so that it can be stored and replayed later.

This also extends swift-parser-cli with a new option, --build-configuration. The input is JSON for a static build configuration, and that configuration will be applied to the parse tree to remove all inactive #if regions before doing anything else with the parse command (printing diagnostics, printing the tree, etc.). Finally, it adds a print subcommand to swift-parser-cli that prints back the tree after applying whatever transformations are described above. One can use this to
implement a basic "preprocess" step that removes all inactive #if regions from the source code.

Introduce StaticBuildConfiguration, a data structure that describes all
of the aspects of a build configuration that are needed for `#if`
evaluation. It is Codable so that it can be stored and replayed later.
@DougGregor DougGregor force-pushed the static-build-configuration branch from 09e6ae7 to 2fd4ea7 Compare September 26, 2025 17:58
Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

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

Is TestingBuildConfiguration still needed, or can it be fully subsumed by StaticBuildConfiguration?

Comment on lines 33 to 34
languageVersion: VersionTuple = VersionTuple(6),
compilerVersion: VersionTuple = VersionTuple(6, 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not super keen on having more places that we need to update version, does this need to be defaulted here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose we don't have to default it here, no

Copy link
Member Author

Choose a reason for hiding this comment

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

No, we can do without

@DougGregor
Copy link
Member Author

Is TestingBuildConfiguration still needed, or can it be fully subsumed by StaticBuildConfiguration?

It's still used to test places where the evaluation can throw errors. I subsumed it where I could.

…tion

Introduce the option "--build-configuration" to this test program. The
input is JSON for a static build configuration, and that configuration
will be applied to the parse tree to remove all inactive #if regions
before doing anything else with the parse command (printing
diagnostics, printing the tree, etc.).

Also add a "print" subcommand that prints back the tree after applying
whatever transformations are described above. One can use this to
implement a basic "preprocess" step that removes all inactive #if
regions from the source code.
@DougGregor
Copy link
Member Author

@swift-ci please test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test

The (optional) build configuration provides information about the
context in which the macro-generated code will be compiled. It allows
one to inspect various aspects of the configuration, including
available attributes and features, the target
architecture/OS/environment/etc., and any custom settings passed via
`-D` on the command line.
@DougGregor
Copy link
Member Author

@swift-ci please test

The set of imported modules in StaticBuildConfiguration cannot really
be complete, because canImport can trigger module lookups that only
the compiler can do. Remove it from the configuration, and have
canImport always throw an error to indicate the issue.
…e parser

The language mode and experimental features are available in the new
static build configuration. When it's thread, thread them down into
the parser used to create the syntax tree passed along macro
implementations.

This is the swift-syntax part of rdar://129687671.
@DougGregor DougGregor force-pushed the static-build-configuration branch from 6342a41 to a8472a9 Compare September 27, 2025 15:18
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Nice improvement. As usual, I have a few nitpicky / API design comments 😉

Also, should we add a way that allows users of assertMacroExpansion to pass in a build configuration?

public var attributes: Set<String> = []

/// The active target OS names, e.g., "Windows", "iOS".
public var targetOSNames: Set<String> = []
Copy link
Member

Choose a reason for hiding this comment

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

I would call this targetOSs since none of the other properties contain the Names suffix, eg. it’s not called targetArchitectureNames.

Copy link
Member Author

Choose a reason for hiding this comment

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

targetOSs reads so weirdly. I guess we can do that.

Copy link
Member

Choose a reason for hiding this comment

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

I thought the same the Names suffix also seems inconsistent. If you have a semi-strong opinion on the naming here, I can also be convinced to stick with targetOSNames.

Copy link
Contributor

@j-f1 j-f1 Sep 30, 2025

Choose a reason for hiding this comment

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

Maybe targetOSes? targetOperatingSystems could also work, although it might feel a bit strange to expand OS in a way that is not done in most settings anymore.

Copy link
Member Author

Choose a reason for hiding this comment

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

We're already abbreviating OperatingSystem to OS in the corresponding BuildConfiguration, so I don't want to write it out expanded here. targetOSes and targetOSs are... equally weird to me.

@DougGregor
Copy link
Member Author

Also, should we add a way that allows users of assertMacroExpansion to pass in a build configuration?

Easy enough, sure.

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor
Copy link
Member Author

swiftlang/sourcekit-lsp#2312

@swift-ci please test

@DougGregor
Copy link
Member Author

swiftlang/sourcekit-lsp#2312

@swift-ci please test macOS

@DougGregor DougGregor merged commit bd7bf0b into swiftlang:main Oct 1, 2025
34 checks passed
@DougGregor DougGregor deleted the static-build-configuration branch October 1, 2025 03:06
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.

5 participants