@@ -197,15 +197,14 @@ bool CompletionInstance::performCachedOperationIfPossible(
197
197
if (CachedArgHash != ArgsHash)
198
198
return false ;
199
199
200
- auto &CI = *CachedCI;
201
- auto *oldSF = CI.getCodeCompletionFile ();
200
+ auto *oldSF = CachedCI->getCodeCompletionFile ();
202
201
assert (oldSF->getBufferID ());
203
202
204
203
auto *oldState = oldSF->getDelayedParserState ();
205
204
assert (oldState->hasCodeCompletionDelayedDeclState ());
206
205
auto &oldInfo = oldState->getCodeCompletionDelayedDeclState ();
207
206
208
- auto &SM = CI. getSourceMgr ();
207
+ auto &SM = CachedCI-> getSourceMgr ();
209
208
auto bufferName = completionBuffer->getBufferIdentifier ();
210
209
if (SM.getIdentifierForBuffer (*oldSF->getBufferID ()) != bufferName)
211
210
return false ;
@@ -222,7 +221,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
222
221
}
223
222
224
223
if (areAnyDependentFilesInvalidated (
225
- CI , *FileSystem, *oldSF->getBufferID (),
224
+ *CachedCI , *FileSystem, *oldSF->getBufferID (),
226
225
DependencyCheckedTimestamp, InMemoryDependencyHash))
227
226
return false ;
228
227
DependencyCheckedTimestamp = std::chrono::system_clock::now ();
@@ -233,10 +232,10 @@ bool CompletionInstance::performCachedOperationIfPossible(
233
232
auto tmpBufferID = tmpSM.addMemBufferCopy (completionBuffer);
234
233
tmpSM.setCodeCompletionPoint (tmpBufferID, Offset);
235
234
236
- LangOptions langOpts = CI. getASTContext ().LangOpts ;
237
- TypeCheckerOptions typeckOpts = CI. getASTContext ().TypeCheckerOpts ;
238
- SILOptions silOpts = CI. getASTContext ().SILOpts ;
239
- SearchPathOptions searchPathOpts = CI. getASTContext ().SearchPathOpts ;
235
+ LangOptions langOpts = CachedCI-> getASTContext ().LangOpts ;
236
+ TypeCheckerOptions typeckOpts = CachedCI-> getASTContext ().TypeCheckerOpts ;
237
+ SILOptions silOpts = CachedCI-> getASTContext ().SILOpts ;
238
+ SearchPathOptions searchPathOpts = CachedCI-> getASTContext ().SearchPathOpts ;
240
239
DiagnosticEngine tmpDiags (tmpSM);
241
240
ClangImporterOptions clangOpts;
242
241
symbolgraphgen::SymbolGraphOptions symbolOpts;
@@ -382,7 +381,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
382
381
newM->addFile (*newSF);
383
382
384
383
// Tell the compiler instance we've replaced the main module.
385
- CI. setMainModule (newM);
384
+ CachedCI-> setMainModule (newM);
386
385
387
386
// Re-process the whole file (parsing will be lazily triggered). Still
388
387
// re-use imported modules.
@@ -407,22 +406,22 @@ bool CompletionInstance::performCachedOperationIfPossible(
407
406
408
407
// The diagnostic engine is keeping track of state which might modify
409
408
// parsing and type checking behaviour. Clear the flags.
410
- CI. getDiags ().resetHadAnyError ();
411
- CI. getASTContext ().CancellationFlag = CancellationFlag;
409
+ CachedCI-> getDiags ().resetHadAnyError ();
410
+ CachedCI-> getASTContext ().CancellationFlag = CancellationFlag;
412
411
413
412
if (DiagC)
414
- CI. addDiagnosticConsumer (DiagC);
413
+ CachedCI-> addDiagnosticConsumer (DiagC);
415
414
416
415
if (CancellationFlag && CancellationFlag->load (std::memory_order_relaxed)) {
417
416
Callback (CancellableResult<CompletionInstanceResult>::cancelled ());
418
417
} else {
419
418
Callback (CancellableResult<CompletionInstanceResult>::success (
420
- {CI , /* reusingASTContext=*/ true ,
419
+ {CachedCI , /* reusingASTContext=*/ true ,
421
420
/* DidFindCodeCompletionToken=*/ true }));
422
421
}
423
422
424
423
if (DiagC)
425
- CI. removeDiagnosticConsumer (DiagC);
424
+ CachedCI-> removeDiagnosticConsumer (DiagC);
426
425
}
427
426
428
427
CachedReuseCount += 1 ;
@@ -443,45 +442,44 @@ void CompletionInstance::performNewOperation(
443
442
// If ArgsHash is None we shouldn't cache the compiler instance.
444
443
bool ShouldCacheCompilerInstance = ArgsHash.hasValue ();
445
444
446
- auto TheInstance = std::make_unique <CompilerInstance>();
445
+ auto CI = std::make_shared <CompilerInstance>();
447
446
448
447
// Track non-system dependencies in fast-completion mode to invalidate the
449
448
// compiler instance if any dependent files are modified.
450
449
Invocation.getFrontendOptions ().IntermoduleDependencyTracking =
451
450
IntermoduleDepTrackingMode::ExcludeSystem;
452
451
453
452
{
454
- auto &CI = *TheInstance;
455
453
if (DiagC)
456
- CI. addDiagnosticConsumer (DiagC);
454
+ CI-> addDiagnosticConsumer (DiagC);
457
455
458
456
SWIFT_DEFER {
459
457
if (DiagC)
460
- CI. removeDiagnosticConsumer (DiagC);
458
+ CI-> removeDiagnosticConsumer (DiagC);
461
459
};
462
460
463
461
if (FileSystem != llvm::vfs::getRealFileSystem ())
464
- CI. getSourceMgr ().setFileSystem (FileSystem);
462
+ CI-> getSourceMgr ().setFileSystem (FileSystem);
465
463
466
464
Invocation.setCodeCompletionPoint (completionBuffer, Offset);
467
465
468
466
std::string InstanceSetupError;
469
- if (CI. setup (Invocation, InstanceSetupError)) {
467
+ if (CI-> setup (Invocation, InstanceSetupError)) {
470
468
Callback (CancellableResult<CompletionInstanceResult>::failure (
471
469
InstanceSetupError));
472
470
return ;
473
471
}
474
- CI. getASTContext ().CancellationFlag = CancellationFlag;
475
- registerIDERequestFunctions (CI. getASTContext ().evaluator );
472
+ CI-> getASTContext ().CancellationFlag = CancellationFlag;
473
+ registerIDERequestFunctions (CI-> getASTContext ().evaluator );
476
474
477
- CI. performParseAndResolveImportsOnly ();
475
+ CI-> performParseAndResolveImportsOnly ();
478
476
479
- bool DidFindCodeCompletionToken = CI. getCodeCompletionFile ()
477
+ bool DidFindCodeCompletionToken = CI-> getCodeCompletionFile ()
480
478
->getDelayedParserState ()
481
479
->hasCodeCompletionDelayedDeclState ();
482
480
ShouldCacheCompilerInstance &= DidFindCodeCompletionToken;
483
481
484
- auto CancellationFlag = CI. getASTContext ().CancellationFlag ;
482
+ auto CancellationFlag = CI-> getASTContext ().CancellationFlag ;
485
483
if (CancellationFlag && CancellationFlag->load (std::memory_order_relaxed)) {
486
484
Callback (CancellableResult<CompletionInstanceResult>::cancelled ());
487
485
// The completion instance may be in an invalid state when it's been
@@ -502,11 +500,11 @@ void CompletionInstance::performNewOperation(
502
500
// because performCachedOperationIfPossible wouldn't have an old code
503
501
// completion state to compare the new one to.
504
502
if (ShouldCacheCompilerInstance)
505
- cacheCompilerInstance (std::move (TheInstance ), *ArgsHash);
503
+ cacheCompilerInstance (std::move (CI ), *ArgsHash);
506
504
}
507
505
508
506
void CompletionInstance::cacheCompilerInstance (
509
- std::unique_ptr <CompilerInstance> CI, llvm::hash_code ArgsHash) {
507
+ std::shared_ptr <CompilerInstance> CI, llvm::hash_code ArgsHash) {
510
508
CachedCI = std::move (CI);
511
509
CachedArgHash = ArgsHash;
512
510
auto now = std::chrono::system_clock::now ();
@@ -602,11 +600,9 @@ void swift::ide::CompletionInstance::codeComplete(
602
600
: ImportDep(ImportDep), CancellationFlag(CancellationFlag),
603
601
Callback (Callback) {}
604
602
605
- void setContext (swift::ASTContext *context,
606
- const swift::CompilerInvocation *invocation,
603
+ void setContext (std::shared_ptr<CompilerInstance> compilerInstance,
607
604
swift::ide::CodeCompletionContext *completionContext) {
608
- SwiftContext.swiftASTContext = context;
609
- SwiftContext.invocation = invocation;
605
+ SwiftContext.compilerInstance = std::move (compilerInstance);
610
606
SwiftContext.completionContext = completionContext;
611
607
}
612
608
void clearContext () { SwiftContext = SwiftCompletionInfo (); }
@@ -617,7 +613,7 @@ void swift::ide::CompletionInstance::codeComplete(
617
613
CancellationFlag->load (std::memory_order_relaxed)) {
618
614
Callback (ResultType::cancelled ());
619
615
} else {
620
- assert (SwiftContext.swiftASTContext );
616
+ assert (SwiftContext.compilerInstance );
621
617
Callback (ResultType::success ({context.getResultSink (), SwiftContext, ImportDep}));
622
618
}
623
619
}
@@ -631,9 +627,9 @@ void swift::ide::CompletionInstance::codeComplete(
631
627
[&CompletionContext, &CancellationFlag](auto &Result,
632
628
auto DeliverTransformed) {
633
629
CompletionContext.ReusingASTContext = Result.DidReuseAST ;
634
- CompilerInstance & CI = Result.CI ;
635
- ImportDepth ImportDep{CI. getASTContext (),
636
- CI. getInvocation ().getFrontendOptions ()};
630
+ std::shared_ptr< CompilerInstance> CI = Result.CI ;
631
+ ImportDepth ImportDep{CI-> getASTContext (),
632
+ CI-> getInvocation ().getFrontendOptions ()};
637
633
ConsumerToCallbackAdapter Consumer (ImportDep, CancellationFlag,
638
634
DeliverTransformed);
639
635
@@ -642,27 +638,22 @@ void swift::ide::CompletionInstance::codeComplete(
642
638
Consumer));
643
639
644
640
if (!Result.DidFindCodeCompletionToken ) {
645
- SwiftCompletionInfo Info{&CI.getASTContext (),
646
- &CI.getInvocation (),
647
- &CompletionContext};
641
+ SwiftCompletionInfo Info{CI, &CompletionContext};
648
642
CodeCompletionResultSink ResultSink;
649
643
DeliverTransformed (ResultType::success ({ResultSink, Info, ImportDep}));
650
644
return ;
651
645
}
652
646
653
- Consumer.setContext (&CI.getASTContext (), &CI.getInvocation (),
654
- &CompletionContext);
655
- performCodeCompletionSecondPass (*CI.getCodeCompletionFile (),
647
+ Consumer.setContext (CI, &CompletionContext);
648
+ performCodeCompletionSecondPass (*CI->getCodeCompletionFile (),
656
649
*callbacksFactory);
657
650
Consumer.clearContext ();
658
651
if (!Consumer.HandleResultsCalled ) {
659
652
// If we didn't receive a handleResult call from the second
660
653
// pass, we didn't receive any results. To make sure Callback
661
654
// gets called exactly once, call it manually with no results
662
655
// here.
663
- SwiftCompletionInfo Info{&CI.getASTContext (),
664
- &CI.getInvocation (),
665
- &CompletionContext};
656
+ SwiftCompletionInfo Info{CI, &CompletionContext};
666
657
CodeCompletionResultSink ResultSink;
667
658
DeliverTransformed (ResultType::success ({ResultSink, Info, ImportDep}));
668
659
}
@@ -724,7 +715,7 @@ void swift::ide::CompletionInstance::typeContextInfo(
724
715
}
725
716
726
717
performCodeCompletionSecondPass (
727
- *Result.CI . getCodeCompletionFile (), *callbacksFactory);
718
+ *Result.CI -> getCodeCompletionFile (), *callbacksFactory);
728
719
if (!Consumer.HandleResultsCalled ) {
729
720
// If we didn't receive a handleResult call from the second
730
721
// pass, we didn't receive any results. To make sure Callback
@@ -792,7 +783,7 @@ void swift::ide::CompletionInstance::conformingMethodList(
792
783
}
793
784
794
785
performCodeCompletionSecondPass (
795
- *Result.CI . getCodeCompletionFile (), *callbacksFactory);
786
+ *Result.CI -> getCodeCompletionFile (), *callbacksFactory);
796
787
if (!Consumer.HandleResultsCalled ) {
797
788
// If we didn't receive a handleResult call from the second
798
789
// pass, we didn't receive any results. To make sure Callback
0 commit comments