|
12 | 12 |
|
13 | 13 | import ASTBridging
|
14 | 14 | import SwiftIfConfig
|
| 15 | +import SwiftSyntax |
15 | 16 |
|
16 | 17 | /// A build configuration that uses the compiler's ASTContext to answer
|
17 | 18 | /// queries.
|
@@ -144,3 +145,52 @@ struct CompilerBuildConfiguration: BuildConfiguration {
|
144 | 145 | return version
|
145 | 146 | }
|
146 | 147 | }
|
| 148 | + |
| 149 | +/// Extract the #if clause range information for the given source file. |
| 150 | +@_cdecl("swift_ASTGen_configuredRegions") |
| 151 | +public func configuredRegions( |
| 152 | + astContext: BridgedASTContext, |
| 153 | + sourceFilePtr: UnsafeRawPointer, |
| 154 | + cRegionsOut: UnsafeMutablePointer<UnsafeMutablePointer<BridgedIfConfigClauseRangeInfo>?> |
| 155 | +) -> Int { |
| 156 | + let sourceFilePtr = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1) |
| 157 | + let configuration = CompilerBuildConfiguration(ctx: astContext) |
| 158 | + let regions = sourceFilePtr.pointee.syntax.configuredRegions(in: configuration) |
| 159 | + |
| 160 | + var cRegions: [BridgedIfConfigClauseRangeInfo] = [] |
| 161 | + for (ifConfig, state) in regions { |
| 162 | + let kind: BridgedIfConfigClauseKind |
| 163 | + switch state { |
| 164 | + case .active: kind = .IfConfigActive |
| 165 | + case .inactive, .unparsed: kind = .IfConfigInactive |
| 166 | + } |
| 167 | + |
| 168 | + let bodyLoc: AbsolutePosition |
| 169 | + if let elements = ifConfig.elements { |
| 170 | + bodyLoc = elements.position |
| 171 | + } else if let condition = ifConfig.condition { |
| 172 | + bodyLoc = condition.endPosition |
| 173 | + } else { |
| 174 | + bodyLoc = ifConfig.endPosition |
| 175 | + } |
| 176 | + |
| 177 | + cRegions.append( |
| 178 | + .init( |
| 179 | + directiveLoc: sourceFilePtr.pointee.sourceLoc( |
| 180 | + at: ifConfig.poundKeyword.positionAfterSkippingLeadingTrivia |
| 181 | + ), |
| 182 | + bodyLoc: sourceFilePtr.pointee.sourceLoc(at: bodyLoc), |
| 183 | + endLoc: sourceFilePtr.pointee.sourceLoc( |
| 184 | + at: ifConfig.endPosition |
| 185 | + ), |
| 186 | + kind: kind |
| 187 | + ) |
| 188 | + ) |
| 189 | + } |
| 190 | + |
| 191 | + let cRegionsBuf: UnsafeMutableBufferPointer<BridgedIfConfigClauseRangeInfo> = |
| 192 | + .allocate(capacity: cRegions.count) |
| 193 | + _ = cRegionsBuf.initialize(from: cRegions) |
| 194 | + cRegionsOut.pointee = cRegionsBuf.baseAddress |
| 195 | + return cRegionsBuf.count |
| 196 | +} |
0 commit comments