Skip to content

Commit 8ef16b2

Browse files
authored
Merge pull request #673 from DougGregor/c-consistency-checking
Introduce a C function that performs parser consistency checks.
2 parents 4c0886e + 144aa31 commit 8ef16b2

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ let package = Package(
110110
dependencies: ["SwiftDiagnostics", "SwiftSyntax"],
111111
exclude: [
112112
"CMakeLists.txt",
113-
"README.md"
113+
"README.md",
114+
"SwiftParserCompilerSupport.h"
114115
]
115116
),
116117
.executableTarget(

Sources/SwiftParser/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_library(SwiftParser STATIC
1212
Declarations.swift
1313
Directives.swift
1414
CharacterInfo.swift
15+
CompilerSupport.swift
1516
Attributes.swift
1617
Lookahead.swift
1718
Expressions.swift
@@ -39,7 +40,8 @@ set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftParser)
3940

4041
# NOTE: workaround for CMake not setting up include flags yet
4142
set_target_properties(SwiftParser PROPERTIES
42-
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
43+
INTERFACE_INCLUDE_DIRECTORIES
44+
"${CMAKE_Swift_MODULE_DIRECTORY} ${CMAKE_CURRENT_SOURCE_DIR}")
4345

4446
install(TARGETS SwiftParser
4547
EXPORT SwiftSyntaxTargets
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===-------------------------- CompilerSupport.swift ---------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// Entry point for the Swift compiler to use for consistency checking.
14+
///
15+
/// - Parameters:
16+
/// - bufferPtr: Pointer to the input buffer.
17+
/// - bufferLength: Length of the input buffer.
18+
/// - filename: The name of the source file, which is used only for diagnostics
19+
/// - flags: Flags that indicate what checks should be performed.
20+
/// 0x01: Perform round-trip checking.
21+
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
22+
@_cdecl("swift_parser_consistencyCheck")
23+
@_spi(SwiftCompiler)
24+
public func _parserConsistencyCheck(
25+
bufferPtr: UnsafePointer<UInt8>, bufferLength: Int,
26+
filename: UnsafePointer<UInt8>, flags: CUnsignedInt
27+
) -> CInt {
28+
let buffer = UnsafeBufferPointer<UInt8>(
29+
start: bufferPtr, count: bufferLength)
30+
var parser = Parser(buffer)
31+
return withExtendedLifetime(parser) { () -> CInt in
32+
// Parse the source file
33+
let sourceFile = parser.parseSourceFile()
34+
35+
// Round-trip test.
36+
if flags & 0x01 != 0 {
37+
var bufferArray = [UInt8](buffer)
38+
bufferArray.append(0)
39+
if "\(sourceFile)" != String(cString: bufferArray) {
40+
print(
41+
"\(String(cString: filename)): error: file failed to round-trip")
42+
return 1
43+
}
44+
}
45+
46+
return 0
47+
}
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*===----------------------- SwiftParserCompilerSupport.h -------------------===
2+
3+
This source file is part of the Swift.org open source project
4+
5+
Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
Licensed under Apache License v2.0 with Runtime Library Exception
7+
8+
See https://swift.org/LICENSE.txt for license information
9+
See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
11+
===----------------------------------------------------------------------===*/
12+
#ifndef SWIFT_PARSER_COMPILER_SUPPORT_H
13+
#define SWIFT_PARSER_COMPILER_SUPPORT_H
14+
15+
#include <stddef.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
enum SwiftParserConsistencyCheckFlags {
22+
/// Ensure that the syntax tree produced by the parser fully reproduces the
23+
/// input source.
24+
SPCC_RoundTrip = 0x01
25+
};
26+
27+
/// Entry point for the Swift compiler to use for consistency checking.
28+
///
29+
/// - Parameters:
30+
/// - bufferPtr: Pointer to the input buffer.
31+
/// - bufferLength: Length of the input buffer.
32+
/// - filename: The name of the source file, which is used only for diagnostics
33+
/// - flags: Flags that indicate what checks should be performed.
34+
/// 0x01: Perform round-trip checking.
35+
/// - Returns: 0 if all requested consistency checks passed, nonzero otherwise.
36+
int swift_parser_consistencyCheck(
37+
const char *buffer, ptrdiff_t bufferLength, const char *filename,
38+
unsigned int flags);
39+
40+
#ifdef __cplusplus
41+
}
42+
#endif
43+
44+
#endif /* SWIFT_PARSER_COMPILER_SUPPORT_H */

0 commit comments

Comments
 (0)