Skip to content

Introduce a C function that performs parser consistency checks. #673

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 2 commits into from
Aug 31, 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
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ let package = Package(
dependencies: ["SwiftDiagnostics", "SwiftSyntax"],
exclude: [
"CMakeLists.txt",
"README.md"
"README.md",
"SwiftParserCompilerSupport.h"
]
),
.executableTarget(
Expand Down
4 changes: 3 additions & 1 deletion Sources/SwiftParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_library(SwiftParser STATIC
Declarations.swift
Directives.swift
CharacterInfo.swift
CompilerSupport.swift
Attributes.swift
Lookahead.swift
Expressions.swift
Expand Down Expand Up @@ -39,7 +40,8 @@ set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftParser)

# NOTE: workaround for CMake not setting up include flags yet
set_target_properties(SwiftParser PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
INTERFACE_INCLUDE_DIRECTORIES
"${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}")

install(TARGETS SwiftParser
EXPORT SwiftSyntaxTargets
Expand Down
48 changes: 48 additions & 0 deletions Sources/SwiftParser/CompilerSupport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//===-------------------------- CompilerSupport.swift ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// 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.
/// - 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 sourceFile = parser.parseSourceFile()

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

return 0
}
}
44 changes: 44 additions & 0 deletions Sources/SwiftParser/SwiftParserCompilerSupport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*===----------------------- SwiftParserCompilerSupport.h -------------------===

This source file is part of the Swift.org open source project

Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

===----------------------------------------------------------------------===*/
#ifndef SWIFT_PARSER_COMPILER_SUPPORT_H
#define SWIFT_PARSER_COMPILER_SUPPORT_H

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

enum SwiftParserConsistencyCheckFlags {
/// Ensure that the syntax tree produced by the parser fully reproduces the
/// input source.
SPCC_RoundTrip = 0x01
};

/// 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.
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
int swift_parser_consistencyCheck(
const char *buffer, ptrdiff_t bufferLength, const char *filename,
unsigned int flags);

#ifdef __cplusplus
}
#endif

#endif /* SWIFT_PARSER_COMPILER_SUPPORT_H */