Skip to content

Implementation of the compiler's "extract inlinable text" #2832

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 5 commits into from
Sep 4, 2024

Conversation

DougGregor
Copy link
Member

The compiler has an operation to extract "inlinable" text for use in Swift interface generation. The operation removes all "inactive" code (per a build configuration) that doesn't involve the compiler version or feature checks, as well as stripping comments and #sourceLocation.

Provide those APIs here based on swift-syntax.

…ving inactive regions

The compiler's handling of inlinable text for Swift interface checking
requires the ability to retain `#if`s involving compiler checks
(e.g., `#if compiler(>=6.0)`) and `$`-based feature checks (`#if $AsyncAwait`)
when stripping inactive regions. Expand the `removingInactive` function with
a parameter that implements this behavior.
Introduce a new `SyntaxProtocol.descriptionWithoutComments` that removes
comments from the given syntax, replacing them with either a space (if
the comment has no newlines) or a newline (if it does have newlines).
@DougGregor
Copy link
Member Author

@swift-ci please test

public var descriptionWithoutCommentsAndSourceLocations: String {
var result = ""
var skipUntilRParen = false
for token in tokens(viewMode: .sourceAccurate) {
Copy link
Member

Choose a reason for hiding this comment

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

SyntaxVisitor is probably faster because it uses SyntaxNodeFactory hack.

class DescriptionWithoutCommentsAndSourceLocationsVisotr: SyntaxVisitor {
  var result: String = ""
  override func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
    token.leadingTrivia.writeWithoutComments(to: &result)
    token.text.write(to: &result)
    token.trailingTrivia.writeWithoutComments(to: &result)
    return .skipChildren
  }
  override func visit(_ node: PoundSourceLocationSyntax) -> SyntaxVisitorContinueKind {
    return .skipChildren
  }
}

Copy link
Member Author

@DougGregor DougGregor Sep 4, 2024

Choose a reason for hiding this comment

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

This is much cleaner than my implementation, too! I will steal this for a follow-up PR.

…compiler SPI

I'm not sure we have any clients for this particular option outside of
the compiler, so for now, limit it to the compiler. We can expose it later
if we'd like.
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Windows

@DougGregor DougGregor merged commit c30bd36 into swiftlang:main Sep 4, 2024
3 checks passed
@DougGregor DougGregor deleted the extract-inlinable-text branch September 4, 2024 14:53
var containsFeatureCheck: Bool {
return clauses.contains { clause in
if let condition = clause.condition {
return condition.containsFeatureCheck
Copy link
Member

Choose a reason for hiding this comment

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

Just checking: If we have the following we don’t want to remove secretCode right?

#if SECRET
secretCode()
#elseif compiler(>=6.0)
swift6Code()
#else
swift5Code()
#endif

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh my, I really screwed that up. Thank you!

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, now I'm looking back at this more closely. I thought I got the basic algorithm wrong, but I did not: this matches what the compiler is doing today, where secretCode() is not removed. We could decide to do better than the compiler here if we want, and it probably wouldn't break anything.

Copy link
Member

Choose a reason for hiding this comment

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

I think that might be worth it 🤔

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.

3 participants