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