Skip to content

Commit 581cf8f

Browse files
authored
Merge pull request swiftlang#75 from nkcsgexi/hash-verify
SyntaxParser: verify hash identity before calling SwiftSyntax parser.
2 parents e05ff89 + 8cf7226 commit 581cf8f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Diff for: Sources/SwiftSyntax/SyntaxNodes.swift.gyb

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
//
2020
//===----------------------------------------------------------------------===//
2121

22+
import _InternalSwiftSyntaxParser
23+
2224
%{
2325
"""
2426
Each Syntax node implements the protocol of a more generic node. For example,
@@ -207,3 +209,10 @@ extension StructDeclSyntax {
207209
return withIdentifier(newToken)
208210
}
209211
}
212+
213+
extension SyntaxParser {
214+
static func verifyNodeDeclarationHash() -> Bool {
215+
return String(cString: swiftparse_syntax_structure_versioning_identifier()!) ==
216+
"${calculate_node_hash()}"
217+
}
218+
}

Diff for: Sources/SwiftSyntax/SyntaxParser.swift

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ public enum ParserError: Error, CustomStringConvertible {
3333
/// normal circumstances, and it should be reported as a bug.
3434
case invalidSyntaxData
3535

36+
/// The SwiftSyntax parser library isn't compatible with this client
37+
case parserCompatibilityCheckFailed
38+
3639
public var description: String {
3740
switch self {
3841
case .invalidSyntaxData:
3942
return "parser created invalid syntax data"
43+
case .parserCompatibilityCheckFailed:
44+
return "SwiftSyntax parser library isn't compatible"
4045
}
4146
}
4247
}
@@ -61,6 +66,11 @@ public protocol IncrementalParseLookup {
6166

6267
/// Namespace for functions to parse swift source and retrieve a syntax tree.
6368
public struct SyntaxParser {
69+
70+
/// True if the parser library is compatible with the SwiftSyntax client;
71+
/// false otherwise.
72+
fileprivate static var nodeHashVerifyResult: Bool = verifyNodeDeclarationHash()
73+
6474
/// Parses the string into a full-fidelity Syntax tree.
6575
///
6676
/// - Parameters:
@@ -73,6 +83,9 @@ public struct SyntaxParser {
7383
source: String,
7484
parseLookup: IncrementalParseLookup? = nil
7585
) throws -> SourceFileSyntax {
86+
guard nodeHashVerifyResult else {
87+
throw ParserError.parserCompatibilityCheckFailed
88+
}
7689
// Get a native UTF8 string for efficient indexing with UTF8 byte offsets.
7790
// If the string is backed by an NSString then such indexing will become
7891
// extremely slow.
@@ -109,7 +122,6 @@ public struct SyntaxParser {
109122
parseLookup: IncrementalParseLookup?
110123
) -> RawSyntax {
111124
assert(source.isNativeUTF8)
112-
113125
let c_parser = swiftparse_parser_create()
114126
defer {
115127
swiftparse_parser_dispose(c_parser)

0 commit comments

Comments
 (0)