-
Notifications
You must be signed in to change notification settings - Fork 439
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
Conversation
…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).
@swift-ci please test |
public var descriptionWithoutCommentsAndSourceLocations: String { | ||
var result = "" | ||
var skipUntilRParen = false | ||
for token in tokens(viewMode: .sourceAccurate) { |
There was a problem hiding this comment.
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
}
}
There was a problem hiding this comment.
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.
@swift-ci please test |
@swift-ci please test Windows |
var containsFeatureCheck: Bool { | ||
return clauses.contains { clause in | ||
if let condition = clause.condition { | ||
return condition.containsFeatureCheck |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 🤔
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.