1313import ASTBridging
1414import SwiftDiagnostics
1515@_spi ( Compiler) import SwiftIfConfig
16- import SwiftParser
17- import SwiftSyntax
16+ @ _spi ( ExperimentalLanguageFeatures ) import SwiftParser
17+ @ _spi ( ExperimentalLanguageFeatures ) import SwiftSyntax
1818
1919/// A build configuration that uses the compiler's ASTContext to answer
2020/// queries.
@@ -517,7 +517,11 @@ public func extractInlinableText(
517517 sourceText: BridgedStringRef
518518) -> BridgedStringRef {
519519 let textBuffer = UnsafeBufferPointer < UInt8 > ( start: sourceText. data, count: sourceText. count)
520- var parser = Parser ( textBuffer)
520+ var parser = Parser (
521+ textBuffer,
522+ swiftVersion: Parser . SwiftVersion ( from: astContext) ,
523+ experimentalFeatures: Parser . ExperimentalFeatures ( from: astContext)
524+ )
521525 let syntax = SourceFileSyntax . parse ( from: & parser)
522526
523527 let configuration = CompilerBuildConfiguration (
@@ -531,6 +535,24 @@ public func extractInlinableText(
531535 retainFeatureCheckIfConfigs: true
532536 ) . result
533537
538+ // Remove "unsafe" expressions.
539+ let inlineableSyntax = syntaxWithoutInactive. withoutUnsafeExpressions
540+
534541 // Remove comments and return the result.
535- return allocateBridgedString ( syntaxWithoutInactive. descriptionWithoutCommentsAndSourceLocations)
542+ return allocateBridgedString ( inlineableSyntax. descriptionWithoutCommentsAndSourceLocations)
543+ }
544+
545+ /// Used by withoutUnsafeExpressions to remove "unsafe" expressions from
546+ /// a syntax tree.
547+ fileprivate class RemoveUnsafeExprSyntaxRewriter : SyntaxRewriter {
548+ override func visit( _ node: UnsafeExprSyntax ) -> ExprSyntax {
549+ return node. expression. with ( \. leadingTrivia, node. leadingTrivia)
550+ }
551+ }
552+
553+ extension SyntaxProtocol {
554+ /// Return a new syntax tree with all "unsafe" expressions removed.
555+ var withoutUnsafeExpressions : Syntax {
556+ RemoveUnsafeExprSyntaxRewriter ( ) . rewrite ( self )
557+ }
536558}
0 commit comments