-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathClang+SourceKitten.swift
295 lines (251 loc) · 10.2 KB
/
Clang+SourceKitten.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
#if !os(Linux)
#if SWIFT_PACKAGE
import Clang_C
#endif
import Foundation
import SWXMLHash
private var _interfaceUUIDMap = [String: String]()
private var _interfaceUUIDMapLock = NSLock()
/// Thread safe read from sourceKitUID map
private func uuidString(`for` sourceKitUID: String) -> String? {
_interfaceUUIDMapLock.lock()
defer { _interfaceUUIDMapLock.unlock() }
return _interfaceUUIDMap[sourceKitUID]
}
/// Thread safe write from sourceKitUID map
private func setUUIDString(uidString: String, `for` file: String) {
_interfaceUUIDMapLock.lock()
defer { _interfaceUUIDMapLock.unlock() }
_interfaceUUIDMap[file] = uidString
}
struct ClangIndex {
private let index = clang_createIndex(0, 1)
func open(file: String, args: [UnsafePointer<Int8>?]) -> CXTranslationUnit {
return clang_createTranslationUnitFromSourceFile(index, file, Int32(args.count), args, 0, nil)!
}
}
public struct ClangAvailability {
public let alwaysDeprecated: Bool
public let alwaysUnavailable: Bool
public let deprecationMessage: String?
public let unavailableMessage: String?
}
extension CXString: CustomStringConvertible {
func str() -> String? {
if let cString = clang_getCString(self) {
return String(validatingUTF8: cString)
}
return nil
}
public var description: String {
return str() ?? "<null>"
}
}
extension CXTranslationUnit {
func cursor() -> CXCursor {
return clang_getTranslationUnitCursor(self)
}
}
extension CXCursor {
func location() -> SourceLocation {
return SourceLocation(clangLocation: clang_getCursorLocation(self))
}
func extent() -> (start: SourceLocation, end: SourceLocation) {
let extent = clang_getCursorExtent(self)
let start = SourceLocation(clangLocation: clang_getRangeStart(extent))
let end = SourceLocation(clangLocation: clang_getRangeEnd(extent))
return (start, end)
}
func shouldDocument() -> Bool {
return clang_isDeclaration(kind) != 0 &&
kind != CXCursor_ParmDecl &&
kind != CXCursor_TemplateTypeParameter &&
clang_Location_isInSystemHeader(clang_getCursorLocation(self)) == 0
}
func declaration() -> String? {
let comment = parsedComment()
if comment.kind() == CXComment_Null {
return str()
}
let commentXML = clang_FullComment_getAsXML(comment).str() ?? ""
guard let rootXML = XMLHash.parse(commentXML).children.first else {
fatalError("couldn't parse XML")
}
guard let text = rootXML["Declaration"].element?.text,
!text.isEmpty else {
return nil
}
return text
.replacingOccurrences(of: "\n@end", with: "")
.replacingOccurrences(of: "@property(", with: "@property (")
}
func objCKind() -> ObjCDeclarationKind {
return ObjCDeclarationKind(kind)
}
func str() -> String? {
let cursorExtent = extent()
guard FileManager.default.fileExists(atPath: cursorExtent.start.file) else {
return nil
}
guard let content = try? String(contentsOfFile: cursorExtent.start.file, encoding: .utf8) else {
return nil
}
let contents = StringView(content)
return contents.substringWithSourceRange(start: cursorExtent.start, end: cursorExtent.end)
}
func name() -> String? {
let type = objCKind()
if type == .category, let usrNSString = usr() as NSString? {
let ext = (usrNSString.range(of: "c:objc(ext)").location == 0)
let regex = try! NSRegularExpression(pattern: "(\\w+)@(\\w+)", options: [])
let range = NSRange(location: 0, length: usrNSString.length)
let matches = regex.matches(in: usrNSString as String, options: [], range: range)
if matches.isEmpty {
return nil
} else {
let categoryOn = usrNSString.substring(with: matches[0].range(at: 1))
let categoryName = ext ? "" : usrNSString.substring(with: matches[0].range(at: 2))
return "\(categoryOn)(\(categoryName))"
}
}
let spelling = clang_getCursorSpelling(self).str()!
if type == .methodInstance {
return "-" + spelling
} else if type == .methodClass {
return "+" + spelling
}
return spelling
}
func usr() -> String? {
return clang_getCursorUSR(self).str()
}
func platformAvailability() -> ClangAvailability {
var alwaysDeprecated: Int32 = 0
var alwaysUnavailable: Int32 = 0
var deprecationString = CXString()
var unavailableString = CXString()
_ = clang_getCursorPlatformAvailability(self,
&alwaysDeprecated,
&deprecationString,
&alwaysUnavailable,
&unavailableString,
nil,
0)
return ClangAvailability(alwaysDeprecated: alwaysDeprecated != 0,
alwaysUnavailable: alwaysUnavailable != 0,
deprecationMessage: deprecationString.description,
unavailableMessage: unavailableString.description)
}
func visit(_ block: @escaping (CXCursor, CXCursor) -> CXChildVisitResult) {
_ = clang_visitChildrenWithBlock(self, block)
}
func parsedComment() -> CXComment {
return clang_Cursor_getParsedComment(self)
}
func compactMap<T>(_ block: @escaping (CXCursor) -> T?) -> [T] {
var ret = [T]()
visit { cursor, _ in
if let val = block(cursor) {
ret.append(val)
}
return CXChildVisit_Continue
}
return ret
}
func commentBody() -> String? {
let rawComment = clang_Cursor_getRawCommentText(self).str()
let replacements = [
"@param ": "- parameter: ",
"@return ": "- returns: ",
"@warning ": "- warning: ",
"@see ": "- see: ",
"@note ": "- note: ",
"@code": "```",
"@endcode": "```"
]
var commentBody = rawComment?.commentBody()
for (original, replacement) in replacements {
commentBody = commentBody?.replacingOccurrences(of: original, with: replacement)
}
// Replace "@c word" with "`word`"
commentBody = commentBody?.replacingOccurrences(of: "@c\\s+(\\S+)", with: "`$1`", options: .regularExpression)
return commentBody
}
func swiftDeclarationAndName(compilerArguments: [String]) -> (swiftDeclaration: String?, swiftName: String?) {
let file = location().file
let swiftUUID: String
if let uuid = uuidString(for: file) {
swiftUUID = uuid
} else {
swiftUUID = NSUUID().uuidString
setUUIDString(uidString: swiftUUID, for: file)
// Generate Swift interface, associating it with the UUID
do {
_ = try Request.interface(file: file, uuid: swiftUUID, arguments: compilerArguments).send()
} catch {
return (nil, nil)
}
}
guard let usr = usr(),
let findUSR = try? Request.findUSR(file: swiftUUID, usr: usr).send(),
let usrOffset = SwiftDocKey.getOffset(findUSR) else {
return (nil, nil)
}
guard let cursorInfo = try? Request.cursorInfo(file: swiftUUID, offset: usrOffset, arguments: compilerArguments).send() else {
return (nil, nil)
}
let swiftDeclaration = (cursorInfo[SwiftDocKey.annotatedDeclaration.rawValue] as? String)
.flatMap(XMLHash.parse)?.element?.recursiveText
let swiftName = cursorInfo[SwiftDocKey.name.rawValue] as? String
return (swiftDeclaration, swiftName)
}
}
extension CXComment {
func paramName() -> String? {
guard clang_Comment_getKind(self) == CXComment_ParamCommand else { return nil }
return clang_ParamCommandComment_getParamName(self).str()
}
func paragraph() -> CXComment {
return clang_BlockCommandComment_getParagraph(self)
}
func paragraphToString(kindString: String? = nil) -> [Text] {
if kind() == CXComment_VerbatimLine {
return [.verbatim(clang_VerbatimLineComment_getText(self).str()!)]
} else if kind() == CXComment_BlockCommand {
return (0..<count()).reduce([]) { returnValue, childIndex in
return returnValue + self[childIndex].paragraphToString()
}
}
guard kind() == CXComment_Paragraph else {
print("not a paragraph: \(kind())")
return []
}
let paragraphString = (0..<count()).reduce("") { paragraphString, childIndex in
let child = self[childIndex]
if let text = clang_TextComment_getText(child).str() {
return paragraphString + (paragraphString != "" ? "\n" : "") + text
} else if child.kind() == CXComment_InlineCommand {
// @autoreleasepool etc. get parsed as commands when not in code blocks
let inlineCommand = child.commandName().map { "@" + $0 }
return paragraphString + (inlineCommand ?? "")
}
// Inline child content like `CXComment_HTMLStartTag` can be ignored b/c subsequent children will contain the html.
return paragraphString
}
return [.para(paragraphString.removingCommonLeadingWhitespaceFromLines(), kindString)]
}
func kind() -> CXCommentKind {
return clang_Comment_getKind(self)
}
func commandName() -> String? {
return clang_BlockCommandComment_getCommandName(self).str() ??
clang_InlineCommandComment_getCommandName(self).str()
}
func count() -> UInt32 {
return clang_Comment_getNumChildren(self)
}
subscript(idx: UInt32) -> CXComment {
return clang_Comment_getChild(self, idx)
}
}
#endif