1010//
1111//===----------------------------------------------------------------------===//
1212
13+ import SwiftDiagnostics
1314import SwiftSyntax
1415
1516extension VersionTuple {
@@ -20,11 +21,35 @@ extension VersionTuple {
2021 /// we are parsing.
2122 /// - versionSyntax: The syntax node that contains the version string, used
2223 /// only for diagnostic purposes.
23- init (
24- parsingCompilerBuildVersion versionString: String ,
24+ static func parseCompilerBuildVersion (
25+ _ versionString: String ,
2526 _ versionSyntax: ExprSyntax
26- ) throws {
27- components = [ ]
27+ ) -> ( version: VersionTuple ? , diagnostics: [ Diagnostic ] ) {
28+ var extraDiagnostics : [ Diagnostic ] = [ ]
29+ let version : VersionTuple ?
30+ do {
31+ version = try parseCompilerBuildVersion ( versionString, versionSyntax, extraDiagnostics: & extraDiagnostics)
32+ } catch {
33+ version = nil
34+ extraDiagnostics. append ( contentsOf: error. asDiagnostics ( at: versionSyntax) )
35+ }
36+
37+ return ( version, extraDiagnostics)
38+ }
39+
40+ /// Parse a compiler build version of the form "5007.*.1.2.3*", which is
41+ /// used by an older if configuration form `_compiler_version("...")`.
42+ /// - Parameters:
43+ /// - versionString: The version string for the compiler build version that
44+ /// we are parsing.
45+ /// - versionSyntax: The syntax node that contains the version string, used
46+ /// only for diagnostic purposes.
47+ static func parseCompilerBuildVersion(
48+ _ versionString: String ,
49+ _ versionSyntax: ExprSyntax ,
50+ extraDiagnostics: inout [ Diagnostic ]
51+ ) throws -> VersionTuple {
52+ var components : [ Int ] = [ ]
2853
2954 // Version value are separated by periods.
3055 let componentStrings = versionString. split ( separator: " . " , omittingEmptySubsequences: false )
@@ -49,7 +74,9 @@ extension VersionTuple {
4974 // The second component is always "*", and is never used for comparison.
5075 if index == 1 {
5176 if componentString != " * " {
52- throw IfConfigError . compilerVersionSecondComponentNotWildcard ( syntax: versionSyntax)
77+ extraDiagnostics. append (
78+ IfConfigError . compilerVersionSecondComponentNotWildcard ( syntax: versionSyntax) . asDiagnostic
79+ )
5380 }
5481 try recordComponent ( 0 )
5582 continue
@@ -102,5 +129,7 @@ extension VersionTuple {
102129 }
103130 components [ 0 ] = components [ 0 ] / 1000
104131 }
132+
133+ return VersionTuple ( components: components)
105134 }
106135}
0 commit comments