Skip to content

Commit 70e3741

Browse files
authored
Merge pull request #1816 from DougGregor/if-config
SwiftIfConfig: A library to evaluate `#if` conditionals within a Swift syntax tree.
2 parents 9cc77ac + 8779074 commit 70e3741

29 files changed

+2955
-19
lines changed

.spi.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ builder:
1212
- SwiftCompilerPlugin
1313
- SwiftDiagnostics
1414
- SwiftIDEUtils
15+
- SwiftIfConfig
1516
- SwiftLexicalLookup
1617
- SwiftOperators
1718
- SwiftParser

Package.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ let package = Package(
1717
.library(name: "SwiftCompilerPlugin", targets: ["SwiftCompilerPlugin"]),
1818
.library(name: "SwiftDiagnostics", targets: ["SwiftDiagnostics"]),
1919
.library(name: "SwiftIDEUtils", targets: ["SwiftIDEUtils"]),
20+
.library(name: "SwiftIfConfig", targets: ["SwiftIfConfig"]),
2021
.library(name: "SwiftLexicalLookup", targets: ["SwiftLexicalLookup"]),
2122
.library(name: "SwiftOperators", targets: ["SwiftOperators"]),
2223
.library(name: "SwiftParser", targets: ["SwiftParser"]),
@@ -138,6 +139,24 @@ let package = Package(
138139
dependencies: ["_SwiftSyntaxTestSupport", "SwiftIDEUtils", "SwiftParser", "SwiftSyntax"]
139140
),
140141

142+
// MARK: SwiftIfConfig
143+
144+
.target(
145+
name: "SwiftIfConfig",
146+
dependencies: ["SwiftSyntax", "SwiftOperators"],
147+
exclude: ["CMakeLists.txt"]
148+
),
149+
150+
.testTarget(
151+
name: "SwiftIfConfigTest",
152+
dependencies: [
153+
"_SwiftSyntaxTestSupport",
154+
"SwiftIfConfig",
155+
"SwiftParser",
156+
"SwiftSyntaxMacrosGenericTestSupport",
157+
]
158+
),
159+
141160
// MARK: SwiftLexicalLookup
142161

143162
.target(

Release Notes/600.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
- Pull request: https://github.com/swiftlang/swift-syntax/pull/2433
134134

135135
- `CanImportExprSyntax` and `CanImportVersionInfoSyntax`
136-
- Description: Instead of parsing `canImport` inside `#if` directives as a special expression node, parse it as a functionc call expression. This is in-line with how the `swift(>=6.0)` and `compiler(>=6.0)` directives are parsed.
136+
- Description: Instead of parsing `canImport` inside `#if` directives as a special expression node, parse it as a function call expression. This is in-line with how the `swift(>=6.0)` and `compiler(>=6.0)` directives are parsed.
137137
- Pull request: https://github.com/swiftlang/swift-syntax/pull/2025
138138

139139
- `SyntaxClassifiedRange.offset`, `length` and `endOffset`

Release Notes/601.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
- Description: Returns the node or the first ancestor that satisfies `condition`.
1212
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2696
1313

14+
- `Error` protocol now has an `asDiagnostics(at:)` method.
15+
- Description: This method translates an error into one or more diagnostics, recognizing `DiagnosticsError` and `DiagnosticMessage` instances or providing its own `Diagnostic` as needed.
16+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/1816
17+
1418
## API Behavior Changes
1519

1620
- `SyntaxProtocol.trimmed` detaches the node

Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_subdirectory(SwiftParser)
1616
add_subdirectory(SwiftParserDiagnostics)
1717
add_subdirectory(SwiftRefactor)
1818
add_subdirectory(SwiftOperators)
19+
add_subdirectory(SwiftIfConfig)
1920
add_subdirectory(SwiftSyntaxBuilder)
2021
add_subdirectory(SwiftSyntaxMacros)
2122
add_subdirectory(SwiftSyntaxMacroExpansion)

Sources/SwiftDiagnostics/Diagnostic.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,38 @@ public struct DiagnosticsError: Error, Sendable {
9292
)
9393
}
9494
}
95+
96+
/// Diagnostic message used for thrown errors.
97+
private struct DiagnosticFromError: DiagnosticMessage {
98+
let error: Error
99+
let severity: DiagnosticSeverity = .error
100+
101+
var message: String {
102+
return String(describing: error)
103+
}
104+
105+
var diagnosticID: MessageID {
106+
.init(domain: "SwiftDiagnostics", id: "\(type(of: error))")
107+
}
108+
}
109+
110+
extension Error {
111+
/// Given an error, produce an array of diagnostics reporting the error,
112+
/// using the given syntax node as the location if it wasn't otherwise known.
113+
///
114+
/// This operation will look for diagnostics of known type, such as
115+
/// `DiagnosticsError` and `DiagnosticMessage` to retain information. If
116+
/// none of those apply, it will produce an `error` diagnostic whose message
117+
/// comes from rendering the error as a string.
118+
public func asDiagnostics(at node: some SyntaxProtocol) -> [Diagnostic] {
119+
if let diagnosticsError = self as? DiagnosticsError {
120+
return diagnosticsError.diagnostics
121+
}
122+
123+
if let message = self as? DiagnosticMessage {
124+
return [Diagnostic(node: Syntax(node), message: message)]
125+
}
126+
127+
return [Diagnostic(node: Syntax(node), message: DiagnosticFromError(error: self))]
128+
}
129+
}

0 commit comments

Comments
 (0)