Skip to content

Extend parser consistency checking to check for failed parses. #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions Sources/SwiftParser/CompilerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_spi(RawSyntax) import SwiftSyntax

extension Syntax {
/// Whether this syntax node is or is enclosed within a #if.
fileprivate var isInIfConfig: Bool {
if self.is(IfConfigDeclSyntax.self) {
return true
}

return parent?.isInIfConfig ?? false
}
}

/// Entry point for the Swift compiler to use for consistency checking.
///
Expand All @@ -18,6 +30,7 @@
/// - filename: The name of the source file, which is used only for diagnostics
/// - flags: Flags that indicate what checks should be performed.
/// 0x01: Perform round-trip checking.
/// 0x02: Check for parser diagnostics.
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
@_cdecl("swift_parser_consistencyCheck")
@_spi(SwiftCompiler)
Expand All @@ -30,19 +43,44 @@ public func _parserConsistencyCheck(
var parser = Parser(buffer)
return withExtendedLifetime(parser) { () -> CInt in
// Parse the source file
let sourceFile = parser.parseSourceFile()
let rawSourceFile = parser.parseSourceFile()

// Round-trip test.
if flags & 0x01 != 0 {
var bufferArray = [UInt8](buffer)
bufferArray.append(0)
if "\(sourceFile)" != String(cString: bufferArray) {
if "\(rawSourceFile)" != String(cString: bufferArray) {
print(
"\(String(cString: filename)): error: file failed to round-trip")
return 1
}
}

// Diagnostics test.
if flags & 0x02 != 0 {
var anyDiags = false

let sourceFile = Syntax(raw: rawSourceFile.raw).as(SourceFileSyntax.self)!

let diags = ParseDiagnosticsGenerator.diagnostics(
for: sourceFile)
for diag in diags {
// Skip over diagnostics within #if, because we don't know whether
// we are in an active region or not.
// FIXME: This heuristic could be improved.
if diag.node.isInIfConfig {
continue
}

print("\(diag.debugDescription)")
anyDiags = true
}

if anyDiags {
return 1
}
}

return 0
}
}
5 changes: 4 additions & 1 deletion Sources/SwiftParser/SwiftParserCompilerSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ extern "C" {
enum SwiftParserConsistencyCheckFlags {
/// Ensure that the syntax tree produced by the parser fully reproduces the
/// input source.
SPCC_RoundTrip = 0x01
SPCC_RoundTrip = 0x01,

/// Check for the presence of parsing diagnostics.
SPCC_ParseDiagnostics = 0x02
};

/// Entry point for the Swift compiler to use for consistency checking.
Expand Down