13
13
import ASTBridging
14
14
import SwiftDiagnostics
15
15
@_spi ( Compiler) import SwiftIfConfig
16
- import SwiftParser
17
- import SwiftSyntax
16
+ @ _spi ( ExperimentalLanguageFeatures ) import SwiftParser
17
+ @ _spi ( ExperimentalLanguageFeatures ) import SwiftSyntax
18
18
19
19
/// A build configuration that uses the compiler's ASTContext to answer
20
20
/// queries.
@@ -517,7 +517,11 @@ public func extractInlinableText(
517
517
sourceText: BridgedStringRef
518
518
) -> BridgedStringRef {
519
519
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
+ )
521
525
let syntax = SourceFileSyntax . parse ( from: & parser)
522
526
523
527
let configuration = CompilerBuildConfiguration (
@@ -531,6 +535,24 @@ public func extractInlinableText(
531
535
retainFeatureCheckIfConfigs: true
532
536
) . result
533
537
538
+ // Remove "unsafe" expressions.
539
+ let inlineableSyntax = syntaxWithoutInactive. withoutUnsafeExpressions
540
+
534
541
// 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
+ }
536
558
}
0 commit comments