Skip to content

Commit

Permalink
Merge pull request #1816 from DougGregor/if-config
Browse files Browse the repository at this point in the history
SwiftIfConfig: A library to evaluate `#if` conditionals within a Swift syntax tree.
  • Loading branch information
DougGregor committed Jul 9, 2024
2 parents 9cc77ac + 8779074 commit 70e3741
Show file tree
Hide file tree
Showing 29 changed files with 2,955 additions and 19 deletions.
1 change: 1 addition & 0 deletions .spi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ builder:
- SwiftCompilerPlugin
- SwiftDiagnostics
- SwiftIDEUtils
- SwiftIfConfig
- SwiftLexicalLookup
- SwiftOperators
- SwiftParser
Expand Down
19 changes: 19 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let package = Package(
.library(name: "SwiftCompilerPlugin", targets: ["SwiftCompilerPlugin"]),
.library(name: "SwiftDiagnostics", targets: ["SwiftDiagnostics"]),
.library(name: "SwiftIDEUtils", targets: ["SwiftIDEUtils"]),
.library(name: "SwiftIfConfig", targets: ["SwiftIfConfig"]),
.library(name: "SwiftLexicalLookup", targets: ["SwiftLexicalLookup"]),
.library(name: "SwiftOperators", targets: ["SwiftOperators"]),
.library(name: "SwiftParser", targets: ["SwiftParser"]),
Expand Down Expand Up @@ -138,6 +139,24 @@ let package = Package(
dependencies: ["_SwiftSyntaxTestSupport", "SwiftIDEUtils", "SwiftParser", "SwiftSyntax"]
),

// MARK: SwiftIfConfig

.target(
name: "SwiftIfConfig",
dependencies: ["SwiftSyntax", "SwiftOperators"],
exclude: ["CMakeLists.txt"]
),

.testTarget(
name: "SwiftIfConfigTest",
dependencies: [
"_SwiftSyntaxTestSupport",
"SwiftIfConfig",
"SwiftParser",
"SwiftSyntaxMacrosGenericTestSupport",
]
),

// MARK: SwiftLexicalLookup

.target(
Expand Down
2 changes: 1 addition & 1 deletion Release Notes/600.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
- Pull request: https://github.com/swiftlang/swift-syntax/pull/2433

- `CanImportExprSyntax` and `CanImportVersionInfoSyntax`
- 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.
- 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.
- Pull request: https://github.com/swiftlang/swift-syntax/pull/2025

- `SyntaxClassifiedRange.offset`, `length` and `endOffset`
Expand Down
4 changes: 4 additions & 0 deletions Release Notes/601.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- Description: Returns the node or the first ancestor that satisfies `condition`.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2696

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

## API Behavior Changes

- `SyntaxProtocol.trimmed` detaches the node
Expand Down
1 change: 1 addition & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_subdirectory(SwiftParser)
add_subdirectory(SwiftParserDiagnostics)
add_subdirectory(SwiftRefactor)
add_subdirectory(SwiftOperators)
add_subdirectory(SwiftIfConfig)
add_subdirectory(SwiftSyntaxBuilder)
add_subdirectory(SwiftSyntaxMacros)
add_subdirectory(SwiftSyntaxMacroExpansion)
Expand Down
35 changes: 35 additions & 0 deletions Sources/SwiftDiagnostics/Diagnostic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,38 @@ public struct DiagnosticsError: Error, Sendable {
)
}
}

/// Diagnostic message used for thrown errors.
private struct DiagnosticFromError: DiagnosticMessage {
let error: Error
let severity: DiagnosticSeverity = .error

var message: String {
return String(describing: error)
}

var diagnosticID: MessageID {
.init(domain: "SwiftDiagnostics", id: "\(type(of: error))")
}
}

extension Error {
/// Given an error, produce an array of diagnostics reporting the error,
/// using the given syntax node as the location if it wasn't otherwise known.
///
/// This operation will look for diagnostics of known type, such as
/// `DiagnosticsError` and `DiagnosticMessage` to retain information. If
/// none of those apply, it will produce an `error` diagnostic whose message
/// comes from rendering the error as a string.
public func asDiagnostics(at node: some SyntaxProtocol) -> [Diagnostic] {
if let diagnosticsError = self as? DiagnosticsError {
return diagnosticsError.diagnostics
}

if let message = self as? DiagnosticMessage {
return [Diagnostic(node: Syntax(node), message: message)]
}

return [Diagnostic(node: Syntax(node), message: DiagnosticFromError(error: self))]
}
}
Loading

0 comments on commit 70e3741

Please sign in to comment.