Skip to content

Conversation

DougGregor
Copy link
Member

Implement support for creating a StaticBuildConfiguration from LangOptions. That static build configuration can be printed as JSON to standard output with the new -print-static-build-config frontend option, and will be passed down to macro implementations so that can perform #if-related evaluations based on the context in which the macro is expanded. To ensure completeness, reseat the existing CompilerBuildConfiguration on top of the created StaticBuildConfiguration so all #if-handling code in the compiler goes through it.

There is a bit of code reshuffling here to establish another layer of Swift library between the C++ Basic and AST libraries. The code to form a StaticBuildConfiguration exists in this layer, so it can be used without an established ASTContext. The ASTContext does, however, have the ability to build a cache the StaticBuildConfiguration instance.

Tracked by rdar://146868706

Introduce the ability to form a `StaticBuildConfiguration` from
language options. Add a frontend option `-print-static-build-config`
to then print that static build configuration as JSON in a manner that
can be decoded into a `StaticBuildConfiguration`.

Most of the change here is in sinking the bridged ASTContext queries
of language options into a new BridgedLangOptions. The printing of the
static build configuration only has a LangOptions (not an ASTContext),
so this refactoring is required for printing.
Thread the static build configuration (formed from language options) in
to the macro plugin handler, which will serialize it for use in the
macro implementation. test this with a simple macro that checks
whether a particular custom configuration (set via `-D`) is enabled or
not.

This required some re-layering, sinking the logic for building a
StaticBuildConfiguration from language options down into a new
swiftBasicSwift library, which sits on top of the C++ swiftBasic and
provides Swift functionality for it. That can be used by the C++
swiftAST to cache the StaticBuildConfiguration on the ASTContext,
making it available for other parts of ASTGen.
Now that we have a per-ASTContext StaticBuildConfiguration, reimplement
(almost) everything in CompilerBuildConfiguration to sit on top of it.
Only canImport requires the full ASTContext, so that gets its own
implementation, as does one other operation that can produce an error.
Aside from more code sharing, this provides additional validation that
the StaticBuildConfiguration we build is complete and accurate.
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#3154

@swift-ci please test

@DougGregor
Copy link
Member Author

swiftlang/sourcekit-lsp#2312

@swift-ci please test

@DougGregor
Copy link
Member Author

@DougGregor DougGregor merged commit c4527dc into swiftlang:main Oct 1, 2025
5 checks passed
@DougGregor DougGregor deleted the static-build-configure branch October 1, 2025 03:47
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.

2 participants