Skip to content

Move swift_parser_consistencyCheck into SwiftCompilerSupport. #814

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 1 commit into from
Sep 18, 2022
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
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ let package = Package(
exclude: [
"CMakeLists.txt",
"README.md",
"SwiftParserCompilerSupport.h",
"TypeAttribute.swift.gyb",
"DeclarationModifier.swift.gyb",
"DeclarationAttribute.swift.gyb",
Expand Down
76 changes: 74 additions & 2 deletions Sources/SwiftCompilerSupport/ConsistencyCheck.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,77 @@
//===----------------------------------------------------------------------===//

import SwiftOperators
import SwiftParser
import SwiftSyntax
@_spi(Testing) @_spi(RawSyntax) import SwiftParser
@_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.
///
/// - Parameters:
/// - bufferPtr: Pointer to the input buffer.
/// - bufferLength: Length of the input buffer.
/// - 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)
public func _parserConsistencyCheck(
bufferPtr: UnsafePointer<UInt8>, bufferLength: Int,
filename: UnsafePointer<UInt8>, flags: CUnsignedInt
) -> CInt {
let buffer = UnsafeBufferPointer<UInt8>(
start: bufferPtr, count: bufferLength)
var parser = Parser(buffer)
return withExtendedLifetime(parser) { () -> CInt in
// Parse the source file
let rawSourceFile = parser.parseSourceFile()

// Round-trip test.
if flags & 0x01 != 0 {
if rawSourceFile.raw.syntaxTextBytes != [UInt8](buffer) {
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
}
}
1 change: 0 additions & 1 deletion Sources/SwiftParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ add_library(SwiftParser STATIC
Attributes.swift
Availability.swift
CharacterInfo.swift
CompilerSupport.swift
Declarations.swift
Directives.swift
Expressions.swift
Expand Down
84 changes: 0 additions & 84 deletions Sources/SwiftParser/CompilerSupport.swift

This file was deleted.

47 changes: 0 additions & 47 deletions Sources/SwiftParser/SwiftParserCompilerSupport.h

This file was deleted.