-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathCompletionInstance.cpp
650 lines (547 loc) · 22.9 KB
/
CompletionInstance.cpp
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
//===--- CompletionInstance.cpp -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//
#include "swift/IDE/CompletionInstance.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/Module.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/SourceManager.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/Driver/FrontendUtil.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Parse/Lexer.h"
#include "swift/Parse/PersistentParserState.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Subsystems.h"
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
#include "clang/AST/ASTContext.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/Support/MemoryBuffer.h"
using namespace swift;
using namespace ide;
std::unique_ptr<llvm::MemoryBuffer>
swift::ide::makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
unsigned &Offset,
StringRef bufferIdentifier) {
auto origBuffSize = origBuf->getBufferSize();
if (Offset > origBuffSize)
Offset = origBuffSize;
auto newBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
origBuffSize + 1, bufferIdentifier);
auto *pos = origBuf->getBufferStart() + Offset;
auto *newPos =
std::copy(origBuf->getBufferStart(), pos, newBuffer->getBufferStart());
*newPos = '\0';
std::copy(pos, origBuf->getBufferEnd(), newPos + 1);
return std::unique_ptr<llvm::MemoryBuffer>(newBuffer.release());
}
namespace {
/// Returns index number of \p D in \p Decls . If it's not found, returns ~0.
template <typename Range>
unsigned findIndexInRange(Decl *D, const Range &Decls) {
unsigned N = 0;
for (auto I = Decls.begin(), E = Decls.end(); I != E; ++I) {
if ((*I)->isImplicit())
continue;
if (*I == D)
return N;
++N;
}
return ~0U;
}
/// Return the element at \p N in \p Decls .
template <typename Range> Decl *getElementAt(const Range &Decls, unsigned N) {
for (auto I = Decls.begin(), E = Decls.end(); I != E; ++I) {
if ((*I)->isImplicit())
continue;
if (N == 0)
return *I;
--N;
}
return nullptr;
}
/// Find the equivalent \c DeclContext with \p DC from \p SF AST.
/// This assumes the AST which contains \p DC has exact the same structure with
/// \p SF.
static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
SourceFile *SF) {
PrettyStackTraceDeclContext trace("getting equivalent decl context for", DC);
auto *newDC = DC;
// NOTE: Shortcut for DC->getParentSourceFile() == SF case is not needed
// because they should be always different.
// Get the index path in the current AST.
SmallVector<unsigned, 4> IndexStack;
do {
auto *D = newDC->getAsDecl();
if (!D)
return nullptr;
auto *parentDC = newDC->getParent();
unsigned N = ~0U;
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
// The AST for accessors is like:
// DeclContext -> AbstractStorageDecl -> AccessorDecl
// We need to push the index of the accessor within the accessor list
// of the storage.
auto *storage = accessor->getStorage();
if (!storage)
return nullptr;
auto accessorN = findIndexInRange(accessor, storage->getAllAccessors());
IndexStack.push_back(accessorN);
D = storage;
}
if (auto parentSF = dyn_cast<SourceFile>(parentDC)) {
N = findIndexInRange(D, parentSF->getTopLevelDecls());
} else if (auto parentIDC = dyn_cast_or_null<IterableDeclContext>(
parentDC->getAsDecl())) {
N = findIndexInRange(D, parentIDC->getMembers());
} else {
#ifndef NDEBUG
llvm_unreachable("invalid DC kind for finding equivalent DC (indexpath)");
#endif
return nullptr;
}
// Not found in the decl context tree.
if (N == ~0U) {
return nullptr;
}
IndexStack.push_back(N);
newDC = parentDC;
} while (!newDC->isModuleScopeContext());
assert(isa<SourceFile>(newDC) && "DC should be in a SourceFile");
// Query the equivalent decl context from the base SourceFile using the index
// path.
newDC = SF;
do {
auto N = IndexStack.pop_back_val();
Decl *D = nullptr;
if (auto parentSF = dyn_cast<SourceFile>(newDC))
D = getElementAt(parentSF->getTopLevelDecls(), N);
else if (auto parentIDC = dyn_cast<IterableDeclContext>(newDC->getAsDecl()))
D = getElementAt(parentIDC->getMembers(), N);
else
llvm_unreachable("invalid DC kind for finding equivalent DC (query)");
if (auto storage = dyn_cast_or_null<AbstractStorageDecl>(D)) {
if (IndexStack.empty())
return nullptr;
auto accessorN = IndexStack.pop_back_val();
D = getElementAt(storage->getAllAccessors(), accessorN);
}
newDC = dyn_cast_or_null<DeclContext>(D);
if (!newDC)
return nullptr;
} while (!IndexStack.empty());
assert(newDC->getContextKind() == DC->getContextKind());
return newDC;
}
/// For each dependency file in \p CI, run \p callback until the callback
/// returns \c true. Returns \c true if any callback call returns \c true, \c
/// false otherwise.
static bool
forEachDependencyUntilTrue(CompilerInstance &CI, unsigned excludeBufferID,
llvm::function_ref<bool(StringRef)> callback) {
// Check files in the current module.
for (FileUnit *file : CI.getMainModule()->getFiles()) {
StringRef filename;
if (auto SF = dyn_cast<SourceFile>(file)) {
if (SF->getBufferID() == excludeBufferID)
continue;
filename = SF->getFilename();
} else if (auto LF = dyn_cast<LoadedFile>(file))
filename = LF->getFilename();
else
continue;
// Ignore synthesized files.
if (filename.empty() || filename.front() == '<')
continue;
if (callback(filename))
return true;
}
// Check other non-system depenencies (e.g. modules, headers).
for (auto &dep : CI.getDependencyTracker()->getDependencies()) {
if (callback(dep))
return true;
}
for (auto dep : CI.getDependencyTracker()->getIncrementalDependencyPaths()) {
if (callback(dep))
return true;
}
return false;
}
/// Collect hash codes of the dependencies into \c Map.
static void cacheDependencyHashIfNeeded(CompilerInstance &CI,
unsigned excludeBufferID,
llvm::StringMap<llvm::hash_code> &Map) {
auto &FS = CI.getFileSystem();
forEachDependencyUntilTrue(
CI, excludeBufferID, [&](StringRef filename) {
if (Map.count(filename))
return false;
auto stat = FS.status(filename);
if (!stat)
return false;
// We will check the hash only if the modification time of the dependecy
// is zero. See 'areAnyDependentFilesInvalidated() below'.
if (stat->getLastModificationTime() != llvm::sys::TimePoint<>())
return false;
auto buf = FS.getBufferForFile(filename);
Map[filename] = llvm::hash_value(buf.get()->getBuffer());
return false;
});
}
/// Check if any dependent files are modified since \p timestamp.
static bool areAnyDependentFilesInvalidated(
CompilerInstance &CI, llvm::vfs::FileSystem &FS,
unsigned excludeBufferID, llvm::sys::TimePoint<> timestamp,
llvm::StringMap<llvm::hash_code> &Map) {
return forEachDependencyUntilTrue(
CI, excludeBufferID, [&](StringRef filePath) {
auto stat = FS.status(filePath);
if (!stat)
// Missing.
return true;
auto lastModTime = stat->getLastModificationTime();
if (lastModTime > timestamp)
// Modified.
return true;
// If the last modification time is zero, this file is probably from a
// virtual file system. We need to check the content.
if (lastModTime == llvm::sys::TimePoint<>()) {
// Get the hash code of the last content.
auto oldHashEntry = Map.find(filePath);
if (oldHashEntry == Map.end())
// Unreachable? Not virtual in old filesystem, but virtual in new
// one.
return true;
auto oldHash = oldHashEntry->second;
// Calculate the hash code of the current content.
auto newContent = FS.getBufferForFile(filePath);
if (!newContent)
// Unreachable? stat succeeded, but coundn't get the content.
return true;
auto newHash = llvm::hash_value(newContent.get()->getBuffer());
if (oldHash != newHash)
return true;
}
return false;
});
}
} // namespace
bool CompletionInstance::performCachedOperationIfPossible(
llvm::hash_code ArgsHash,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
DiagnosticConsumer *DiagC,
llvm::function_ref<void(CompilerInstance &, bool)> Callback) {
llvm::PrettyStackTraceString trace(
"While performing cached completion if possible");
// Check the invalidation first. Otherwise, in case no 'CacheCI' exists yet,
// the flag will remain 'true' even after 'CachedCI' is populated.
if (CachedCIShouldBeInvalidated.exchange(false))
return false;
if (!CachedCI)
return false;
if (CachedReuseCount >= Opts.MaxASTReuseCount)
return false;
if (CachedArgHash != ArgsHash)
return false;
auto &CI = *CachedCI;
auto *oldSF = CI.getCodeCompletionFile();
assert(oldSF->getBufferID());
auto *oldState = oldSF->getDelayedParserState();
assert(oldState->hasCodeCompletionDelayedDeclState());
auto &oldInfo = oldState->getCodeCompletionDelayedDeclState();
auto &SM = CI.getSourceMgr();
auto bufferName = completionBuffer->getBufferIdentifier();
if (SM.getIdentifierForBuffer(*oldSF->getBufferID()) != bufferName)
return false;
if (shouldCheckDependencies()) {
if (areAnyDependentFilesInvalidated(
CI, *FileSystem, *oldSF->getBufferID(),
DependencyCheckedTimestamp, InMemoryDependencyHash))
return false;
DependencyCheckedTimestamp = std::chrono::system_clock::now();
}
// Parse the new buffer into temporary SourceFile.
SourceManager tmpSM;
auto tmpBufferID = tmpSM.addMemBufferCopy(completionBuffer);
tmpSM.setCodeCompletionPoint(tmpBufferID, Offset);
LangOptions langOpts = CI.getASTContext().LangOpts;
TypeCheckerOptions typeckOpts = CI.getASTContext().TypeCheckerOpts;
SILOptions silOpts = CI.getASTContext().SILOpts;
SearchPathOptions searchPathOpts = CI.getASTContext().SearchPathOpts;
DiagnosticEngine tmpDiags(tmpSM);
ClangImporterOptions clangOpts;
symbolgraphgen::SymbolGraphOptions symbolOpts;
std::unique_ptr<ASTContext> tmpCtx(
ASTContext::get(langOpts, typeckOpts, silOpts, searchPathOpts, clangOpts,
symbolOpts, tmpSM, tmpDiags));
registerParseRequestFunctions(tmpCtx->evaluator);
registerIDERequestFunctions(tmpCtx->evaluator);
registerTypeCheckerRequestFunctions(tmpCtx->evaluator);
registerClangImporterRequestFunctions(tmpCtx->evaluator);
registerSILGenRequestFunctions(tmpCtx->evaluator);
ModuleDecl *tmpM = ModuleDecl::create(Identifier(), *tmpCtx);
SourceFile *tmpSF = new (*tmpCtx)
SourceFile(*tmpM, oldSF->Kind, tmpBufferID, oldSF->getParsingOptions());
// FIXME: Since we don't setup module loaders on the temporary AST context,
// 'canImport()' conditional compilation directive always fails. That causes
// interface hash change and prevents fast-completion.
// Parse and get the completion context.
auto *newState = tmpSF->getDelayedParserState();
// Couldn't find any completion token?
if (!newState->hasCodeCompletionDelayedDeclState())
return false;
auto &newInfo = newState->getCodeCompletionDelayedDeclState();
unsigned newBufferID;
DeclContext *traceDC = nullptr;
switch (newInfo.Kind) {
case CodeCompletionDelayedDeclKind::FunctionBody: {
// If the interface has changed, AST must be refreshed.
// See if the inteface of the function and types visible from a function
// body has changed since the last completion. If they haven't changed,
// completion can reuse the existing AST of the source file.
// \c getInterfaceHash() is not enough because it doesn't take the interface
// of the type members into account. For example:
//
// struct S {
// func foo() {}
// }
// func main(val: S) {
// val.<HERE>
// }
//
// In this case, we need to ensure that the interface of \c S hasn't
// changed. Note that we don't care about local types (i.e. type
// declarations inside function bodies, closures, or top level statement
// bodies) because they are not visible from other functions where the
// completion is happening.
const auto oldInterfaceHash = oldSF->getInterfaceHashIncludingTypeMembers();
const auto newInterfaceHash = tmpSF->getInterfaceHashIncludingTypeMembers();
if (oldInterfaceHash != newInterfaceHash)
return false;
DeclContext *DC =
getEquivalentDeclContextFromSourceFile(newInfo.ParentContext, oldSF);
if (!DC || !isa<AbstractFunctionDecl>(DC))
return false;
// OK, we can perform fast completion for this. Update the orignal delayed
// decl state.
// Fast completion keeps the buffer in memory for multiple completions.
// To reduce the consumption, slice the source buffer so it only holds
// the portion that is needed for the second pass.
auto startOffset = newInfo.StartOffset;
if (newInfo.PrevOffset != ~0u)
startOffset = newInfo.PrevOffset;
auto startLoc = tmpSM.getLocForOffset(tmpBufferID, startOffset);
startLoc = Lexer::getLocForStartOfLine(tmpSM, startLoc);
startOffset = tmpSM.getLocOffsetInBuffer(startLoc, tmpBufferID);
auto endOffset = newInfo.EndOffset;
auto endLoc = tmpSM.getLocForOffset(tmpBufferID, endOffset);
endLoc = Lexer::getLocForEndOfToken(tmpSM, endLoc);
endOffset = tmpSM.getLocOffsetInBuffer(endLoc, tmpBufferID);
newInfo.StartOffset -= startOffset;
newInfo.EndOffset -= startOffset;
if (newInfo.PrevOffset != ~0u)
newInfo.PrevOffset -= startOffset;
auto sourceText =
completionBuffer->getBuffer().slice(startOffset, endOffset);
auto newOffset = Offset - startOffset;
newBufferID = SM.addMemBufferCopy(sourceText, bufferName);
SM.openVirtualFile(SM.getLocForBufferStart(newBufferID),
tmpSM.getDisplayNameForLoc(startLoc),
tmpSM.getPresumedLineAndColumnForLoc(startLoc).first -
1);
SM.setCodeCompletionPoint(newBufferID, newOffset);
oldInfo.ParentContext = DC;
oldInfo.StartOffset = newInfo.StartOffset;
oldInfo.EndOffset = newInfo.EndOffset;
oldInfo.PrevOffset = newInfo.PrevOffset;
oldState->restoreCodeCompletionDelayedDeclState(oldInfo);
auto newBufferStart = SM.getRangeForBuffer(newBufferID).getStart();
SourceRange newBodyRange(newBufferStart.getAdvancedLoc(newInfo.StartOffset),
newBufferStart.getAdvancedLoc(newInfo.EndOffset));
auto *AFD = cast<AbstractFunctionDecl>(DC);
AFD->setBodyToBeReparsed(newBodyRange);
SM.setReplacedRange({AFD->getOriginalBodySourceRange(), newBodyRange});
oldSF->clearScope();
traceDC = AFD;
break;
}
case CodeCompletionDelayedDeclKind::Decl:
case CodeCompletionDelayedDeclKind::TopLevelCodeDecl: {
// Support decl/top-level code only if the completion happens in a single
// file 'main' script (e.g. playground).
auto *oldM = oldInfo.ParentContext->getParentModule();
if (oldM->getFiles().size() != 1 || oldSF->Kind != SourceFileKind::Main)
return false;
// Perform fast completion.
// Prepare the new buffer in the source manager.
auto sourceText = completionBuffer->getBuffer();
if (newInfo.Kind == CodeCompletionDelayedDeclKind::TopLevelCodeDecl) {
// We don't need the source text after the top-level code.
auto endOffset = newInfo.EndOffset;
auto endLoc = tmpSM.getLocForOffset(tmpBufferID, endOffset);
endLoc = Lexer::getLocForEndOfToken(tmpSM, endLoc);
endOffset = tmpSM.getLocOffsetInBuffer(endLoc, tmpBufferID);
sourceText = sourceText.slice(0, endOffset);
}
newBufferID = SM.addMemBufferCopy(sourceText, bufferName);
SM.setCodeCompletionPoint(newBufferID, Offset);
// Create a new module and a source file using the current AST context.
auto &Ctx = oldM->getASTContext();
auto *newM = ModuleDecl::createMainModule(Ctx, oldM->getName(),
oldM->getImplicitImportInfo());
newM->setABIName(oldM->getABIName());
auto *newSF = new (Ctx) SourceFile(*newM, SourceFileKind::Main, newBufferID,
oldSF->getParsingOptions());
newM->addFile(*newSF);
// Tell the compiler instance we've replaced the main module.
CI.setMainModule(newM);
// Re-process the whole file (parsing will be lazily triggered). Still
// re-use imported modules.
performImportResolution(*newSF);
bindExtensions(*newM);
traceDC = newM;
#ifndef NDEBUG
const auto *reparsedState = newSF->getDelayedParserState();
assert(reparsedState->hasCodeCompletionDelayedDeclState() &&
"Didn't find completion token?");
auto &reparsedInfo = reparsedState->getCodeCompletionDelayedDeclState();
assert(reparsedInfo.Kind == newInfo.Kind);
#endif
break;
}
}
{
PrettyStackTraceDeclContext trace("performing cached completion", traceDC);
// The diagnostic engine is keeping track of state which might modify
// parsing and type checking behaviour. Clear the flags.
CI.getDiags().resetHadAnyError();
if (DiagC)
CI.addDiagnosticConsumer(DiagC);
Callback(CI, /*reusingASTContext=*/true);
if (DiagC)
CI.removeDiagnosticConsumer(DiagC);
}
CachedReuseCount += 1;
return true;
}
bool CompletionInstance::performNewOperation(
Optional<llvm::hash_code> ArgsHash, swift::CompilerInvocation &Invocation,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
std::string &Error, DiagnosticConsumer *DiagC,
llvm::function_ref<void(CompilerInstance &, bool)> Callback) {
llvm::PrettyStackTraceString trace("While performing new completion");
auto isCachedCompletionRequested = ArgsHash.hasValue();
auto TheInstance = std::make_unique<CompilerInstance>();
// Track non-system dependencies in fast-completion mode to invalidate the
// compiler instance if any dependent files are modified.
Invocation.getFrontendOptions().IntermoduleDependencyTracking =
IntermoduleDepTrackingMode::ExcludeSystem;
{
auto &CI = *TheInstance;
if (DiagC)
CI.addDiagnosticConsumer(DiagC);
SWIFT_DEFER {
if (DiagC)
CI.removeDiagnosticConsumer(DiagC);
};
if (FileSystem != llvm::vfs::getRealFileSystem())
CI.getSourceMgr().setFileSystem(FileSystem);
Invocation.setCodeCompletionPoint(completionBuffer, Offset);
if (CI.setup(Invocation)) {
Error = "failed to setup compiler instance";
return false;
}
registerIDERequestFunctions(CI.getASTContext().evaluator);
// If we're expecting a standard library, but there either isn't one, or it
// failed to load, let's bail early and hand back an empty completion
// result to avoid any downstream crashes.
if (CI.loadStdlibIfNeeded())
return true;
CI.performParseAndResolveImportsOnly();
// If we didn't find a code completion token, bail.
auto *state = CI.getCodeCompletionFile()->getDelayedParserState();
if (!state->hasCodeCompletionDelayedDeclState())
return true;
Callback(CI, /*reusingASTContext=*/false);
}
// Cache the compiler instance if fast completion is enabled.
if (isCachedCompletionRequested)
cacheCompilerInstance(std::move(TheInstance), *ArgsHash);
return true;
}
void CompletionInstance::cacheCompilerInstance(
std::unique_ptr<CompilerInstance> CI, llvm::hash_code ArgsHash) {
CachedCI = std::move(CI);
CachedArgHash = ArgsHash;
auto now = std::chrono::system_clock::now();
DependencyCheckedTimestamp = now;
CachedReuseCount = 0;
InMemoryDependencyHash.clear();
cacheDependencyHashIfNeeded(
*CachedCI,
CachedCI->getASTContext().SourceMgr.getCodeCompletionBufferID(),
InMemoryDependencyHash);
}
bool CompletionInstance::shouldCheckDependencies() const {
assert(CachedCI);
using namespace std::chrono;
auto now = system_clock::now();
auto threshold = DependencyCheckedTimestamp +
seconds(Opts.DependencyCheckIntervalSecond);
return threshold <= now;
}
void CompletionInstance::markCachedCompilerInstanceShouldBeInvalidated() {
CachedCIShouldBeInvalidated = true;
}
void CompletionInstance::setOptions(CompletionInstance::Options NewOpts) {
std::lock_guard<std::mutex> lock(mtx);
Opts = NewOpts;
}
bool swift::ide::CompletionInstance::performOperation(
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
std::string &Error, DiagnosticConsumer *DiagC,
llvm::function_ref<void(CompilerInstance &, bool)> Callback) {
// Always disable source location resolutions from .swiftsourceinfo file
// because they're somewhat heavy operations and aren't needed for completion.
Invocation.getFrontendOptions().IgnoreSwiftSourceInfo = true;
// Disable to build syntax tree because code-completion skips some portion of
// source text. That breaks an invariant of syntax tree building.
Invocation.getLangOptions().BuildSyntaxTree = false;
// We don't need token list.
Invocation.getLangOptions().CollectParsedToken = false;
// Compute the signature of the invocation.
llvm::hash_code ArgsHash(0);
for (auto arg : Args)
ArgsHash = llvm::hash_combine(ArgsHash, StringRef(arg));
// Concurrent completions will block so that they have higher chance to use
// the cached completion instance.
std::lock_guard<std::mutex> lock(mtx);
if (performCachedOperationIfPossible(ArgsHash, FileSystem, completionBuffer,
Offset, DiagC, Callback)) {
return true;
}
if(performNewOperation(ArgsHash, Invocation, FileSystem, completionBuffer,
Offset, Error, DiagC, Callback)) {
return true;
}
assert(!Error.empty());
return false;
}