|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +public import SwiftSyntax |
| 12 | +public import SwiftSyntaxMacros |
| 13 | + |
| 14 | +/// A type describing the expansion of the `@__testing` attribute macro. |
| 15 | +/// |
| 16 | +/// Supported uses: |
| 17 | +/// |
| 18 | +/// - `@__testing(semantics: "nomacrowarnings")`: suppress warning diagnostics |
| 19 | +/// generated by macros. (The implementation of this use case is held in trust |
| 20 | +/// at ``MacroExpansionContext/areWarningsSuppressed``. |
| 21 | +/// |
| 22 | +/// This type is used to implement the `@__testing` attribute macro. Do not use |
| 23 | +/// it directly. |
| 24 | +public struct PragmaMacro: PeerMacro, Sendable { |
| 25 | + public static func expansion( |
| 26 | + of node: AttributeSyntax, |
| 27 | + providingPeersOf declaration: some DeclSyntaxProtocol, |
| 28 | + in context: some MacroExpansionContext |
| 29 | + ) throws -> [DeclSyntax] { |
| 30 | + return [] |
| 31 | + } |
| 32 | + |
| 33 | + public static var formatMode: FormatMode { |
| 34 | + .disabled |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Get all pragma attributes (`@__testing`) associated with a syntax node. |
| 39 | +/// |
| 40 | +/// - Parameters: |
| 41 | +/// - node: The syntax node to inspect. |
| 42 | +/// |
| 43 | +/// - Returns: The set of pragma attributes strings associated with `node`. |
| 44 | +/// |
| 45 | +/// Attributes conditionally applied with `#if` are ignored. |
| 46 | +func pragmas(on node: some WithAttributesSyntax) -> [AttributeSyntax] { |
| 47 | + node.attributes |
| 48 | + .compactMap { attribute in |
| 49 | + if case let .attribute(attribute) = attribute { |
| 50 | + return attribute |
| 51 | + } |
| 52 | + return nil |
| 53 | + }.filter { attribute in |
| 54 | + attribute.attributeName.isNamed("__testing", inModuleNamed: "Testing") |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Get all "semantics" attributed to a syntax node using the |
| 59 | +/// `@__testing(semantics:)` attribute. |
| 60 | +/// |
| 61 | +/// - Parameters: |
| 62 | +/// - node: The syntax node to inspect. |
| 63 | +/// |
| 64 | +/// - Returns: The set of "semantics" strings associated with `node`. |
| 65 | +/// |
| 66 | +/// Attributes conditionally applied with `#if` are ignored. |
| 67 | +func semantics(of node: some WithAttributesSyntax) -> [String] { |
| 68 | + pragmas(on: node) |
| 69 | + .compactMap { attribute in |
| 70 | + if case let .argumentList(arguments) = attribute.arguments { |
| 71 | + return arguments |
| 72 | + } |
| 73 | + return nil |
| 74 | + }.filter { arguments in |
| 75 | + arguments.first?.label?.textWithoutBackticks == "semantics" |
| 76 | + }.flatMap { argument in |
| 77 | + argument.compactMap { $0.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue } |
| 78 | + } |
| 79 | +} |
0 commit comments