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