|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftDiagnostics |
| 14 | +import SwiftParser |
| 15 | +import SwiftSyntax |
| 16 | +import SwiftSyntaxBuilder |
| 17 | +import SwiftSyntaxMacros |
| 18 | + |
| 19 | +struct TaskMacroDiagnostic: DiagnosticMessage { |
| 20 | + static func diagnose(at node: some SyntaxProtocol) -> Diagnostic { |
| 21 | + Diagnostic(node: Syntax(node), message: Self.init()) |
| 22 | + } |
| 23 | + |
| 24 | + var message: String { |
| 25 | + "'@StartTask' macro can only be used on functions with an implementation" |
| 26 | + } |
| 27 | + |
| 28 | + var severity: DiagnosticSeverity { .error } |
| 29 | + |
| 30 | + var diagnosticID: MessageID { |
| 31 | + MessageID(domain: "_Concurrency", id: "StartMacro.\(self)") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +public struct StartTaskMacro: BodyMacro { |
| 37 | + public static func expansion( |
| 38 | + of node: AttributeSyntax, |
| 39 | + providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax, |
| 40 | + in context: some MacroExpansionContext |
| 41 | + ) throws -> [CodeBlockItemSyntax] { |
| 42 | + guard let taskBody = declaration.body else { |
| 43 | + context.diagnose(TaskMacroDiagnostic.diagnose(at: node)) |
| 44 | + return [] |
| 45 | + } |
| 46 | + |
| 47 | + return [ |
| 48 | + """ |
| 49 | + Task \(taskBody) |
| 50 | + """ |
| 51 | + ] |
| 52 | + } |
| 53 | +} |
0 commit comments