Skip to content

Commit 66dba4a

Browse files
committed
Use a cached copy of ConfiguredRegions within an ExportedSourceFile
Rather than potentially computing ConfiguredRegions multiple times in ASTGen and other queries, cache the result in the ExportedSourceFile. This is temporary; we should bring up a Swift equivalent to the request-evaluator (or bridge to the C++ one) to manage state like this.
1 parent 08eefcb commit 66dba4a

File tree

4 files changed

+45
-30
lines changed

4 files changed

+45
-30
lines changed

lib/ASTGen/Sources/ASTGen/ASTGen+CompilerBuildConfiguration.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ extension ASTGenVisitor {
2525
/// produced due to the evaluation.
2626
func activeClause(in node: IfConfigDeclSyntax) -> IfConfigClauseSyntax? {
2727
// Determine the active clause.
28-
let (activeClause, diagnostics) = node.activeClause(in: buildConfiguration)
29-
diagnoseAll(diagnostics)
30-
31-
return activeClause
28+
return configuredRegions.activeClause(for: node)
3229
}
3330

3431
/// Visit a collection of elements that may contain #if clauses within it

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ASTBridging
1414
import BasicBridging
1515
import ParseBridging
16+
import SwiftIfConfig
1617
// Needed to use BumpPtrAllocator
1718
@_spi(BumpPtrAllocator) @_spi(RawSyntax) import SwiftSyntax
1819

@@ -76,7 +77,7 @@ struct ASTGenVisitor {
7677

7778
let ctx: BridgedASTContext
7879

79-
let buildConfiguration: CompilerBuildConfiguration
80+
let configuredRegions: ConfiguredRegions
8081

8182
fileprivate let allocator: SwiftSyntax.BumpPtrAllocator = .init(initialSlabSize: 256)
8283

@@ -89,17 +90,15 @@ struct ASTGenVisitor {
8990
sourceBuffer: UnsafeBufferPointer<UInt8>,
9091
declContext: BridgedDeclContext,
9192
astContext: BridgedASTContext,
93+
configuredRegions: ConfiguredRegions,
9294
legacyParser: BridgedLegacyParser
9395
) {
9496
self.diagnosticEngine = diagnosticEngine
9597
self.base = sourceBuffer
9698
self.declContext = declContext
9799
self.ctx = astContext
100+
self.configuredRegions = configuredRegions
98101
self.legacyParse = legacyParser
99-
self.buildConfiguration = CompilerBuildConfiguration(
100-
ctx: ctx,
101-
sourceBuffer: sourceBuffer
102-
)
103102
}
104103

105104
func generate(sourceFile node: SourceFileSyntax) -> [BridgedDecl] {
@@ -423,31 +422,36 @@ extension TokenSyntax {
423422
@_cdecl("swift_ASTGen_buildTopLevelASTNodes")
424423
public func buildTopLevelASTNodes(
425424
diagEngine: BridgedDiagnosticEngine,
426-
sourceFilePtr: UnsafeRawPointer,
425+
sourceFilePtr: UnsafeMutableRawPointer,
427426
dc: BridgedDeclContext,
428427
ctx: BridgedASTContext,
429428
legacyParser: BridgedLegacyParser,
430429
outputContext: UnsafeMutableRawPointer,
431430
callback: @convention(c) (UnsafeMutableRawPointer, UnsafeMutableRawPointer) -> Void
432431
) {
433432
let sourceFile = sourceFilePtr.assumingMemoryBound(to: ExportedSourceFile.self)
434-
ASTGenVisitor(
433+
let visitor = ASTGenVisitor(
435434
diagnosticEngine: diagEngine,
436435
sourceBuffer: sourceFile.pointee.buffer,
437436
declContext: dc,
438437
astContext: ctx,
438+
configuredRegions: sourceFile.pointee.configuredRegions(astContext: ctx),
439439
legacyParser: legacyParser
440440
)
441-
.generate(sourceFile: sourceFile.pointee.syntax)
442-
.forEach { callback($0.raw, outputContext) }
441+
442+
visitor.generate(sourceFile: sourceFile.pointee.syntax)
443+
.forEach { callback($0.raw, outputContext) }
444+
445+
// Diagnose any errors from evaluating #ifs.
446+
visitor.diagnoseAll(visitor.configuredRegions.diagnostics)
443447
}
444448

445449
/// Generate an AST node at the given source location. Returns the generated
446450
/// ASTNode and mutate the pointee of `endLocPtr` to the end of the node.
447451
private func _build<Node: SyntaxProtocol, Result>(
448452
generator: (ASTGenVisitor) -> (Node) -> Result,
449453
diagEngine: BridgedDiagnosticEngine,
450-
sourceFilePtr: UnsafeRawPointer,
454+
sourceFilePtr: UnsafeMutableRawPointer,
451455
sourceLoc: BridgedSourceLoc,
452456
declContext: BridgedDeclContext,
453457
astContext: BridgedASTContext,
@@ -480,6 +484,7 @@ private func _build<Node: SyntaxProtocol, Result>(
480484
sourceBuffer: sourceFile.pointee.buffer,
481485
declContext: declContext,
482486
astContext: astContext,
487+
configuredRegions: sourceFile.pointee.configuredRegions(astContext: astContext),
483488
legacyParser: legacyParser
484489
)
485490
)(node)
@@ -489,7 +494,7 @@ private func _build<Node: SyntaxProtocol, Result>(
489494
@usableFromInline
490495
func buildTypeRepr(
491496
diagEngine: BridgedDiagnosticEngine,
492-
sourceFilePtr: UnsafeRawPointer,
497+
sourceFilePtr: UnsafeMutableRawPointer,
493498
sourceLoc: BridgedSourceLoc,
494499
declContext: BridgedDeclContext,
495500
astContext: BridgedASTContext,
@@ -512,7 +517,7 @@ func buildTypeRepr(
512517
@usableFromInline
513518
func buildDecl(
514519
diagEngine: BridgedDiagnosticEngine,
515-
sourceFilePtr: UnsafeRawPointer,
520+
sourceFilePtr: UnsafeMutableRawPointer,
516521
sourceLoc: BridgedSourceLoc,
517522
declContext: BridgedDeclContext,
518523
astContext: BridgedASTContext,
@@ -535,7 +540,7 @@ func buildDecl(
535540
@usableFromInline
536541
func buildExpr(
537542
diagEngine: BridgedDiagnosticEngine,
538-
sourceFilePtr: UnsafeRawPointer,
543+
sourceFilePtr: UnsafeMutableRawPointer,
539544
sourceLoc: BridgedSourceLoc,
540545
declContext: BridgedDeclContext,
541546
astContext: BridgedASTContext,
@@ -558,7 +563,7 @@ func buildExpr(
558563
@usableFromInline
559564
func buildStmt(
560565
diagEngine: BridgedDiagnosticEngine,
561-
sourceFilePtr: UnsafeRawPointer,
566+
sourceFilePtr: UnsafeMutableRawPointer,
562567
sourceLoc: BridgedSourceLoc,
563568
declContext: BridgedDeclContext,
564569
astContext: BridgedASTContext,

lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,33 @@ enum IfConfigError: Error, CustomStringConvertible {
179179
}
180180
}
181181

182+
extension ExportedSourceFile {
183+
/// Return the configured regions for this source file.
184+
mutating func configuredRegions(astContext: BridgedASTContext) -> ConfiguredRegions {
185+
if let _configuredRegions {
186+
return _configuredRegions
187+
}
188+
189+
let configuration = CompilerBuildConfiguration(
190+
ctx: astContext,
191+
sourceBuffer: buffer
192+
)
193+
194+
let regions = syntax.configuredRegions(in: configuration)
195+
_configuredRegions = regions
196+
return regions
197+
}
198+
}
182199

183200
/// Extract the #if clause range information for the given source file.
184201
@_cdecl("swift_ASTGen_configuredRegions")
185202
public func configuredRegions(
186203
astContext: BridgedASTContext,
187-
sourceFilePtr: UnsafeRawPointer,
204+
sourceFilePtr: UnsafeMutableRawPointer,
188205
cRegionsOut: UnsafeMutablePointer<UnsafeMutablePointer<BridgedIfConfigClauseRangeInfo>?>
189206
) -> Int {
190207
let sourceFilePtr = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1)
191-
let configuration = CompilerBuildConfiguration(
192-
ctx: astContext,
193-
sourceBuffer: sourceFilePtr.pointee.buffer
194-
)
195-
let regions = sourceFilePtr.pointee.syntax.configuredRegions(in: configuration)
208+
let regions = sourceFilePtr.pointee.configuredRegions(astContext: astContext)
196209

197210
var cRegions: [BridgedIfConfigClauseRangeInfo] = []
198211

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public struct ExportedSourceFile {
3838
/// Cached so we don't need to re-build the line table every time we need to convert a position.
3939
let sourceLocationConverter: SourceLocationConverter
4040

41+
/// Configured regions for this source file.
42+
///
43+
/// This is a cached value; access via configuredRegions(astContext:).
44+
var _configuredRegions: ConfiguredRegions? = nil
45+
4146
public func position(of location: BridgedSourceLoc) -> AbsolutePosition? {
4247
let sourceFileBaseAddress = UnsafeRawPointer(buffer.baseAddress!)
4348
guard let opaqueValue = location.getOpaquePointerValue() else {
@@ -162,12 +167,7 @@ public func emitParserDiagnostics(
162167
let diags = ParseDiagnosticsGenerator.diagnostics(for: sourceFileSyntax)
163168

164169
let diagnosticEngine = BridgedDiagnosticEngine(raw: diagEnginePtr)
165-
let buildConfiguration = CompilerBuildConfiguration(
166-
ctx: ctx,
167-
sourceBuffer: sourceFile.pointee.buffer
168-
)
169-
170-
let configuredRegions = sourceFileSyntax.configuredRegions(in: buildConfiguration)
170+
let configuredRegions = sourceFile.pointee.configuredRegions(astContext: ctx)
171171
for diag in diags {
172172
// If the diagnostic is in an unparsed #if region, don't emit it.
173173
if configuredRegions.isActive(diag.node) == .unparsed {

0 commit comments

Comments
 (0)