|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | import SwiftOperators
|
14 |
| -import SwiftParser |
15 |
| -import SwiftSyntax |
| 14 | +@_spi(Testing) @_spi(RawSyntax) import SwiftParser |
| 15 | +@_spi(RawSyntax) import SwiftSyntax |
| 16 | + |
| 17 | +extension Syntax { |
| 18 | + /// Whether this syntax node is or is enclosed within a #if. |
| 19 | + fileprivate var isInIfConfig: Bool { |
| 20 | + if self.is(IfConfigDeclSyntax.self) { |
| 21 | + return true |
| 22 | + } |
| 23 | + |
| 24 | + return parent?.isInIfConfig ?? false |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Entry point for the Swift compiler to use for consistency checking. |
| 29 | +/// |
| 30 | +/// - Parameters: |
| 31 | +/// - bufferPtr: Pointer to the input buffer. |
| 32 | +/// - bufferLength: Length of the input buffer. |
| 33 | +/// - filename: The name of the source file, which is used only for diagnostics |
| 34 | +/// - flags: Flags that indicate what checks should be performed. |
| 35 | +/// 0x01: Perform round-trip checking. |
| 36 | +/// 0x02: Check for parser diagnostics. |
| 37 | +/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise. |
| 38 | +@_cdecl("swift_parser_consistencyCheck") |
| 39 | +@_spi(SwiftCompiler) |
| 40 | +public func _parserConsistencyCheck( |
| 41 | + bufferPtr: UnsafePointer<UInt8>, bufferLength: Int, |
| 42 | + filename: UnsafePointer<UInt8>, flags: CUnsignedInt |
| 43 | +) -> CInt { |
| 44 | + let buffer = UnsafeBufferPointer<UInt8>( |
| 45 | + start: bufferPtr, count: bufferLength) |
| 46 | + var parser = Parser(buffer) |
| 47 | + return withExtendedLifetime(parser) { () -> CInt in |
| 48 | + // Parse the source file |
| 49 | + let rawSourceFile = parser.parseSourceFile() |
| 50 | + |
| 51 | + // Round-trip test. |
| 52 | + if flags & 0x01 != 0 { |
| 53 | + if rawSourceFile.raw.syntaxTextBytes != [UInt8](buffer) { |
| 54 | + print( |
| 55 | + "\(String(cString: filename)): error: file failed to round-trip") |
| 56 | + return 1 |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Diagnostics test. |
| 61 | + if flags & 0x02 != 0 { |
| 62 | + var anyDiags = false |
| 63 | + |
| 64 | + let sourceFile = Syntax(raw: rawSourceFile.raw).as(SourceFileSyntax.self)! |
| 65 | + |
| 66 | + let diags = ParseDiagnosticsGenerator.diagnostics( |
| 67 | + for: sourceFile) |
| 68 | + for diag in diags { |
| 69 | + // Skip over diagnostics within #if, because we don't know whether |
| 70 | + // we are in an active region or not. |
| 71 | + // FIXME: This heuristic could be improved. |
| 72 | + if diag.node.isInIfConfig { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + print("\(diag.debugDescription)") |
| 77 | + anyDiags = true |
| 78 | + } |
| 79 | + |
| 80 | + if anyDiags { |
| 81 | + return 1 |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return 0 |
| 86 | + } |
| 87 | +} |
0 commit comments