forked from swiftlang/sourcekit-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClangLanguageService.swift
669 lines (577 loc) · 23.8 KB
/
ClangLanguageService.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//
import Foundation
import LSPLogging
import LanguageServerProtocol
import LanguageServerProtocolJSONRPC
import SKCore
import SKSupport
import struct TSCBasic.AbsolutePath
#if os(Windows)
import WinSDK
#endif
extension NSLock {
/// NOTE: Keep in sync with SwiftPM's 'Sources/Basics/NSLock+Extensions.swift'
fileprivate func withLock<T>(_ body: () throws -> T) rethrows -> T {
lock()
defer { unlock() }
return try body()
}
}
/// Gathers data from clangd's stderr pipe. When it has accumulated a full line, writes the the line to the logger.
fileprivate actor ClangdStderrLogForwarder {
/// Queue on which all data from `clangd`’s stderr will be forwarded to `stderr`. This allows us to have a
/// nonisolated `handle` function but ensure that data gets processed in order.
private let queue = AsyncQueue<Serial>()
private var buffer = Data()
private func handleImpl(_ newData: Data) {
self.buffer += newData
while let newlineIndex = self.buffer.firstIndex(of: UInt8(ascii: "\n")) {
// Output a separate log message for every line in clangd's stderr.
// The reason why we don't output multiple lines in a single log message is that
// a) os_log truncates log messages at about 1000 bytes. The assumption is that a single line is usually less
// than 1000 bytes long but if we merge multiple lines into one message, we might easily exceed this limit.
// b) It might be confusing why sometimes a single log message contains one line while sometimes it contains
// multiple.
let logger = Logger(subsystem: LoggingScope.subsystem, category: "clangd-stderr")
logger.info("\(String(data: self.buffer[...newlineIndex], encoding: .utf8) ?? "<invalid UTF-8>")")
buffer = buffer[buffer.index(after: newlineIndex)...]
}
}
nonisolated func handle(_ newData: Data) {
queue.async {
await self.handleImpl(newData)
}
}
}
/// A thin wrapper over a connection to a clangd server providing build setting handling.
///
/// In addition, it also intercepts notifications and replies from clangd in order to do things
/// like withholding diagnostics when fallback build settings are being used.
///
/// ``ClangLanguageServerShim`` conforms to ``MessageHandler`` to receive
/// requests and notifications **from** clangd, not from the editor, and it will
/// forward these requests and notifications to the editor.
actor ClangLanguageService: LanguageService, MessageHandler {
/// The queue on which all messages that originate from clangd are handled.
///
/// These are requests and notifications sent *from* clangd, not replies from
/// clangd.
///
/// Since we are blindly forwarding requests from clangd to the editor, we
/// cannot allow concurrent requests. This should be fine since the number of
/// requests and notifications sent from clangd to the client is quite small.
public let clangdMessageHandlingQueue = AsyncQueue<Serial>()
/// The ``SourceKitLSPServer`` instance that created this `ClangLanguageService`.
///
/// Used to send requests and notifications to the editor.
private weak var sourceKitLSPServer: SourceKitLSPServer?
/// The connection to the clangd LSP. `nil` until `startClangdProcesss` has been called.
var clangd: Connection!
/// Capabilities of the clangd LSP, if received.
var capabilities: ServerCapabilities? = nil
/// Path to the clang binary.
let clangPath: AbsolutePath?
/// Path to the `clangd` binary.
let clangdPath: AbsolutePath
let clangdOptions: [String]
/// The current state of the `clangd` language server.
/// Changing the property automatically notified the state change handlers.
private var state: LanguageServerState {
didSet {
for handler in stateChangeHandlers {
handler(oldValue, state)
}
}
}
private var stateChangeHandlers: [(_ oldState: LanguageServerState, _ newState: LanguageServerState) -> Void] = []
/// The date at which `clangd` was last restarted.
/// Used to delay restarting in case of a crash loop.
private var lastClangdRestart: Date?
/// Whether or not a restart of `clangd` has been scheduled.
/// Used to make sure we are not restarting `clangd` twice.
private var clangRestartScheduled = false
/// The `InitializeRequest` with which `clangd` was originally initialized.
/// Stored so we can replay the initialization when clangd crashes.
private var initializeRequest: InitializeRequest?
/// The workspace this `ClangLanguageServer` was opened for.
///
/// `clangd` doesn't have support for multi-root workspaces, so we need to start a separate `clangd` instance for every workspace root.
private let workspace: WeakWorkspace
/// The documents that have been opened and which language they have been
/// opened with.
private var openDocuments: [DocumentURI: Language] = [:]
/// While `clangd` is running, its PID.
#if os(Windows)
private var hClangd: HANDLE = INVALID_HANDLE_VALUE
#else
private var clangdPid: Int32?
#endif
/// Creates a language server for the given client referencing the clang binary specified in `toolchain`.
/// Returns `nil` if `clangd` can't be found.
public init?(
sourceKitLSPServer: SourceKitLSPServer,
toolchain: Toolchain,
options: SourceKitLSPServer.Options,
workspace: Workspace
) async throws {
guard let clangdPath = toolchain.clangd else {
return nil
}
self.clangPath = toolchain.clang
self.clangdPath = clangdPath
self.clangdOptions = options.clangdOptions
self.workspace = WeakWorkspace(workspace)
self.state = .connected
self.sourceKitLSPServer = sourceKitLSPServer
try startClangdProcess()
}
private func buildSettings(for document: DocumentURI) async -> ClangBuildSettings? {
guard let workspace = workspace.value, let language = openDocuments[document] else {
return nil
}
guard
let settings = await workspace.buildSystemManager.buildSettingsInferredFromMainFile(
for: document,
language: language
)
else {
return nil
}
return ClangBuildSettings(settings, clangPath: clangdPath)
}
nonisolated func canHandle(workspace: Workspace) -> Bool {
// We launch different clangd instance for each workspace because clangd doesn't have multi-root workspace support.
return workspace === self.workspace.value
}
func addStateChangeHandler(handler: @escaping (LanguageServerState, LanguageServerState) -> Void) {
self.stateChangeHandlers.append(handler)
}
/// Called after the `clangd` process exits.
///
/// Restarts `clangd` if it has crashed.
///
/// - Parameter terminationStatus: The exit code of `clangd`.
private func handleClangdTermination(terminationStatus: Int32) {
#if os(Windows)
self.hClangd = INVALID_HANDLE_VALUE
#else
self.clangdPid = nil
#endif
if terminationStatus != 0 {
self.state = .connectionInterrupted
self.restartClangd()
}
}
/// Start the `clangd` process, either on creation of the `ClangLanguageService` or after `clangd` has crashed.
private func startClangdProcess() throws {
// Since we are starting a new clangd process, reset the list of open document
openDocuments = [:]
let usToClangd: Pipe = Pipe()
let clangdToUs: Pipe = Pipe()
let connectionToClangd = JSONRPCConnection(
name: "clangd",
protocol: MessageRegistry.lspProtocol,
inFD: clangdToUs.fileHandleForReading,
outFD: usToClangd.fileHandleForWriting
)
self.clangd = connectionToClangd
connectionToClangd.start(receiveHandler: self) {
// FIXME: keep the pipes alive until we close the connection. This
// should be fixed systemically.
withExtendedLifetime((usToClangd, clangdToUs)) {}
}
let process = Foundation.Process()
process.executableURL = clangdPath.asURL
process.arguments =
[
"-compile_args_from=lsp", // Provide compiler args programmatically.
"-background-index=false", // Disable clangd indexing, we use the build
"-index=false", // system index store instead.
] + clangdOptions
process.standardOutput = clangdToUs
process.standardInput = usToClangd
let logForwarder = ClangdStderrLogForwarder()
let stderrHandler = Pipe()
stderrHandler.fileHandleForReading.readabilityHandler = { fileHandle in
let newData = fileHandle.availableData
if newData.count == 0 {
stderrHandler.fileHandleForReading.readabilityHandler = nil
} else {
logForwarder.handle(newData)
}
}
process.standardError = stderrHandler
process.terminationHandler = { [weak self] process in
logger.log(
level: process.terminationReason == .exit ? .default : .error,
"clangd exited: \(String(reflecting: process.terminationReason)) \(process.terminationStatus)"
)
connectionToClangd.close()
guard let self = self else { return }
Task {
await self.handleClangdTermination(terminationStatus: process.terminationStatus)
}
}
try process.run()
#if os(Windows)
self.hClangd = process.processHandle
#else
self.clangdPid = process.processIdentifier
#endif
}
/// Restart `clangd` after it has crashed.
/// Delays restarting of `clangd` in case there is a crash loop.
private func restartClangd() {
precondition(self.state == .connectionInterrupted)
precondition(self.clangRestartScheduled == false)
self.clangRestartScheduled = true
guard let initializeRequest = self.initializeRequest else {
logger.error("clangd crashed before it was sent an InitializeRequest.")
return
}
let restartDelay: Int
if let lastClangdRestart = self.lastClangdRestart, Date().timeIntervalSince(lastClangdRestart) < 30 {
logger.log("clangd has already been restarted in the last 30 seconds. Delaying another restart by 10 seconds.")
restartDelay = 10
} else {
restartDelay = 0
}
self.lastClangdRestart = Date()
Task {
try await Task.sleep(nanoseconds: UInt64(restartDelay) * 1_000_000_000)
self.clangRestartScheduled = false
do {
try self.startClangdProcess()
// FIXME: We assume that clangd will return the same capabilities after restarting.
// Theoretically they could have changed and we would need to inform SourceKitLSPServer about them.
// But since SourceKitLSPServer more or less ignores them right now anyway, this should be fine for now.
_ = try await self.initialize(initializeRequest)
self.clientInitialized(InitializedNotification())
if let sourceKitLSPServer {
await sourceKitLSPServer.reopenDocuments(for: self)
} else {
logger.fault("Cannot reopen documents because SourceKitLSPServer is no longer alive")
}
self.state = .connected
} catch {
logger.fault("Failed to restart clangd after a crash.")
}
}
}
/// Handler for notifications received **from** clangd, ie. **clangd** is
/// sending a notification that's intended for the editor.
///
/// We should either handle it ourselves or forward it to the editor.
nonisolated func handle(_ params: some NotificationType) {
logger.info(
"""
Received notification from clangd:
\(params.forLogging)
"""
)
clangdMessageHandlingQueue.async {
switch params {
case let publishDiags as PublishDiagnosticsNotification:
await self.publishDiagnostics(publishDiags)
default:
// We don't know how to handle any other notifications and ignore them.
logger.error("Ignoring unknown notification \(type(of: params))")
break
}
}
}
/// Handler for requests received **from** clangd, ie. **clangd** is
/// sending a notification that's intended for the editor.
///
/// We should either handle it ourselves or forward it to the client.
nonisolated func handle<R: RequestType>(
_ params: R,
id: RequestID,
reply: @Sendable @escaping (LSPResult<R.Response>) -> Void
) {
logger.info(
"""
Received request from clangd:
\(params.forLogging)
"""
)
clangdMessageHandlingQueue.async {
guard let sourceKitLSPServer = await self.sourceKitLSPServer else {
// `SourceKitLSPServer` has been destructed. We are tearing down the language
// server. Nothing left to do.
reply(.failure(.unknown("Connection to the editor closed")))
return
}
do {
let result = try await sourceKitLSPServer.sendRequestToClient(params)
reply(.success(result))
} catch {
reply(.failure(ResponseError(error)))
}
}
}
/// Forward the given request to `clangd`.
///
/// This method calls `readyToHandleNextRequest` once the request has been
/// transmitted to `clangd` and another request can be safely transmitted to
/// `clangd` while guaranteeing ordering.
///
/// The response of the request is returned asynchronously as the return value.
func forwardRequestToClangd<R: RequestType>(_ request: R) async throws -> R.Response {
return try await clangd.send(request)
}
func _crash() {
// Since `clangd` doesn't have a method to crash it, kill it.
#if os(Windows)
if self.hClangd != INVALID_HANDLE_VALUE {
// FIXME(compnerd) this is a bad idea - we can potentially deadlock the
// process if a kobject is a pending state. Unfortunately, the
// `OpenProcess(PROCESS_TERMINATE, ...)`, `CreateRemoteThread`,
// `ExitProcess` dance, while safer, can also indefinitely hang as
// `CreateRemoteThread` may not be serviced depending on the state of
// the process. This just attempts to terminate the process, risking a
// deadlock and resource leaks.
_ = TerminateProcess(self.hClangd, 0)
}
#else
if let pid = self.clangdPid {
kill(pid, SIGKILL)
}
#endif
}
}
// MARK: - LanguageServer
extension ClangLanguageService {
/// Intercept clangd's `PublishDiagnosticsNotification` to withold it if we're using fallback
/// build settings.
func publishDiagnostics(_ notification: PublishDiagnosticsNotification) async {
// Technically, the publish diagnostics notification could still originate
// from when we opened the file with fallback build settings and we could
// have received real build settings since, which haven't been acknowledged
// by clangd yet.
//
// Since there is no way to tell which build settings clangd used to generate
// the diagnostics, there's no good way to resolve this race. For now, this
// should be good enough since the time in which the race may occur is pretty
// short and we expect clangd to send us new diagnostics with the updated
// non-fallback settings very shortly after, which will override the
// incorrect result, making it very temporary.
let buildSettings = await self.buildSettings(for: notification.uri)
guard let sourceKitLSPServer else {
logger.fault("Cannot publish diagnostics because SourceKitLSPServer has been destroyed")
return
}
if buildSettings?.isFallback ?? true {
// Fallback: send empty publish notification instead.
await sourceKitLSPServer.sendNotificationToClient(
PublishDiagnosticsNotification(
uri: notification.uri,
version: notification.version,
diagnostics: []
)
)
} else {
await sourceKitLSPServer.sendNotificationToClient(notification)
}
}
}
// MARK: - LanguageService
extension ClangLanguageService {
func initialize(_ initialize: InitializeRequest) async throws -> InitializeResult {
// Store the initialize request so we can replay it in case clangd crashes
self.initializeRequest = initialize
let result = try await clangd.send(initialize)
self.capabilities = result.capabilities
return result
}
public func clientInitialized(_ initialized: InitializedNotification) {
clangd.send(initialized)
}
public func shutdown() async {
let clangd = clangd!
await withCheckedContinuation { continuation in
_ = clangd.send(ShutdownRequest()) { _ in
Task {
clangd.send(ExitNotification())
continuation.resume()
}
}
}
}
// MARK: - Text synchronization
public func openDocument(_ notification: DidOpenTextDocumentNotification) async {
openDocuments[notification.textDocument.uri] = notification.textDocument.language
// Send clangd the build settings for the new file. We need to do this before
// sending the open notification, so that the initial diagnostics already
// have build settings.
await documentUpdatedBuildSettings(notification.textDocument.uri)
clangd.send(notification)
}
public func closeDocument(_ notification: DidCloseTextDocumentNotification) {
openDocuments[notification.textDocument.uri] = nil
clangd.send(notification)
}
public func changeDocument(_ notification: DidChangeTextDocumentNotification) {
clangd.send(notification)
}
public func willSaveDocument(_ notification: WillSaveTextDocumentNotification) {
}
public func didSaveDocument(_ notification: DidSaveTextDocumentNotification) {
clangd.send(notification)
}
// MARK: - Build System Integration
public func documentUpdatedBuildSettings(_ uri: DocumentURI) async {
guard let url = uri.fileURL else {
// FIXME: The clang workspace can probably be reworked to support non-file URIs.
logger.error("Received updated build settings for non-file URI '\(uri.forLogging)'. Ignoring the update.")
return
}
let clangBuildSettings = await self.buildSettings(for: uri)
// The compile command changed, send over the new one.
// FIXME: what should we do if we no longer have valid build settings?
if let compileCommand = clangBuildSettings?.compileCommand,
let pathString = (try? AbsolutePath(validating: url.path))?.pathString
{
let notification = DidChangeConfigurationNotification(
settings: .clangd(
ClangWorkspaceSettings(
compilationDatabaseChanges: [pathString: compileCommand])
)
)
clangd.send(notification)
}
}
public func documentDependenciesUpdated(_ uri: DocumentURI) {
// In order to tell clangd to reload an AST, we send it an empty `didChangeTextDocument`
// with `forceRebuild` set in case any missing header files have been added.
// This works well for us as the moment since clangd ignores the document version.
let notification = DidChangeTextDocumentNotification(
textDocument: VersionedTextDocumentIdentifier(uri, version: 0),
contentChanges: [],
forceRebuild: true
)
clangd.send(notification)
}
// MARK: - Text Document
public func definition(_ req: DefinitionRequest) async throws -> LocationsOrLocationLinksResponse? {
// We handle it to provide jump-to-header support for #import/#include.
return try await self.forwardRequestToClangd(req)
}
public func declaration(_ req: DeclarationRequest) async throws -> LocationsOrLocationLinksResponse? {
return try await forwardRequestToClangd(req)
}
func completion(_ req: CompletionRequest) async throws -> CompletionList {
return try await forwardRequestToClangd(req)
}
func hover(_ req: HoverRequest) async throws -> HoverResponse? {
return try await forwardRequestToClangd(req)
}
func symbolInfo(_ req: SymbolInfoRequest) async throws -> [SymbolDetails] {
return try await forwardRequestToClangd(req)
}
func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
return try await forwardRequestToClangd(req)
}
func documentSymbol(_ req: DocumentSymbolRequest) async throws -> DocumentSymbolResponse? {
return try await forwardRequestToClangd(req)
}
func documentColor(_ req: DocumentColorRequest) async throws -> [ColorInformation] {
guard self.capabilities?.colorProvider?.isSupported ?? false else {
return []
}
return try await forwardRequestToClangd(req)
}
func documentSemanticTokens(_ req: DocumentSemanticTokensRequest) async throws -> DocumentSemanticTokensResponse? {
return try await forwardRequestToClangd(req)
}
func documentSemanticTokensDelta(
_ req: DocumentSemanticTokensDeltaRequest
) async throws -> DocumentSemanticTokensDeltaResponse? {
return try await forwardRequestToClangd(req)
}
func documentSemanticTokensRange(
_ req: DocumentSemanticTokensRangeRequest
) async throws -> DocumentSemanticTokensResponse? {
return try await forwardRequestToClangd(req)
}
func colorPresentation(_ req: ColorPresentationRequest) async throws -> [ColorPresentation] {
guard self.capabilities?.colorProvider?.isSupported ?? false else {
return []
}
return try await forwardRequestToClangd(req)
}
func documentFormatting(_ req: DocumentFormattingRequest) async throws -> [TextEdit]? {
return try await forwardRequestToClangd(req)
}
func codeAction(_ req: CodeActionRequest) async throws -> CodeActionRequestResponse? {
return try await forwardRequestToClangd(req)
}
func inlayHint(_ req: InlayHintRequest) async throws -> [InlayHint] {
return try await forwardRequestToClangd(req)
}
func documentDiagnostic(_ req: DocumentDiagnosticsRequest) async throws -> DocumentDiagnosticReport {
return try await forwardRequestToClangd(req)
}
func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
guard self.capabilities?.foldingRangeProvider?.isSupported ?? false else {
return nil
}
return try await forwardRequestToClangd(req)
}
func openInterface(_ request: OpenInterfaceRequest) async throws -> InterfaceDetails? {
throw ResponseError.unknown("unsupported method")
}
func indexedRename(_ request: IndexedRenameRequest) async throws -> WorkspaceEdit? {
return try await forwardRequestToClangd(request)
}
// MARK: - Other
func executeCommand(_ req: ExecuteCommandRequest) async throws -> LSPAny? {
return try await forwardRequestToClangd(req)
}
}
/// Clang build settings derived from a `FileBuildSettingsChange`.
private struct ClangBuildSettings: Equatable {
/// The compiler arguments, including the program name, argv[0].
public let compilerArgs: [String]
/// The working directory for the invocation.
public let workingDirectory: String
/// Whether the compiler arguments are considered fallback - we withhold diagnostics for
/// fallback arguments and represent the file state differently.
public let isFallback: Bool
public init(_ settings: FileBuildSettings, clangPath: AbsolutePath?) {
var arguments = [clangPath?.pathString ?? "clang"] + settings.compilerArguments
if arguments.contains("-fmodules") {
// Clangd is not built with support for the 'obj' format.
arguments.append(contentsOf: [
"-Xclang", "-fmodule-format=raw",
])
}
if let workingDirectory = settings.workingDirectory {
// FIXME: this is a workaround for clangd not respecting the compilation
// database's "directory" field for relative -fmodules-cache-path.
// rdar://63984913
arguments.append(contentsOf: [
"-working-directory", workingDirectory,
])
}
self.compilerArgs = arguments
self.workingDirectory = settings.workingDirectory ?? ""
self.isFallback = settings.isFallback
}
public var compileCommand: ClangCompileCommand {
return ClangCompileCommand(
compilationCommand: self.compilerArgs,
workingDirectory: self.workingDirectory
)
}
}