-
Notifications
You must be signed in to change notification settings - Fork 126
Don't use @_semantics, use our own attribute instead.
#874
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0ce9847
Don't use `@_semantics`, use our own attribute instead.
grynspan 9be37f4
Update CMake
grynspan e24030d
Refactor a bit and add a unit test
grynspan 40dd791
Detect fully-qualified attribute name
grynspan 4e63cc0
Update Sources/Testing/Test+Macro.swift
grynspan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| public import SwiftSyntax | ||
| public import SwiftSyntaxMacros | ||
|
|
||
| /// A type describing the expansion of the `@__testing` attribute macro. | ||
| /// | ||
| /// Supported uses: | ||
| /// | ||
| /// - `@__testing(semantics: "nomacrowarnings")`: suppress warning diagnostics | ||
| /// generated by macros. (The implementation of this use case is held in trust | ||
| /// at ``MacroExpansionContext/areWarningsSuppressed``. | ||
| /// | ||
| /// This type is used to implement the `@__testing` attribute macro. Do not use | ||
| /// it directly. | ||
| public struct PragmaMacro: PeerMacro, Sendable { | ||
| public static func expansion( | ||
| of node: AttributeSyntax, | ||
| providingPeersOf declaration: some DeclSyntaxProtocol, | ||
| in context: some MacroExpansionContext | ||
| ) throws -> [DeclSyntax] { | ||
| return [] | ||
| } | ||
|
|
||
| public static var formatMode: FormatMode { | ||
| .disabled | ||
| } | ||
| } | ||
|
|
||
| /// Get all pragma attributes (`@__testing`) associated with a syntax node. | ||
| /// | ||
| /// - Parameters: | ||
| /// - node: The syntax node to inspect. | ||
| /// | ||
| /// - Returns: The set of pragma attributes strings associated with `node`. | ||
| /// | ||
| /// Attributes conditionally applied with `#if` are ignored. | ||
| func pragmas(on node: some WithAttributesSyntax) -> [AttributeSyntax] { | ||
| node.attributes | ||
| .compactMap { attribute in | ||
| if case let .attribute(attribute) = attribute { | ||
| return attribute | ||
| } | ||
| return nil | ||
| }.filter { attribute in | ||
| attribute.attributeNameText == "__testing" | ||
grynspan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /// Get all "semantics" attributed to a syntax node using the | ||
| /// `@__testing(semantics:)` attribute. | ||
| /// | ||
| /// - Parameters: | ||
| /// - node: The syntax node to inspect. | ||
| /// | ||
| /// - Returns: The set of "semantics" strings associated with `node`. | ||
| /// | ||
| /// Attributes conditionally applied with `#if` are ignored. | ||
| func semantics(of node: some WithAttributesSyntax) -> [String] { | ||
| pragmas(on: node) | ||
| .compactMap { attribute in | ||
| if case let .argumentList(arguments) = attribute.arguments { | ||
| return arguments | ||
| } | ||
| return nil | ||
| }.filter { arguments in | ||
| arguments.first?.label?.textWithoutBackticks == "semantics" | ||
| }.flatMap { argument in | ||
| argument.compactMap { $0.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
| // | ||
|
|
||
| import Testing | ||
| @testable import TestingMacros | ||
|
|
||
| import SwiftParser | ||
| import SwiftSyntax | ||
|
|
||
| @Suite("PragmaMacro Tests") | ||
| struct PragmaMacroTests { | ||
| @Test func findSemantics() throws { | ||
| let node = """ | ||
| @__testing(semantics: "abc123") | ||
| @__testing(semantics: "def456") | ||
| let x = 0 | ||
| """ as DeclSyntax | ||
| let nodeWithAttributes = try #require(node.asProtocol((any WithAttributesSyntax).self)) | ||
| let semantics = semantics(of: nodeWithAttributes) | ||
| #expect(semantics == ["abc123", "def456"]) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.