Skip to content

Adopt SwiftIfConfig library to help with #if clause handling #75014

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

Conversation

DougGregor
Copy link
Member

Start adopting the new SwiftIfConfig library to reason about #if clauses in a few places in the compiler. There are two aspects to this:

  • Introduce CompilerBuildConfiguration, a wrapper around an ASTContext that conforms to the new BuildConfiguration protocol from the SwiftIfConfig library. This allows us to use the APIs in SwiftIfConfig backed by the logic for compiler conditionals (hasFeature, os(XYZ), canImport, etc.) within the compiler itself.
  • Replace the implementation of SourceFile::getIfConfigClauseRanges() with one based on SwiftIfConfig's enumeration of configured regions. This has the advantage of being less "stateful", because it doesn't need to rely on the C++ parser populating an array as it parses.

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#1816

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the swiftifconfig-build-configuration branch from 7813360 to 444a9d6 Compare July 7, 2024 04:06
@DougGregor
Copy link
Member Author

I forgot a public, heh. Trying again

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#1816

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the swiftifconfig-build-configuration branch from 444a9d6 to 37ebac2 Compare July 7, 2024 16:39
@DougGregor
Copy link
Member Author

Turns out that we had to model where the #endif directive goes, because SIL coverage map generation depends on it. No problem!

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#1816

@swift-ci please smoke test

Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

Cool!

Comment on lines +22 to +24
var name = name
return name.withBridgedString { nameRef in
Copy link
Contributor

Choose a reason for hiding this comment

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

Could take this opportunity to sink the mutating-ness into withBridgedString, it doesn't seem like any clients are really benefitting from it

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 thought it was intentional (because String's withUTF8 is mutating), but if we don't care... yeah, we could do that.

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#1816

@swift-ci please smoke test

@DougGregor DougGregor force-pushed the swiftifconfig-build-configuration branch from 5a3ff4a to a48871d Compare July 15, 2024 16:54
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

This concrete implementation of the BuildConfiguration allows the use of
the SwiftIfConfig library's APIs where the build configuration comes from
the compiler itself.
The SwiftIfConfig library provides APIs for evaluating and extracting
the active #if regions in source code. Use its "configured regions" API
along with the ASTContext-backed build configuration to reimplement the
extraction of active/inactive regions from the source.

This approach has the benefit of being effectively stateless: where the
existing solution relies on the C++ parser recording all of the `#if`
clauses it sees as it is parsing (and then might have to sort them later),
this version does a scan of source to collect the list without requiring
any other state. The newer implementation is also conceptually cleaner,
and can be shared with other clients that have their own take on the
build configuration.

The primary client of this information is the SourceKit request that
identifies "inactive" regions within the source file, which IDEs can
use to grey out inactive code within the current build configuration.
There is also some profiling information that uses it. Those clients
should be unaffected by this under-the-hood change.

For the moment, I'm leaving the old code path in place for compiler
builds that don't have swift-syntax. This should be considered
temporary, and that code should be removed in favor of request'ifying
this function and removing the incrementally-built state entirely.
…SwiftIfConfig

The SIL coverage map generation depends on the locations of the `#endif`
directives, but the mapping from SwiftIfConfig's configured regions wasn't
producing them. The information is implicitly available in the
SwiftIfConfig configured regions, so reconstitute it as we translate
regions.
This implementation depends on ASTGen, which isn't linked as part of the
AST library.
@DougGregor DougGregor force-pushed the swiftifconfig-build-configuration branch from 7d52780 to a844f4f Compare August 2, 2024 23:22
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

…dConfiguration

Unlike all of the other build configuration checks, `canImport` has
side effects including the emission of various diagnostics. Thread a
source location through here so the diagnostics end up on the right
line.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor
Copy link
Member Author

@DougGregor DougGregor merged commit fef8782 into swiftlang:main Aug 6, 2024
4 of 5 checks passed
@DougGregor DougGregor deleted the swiftifconfig-build-configuration branch August 6, 2024 06:24
hyp added a commit to swiftlang/swift-installer-scripts that referenced this pull request Aug 6, 2024
…nstaller step

swiftlang/swift#75014 added the new dependency, that the swift compiler depends on, and thus we need to make sure that this DLL is installed
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