|
| 1 | +// |
| 2 | +// Attribute+Debug.swift |
| 3 | +// OpenGraphShims |
| 4 | + |
| 5 | +#if canImport(Darwin) && DEBUG // Compiler crash for Darwin + release and non-Darwin build |
| 6 | + |
| 7 | +// Use 4 spaces instead of \t for bettern test case expect |
| 8 | +private let tab = " " |
| 9 | + |
| 10 | +extension Attribute: Swift.CustomDebugStringConvertible { |
| 11 | + public var debugDescription: String { |
| 12 | + _debugDescription(indent: 0) |
| 13 | + } |
| 14 | + |
| 15 | + func _debugDescription(indent: Int) -> String { |
| 16 | + let tabs = String(repeating: tab, count: indent) |
| 17 | + return #""" |
| 18 | + \#(tabs)Attribute<\#(Value.self)> { |
| 19 | + \#(identifier._debugDescription(indent: indent + 1)) |
| 20 | + \#(tabs)} |
| 21 | + """# |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +extension AnyAttribute: Swift.CustomDebugStringConvertible { |
| 26 | + public var debugDescription: String { |
| 27 | + _debugDescription(indent: 0) |
| 28 | + } |
| 29 | + |
| 30 | + func _debugDescription(indent: Int) -> String { |
| 31 | + let tabs = String(repeating: tab, count: indent) |
| 32 | + |
| 33 | + guard self != .nil else { |
| 34 | + return "\(tabs)AnyAttribute.nil" |
| 35 | + } |
| 36 | + |
| 37 | + var description = #""" |
| 38 | + \#(tabs)rawValue: \#(rawValue) |
| 39 | + \#(tabs)graph: \#(graph) |
| 40 | + """# |
| 41 | + |
| 42 | + if rawValue % 2 == 0 { // direct |
| 43 | + description.append("\n\(tabs)(direct attribute)") |
| 44 | + |
| 45 | + let valueType = valueType |
| 46 | + description.append("\n\(tabs)valueType: \(valueType)") |
| 47 | + |
| 48 | + var value: Any! |
| 49 | + func project1<T>(type: T.Type) { |
| 50 | + value = unsafeCast(to: type).value |
| 51 | + } |
| 52 | + _openExistential(valueType, do: project1) |
| 53 | + description.append("\n\(tabs)value: \(value!)") |
| 54 | + |
| 55 | + let bodyType = _bodyType |
| 56 | + description.append("\n\(tabs)bodyType: \(bodyType)") |
| 57 | + |
| 58 | + var bodyValue: Any! |
| 59 | + func project2<T>(type: T.Type) { |
| 60 | + bodyValue = _bodyPointer.assumingMemoryBound(to: type).pointee |
| 61 | + } |
| 62 | + _openExistential(bodyType, do: project2) |
| 63 | + |
| 64 | + let bodyValueDescription = _formatBodyValue(bodyValue!, indent: indent) |
| 65 | + description.append("\n\(tabs)bodyValue:\n\(bodyValueDescription)") |
| 66 | + |
| 67 | + } else { // indirect |
| 68 | + description.append("\n\(tabs)(indirect attribute)") |
| 69 | + description.append("\n\(tabs)source attribute:") |
| 70 | + description.append("\n\(source._debugDescription(indent: indent + 1))") |
| 71 | + } |
| 72 | + return description |
| 73 | + } |
| 74 | + |
| 75 | + private func _formatBodyValue(_ bodyValue: Any, indent: Int) -> String { |
| 76 | + let bodyValueString = String(describing: bodyValue) |
| 77 | + let nextTabs = String(repeating: tab, count: indent + 1) |
| 78 | + |
| 79 | + // Check if the body value contains attributes |
| 80 | + if bodyValueString.contains("Attribute<") { |
| 81 | + // Split lines and add proper indentation |
| 82 | + let lines = bodyValueString.components(separatedBy: .newlines) |
| 83 | + return lines.enumerated().map { index, line in |
| 84 | + return "\(nextTabs)\(line)" |
| 85 | + }.joined(separator: "\n") |
| 86 | + } |
| 87 | + |
| 88 | + return "\(nextTabs)\(bodyValueString)" |
| 89 | + } |
| 90 | +} |
| 91 | +#endif |
0 commit comments