Skip to content

[Macros] Copy attrs/modifiers on MacroExpansionDecl to expanded decls #1666

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 3 commits into from
May 16, 2023
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
3 changes: 2 additions & 1 deletion CodeGeneration/Sources/SyntaxSupport/CommonNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ public let COMMON_NODES: [Node] = [
description: "In case the source code is missing a declaration, this node stands in place of the missing declaration.",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down
65 changes: 43 additions & 22 deletions CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "accessor",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes"
],
parserFunction: "parseAccessorDecl",
children: [
Expand Down Expand Up @@ -151,7 +151,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -235,7 +236,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -322,7 +324,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -469,7 +472,8 @@ public let DECL_NODES: [Node] = [
""",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -577,7 +581,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "parameter",
kind: "Syntax",
traits: [
"WithTrailingComma"
"WithTrailingComma",
"WithModifiers",
],
parserFunction: "parseEnumCaseParameter",
children: [
Expand Down Expand Up @@ -631,7 +636,8 @@ public let DECL_NODES: [Node] = [
description: "A `case` declaration of a Swift `enum`. It can have 1 or more `EnumCaseElement`s inside, each declaring a different case of the enum.",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -714,7 +720,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -782,7 +789,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"DeclGroup",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -830,7 +838,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -894,7 +903,8 @@ public let DECL_NODES: [Node] = [
kind: "Syntax",
traits: [
"WithTrailingComma",
"Attributed",
"WithAttributes",
"WithModifiers",
],
parserFunction: "parseFunctionParameter",
children: [
Expand Down Expand Up @@ -1062,7 +1072,8 @@ public let DECL_NODES: [Node] = [
""",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1157,7 +1168,8 @@ public let DECL_NODES: [Node] = [
""",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1220,7 +1232,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1275,7 +1288,9 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "macro expansion",
kind: "Decl",
traits: [
"FreestandingMacroExpansion"
"FreestandingMacroExpansion",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1426,7 +1441,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1702,7 +1718,8 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1813,7 +1830,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -1980,7 +1998,8 @@ public let DECL_NODES: [Node] = [
traits: [
"DeclGroup",
"IdentifiedDecl",
"Attributed",
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -2041,7 +2060,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "subscript",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down Expand Up @@ -2142,7 +2162,7 @@ public let DECL_NODES: [Node] = [
kind: "Decl",
traits: [
"IdentifiedDecl",
"Attributed",
"WithAttributes",
],
children: [
Child(
Expand Down Expand Up @@ -2189,7 +2209,8 @@ public let DECL_NODES: [Node] = [
nameForDiagnostics: "variable",
kind: "Decl",
traits: [
"Attributed"
"WithAttributes",
"WithModifiers",
],
children: [
Child(
Expand Down
6 changes: 4 additions & 2 deletions CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@ public let EXPR_NODES: [Node] = [
nameForDiagnostics: "parameter",
kind: "Syntax",
traits: [
"WithTrailingComma"
"WithTrailingComma",
"WithAttributes",
"WithModifiers",
],
parserFunction: "parseClosureParameter",
children: [
Expand Down Expand Up @@ -515,7 +517,7 @@ public let EXPR_NODES: [Node] = [
nameForDiagnostics: "closure signature",
kind: "Syntax",
traits: [
"Attributed"
"WithAttributes"
],
children: [
Child(
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/GenericNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public let GENERIC_NODES: [Node] = [
kind: "Syntax",
traits: [
"WithTrailingComma",
"Attributed",
"WithAttributes",
],
children: [
Child(
Expand Down
18 changes: 12 additions & 6 deletions CodeGeneration/Sources/SyntaxSupport/Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ public class Trait {
}

public let TRAITS: [Trait] = [
Trait(
traitName: "Attributed",
children: [
Child(name: "Attributes", kind: .node(kind: "AttributeList"), isOptional: true)
]
),
Trait(
traitName: "Braced",
children: [
Expand Down Expand Up @@ -80,12 +74,24 @@ public let TRAITS: [Trait] = [
Child(name: "RightParen", kind: .token(choices: [.token(tokenKind: "RightParenToken")])),
]
),
Trait(
traitName: "WithAttributes",
children: [
Child(name: "Attributes", kind: .node(kind: "AttributeList"), isOptional: true)
]
),
Trait(
traitName: "WithCodeBlock",
children: [
Child(name: "Body", kind: .node(kind: "CodeBlock"))
]
),
Trait(
traitName: "WithModifiers",
children: [
Child(name: "Modifiers", kind: .node(kind: "ModifierList"), isOptional: true)
]
),
Trait(
traitName: "WithStatements",
children: [
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public let TYPE_NODES: [Node] = [
nameForDiagnostics: "type",
kind: "Type",
traits: [
"Attributed"
"WithAttributes"
],
children: [
Child(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
DeclSyntax(
"""
public protocol SyntaxCollection: SyntaxProtocol, Sequence where Element: SyntaxProtocol {
/// Creates a new collection with the elements.
init(_ children: [Element])
/// The number of elements, `present` or `missing`, in this collection.
var count: Int { get }
}
Expand Down
12 changes: 7 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,19 @@ let package = Package(
exclude: ["CMakeLists.txt"]
),

.testTarget(
name: "SwiftSyntaxMacrosTest",
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntaxBuilder", "SwiftSyntaxMacros", "SwiftSyntaxMacrosTestSupport"]
),

// MARK: SwiftSyntaxMacroExpansion

.target(
name: "SwiftSyntaxMacroExpansion",
dependencies: ["SwiftSyntax", "SwiftSyntaxMacros"],
exclude: ["CMakeLists.txt"]
),

.testTarget(
name: "SwiftSyntaxMacrosTest",
dependencies: ["_SwiftSyntaxTestSupport", "SwiftDiagnostics", "SwiftOperators", "SwiftParser", "SwiftSyntaxBuilder", "SwiftSyntaxMacros", "SwiftSyntaxMacrosTestSupport"]
),

// MARK: SwiftSyntaxMacrosTestSupport

.target(
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftParser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_swift_host_library(SwiftParser
Specifiers.swift
Statements.swift
StringLiterals.swift
StringLiteralRepresentedLiteralValue.swift
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is now unrelated to this PR. I wanted to use this feature (representedLiteralValue ) in swift repo test cases, but ended up with not using it because the compiler doesn't support @freestanding(declaration, names: arbitrary) macros.

But this was missing anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing and fixing 🙏🏽

SyntaxUtils.swift
TokenConsumer.swift
TokenPrecedence.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,15 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.

### Traits

- <doc:SwiftSyntax/Attributed>
- <doc:SwiftSyntax/Braced>
- <doc:SwiftSyntax/DeclGroup>
- <doc:SwiftSyntax/EffectSpecifiers>
- <doc:SwiftSyntax/FreestandingMacroExpansion>
- <doc:SwiftSyntax/IdentifiedDecl>
- <doc:SwiftSyntax/Parenthesized>
- <doc:SwiftSyntax/WithAttributes>
- <doc:SwiftSyntax/WithCodeBlock>
- <doc:SwiftSyntax/WithModifiers>
- <doc:SwiftSyntax/WithStatements>
- <doc:SwiftSyntax/WithTrailingComma>

Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftSyntax/SwiftSyntaxCompatibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ public typealias AccessPathSyntax = ImportPathSyntax

@available(*, deprecated, renamed: "ImportPathComponentSyntax")
public typealias AccessPathComponentSyntax = ImportPathComponentSyntax

@available(*, deprecated, renamed: "WithAttributesSyntax")
public typealias AttributedSyntax = WithAttributesSyntax
3 changes: 3 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxCollections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
//===----------------------------------------------------------------------===//

public protocol SyntaxCollection: SyntaxProtocol, Sequence where Element: SyntaxProtocol {
/// Creates a new collection with the elements.
init(_ children: [Element])

/// The number of elements, `present` or `missing`, in this collection.
var count: Int {
get
Expand Down
Loading