Skip to content

Commit d4c86ab

Browse files
committed
Move swift_parser_consistencyCheck into SwiftCompilerSupport.
1 parent c24622b commit d4c86ab

File tree

5 files changed

+74
-135
lines changed

5 files changed

+74
-135
lines changed

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ let package = Package(
114114
exclude: [
115115
"CMakeLists.txt",
116116
"README.md",
117-
"SwiftParserCompilerSupport.h",
118117
"TypeAttribute.swift.gyb",
119118
"DeclarationModifier.swift.gyb",
120119
"DeclarationAttribute.swift.gyb",

Sources/SwiftCompilerSupport/ConsistencyCheck.swift

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,77 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
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+
}

Sources/SwiftParser/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ add_library(SwiftParser STATIC
1010
Attributes.swift
1111
Availability.swift
1212
CharacterInfo.swift
13-
CompilerSupport.swift
1413
Declarations.swift
1514
Directives.swift
1615
Expressions.swift

Sources/SwiftParser/CompilerSupport.swift

Lines changed: 0 additions & 84 deletions
This file was deleted.

Sources/SwiftParser/SwiftParserCompilerSupport.h

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)