Skip to content

Various improvements and tweaks to bring SwiftIfConfig in line with the compiler #2774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ let package = Package(

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

Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftIfConfig/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ add_swift_syntax_library(SwiftIfConfig

target_link_swift_syntax_libraries(SwiftIfConfig PUBLIC
SwiftSyntax
SwiftSyntaxBuilder
SwiftDiagnostics
SwiftOperators)
70 changes: 67 additions & 3 deletions Sources/SwiftIfConfig/IfConfigError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxBuilder

/// Describes the kinds of errors that can occur when processing #if conditions.
enum IfConfigError: Error, CustomStringConvertible {
Expand All @@ -29,6 +30,12 @@ enum IfConfigError: Error, CustomStringConvertible {
case canImportTwoParameters(syntax: ExprSyntax)
case ignoredTrailingComponents(version: VersionTuple, syntax: ExprSyntax)
case integerLiteralCondition(syntax: ExprSyntax, replacement: Bool)
case likelySimulatorPlatform(syntax: ExprSyntax)
case endiannessDoesNotMatch(syntax: ExprSyntax, argument: String)
case macabiIsMacCatalyst(syntax: ExprSyntax)
case expectedModuleName(syntax: ExprSyntax)
case badInfixOperator(syntax: ExprSyntax)
case badPrefixOperator(syntax: ExprSyntax)

var description: String {
switch self {
Expand Down Expand Up @@ -65,7 +72,7 @@ enum IfConfigError: Error, CustomStringConvertible {
return "canImport requires a module name"

case .canImportLabel(syntax: _):
return "2nd parameter of canImport should be labeled as _version or _underlyingVersion"
return "second parameter of canImport should be labeled as _version or _underlyingVersion"

case .canImportTwoParameters(syntax: _):
return "canImport can take only two parameters"
Expand All @@ -75,6 +82,25 @@ enum IfConfigError: Error, CustomStringConvertible {

case .integerLiteralCondition(syntax: let syntax, replacement: let replacement):
return "'\(syntax.trimmedDescription)' is not a valid conditional compilation expression, use '\(replacement)'"

case .likelySimulatorPlatform:
return
"platform condition appears to be testing for simulator environment; use 'targetEnvironment(simulator)' instead"

case .macabiIsMacCatalyst:
return "'macabi' has been renamed to 'macCatalyst'"

case .endiannessDoesNotMatch:
return "unknown endianness for build configuration '_endian' (must be 'big' or 'little')"

case .expectedModuleName:
return "expected module name"

case .badInfixOperator:
return "expected '&&' or '||' expression"

case .badPrefixOperator:
return "expected unary '!' expression"
}
}

Expand All @@ -93,7 +119,13 @@ enum IfConfigError: Error, CustomStringConvertible {
.canImportLabel(syntax: let syntax),
.canImportTwoParameters(syntax: let syntax),
.ignoredTrailingComponents(version: _, syntax: let syntax),
.integerLiteralCondition(syntax: let syntax, replacement: _):
.integerLiteralCondition(syntax: let syntax, replacement: _),
.likelySimulatorPlatform(syntax: let syntax),
.endiannessDoesNotMatch(syntax: let syntax, argument: _),
.macabiIsMacCatalyst(syntax: let syntax),
.expectedModuleName(syntax: let syntax),
.badInfixOperator(syntax: let syntax),
.badPrefixOperator(syntax: let syntax):
return Syntax(syntax)

case .unsupportedVersionOperator(name: _, operator: let op):
Expand All @@ -111,7 +143,9 @@ extension IfConfigError: DiagnosticMessage {

var severity: SwiftDiagnostics.DiagnosticSeverity {
switch self {
case .ignoredTrailingComponents: return .warning
case .compilerVersionSecondComponentNotWildcard, .ignoredTrailingComponents,
.likelySimulatorPlatform, .endiannessDoesNotMatch, .macabiIsMacCatalyst:
return .warning
default: return .error
}
}
Expand Down Expand Up @@ -142,6 +176,36 @@ extension IfConfigError: DiagnosticMessage {
)
}

// For the likely targetEnvironment(simulator) condition we have a Fix-It.
if case .likelySimulatorPlatform(let syntax) = self {
return Diagnostic(
node: syntax,
message: self,
fixIt: .replace(
message: SimpleFixItMessage(
message: "replace with 'targetEnvironment(simulator)'"
),
oldNode: syntax,
newNode: "targetEnvironment(simulator)" as ExprSyntax
)
)
}

// For the targetEnvironment(macabi) -> macCatalyst rename we have a Fix-It.
if case .macabiIsMacCatalyst(syntax: let syntax) = self {
return Diagnostic(
node: syntax,
message: self,
fixIt: .replace(
message: SimpleFixItMessage(
message: "replace with 'macCatalyst'"
),
oldNode: syntax,
newNode: "macCatalyst" as ExprSyntax
)
)
}

return Diagnostic(node: syntax, message: self)
}
}
Loading