Skip to content

[ASTGen] Implement #if config handling using the SwiftIfConfig library #75937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 21, 2024

Conversation

DougGregor
Copy link
Member

Wherever there is a syntax collection in the syntax tree that can involve an #if clause, evaluate the #if conditions to find the active clause, then recurse into the active clause (if one exists) to "flatten" the translated collection to only contain active elements.

Note that this does not yet handle #if for postfix expressions.

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

Wherever there is a syntax collection in the syntax tree that can involve
an `#if` clause, evaluate the `#if` conditions to find the active clause,
then recurse into the active clause (if one exists) to "flatten" the
translated collection to only contain active elements.

Note that this does not yet handle #if for postfix expressions.
@DougGregor DougGregor force-pushed the astgen-swiftifconfig branch from d2f5e13 to 1da2631 Compare August 18, 2024 05:48
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@@ -18,9 +18,14 @@ import SwiftSyntax

/// A build configuration that uses the compiler's ASTContext to answer
/// queries.
struct CompilerBuildConfiguration: BuildConfiguration {
final class CompilerBuildConfiguration: BuildConfiguration {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we keep this a struct and construct it directly in the ASTGenVisitor's activeClause(in:) instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. The real problem is that we don't have locations for import paths, so CompilerBuildConfiguration has mutable state for the location. I'm considering expanding BuildConfiguration to have a new "canImport" check that provides more source locations, which makes all of this cleaner.

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 really, really, really wanted to avoid having syntax nodes or source locations passed in to the BuildConfiguration for philosophical reasons, but canImport needs them. I'll go make those changes on the swift-syntax side and clean things up before coming back here to make this a struct again and drop the mutable state.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be clear, I meant keeping the conditionLoc on CompilerBuildConfiguration, but keeping it immutable by constructing it when querying the active clause, it doesn't seem like there are any other ASTGen methods that need it to be mutable state on self. I agree that sinking it down into canImport would probably be cleaner though.

Copy link
Member Author

@DougGregor DougGregor Aug 21, 2024

Choose a reason for hiding this comment

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

I've moved it back to a struct for now, pending the larger cleanup

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit e27f195 into swiftlang:main Aug 21, 2024
3 checks passed
@DougGregor DougGregor deleted the astgen-swiftifconfig branch August 21, 2024 13:34
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