|
| 1 | +//===--- PrintForDebugger.swift -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +public enum _DebuggerSupport { |
| 14 | + internal enum CollectionStatus { |
| 15 | + case NotACollection |
| 16 | + case CollectionOfElements |
| 17 | + case CollectionOfPairs |
| 18 | + case Element |
| 19 | + case Pair |
| 20 | + case ElementOfPair |
| 21 | + |
| 22 | + internal var isCollection: Bool { |
| 23 | + return self != .NotACollection |
| 24 | + } |
| 25 | + |
| 26 | + internal func getChildStatus(child: Mirror) -> CollectionStatus { |
| 27 | + let disposition = child.displayStyle ?? .struct |
| 28 | + |
| 29 | + if disposition == .collection { return .CollectionOfElements } |
| 30 | + if disposition == .dictionary { return .CollectionOfPairs } |
| 31 | + if disposition == .set { return .CollectionOfElements } |
| 32 | + |
| 33 | + if self == .CollectionOfElements { return .Element } |
| 34 | + if self == .CollectionOfPairs { return .Pair } |
| 35 | + if self == .Pair { return .ElementOfPair } |
| 36 | + |
| 37 | + return .NotACollection |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + internal static func asObjectIdentifier(_ value: Any) -> ObjectIdentifier? { |
| 42 | + if let ao = value as? AnyObject { |
| 43 | + return ObjectIdentifier(ao) |
| 44 | + } else { |
| 45 | + return nil |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + internal static func asNumericValue(_ value: Any) -> Int { |
| 50 | + if let ao = value as? AnyObject { |
| 51 | + return unsafeBitCast(ao, to: Int.self) |
| 52 | + } else { |
| 53 | + return 0 |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + internal static func asStringRepresentation( |
| 58 | + value: Any?, |
| 59 | + mirror: Mirror, |
| 60 | + count: Int |
| 61 | + ) -> String? { |
| 62 | + let ds = mirror.displayStyle ?? .`struct` |
| 63 | + switch ds { |
| 64 | + case .optional: |
| 65 | + if count > 0 { |
| 66 | + return "\(mirror.subjectType)" |
| 67 | + } |
| 68 | + else { |
| 69 | + if let x = value { |
| 70 | + return String(reflecting: x) |
| 71 | + } |
| 72 | + } |
| 73 | + case .collection: |
| 74 | + fallthrough |
| 75 | + case .dictionary: |
| 76 | + fallthrough |
| 77 | + case .set: |
| 78 | + fallthrough |
| 79 | + case .tuple: |
| 80 | + return "\(Int(mirror.children.count)) elements" |
| 81 | + case .`struct`: |
| 82 | + fallthrough |
| 83 | + case .`enum`: |
| 84 | + if let x = value { |
| 85 | + if let cdsc = (x as? CustomDebugStringConvertible) { |
| 86 | + return cdsc.debugDescription |
| 87 | + } |
| 88 | + if let csc = (x as? CustomStringConvertible) { |
| 89 | + return csc.description |
| 90 | + } |
| 91 | + } |
| 92 | + if count > 0 { |
| 93 | + return "\(mirror.subjectType)" |
| 94 | + } |
| 95 | + case .`class`: |
| 96 | + if let x = value { |
| 97 | + if let cdsc = (x as? CustomDebugStringConvertible) { |
| 98 | + return cdsc.debugDescription |
| 99 | + } |
| 100 | + if let csc = (x as? CustomStringConvertible) { |
| 101 | + return csc.description |
| 102 | + } |
| 103 | + // for a Class with no custom summary, mimic the Foundation default |
| 104 | + return "<\(x.dynamicType): 0x\(String(asNumericValue(x), radix: 16, uppercase: false))>" |
| 105 | + } else { |
| 106 | + // but if I can't provide a value, just use the type anyway |
| 107 | + return "\(mirror.subjectType)" |
| 108 | + } |
| 109 | + } |
| 110 | + if let x = value { |
| 111 | + return String(reflecting: x) |
| 112 | + } |
| 113 | + return nil |
| 114 | + } |
| 115 | + |
| 116 | + internal static func ivarCount(mirror: Mirror) -> Int { |
| 117 | + let count = Int(mirror.children.count) |
| 118 | + if let sc = mirror.superclassMirror { |
| 119 | + return ivarCount(mirror: sc) + count |
| 120 | + } else { |
| 121 | + return count |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + internal static func shouldExpand( |
| 127 | + mirror: Mirror, |
| 128 | + collectionStatus: CollectionStatus, |
| 129 | + isRoot: Bool |
| 130 | + ) -> Bool { |
| 131 | + if isRoot || collectionStatus.isCollection { return true } |
| 132 | + let count = Int(mirror.children.count) |
| 133 | + if count > 0 { return true } |
| 134 | + if let sc = mirror.superclassMirror { |
| 135 | + return ivarCount(mirror: sc) > 0 |
| 136 | + } else { |
| 137 | + return true |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + internal static func printForDebuggerImpl<StreamType : OutputStream>( |
| 142 | + value: Any?, |
| 143 | + mirror: Mirror, |
| 144 | + name: String?, |
| 145 | + indent: Int, |
| 146 | + maxDepth: Int, |
| 147 | + isRoot: Bool, |
| 148 | + parentCollectionStatus: CollectionStatus, |
| 149 | + refsAlreadySeen: inout Set<ObjectIdentifier>, |
| 150 | + maxItemCounter: inout Int, |
| 151 | + targetStream: inout StreamType |
| 152 | + ) { |
| 153 | + if maxItemCounter <= 0 { |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + if !shouldExpand(mirror: mirror, |
| 158 | + collectionStatus: parentCollectionStatus, |
| 159 | + isRoot: isRoot) { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + maxItemCounter -= 1 |
| 164 | + |
| 165 | + for _ in 0..<indent { |
| 166 | + print(" ", terminator: "", to: &targetStream) |
| 167 | + } |
| 168 | + |
| 169 | + // do not expand classes with no custom Mirror |
| 170 | + // yes, a type can lie and say it's a class when it's not since we only |
| 171 | + // check the displayStyle - but then the type would have a custom Mirror |
| 172 | + // anyway, so there's that... |
| 173 | + var willExpand = true |
| 174 | + if let ds = mirror.displayStyle { |
| 175 | + if ds == .`class` { |
| 176 | + if let x = value { |
| 177 | + if !(x is CustomReflectable) { |
| 178 | + willExpand = false |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + let count = Int(mirror.children.count) |
| 185 | + let bullet = isRoot && (count == 0 || willExpand == false) ? "" |
| 186 | + : count == 0 ? "- " |
| 187 | + : maxDepth <= 0 ? "▹ " : "▿ " |
| 188 | + print("\(bullet)", terminator: "", to: &targetStream) |
| 189 | + |
| 190 | + let collectionStatus = parentCollectionStatus.getChildStatus(child: mirror) |
| 191 | + |
| 192 | + if let nam = name { |
| 193 | + print("\(nam) : ", terminator: "", to: &targetStream) |
| 194 | + } |
| 195 | + |
| 196 | + if let str = asStringRepresentation(value: value, mirror: mirror, count: count) { |
| 197 | + print("\(str)", terminator: "", to: &targetStream) |
| 198 | + } |
| 199 | + |
| 200 | + if (maxDepth <= 0) || !willExpand { |
| 201 | + print("", to: &targetStream) |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + if let x = value { |
| 206 | + if let valueIdentifier = asObjectIdentifier(x) { |
| 207 | + if refsAlreadySeen.contains(valueIdentifier) { |
| 208 | + print(" { ... }", to: &targetStream) |
| 209 | + return |
| 210 | + } else { |
| 211 | + refsAlreadySeen.insert(valueIdentifier) |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + print("", to: &targetStream) |
| 217 | + |
| 218 | + var printedElements = 0 |
| 219 | + |
| 220 | + if let sc = mirror.superclassMirror { |
| 221 | + printForDebuggerImpl( |
| 222 | + value: nil, |
| 223 | + mirror: sc, |
| 224 | + name: "super", |
| 225 | + indent: indent + 2, |
| 226 | + maxDepth: maxDepth - 1, |
| 227 | + isRoot: false, |
| 228 | + parentCollectionStatus: .NotACollection, |
| 229 | + refsAlreadySeen: &refsAlreadySeen, |
| 230 | + maxItemCounter: &maxItemCounter, |
| 231 | + targetStream: &targetStream) |
| 232 | + } |
| 233 | + |
| 234 | + for (optionalName,child) in mirror.children { |
| 235 | + let childName = optionalName ?? "\(printedElements)" |
| 236 | + if maxItemCounter <= 0 { |
| 237 | + for _ in 0..<(indent+4) { |
| 238 | + print(" ", terminator: "", to: &targetStream) |
| 239 | + } |
| 240 | + let remainder = count - printedElements |
| 241 | + print("(\(remainder)", terminator: "", to: &targetStream) |
| 242 | + if printedElements > 0 { |
| 243 | + print(" more", terminator: "", to: &targetStream) |
| 244 | + } |
| 245 | + if remainder == 1 { |
| 246 | + print(" child)", to: &targetStream) |
| 247 | + } else { |
| 248 | + print(" children)", to: &targetStream) |
| 249 | + } |
| 250 | + return |
| 251 | + } |
| 252 | + |
| 253 | + printForDebuggerImpl( |
| 254 | + value: child, |
| 255 | + mirror: Mirror(reflecting: child), |
| 256 | + name: childName, |
| 257 | + indent: indent + 2, |
| 258 | + maxDepth: maxDepth - 1, |
| 259 | + isRoot: false, |
| 260 | + parentCollectionStatus: collectionStatus, |
| 261 | + refsAlreadySeen: &refsAlreadySeen, |
| 262 | + maxItemCounter: &maxItemCounter, |
| 263 | + targetStream: &targetStream) |
| 264 | + printedElements += 1 |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + public static func stringForPrintObject(_ value: Any) -> String { |
| 269 | + var maxItemCounter = Int.max |
| 270 | + var refs = Set<ObjectIdentifier>() |
| 271 | + var targetStream = "" |
| 272 | + |
| 273 | + printForDebuggerImpl( |
| 274 | + value: value, |
| 275 | + mirror: Mirror(reflecting: value), |
| 276 | + name: nil, |
| 277 | + indent: 0, |
| 278 | + maxDepth: maxItemCounter, |
| 279 | + isRoot: true, |
| 280 | + parentCollectionStatus: .NotACollection, |
| 281 | + refsAlreadySeen: &refs, |
| 282 | + maxItemCounter: &maxItemCounter, |
| 283 | + targetStream: &targetStream) |
| 284 | + |
| 285 | + return targetStream |
| 286 | + } |
| 287 | +} |
| 288 | + |
0 commit comments