Skip to content

[5.9] Fix debugInitCall #1625

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ let childNameForKeyPathFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
"""
/// If the keyPath is one from a layout structure, return the property name
/// of it.
internal func childName(_ keyPath: AnyKeyPath) -> String?
@_spi(RawSyntax)
public func childName(_ keyPath: AnyKeyPath) -> String?
"""
) {
try! SwitchExprSyntax("switch keyPath") {
Expand Down
3 changes: 2 additions & 1 deletion Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

/// If the keyPath is one from a layout structure, return the property name
/// of it.
internal func childName(_ keyPath: AnyKeyPath) -> String? {
@_spi(RawSyntax)
public func childName(_ keyPath: AnyKeyPath) -> String? {
switch keyPath {
case \AccessPathComponentSyntax.unexpectedBeforeName:
return "unexpectedBeforeName"
Expand Down
45 changes: 31 additions & 14 deletions Sources/_SwiftSyntaxTestSupport/SyntaxProtocol+Initializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,36 @@ extension SyntaxProtocol {
/// (or at least an expression that's very close to constructing this node, the addition of a few manual upcast by hand is still needed).
/// The intended use case for this is to print a syntax tree and create a substructure assertion from the generated expression.
/// When `includeTrivia` is set to `false`, the token's leading and trailing trivia will not be included in the generated expression.
///
/// - Warning: This is only designed for use in the debugger. Do not call it outside of the debugger.
@available(*, deprecated, message: "For use in debugger only")
public func debugInitCall(includeTrivia: Bool = true) -> String {
return self.debugInitCallExpr(includeTrivia: includeTrivia).formatted(using: InitializerExprFormat()).description
}

private func debugInitCallExpr(includeTrivia: Bool) -> ExprSyntax {
let mirror = Mirror(reflecting: self)
if self.kind.isSyntaxCollection {
if type(of: self) != self.syntaxNodeType {
let nestedInitCall = Syntax(self).asProtocol(SyntaxProtocol.self).debugInitCallExpr(includeTrivia: includeTrivia)
var typeName = "\(type(of: self))"
// If the type is `SyntaxChildChoices`, it is a nested type that needs to be qualified.
if self is SyntaxChildChoices, let parent = parent {
typeName = "\(parent.syntaxNodeType).\(typeName)"
}
return ExprSyntax(
FunctionCallExprSyntax(callee: ExprSyntax("\(raw: typeName)")) {
TupleExprElementSyntax(expression: nestedInitCall)
}
)
}

if case .collection(let collectionElementType) = self.syntaxNodeType.structure {
let typeName = String(describing: type(of: self))
return ExprSyntax(
FunctionCallExprSyntax(callee: IdentifierExprSyntax(identifier: .identifier(typeName))) {
TupleExprElementSyntax(
expression: ArrayExprSyntax {
for child in mirror.children {
let value = child.value as! SyntaxProtocol?
ArrayElementSyntax(expression: value?.debugInitCallExpr(includeTrivia: includeTrivia) ?? ExprSyntax(NilLiteralExprSyntax()))
for child in self.children(viewMode: .all) {
ArrayElementSyntax(expression: child.as(collectionElementType)!.debugInitCallExpr(includeTrivia: includeTrivia))
}
}
)
Expand All @@ -134,12 +149,12 @@ extension SyntaxProtocol {
let tokenKind = token.tokenKind
let tokenInitializerName: String
let tokenKindArgument: ExprSyntax?
if tokenKind.isLexerClassifiedKeyword || tokenKind == .eof {
tokenInitializerName = String(describing: tokenKind)
tokenKindArgument = nil
} else if case .keyword(let keyword) = tokenKind {
if case .keyword(let keyword) = tokenKind {
tokenInitializerName = "keyword"
tokenKindArgument = ExprSyntax(".\(raw: keyword)")
} else if tokenKind.isLexerClassifiedKeyword || tokenKind == .eof {
tokenInitializerName = String(describing: tokenKind)
tokenKindArgument = nil
} else if tokenKind.decomposeToRaw().rawKind.defaultText != nil {
tokenInitializerName = "\(String(describing: tokenKind))Token"
tokenKindArgument = nil
Expand Down Expand Up @@ -176,15 +191,15 @@ extension SyntaxProtocol {
}
}
)
} else {
} else if case .layout(let layout) = self.syntaxNodeType.structure {
let typeName = String(describing: type(of: self))
return ExprSyntax(
FunctionCallExprSyntax(callee: IdentifierExprSyntax(identifier: .identifier(typeName))) {
for child in mirror.children {
let label = child.label!
let value = child.value as! SyntaxProtocol?
for keyPath in layout {
let label = childName(keyPath) ?? ""
let value = self[keyPath: keyPath as! PartialKeyPath<Self>] as! SyntaxProtocol?
let isUnexpected = label.hasPrefix("unexpected")
if !isUnexpected || value != nil {
if value != nil {
TupleExprElementSyntax(
label: isUnexpected ? nil : .identifier(label),
colon: isUnexpected ? nil : .colonToken(),
Expand All @@ -194,6 +209,8 @@ extension SyntaxProtocol {
}
}
)
} else {
fatalError()
}
}
}
62 changes: 57 additions & 5 deletions Tests/SwiftSyntaxTest/DebugDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import XCTest
import SwiftSyntax
import _SwiftSyntaxTestSupport
import SwiftParser

private extension String {
// This implementation is really slow; to use it outside a test it should be optimized.
Expand Down Expand Up @@ -158,7 +159,8 @@ public class DebugDescriptionTests: XCTestCase {

testCases.forEach { keyAndValue in
let (key:line, value:testCase) = keyAndValue
let actualDumped = dumped(testCase.syntax)
var actualDumped = ""
dump(testCase.syntax, to: &actualDumped)
assertStringsEqualWithDiff(
actualDumped.trimmingTrailingWhitespace(),
testCase.expectedDumped.trimmingTrailingWhitespace(),
Expand All @@ -167,9 +169,59 @@ public class DebugDescriptionTests: XCTestCase {
}
}

public func dumped(_ syntax: Any) -> String {
var result = ""
dump(syntax, to: &result)
return result
#if DEBUG
// debugInitCall is only available in debug builds, so we can only test it in those.
@available(*, deprecated, message: "Purposefully tests debugInitCall, which is marked deprecated for debugger use only")
func testDebugInitCall() {
let sourceFile: SourceFileSyntax = """
test(1, 2)
"""

assertStringsEqualWithDiff(
sourceFile.debugInitCall(includeTrivia: true).trimmingTrailingWhitespace(),
"""
SourceFileSyntax(
statements: CodeBlockItemListSyntax([
CodeBlockItemSyntax(item: CodeBlockItemSyntax.Item(FunctionCallExprSyntax(
calledExpression: ExprSyntax(IdentifierExprSyntax(identifier: .identifier("test"))),
leftParen: .leftParenToken(),
argumentList: TupleExprElementListSyntax([
TupleExprElementSyntax(
expression: ExprSyntax(IntegerLiteralExprSyntax(digits: .integerLiteral("1"))),
trailingComma: .commaToken(trailingTrivia: .space)
),
TupleExprElementSyntax(expression: ExprSyntax(IntegerLiteralExprSyntax(digits: .integerLiteral("2"))))
]),
rightParen: .rightParenToken()
)))
]),
eofToken: .eof()
)
"""
)

assertStringsEqualWithDiff(
sourceFile.debugInitCall(includeTrivia: false).trimmingTrailingWhitespace(),
"""
SourceFileSyntax(
statements: CodeBlockItemListSyntax([
CodeBlockItemSyntax(item: CodeBlockItemSyntax.Item(FunctionCallExprSyntax(
calledExpression: ExprSyntax(IdentifierExprSyntax(identifier: .identifier("test"))),
leftParen: .leftParenToken(),
argumentList: TupleExprElementListSyntax([
TupleExprElementSyntax(
expression: ExprSyntax(IntegerLiteralExprSyntax(digits: .integerLiteral("1"))),
trailingComma: .commaToken()
),
TupleExprElementSyntax(expression: ExprSyntax(IntegerLiteralExprSyntax(digits: .integerLiteral("2"))))
]),
rightParen: .rightParenToken()
)))
]),
eofToken: .eof()
)
"""
)
}
#endif
}