|
| 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 | +#if compiler(>=5.11) |
| 12 | +import SwiftSyntax |
| 13 | +import SwiftSyntaxMacros |
| 14 | +#else |
| 15 | +public import SwiftSyntax |
| 16 | +public import SwiftSyntaxMacros |
| 17 | +#endif |
| 18 | +import SwiftDiagnostics |
| 19 | + |
| 20 | +extension MacroExpansionContext { |
| 21 | + /// Get the type of the lexical context enclosing the given node. |
| 22 | + /// |
| 23 | + /// - Parameters: |
| 24 | + /// - node: The node whose lexical context should be examined. |
| 25 | + /// |
| 26 | + /// - Returns: The type of the lexical context enclosing `node`, or `nil` if |
| 27 | + /// the lexical context cannot be represented as a type. |
| 28 | + /// |
| 29 | + /// If the lexical context includes functions, closures, or some other |
| 30 | + /// non-type scope, the value of this property is `nil`. |
| 31 | + /// |
| 32 | + /// If swift-syntax-600 or newer is available, `node` is ignored. The argument |
| 33 | + /// will be removed once the testing library's swift-syntax dependency is |
| 34 | + /// updated to swift-syntax-600 or later. |
| 35 | + func typeOfLexicalContext(containing node: some WithAttributesSyntax) -> TypeSyntax? { |
| 36 | +#if canImport(SwiftSyntax600) |
| 37 | + var typeNames = [String]() |
| 38 | + for lexicalContext in lexicalContext.reversed() { |
| 39 | + guard let decl = lexicalContext.asProtocol((any DeclGroupSyntax).self) else { |
| 40 | + return nil |
| 41 | + } |
| 42 | + typeNames.append(decl.type.trimmedDescription) |
| 43 | + } |
| 44 | + if typeNames.isEmpty { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + return "\(raw: typeNames.joined(separator: "."))" |
| 49 | +#else |
| 50 | + // Find the beginning of the first attribute on the declaration, including |
| 51 | + // those embedded in #if statements, to account for patterns like |
| 52 | + // `@MainActor @Test func` where there's a space ahead of @Test, but the |
| 53 | + // whole function is still at the top level. |
| 54 | + func firstAttribute(in attributes: AttributeListSyntax) -> AttributeSyntax? { |
| 55 | + attributes.lazy |
| 56 | + .compactMap { attribute in |
| 57 | + switch (attribute as AttributeListSyntax.Element?) { |
| 58 | + case let .ifConfigDecl(ifConfigDecl): |
| 59 | + ifConfigDecl.clauses.lazy |
| 60 | + .compactMap { clause in |
| 61 | + if case let .attributes(attributes) = clause.elements { |
| 62 | + return firstAttribute(in: attributes) |
| 63 | + } |
| 64 | + return nil |
| 65 | + }.first |
| 66 | + case let .attribute(attribute): |
| 67 | + attribute |
| 68 | + default: |
| 69 | + nil |
| 70 | + } |
| 71 | + }.first |
| 72 | + } |
| 73 | + let firstAttribute = firstAttribute(in: node.attributes)! |
| 74 | + |
| 75 | + // HACK: If the test function appears to be indented, assume it is nested in |
| 76 | + // a type. Use `Self` as the presumptive name of the type. |
| 77 | + // |
| 78 | + // This hack works around rdar://105470382. |
| 79 | + if let lastLeadingTrivia = firstAttribute.leadingTrivia.pieces.last, |
| 80 | + lastLeadingTrivia.isWhitespace && !lastLeadingTrivia.isNewline { |
| 81 | + return TypeSyntax(IdentifierTypeSyntax(name: .keyword(.Self))) |
| 82 | + } |
| 83 | + return nil |
| 84 | +#endif |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// MARK: - |
| 89 | + |
| 90 | +extension MacroExpansionContext { |
| 91 | + /// Create a unique name for a function that thunks another function. |
| 92 | + /// |
| 93 | + /// - Parameters: |
| 94 | + /// - functionDecl: The function to thunk. |
| 95 | + /// - prefix: A prefix to apply to the thunked name before returning. |
| 96 | + /// |
| 97 | + /// - Returns: A unique name to use for a thunk function that thunks |
| 98 | + /// `functionDecl`. |
| 99 | + func makeUniqueName(thunking functionDecl: FunctionDeclSyntax, withPrefix prefix: String = "") -> TokenSyntax { |
| 100 | + // Find all the tokens of the function declaration including argument |
| 101 | + // types, specifiers, etc. (but not any attributes nor the body of the |
| 102 | + // function.) Use them as the base name we pass to makeUniqueName(). This |
| 103 | + // ensures that we will end up with a unique identifier even if two |
| 104 | + // functions in the same scope have the exact same identifier. |
| 105 | + let identifierCharacters = functionDecl |
| 106 | + .with(\.attributes, []) |
| 107 | + .with(\.body, nil) |
| 108 | + .tokens(viewMode: .fixedUp) |
| 109 | + .map(\.textWithoutBackticks) |
| 110 | + .joined() |
| 111 | + |
| 112 | + // Strip out any characters in the function's signature that won't play well |
| 113 | + // in a generated symbol name. |
| 114 | + let identifier = String( |
| 115 | + identifierCharacters.map { character in |
| 116 | + if character.isLetter || character.isWholeNumber { |
| 117 | + return character |
| 118 | + } |
| 119 | + return "_" |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + // If there is a non-ASCII character in the identifier, we might be |
| 124 | + // stripping it out above because we are only looking for letters and |
| 125 | + // digits. If so, add in a hash of the identifier to improve entropy and |
| 126 | + // reduce the risk of a collision. |
| 127 | + // |
| 128 | + // For example, the following function names will produce identical unique |
| 129 | + // names without this mutation: |
| 130 | + // |
| 131 | + // @Test(arguments: [0]) func A(🙃: Int) {} |
| 132 | + // @Test(arguments: [0]) func A(🙂: Int) {} |
| 133 | + // |
| 134 | + // Note the check here is not the same as the one above: punctuation like |
| 135 | + // "(" should be replaced, but should not cause a hash to be emitted since |
| 136 | + // it does not contribute any entropy to the makeUniqueName() algorithm. |
| 137 | + // |
| 138 | + // The intent here is not to produce a cryptographically strong hash, but to |
| 139 | + // disambiguate between superficially similar function names. A collision |
| 140 | + // may still occur, but we only need it to be _unlikely_. CRC-32 is good |
| 141 | + // enough for our purposes. |
| 142 | + if !identifierCharacters.allSatisfy(\.isASCII) { |
| 143 | + let crcValue = crc32(identifierCharacters.utf8) |
| 144 | + let suffix = String(crcValue, radix: 16, uppercase: false) |
| 145 | + return makeUniqueName("\(prefix)\(identifier)_\(suffix)") |
| 146 | + } |
| 147 | + |
| 148 | + return makeUniqueName("\(prefix)\(identifier)") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// MARK: - |
| 153 | + |
| 154 | +extension MacroExpansionContext { |
| 155 | + /// Emit a diagnostic message. |
| 156 | + /// |
| 157 | + /// - Parameters: |
| 158 | + /// - message: The diagnostic message to emit. The `node` and `position` |
| 159 | + /// arguments to `Diagnostic.init()` are derived from the message's |
| 160 | + /// `syntax` property. |
| 161 | + func diagnose(_ message: DiagnosticMessage) { |
| 162 | + diagnose( |
| 163 | + Diagnostic( |
| 164 | + node: message.syntax, |
| 165 | + position: message.syntax.positionAfterSkippingLeadingTrivia, |
| 166 | + message: message, |
| 167 | + fixIts: message.fixIts |
| 168 | + ) |
| 169 | + ) |
| 170 | + } |
| 171 | + |
| 172 | + /// Emit a sequence of diagnostic messages. |
| 173 | + /// |
| 174 | + /// - Parameters: |
| 175 | + /// - messages: The diagnostic messages to emit. |
| 176 | + func diagnose(_ messages: some Sequence<DiagnosticMessage>) { |
| 177 | + for message in messages { |
| 178 | + diagnose(message) |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + /// Emit a diagnostic message for debugging purposes during development of the |
| 183 | + /// testing library. |
| 184 | + /// |
| 185 | + /// - Parameters: |
| 186 | + /// - message: The message to emit into the build log. |
| 187 | + func debug(_ message: some Any, node: some SyntaxProtocol) { |
| 188 | + diagnose(DiagnosticMessage(syntax: Syntax(node), message: String(describing: message), severity: .warning)) |
| 189 | + } |
| 190 | +} |
0 commit comments