@@ -197,15 +197,14 @@ bool CompletionInstance::performCachedOperationIfPossible(
197197 if (CachedArgHash != ArgsHash)
198198 return false ;
199199
200- auto &CI = *CachedCI;
201- auto *oldSF = CI.getCodeCompletionFile ();
200+ auto *oldSF = CachedCI->getCodeCompletionFile ();
202201 assert (oldSF->getBufferID ());
203202
204203 auto *oldState = oldSF->getDelayedParserState ();
205204 assert (oldState->hasCodeCompletionDelayedDeclState ());
206205 auto &oldInfo = oldState->getCodeCompletionDelayedDeclState ();
207206
208- auto &SM = CI. getSourceMgr ();
207+ auto &SM = CachedCI-> getSourceMgr ();
209208 auto bufferName = completionBuffer->getBufferIdentifier ();
210209 if (SM.getIdentifierForBuffer (*oldSF->getBufferID ()) != bufferName)
211210 return false ;
@@ -222,7 +221,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
222221 }
223222
224223 if (areAnyDependentFilesInvalidated (
225- CI , *FileSystem, *oldSF->getBufferID (),
224+ *CachedCI , *FileSystem, *oldSF->getBufferID (),
226225 DependencyCheckedTimestamp, InMemoryDependencyHash))
227226 return false ;
228227 DependencyCheckedTimestamp = std::chrono::system_clock::now ();
@@ -233,10 +232,10 @@ bool CompletionInstance::performCachedOperationIfPossible(
233232 auto tmpBufferID = tmpSM.addMemBufferCopy (completionBuffer);
234233 tmpSM.setCodeCompletionPoint (tmpBufferID, Offset);
235234
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 ;
240239 DiagnosticEngine tmpDiags (tmpSM);
241240 ClangImporterOptions clangOpts;
242241 symbolgraphgen::SymbolGraphOptions symbolOpts;
@@ -382,7 +381,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
382381 newM->addFile (*newSF);
383382
384383 // Tell the compiler instance we've replaced the main module.
385- CI. setMainModule (newM);
384+ CachedCI-> setMainModule (newM);
386385
387386 // Re-process the whole file (parsing will be lazily triggered). Still
388387 // re-use imported modules.
@@ -407,22 +406,22 @@ bool CompletionInstance::performCachedOperationIfPossible(
407406
408407 // The diagnostic engine is keeping track of state which might modify
409408 // 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;
412411
413412 if (DiagC)
414- CI. addDiagnosticConsumer (DiagC);
413+ CachedCI-> addDiagnosticConsumer (DiagC);
415414
416415 if (CancellationFlag && CancellationFlag->load (std::memory_order_relaxed)) {
417416 Callback (CancellableResult<CompletionInstanceResult>::cancelled ());
418417 } else {
419418 Callback (CancellableResult<CompletionInstanceResult>::success (
420- {CI , /* reusingASTContext=*/ true ,
419+ {CachedCI , /* reusingASTContext=*/ true ,
421420 /* DidFindCodeCompletionToken=*/ true }));
422421 }
423422
424423 if (DiagC)
425- CI. removeDiagnosticConsumer (DiagC);
424+ CachedCI-> removeDiagnosticConsumer (DiagC);
426425 }
427426
428427 CachedReuseCount += 1 ;
@@ -443,45 +442,44 @@ void CompletionInstance::performNewOperation(
443442 // If ArgsHash is None we shouldn't cache the compiler instance.
444443 bool ShouldCacheCompilerInstance = ArgsHash.hasValue ();
445444
446- auto TheInstance = std::make_unique <CompilerInstance>();
445+ auto CI = std::make_shared <CompilerInstance>();
447446
448447 // Track non-system dependencies in fast-completion mode to invalidate the
449448 // compiler instance if any dependent files are modified.
450449 Invocation.getFrontendOptions ().IntermoduleDependencyTracking =
451450 IntermoduleDepTrackingMode::ExcludeSystem;
452451
453452 {
454- auto &CI = *TheInstance;
455453 if (DiagC)
456- CI. addDiagnosticConsumer (DiagC);
454+ CI-> addDiagnosticConsumer (DiagC);
457455
458456 SWIFT_DEFER {
459457 if (DiagC)
460- CI. removeDiagnosticConsumer (DiagC);
458+ CI-> removeDiagnosticConsumer (DiagC);
461459 };
462460
463461 if (FileSystem != llvm::vfs::getRealFileSystem ())
464- CI. getSourceMgr ().setFileSystem (FileSystem);
462+ CI-> getSourceMgr ().setFileSystem (FileSystem);
465463
466464 Invocation.setCodeCompletionPoint (completionBuffer, Offset);
467465
468466 std::string InstanceSetupError;
469- if (CI. setup (Invocation, InstanceSetupError)) {
467+ if (CI-> setup (Invocation, InstanceSetupError)) {
470468 Callback (CancellableResult<CompletionInstanceResult>::failure (
471469 InstanceSetupError));
472470 return ;
473471 }
474- CI. getASTContext ().CancellationFlag = CancellationFlag;
475- registerIDERequestFunctions (CI. getASTContext ().evaluator );
472+ CI-> getASTContext ().CancellationFlag = CancellationFlag;
473+ registerIDERequestFunctions (CI-> getASTContext ().evaluator );
476474
477- CI. performParseAndResolveImportsOnly ();
475+ CI-> performParseAndResolveImportsOnly ();
478476
479- bool DidFindCodeCompletionToken = CI. getCodeCompletionFile ()
477+ bool DidFindCodeCompletionToken = CI-> getCodeCompletionFile ()
480478 ->getDelayedParserState ()
481479 ->hasCodeCompletionDelayedDeclState ();
482480 ShouldCacheCompilerInstance &= DidFindCodeCompletionToken;
483481
484- auto CancellationFlag = CI. getASTContext ().CancellationFlag ;
482+ auto CancellationFlag = CI-> getASTContext ().CancellationFlag ;
485483 if (CancellationFlag && CancellationFlag->load (std::memory_order_relaxed)) {
486484 Callback (CancellableResult<CompletionInstanceResult>::cancelled ());
487485 // The completion instance may be in an invalid state when it's been
@@ -502,11 +500,11 @@ void CompletionInstance::performNewOperation(
502500 // because performCachedOperationIfPossible wouldn't have an old code
503501 // completion state to compare the new one to.
504502 if (ShouldCacheCompilerInstance)
505- cacheCompilerInstance (std::move (TheInstance ), *ArgsHash);
503+ cacheCompilerInstance (std::move (CI ), *ArgsHash);
506504}
507505
508506void CompletionInstance::cacheCompilerInstance (
509- std::unique_ptr <CompilerInstance> CI, llvm::hash_code ArgsHash) {
507+ std::shared_ptr <CompilerInstance> CI, llvm::hash_code ArgsHash) {
510508 CachedCI = std::move (CI);
511509 CachedArgHash = ArgsHash;
512510 auto now = std::chrono::system_clock::now ();
@@ -602,11 +600,9 @@ void swift::ide::CompletionInstance::codeComplete(
602600 : ImportDep(ImportDep), CancellationFlag(CancellationFlag),
603601 Callback (Callback) {}
604602
605- void setContext (swift::ASTContext *context,
606- const swift::CompilerInvocation *invocation,
603+ void setContext (std::shared_ptr<CompilerInstance> compilerInstance,
607604 swift::ide::CodeCompletionContext *completionContext) {
608- SwiftContext.swiftASTContext = context;
609- SwiftContext.invocation = invocation;
605+ SwiftContext.compilerInstance = std::move (compilerInstance);
610606 SwiftContext.completionContext = completionContext;
611607 }
612608 void clearContext () { SwiftContext = SwiftCompletionInfo (); }
@@ -617,7 +613,7 @@ void swift::ide::CompletionInstance::codeComplete(
617613 CancellationFlag->load (std::memory_order_relaxed)) {
618614 Callback (ResultType::cancelled ());
619615 } else {
620- assert (SwiftContext.swiftASTContext );
616+ assert (SwiftContext.compilerInstance );
621617 Callback (ResultType::success ({context.getResultSink (), SwiftContext, ImportDep}));
622618 }
623619 }
@@ -631,9 +627,9 @@ void swift::ide::CompletionInstance::codeComplete(
631627 [&CompletionContext, &CancellationFlag](auto &Result,
632628 auto DeliverTransformed) {
633629 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 ()};
637633 ConsumerToCallbackAdapter Consumer (ImportDep, CancellationFlag,
638634 DeliverTransformed);
639635
@@ -642,27 +638,22 @@ void swift::ide::CompletionInstance::codeComplete(
642638 Consumer));
643639
644640 if (!Result.DidFindCodeCompletionToken ) {
645- SwiftCompletionInfo Info{&CI.getASTContext (),
646- &CI.getInvocation (),
647- &CompletionContext};
641+ SwiftCompletionInfo Info{CI, &CompletionContext};
648642 CodeCompletionResultSink ResultSink;
649643 DeliverTransformed (ResultType::success ({ResultSink, Info, ImportDep}));
650644 return ;
651645 }
652646
653- Consumer.setContext (&CI.getASTContext (), &CI.getInvocation (),
654- &CompletionContext);
655- performCodeCompletionSecondPass (*CI.getCodeCompletionFile (),
647+ Consumer.setContext (CI, &CompletionContext);
648+ performCodeCompletionSecondPass (*CI->getCodeCompletionFile (),
656649 *callbacksFactory);
657650 Consumer.clearContext ();
658651 if (!Consumer.HandleResultsCalled ) {
659652 // If we didn't receive a handleResult call from the second
660653 // pass, we didn't receive any results. To make sure Callback
661654 // gets called exactly once, call it manually with no results
662655 // here.
663- SwiftCompletionInfo Info{&CI.getASTContext (),
664- &CI.getInvocation (),
665- &CompletionContext};
656+ SwiftCompletionInfo Info{CI, &CompletionContext};
666657 CodeCompletionResultSink ResultSink;
667658 DeliverTransformed (ResultType::success ({ResultSink, Info, ImportDep}));
668659 }
@@ -724,7 +715,7 @@ void swift::ide::CompletionInstance::typeContextInfo(
724715 }
725716
726717 performCodeCompletionSecondPass (
727- *Result.CI . getCodeCompletionFile (), *callbacksFactory);
718+ *Result.CI -> getCodeCompletionFile (), *callbacksFactory);
728719 if (!Consumer.HandleResultsCalled ) {
729720 // If we didn't receive a handleResult call from the second
730721 // pass, we didn't receive any results. To make sure Callback
@@ -792,7 +783,7 @@ void swift::ide::CompletionInstance::conformingMethodList(
792783 }
793784
794785 performCodeCompletionSecondPass (
795- *Result.CI . getCodeCompletionFile (), *callbacksFactory);
786+ *Result.CI -> getCodeCompletionFile (), *callbacksFactory);
796787 if (!Consumer.HandleResultsCalled ) {
797788 // If we didn't receive a handleResult call from the second
798789 // pass, we didn't receive any results. To make sure Callback
0 commit comments