Skip to content

[Debug] Fix regex type names in DebugDescriptionMacro #71379

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
12 changes: 8 additions & 4 deletions lib/Macros/Sources/SwiftMacros/DebugDescriptionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ extension DebugDescriptionMacro: MemberAttributeMacro {
return []
}

// Warning: To use a backslash escape in `typeIdentifier`, it needs to be double escaped. This is because
// the string is serialized to a String literal (an argument to `@_DebugDescriptionProperty`), which
// effectively "consumes" one level of escaping. To avoid mistakes, dots are matched with `[.]` instead
Copy link
Contributor

Choose a reason for hiding this comment

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

So a . inside of a character enumeration [] does not match any character?

Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently.

echo 'Hello. World.' | sed -e 's/[.]/!/g'
Hello! World!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, character classes match their contents literally (generally speaking, there could be some exceptions I am unaware of). This works for other metacharacters too, ex [+], [$], etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This kind of usage is used as an alternative to backslash escaping.

// of the more conventional `\.`.
var typeIdentifier: String
if let typeParameters = declaration.asProtocol(WithGenericParametersSyntax.self)?.genericParameterClause?.parameters, typeParameters.count > 0 {
let typePatterns = Array(repeating: ".+", count: typeParameters.count).joined(separator: ",")
// A regex matching that matches the generic type.
typeIdentifier = "^\(moduleName)\\.\(typeName)<\(typePatterns)>"
typeIdentifier = "^\(moduleName)[.]\(typeName)<\(typePatterns)>"
} else if declaration.is(ExtensionDeclSyntax.self) {
// When attached to an extension, the type may or may not be a generic type.
// This regular expression handles both cases.
typeIdentifier = "^\(moduleName)\\.\(typeName)(<.+>)?$"
typeIdentifier = "^\(moduleName)[.]\(typeName)(<.+>)?$"
} else {
typeIdentifier = "\(moduleName).\(typeName)"
}
Expand Down Expand Up @@ -249,7 +253,7 @@ fileprivate let ENCODING_VERSION: UInt = 1

/// Construct an LLDB type summary record.
///
/// The record is serializeed as a tuple of `UInt8` bytes.
/// The record is serialized as a tuple of `UInt8` bytes.
///
/// The record contains the following:
/// * Version number of the record format
Expand Down Expand Up @@ -352,7 +356,7 @@ extension DeclGroupSyntax {
case .actorDecl, .classDecl, .enumDecl, .structDecl:
return self.asProtocol(NamedDeclSyntax.self)?.name.text
case .extensionDecl:
return self.as(ExtensionDeclSyntax.self)?.extendedType.description
return self.as(ExtensionDeclSyntax.self)?.extendedType.trimmedDescription
default:
// New types of decls are not presumed to be valid.
return nil
Expand Down
26 changes: 26 additions & 0 deletions test/Macros/DebugDescription/extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// REQUIRES: swift_swift_parser

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -enable-experimental-feature SymbolLinkageMarkers -plugin-path %swift-plugin-dir -dump-macro-expansions > %t/expansions-dump.txt 2>&1
// RUN: %FileCheck %s < %t/expansions-dump.txt

struct MyStruct {}

@_DebugDescription
extension MyStruct {
var debugDescription: String { "thirty" }
}
// CHECK: #if os(Linux)
// CHECK: @_section(".lldbsummaries")
// CHECK: #elseif os(Windows)
// CHECK: @_section(".lldbsummaries")
// CHECK: #else
// CHECK: @_section("__DATA_CONST,__lldbsummaries")
// CHECK: #endif
// CHECK: @_used
// CHECK: static let _lldb_summary = (
// CHECK: /* version */ 1 as UInt8,
// CHECK: /* record size */ 34 as UInt8,
// CHECK: /* "^main[.]MyStruct(<.+>)?$" */ 25 as UInt8, 94 as UInt8, 109 as UInt8, 97 as UInt8, 105 as UInt8, 110 as UInt8, 91 as UInt8, 46 as UInt8, 93 as UInt8, 77 as UInt8, 121 as UInt8, 83 as UInt8, 116 as UInt8, 114 as UInt8, 117 as UInt8, 99 as UInt8, 116 as UInt8, 40 as UInt8, 60 as UInt8, 46 as UInt8, 43 as UInt8, 62 as UInt8, 41 as UInt8, 63 as UInt8, 36 as UInt8, 0 as UInt8,
// CHECK: /* "thirty" */ 7 as UInt8, 116 as UInt8, 104 as UInt8, 105 as UInt8, 114 as UInt8, 116 as UInt8, 121 as UInt8, 0 as UInt8
// CHECK: )