Skip to content

Commit 22f67e8

Browse files
committed
[SourceKit] Explicitly specify frontend action type when creating compiler invocation
Explicitly specify the frontend action to make sure we aren’t loading thes stdlib if the performed action is syntactic only.
1 parent 63c3103 commit 22f67e8

File tree

10 files changed

+93
-70
lines changed

10 files changed

+93
-70
lines changed

include/swift/IDE/Utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/Effects.h"
2121
#include "swift/AST/Module.h"
2222
#include "swift/AST/ASTPrinter.h"
23+
#include "swift/Frontend/FrontendOptions.h"
2324
#include "swift/IDE/SourceEntityWalker.h"
2425
#include "swift/Parse/Token.h"
2526
#include "llvm/ADT/StringRef.h"
@@ -85,7 +86,8 @@ SourceCompleteResult isSourceInputComplete(StringRef Text, SourceFileKind SFKind
8586

8687
bool initCompilerInvocation(
8788
CompilerInvocation &Invocation, ArrayRef<const char *> OrigArgs,
88-
DiagnosticEngine &Diags, StringRef UnresolvedPrimaryFile,
89+
FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
90+
StringRef UnresolvedPrimaryFile,
8991
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
9092
const std::string &runtimeResourcePath,
9193
const std::string &diagnosticDocumentationPath, time_t sessionTimestamp,

lib/IDE/Utils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ class StreamDiagConsumer : public DiagnosticConsumer {
273273

274274
bool ide::initCompilerInvocation(
275275
CompilerInvocation &Invocation, ArrayRef<const char *> OrigArgs,
276-
DiagnosticEngine &Diags, StringRef UnresolvedPrimaryFile,
276+
FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
277+
StringRef UnresolvedPrimaryFile,
277278
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
278279
const std::string &runtimeResourcePath,
279280
const std::string &diagnosticDocumentationPath, time_t sessionTimestamp,

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -650,42 +650,43 @@ convertFileContentsToInputs(ArrayRef<FileContent> contents) {
650650

651651
bool SwiftASTManager::initCompilerInvocation(
652652
CompilerInvocation &Invocation, ArrayRef<const char *> OrigArgs,
653-
DiagnosticEngine &Diags, StringRef UnresolvedPrimaryFile,
654-
std::string &Error) {
655-
return initCompilerInvocation(Invocation, OrigArgs, Diags,
653+
swift::FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
654+
StringRef UnresolvedPrimaryFile, std::string &Error) {
655+
return initCompilerInvocation(Invocation, OrigArgs, Action, Diags,
656656
UnresolvedPrimaryFile,
657-
llvm::vfs::getRealFileSystem(),
658-
Error);
657+
llvm::vfs::getRealFileSystem(), Error);
659658
}
660659

661660
bool SwiftASTManager::initCompilerInvocation(
662661
CompilerInvocation &Invocation, ArrayRef<const char *> OrigArgs,
663-
DiagnosticEngine &Diags, StringRef UnresolvedPrimaryFile,
662+
FrontendOptions::ActionType Action, DiagnosticEngine &Diags,
663+
StringRef UnresolvedPrimaryFile,
664664
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
665665
std::string &Error) {
666666
return ide::initCompilerInvocation(
667-
Invocation, OrigArgs, Diags, UnresolvedPrimaryFile, FileSystem,
667+
Invocation, OrigArgs, Action, Diags, UnresolvedPrimaryFile, FileSystem,
668668
Impl.RuntimeResourcePath, Impl.DiagnosticDocumentationPath,
669669
Impl.SessionTimestamp, Error);
670670
}
671671

672-
bool SwiftASTManager::initCompilerInvocation(CompilerInvocation &CompInvok,
673-
ArrayRef<const char *> OrigArgs,
674-
StringRef PrimaryFile,
675-
std::string &Error) {
672+
bool SwiftASTManager::initCompilerInvocation(
673+
CompilerInvocation &CompInvok, ArrayRef<const char *> OrigArgs,
674+
swift::FrontendOptions::ActionType Action, StringRef PrimaryFile,
675+
std::string &Error) {
676676
DiagnosticEngine Diagnostics(Impl.SourceMgr);
677-
return initCompilerInvocation(CompInvok, OrigArgs, Diagnostics, PrimaryFile,
678-
Error);
677+
return initCompilerInvocation(CompInvok, OrigArgs, Action, Diagnostics,
678+
PrimaryFile, Error);
679679
}
680680

681681
bool SwiftASTManager::initCompilerInvocationNoInputs(
682682
swift::CompilerInvocation &Invocation, ArrayRef<const char *> OrigArgs,
683-
swift::DiagnosticEngine &Diags, std::string &Error, bool AllowInputs) {
683+
swift::FrontendOptions::ActionType Action, swift::DiagnosticEngine &Diags,
684+
std::string &Error, bool AllowInputs) {
684685

685686
SmallVector<const char *, 16> Args(OrigArgs.begin(), OrigArgs.end());
686687
// Use stdin as a .swift input to satisfy the driver.
687688
Args.push_back("-");
688-
if (initCompilerInvocation(Invocation, Args, Diags, "", Error))
689+
if (initCompilerInvocation(Invocation, Args, Action, Diags, "", Error))
689690
return true;
690691

691692
if (!AllowInputs &&
@@ -699,13 +700,15 @@ bool SwiftASTManager::initCompilerInvocationNoInputs(
699700
return false;
700701
}
701702

702-
SwiftInvocationRef SwiftASTManager::getInvocation(
703-
ArrayRef<const char *> OrigArgs, StringRef PrimaryFile, std::string &Error) {
704-
return getInvocation(OrigArgs, PrimaryFile, llvm::vfs::getRealFileSystem(),
705-
Error);
703+
SwiftInvocationRef
704+
SwiftASTManager::getTypecheckInvocation(ArrayRef<const char *> OrigArgs,
705+
StringRef PrimaryFile,
706+
std::string &Error) {
707+
return getTypecheckInvocation(OrigArgs, PrimaryFile,
708+
llvm::vfs::getRealFileSystem(), Error);
706709
}
707710

708-
SwiftInvocationRef SwiftASTManager::getInvocation(
711+
SwiftInvocationRef SwiftASTManager::getTypecheckInvocation(
709712
ArrayRef<const char *> OrigArgs, StringRef PrimaryFile,
710713
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
711714
std::string &Error) {
@@ -716,8 +719,9 @@ SwiftInvocationRef SwiftASTManager::getInvocation(
716719
Diags.addConsumer(CollectDiagConsumer);
717720

718721
CompilerInvocation CompInvok;
719-
if (initCompilerInvocation(CompInvok, OrigArgs, Diags, PrimaryFile,
720-
FileSystem, Error)) {
722+
if (initCompilerInvocation(CompInvok, OrigArgs,
723+
FrontendOptions::ActionType::Typecheck, Diags,
724+
PrimaryFile, FileSystem, Error)) {
721725
// We create a traced operation here to represent the failure to parse
722726
// arguments since we cannot reach `createAST` where that would normally
723727
// happen.

tools/SourceKit/lib/SwiftLang/SwiftASTManager.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "SourceKit/Core/LLVM.h"
7979
#include "SourceKit/Support/CancellationToken.h"
8080
#include "SwiftInvocation.h"
81+
#include "swift/Frontend/FrontendOptions.h"
8182
#include "llvm/ADT/ArrayRef.h"
8283
#include "llvm/ADT/IntrusiveRefCntPtr.h"
8384
#include "llvm/ADT/StringRef.h"
@@ -238,12 +239,13 @@ class SwiftASTManager : public std::enable_shared_from_this<SwiftASTManager> {
238239
StringRef DiagnosticDocumentationPath);
239240
~SwiftASTManager();
240241

241-
SwiftInvocationRef getInvocation(
242-
ArrayRef<const char *> Args, StringRef PrimaryFile, std::string &Error);
242+
SwiftInvocationRef getTypecheckInvocation(ArrayRef<const char *> Args,
243+
StringRef PrimaryFile,
244+
std::string &Error);
243245

244246
/// Same as the previous `getInvocation`, but allows the caller to specify a
245247
/// custom `FileSystem` to be used throughout the invocation.
246-
SwiftInvocationRef getInvocation(
248+
SwiftInvocationRef getTypecheckInvocation(
247249
ArrayRef<const char *> Args, StringRef PrimaryFile,
248250
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
249251
std::string &Error);
@@ -262,29 +264,33 @@ class SwiftASTManager : public std::enable_shared_from_this<SwiftASTManager> {
262264
std::unique_ptr<llvm::MemoryBuffer> getMemoryBuffer(StringRef Filename,
263265
std::string &Error);
264266

265-
bool initCompilerInvocation(
266-
swift::CompilerInvocation &Invocation, ArrayRef<const char *> Args,
267-
swift::DiagnosticEngine &Diags, StringRef PrimaryFile, std::string &Error);
267+
bool initCompilerInvocation(swift::CompilerInvocation &Invocation,
268+
ArrayRef<const char *> Args,
269+
swift::FrontendOptions::ActionType Action,
270+
swift::DiagnosticEngine &Diags,
271+
StringRef PrimaryFile, std::string &Error);
268272

269273
/// Same as the previous `initCompilerInvocation`, but allows the caller to
270274
/// specify a custom `FileSystem` to be used throughout the invocation.
271275
bool initCompilerInvocation(
272276
swift::CompilerInvocation &Invocation, ArrayRef<const char *> Args,
273-
swift::DiagnosticEngine &Diags, StringRef PrimaryFile,
277+
swift::FrontendOptions::ActionType Action, swift::DiagnosticEngine &Diags,
278+
StringRef PrimaryFile,
274279
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
275280
std::string &Error);
276281

277282
bool initCompilerInvocation(swift::CompilerInvocation &CompInvok,
278283
ArrayRef<const char *> OrigArgs,
279-
StringRef PrimaryFile,
280-
std::string &Error);
284+
swift::FrontendOptions::ActionType Action,
285+
StringRef PrimaryFile, std::string &Error);
281286

282287
/// Initializes \p Invocation as if for typechecking, but with no inputs.
283288
///
284289
/// If \p AllowInputs is false, it is an error for \p OrigArgs to contain any
285290
/// input files.
286291
bool initCompilerInvocationNoInputs(swift::CompilerInvocation &Invocation,
287292
ArrayRef<const char *> OrigArgs,
293+
swift::FrontendOptions::ActionType Action,
288294
swift::DiagnosticEngine &Diags,
289295
std::string &Error,
290296
bool AllowInputs = true);

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,8 @@ void SwiftLangSupport::findLocalRenameRanges(
14171417
ArrayRef<const char *> Args, SourceKitCancellationToken CancellationToken,
14181418
CategorizedRenameRangesReceiver Receiver) {
14191419
std::string Error;
1420-
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, Filename, Error);
1420+
SwiftInvocationRef Invok =
1421+
ASTMgr->getTypecheckInvocation(Args, Filename, Error);
14211422
if (!Invok) {
14221423
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
14231424
Receiver(RequestResult<ArrayRef<CategorizedRenameRanges>>::fromError(Error));
@@ -1465,7 +1466,8 @@ SourceFile *SwiftLangSupport::getSyntacticSourceFile(
14651466
CompilerInvocation Invocation;
14661467

14671468
bool Failed = getASTManager()->initCompilerInvocationNoInputs(
1468-
Invocation, Args, ParseCI.getDiags(), Error);
1469+
Invocation, Args, FrontendOptions::ActionType::Parse, ParseCI.getDiags(),
1470+
Error);
14691471
if (Failed) {
14701472
Error = "Compiler invocation init failed";
14711473
return nullptr;
@@ -1518,7 +1520,8 @@ void SwiftLangSupport::getDocInfo(llvm::MemoryBuffer *InputBuf,
15181520
CompilerInvocation Invocation;
15191521
std::string Error;
15201522
bool Failed = getASTManager()->initCompilerInvocationNoInputs(
1521-
Invocation, Args, CI.getDiags(), Error, /*AllowInputs=*/false);
1523+
Invocation, Args, FrontendOptions::ActionType::Typecheck, CI.getDiags(),
1524+
Error, /*AllowInputs=*/false);
15221525

15231526
if (Failed) {
15241527
Consumer.failed(Error);
@@ -1551,8 +1554,9 @@ findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
15511554
PrintingDiagnosticConsumer PrintDiags;
15521555
CI.addDiagnosticConsumer(&PrintDiags);
15531556
std::string Error;
1554-
if (getASTManager()->initCompilerInvocationNoInputs(Invocation, Args,
1555-
CI.getDiags(), Error)) {
1557+
if (getASTManager()->initCompilerInvocationNoInputs(
1558+
Invocation, Args, FrontendOptions::ActionType::Typecheck,
1559+
CI.getDiags(), Error)) {
15561560
Receiver(RequestResult<ArrayRef<StringRef>>::fromError(Error));
15571561
return;
15581562
}
@@ -1563,12 +1567,6 @@ findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
15631567

15641568
// Load standard library so that Clang importer can use it.
15651569
ASTContext &Ctx = CI.getASTContext();
1566-
auto *Stdlib = Ctx.getModuleByIdentifier(Ctx.StdlibModuleName);
1567-
if (!Stdlib) {
1568-
Error = "Cannot load stdlib.";
1569-
Receiver(RequestResult<ArrayRef<StringRef>>::fromError(Error));
1570-
return;
1571-
}
15721570
auto *M = Ctx.getModuleByName(ModuleName);
15731571
if (!M) {
15741572
Error = "Cannot find the module.";

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ class SwiftDocumentSemanticInfo :
654654
void setCompilerArgs(ArrayRef<const char *> Args) {
655655
if (auto ASTMgr = this->ASTMgr.lock()) {
656656
InvokRef =
657-
ASTMgr->getInvocation(Args, Filename, CompilerArgsError);
657+
ASTMgr->getTypecheckInvocation(Args, Filename, CompilerArgsError);
658658
}
659659
}
660660

@@ -2015,8 +2015,8 @@ void SwiftEditorDocument::resetSyntaxInfo(ImmutableTextSnapshotRef Snapshot,
20152015
Args.push_back("-");
20162016
std::string Error;
20172017
// Ignore possible error(s)
2018-
Lang.getASTManager()->
2019-
initCompilerInvocation(CompInv, Args, StringRef(), Error);
2018+
Lang.getASTManager()->initCompilerInvocation(
2019+
CompInv, Args, FrontendOptions::ActionType::Parse, StringRef(), Error);
20202020
}
20212021
CompInv.getLangOptions().BuildSyntaxTree = BuildSyntaxTree;
20222022
CompInv.setMainFileSyntaxParsingCache(SyntaxCache);

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,9 @@ void SwiftLangSupport::editorOpenTypeInterface(EditorConsumer &Consumer,
634634

635635
CompilerInvocation Invocation;
636636
std::string Error;
637-
if (getASTManager()->initCompilerInvocation(Invocation, Args, CI.getDiags(),
638-
StringRef(), Error)) {
637+
if (getASTManager()->initCompilerInvocation(
638+
Invocation, Args, FrontendOptions::ActionType::Typecheck,
639+
CI.getDiags(), StringRef(), Error)) {
639640
Consumer.handleRequestError(Error.c_str());
640641
return;
641642
}
@@ -674,8 +675,9 @@ void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
674675

675676
CompilerInvocation Invocation;
676677
std::string Error;
677-
if (getASTManager()->initCompilerInvocationNoInputs(Invocation, Args,
678-
CI.getDiags(), Error)) {
678+
if (getASTManager()->initCompilerInvocationNoInputs(
679+
Invocation, Args, FrontendOptions::ActionType::Typecheck,
680+
CI.getDiags(), Error)) {
679681
Consumer.handleRequestError(Error.c_str());
680682
return;
681683
}
@@ -740,7 +742,7 @@ void SwiftLangSupport::editorOpenSwiftSourceInterface(
740742
SourceKitCancellationToken CancellationToken,
741743
std::shared_ptr<EditorConsumer> Consumer) {
742744
std::string Error;
743-
auto Invocation = ASTMgr->getInvocation(Args, SourceName, Error);
745+
auto Invocation = ASTMgr->getTypecheckInvocation(Args, SourceName, Error);
744746
if (!Invocation) {
745747
Consumer->handleRequestError(Error.c_str());
746748
return;
@@ -769,8 +771,9 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
769771
std::string Error;
770772

771773
ArrayRef<const char *> SwiftArgs = UsingSwiftArgs ? Args : llvm::None;
772-
if (getASTManager()->initCompilerInvocationNoInputs(Invocation, SwiftArgs,
773-
CI.getDiags(), Error)) {
774+
if (getASTManager()->initCompilerInvocationNoInputs(
775+
Invocation, SwiftArgs, FrontendOptions::ActionType::Typecheck,
776+
CI.getDiags(), Error)) {
774777
Consumer.handleRequestError(Error.c_str());
775778
return;
776779
}
@@ -819,8 +822,9 @@ void SwiftLangSupport::findInterfaceDocument(StringRef ModuleName,
819822

820823
CompilerInvocation Invocation;
821824
std::string Error;
822-
if (getASTManager()->initCompilerInvocation(Invocation, Args, CI.getDiags(),
823-
StringRef(), Error)) {
825+
if (getASTManager()->initCompilerInvocation(
826+
Invocation, Args, FrontendOptions::ActionType::Typecheck,
827+
CI.getDiags(), StringRef(), Error)) {
824828
return Receiver(RequestResult<InterfaceDocInfo>::fromError(Error));
825829
}
826830

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ void SwiftLangSupport::indexSource(StringRef InputFile,
303303
bool Failed = true;
304304
if (IsModuleIndexing) {
305305
Failed = getASTManager()->initCompilerInvocationNoInputs(
306-
Invocation, Args, CI.getDiags(), Error);
306+
Invocation, Args, FrontendOptions::ActionType::Typecheck, CI.getDiags(),
307+
Error);
307308
} else {
308309
Failed = getASTManager()->initCompilerInvocation(
309-
Invocation, Args, CI.getDiags(), InputFile, Error);
310+
Invocation, Args, FrontendOptions::ActionType::Typecheck, CI.getDiags(),
311+
InputFile, Error);
310312
}
311313
if (Failed) {
312314
IdxConsumer.failed(Error);

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,8 @@ void SwiftLangSupport::performWithParamsToCompletionLikeOperation(
10431043
CompilerInvocation Invocation;
10441044
std::string CompilerInvocationError;
10451045
bool CreatingInvocationFailed = getASTManager()->initCompilerInvocation(
1046-
Invocation, Args, Diags, newBuffer->getBufferIdentifier(), FileSystem,
1047-
CompilerInvocationError);
1046+
Invocation, Args, FrontendOptions::ActionType::Typecheck, Diags,
1047+
newBuffer->getBufferIdentifier(), FileSystem, CompilerInvocationError);
10481048
if (CreatingInvocationFailed) {
10491049
PerformOperation(CancellableResult<CompletionLikeOperationParams>::failure(
10501050
CompilerInvocationError));

0 commit comments

Comments
 (0)