Skip to content

Commit 1ed8106

Browse files
committed
[Frontend] Remove performParseOnly
Most clients were only using it to populate the main module with files, which is now done by `getMainModule`. Instead, they can now just rely on parsing happening lazily.
1 parent 7824dc1 commit 1ed8106

File tree

11 files changed

+34
-42
lines changed

11 files changed

+34
-42
lines changed

include/swift/AST/SourceFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class SourceFile final : public FileUnit {
490490
}
491491

492492
SWIFT_DEBUG_DUMP;
493-
void dump(raw_ostream &os) const;
493+
void dump(raw_ostream &os, bool parseIfNeeded = false) const;
494494

495495
/// Pretty-print the contents of this source file.
496496
///

include/swift/Frontend/Frontend.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,6 @@ class CompilerInstance {
616616
/// Parses and type-checks all input files.
617617
void performSema();
618618

619-
/// Parses the input file but does no type-checking or module imports.
620-
void performParseOnly();
621-
622619
/// Parses and performs import resolution on all input files.
623620
///
624621
/// This is similar to a parse-only invocation, but module imports will also

lib/AST/ASTDumper.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,13 @@ void SourceFile::dump() const {
14201420
dump(llvm::errs());
14211421
}
14221422

1423-
void SourceFile::dump(llvm::raw_ostream &OS) const {
1423+
void SourceFile::dump(llvm::raw_ostream &OS, bool parseIfNeeded) const {
1424+
// If we're allowed to parse the SourceFile, do so now. We need to force the
1425+
// parsing request as by default the dumping logic tries not to kick any
1426+
// requests.
1427+
if (parseIfNeeded)
1428+
(void)getTopLevelDecls();
1429+
14241430
PrintDecl(OS).visitSourceFile(*this);
14251431
llvm::errs() << '\n';
14261432
}

lib/Frontend/Frontend.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -776,16 +776,6 @@ void CompilerInstance::setMainModule(ModuleDecl *newMod) {
776776
Context->LoadedModules[newMod->getName()] = newMod;
777777
}
778778

779-
void CompilerInstance::performParseOnly() {
780-
const InputFileKind Kind = Invocation.getInputKind();
781-
assert((Kind == InputFileKind::Swift || Kind == InputFileKind::SwiftLibrary ||
782-
Kind == InputFileKind::SwiftModuleInterface) &&
783-
"only supports parsing .swift files");
784-
(void)Kind;
785-
786-
performSemaUpTo(SourceFile::Unprocessed);
787-
}
788-
789779
void CompilerInstance::performParseAndResolveImportsOnly() {
790780
performSemaUpTo(SourceFile::ImportsResolved);
791781
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,6 @@ getFileOutputStream(StringRef OutputFilename, ASTContext &Ctx) {
510510

511511
/// Writes the Syntax tree to the given file
512512
static bool emitSyntax(SourceFile *SF, StringRef OutputFilename) {
513-
auto bufferID = SF->getBufferID();
514-
assert(bufferID && "frontend should have a buffer ID "
515-
"for the main source file");
516-
(void)bufferID;
517-
518513
auto os = getFileOutputStream(OutputFilename, SF->getASTContext());
519514
if (!os) return true;
520515

@@ -884,12 +879,13 @@ static void dumpAST(CompilerInstance &Instance) {
884879
auto PSPs = Instance.getPrimarySpecificPathsForSourceFile(*sourceFile);
885880
auto OutputFilename = PSPs.OutputFilename;
886881
auto OS = getFileOutputStream(OutputFilename, Instance.getASTContext());
887-
sourceFile->dump(*OS);
882+
sourceFile->dump(*OS, /*parseIfNeeded*/ true);
888883
}
889884
} else {
890885
// Some invocations don't have primary files. In that case, we default to
891886
// looking for the main file and dumping it to `stdout`.
892-
getPrimaryOrMainSourceFile(Instance)->dump(llvm::outs());
887+
auto *SF = getPrimaryOrMainSourceFile(Instance);
888+
SF->dump(llvm::outs(), /*parseIfNeeded*/ true);
893889
}
894890
}
895891

@@ -1292,16 +1288,30 @@ static bool performCompile(CompilerInstance &Instance,
12921288
};
12931289

12941290
if (FrontendOptions::shouldActionOnlyParse(Action)) {
1295-
Instance.performParseOnly();
1291+
// Parsing gets triggered lazily, but let's make sure we have the right
1292+
// input kind.
1293+
auto kind = Invocation.getInputKind();
1294+
assert((kind == InputFileKind::Swift ||
1295+
kind == InputFileKind::SwiftLibrary ||
1296+
kind == InputFileKind::SwiftModuleInterface) &&
1297+
"Only supports parsing .swift files");
1298+
(void)kind;
12961299
} else if (Action == FrontendOptions::ActionType::ResolveImports) {
12971300
Instance.performParseAndResolveImportsOnly();
12981301
} else {
12991302
Instance.performSema();
13001303
}
13011304

13021305
ASTContext &Context = Instance.getASTContext();
1303-
if (Action == FrontendOptions::ActionType::Parse)
1306+
if (Action == FrontendOptions::ActionType::Parse) {
1307+
// A -parse invocation only cares about the side effects of parsing, so
1308+
// force the parsing of all the source files.
1309+
for (auto *file : Instance.getMainModule()->getFiles()) {
1310+
if (auto *SF = dyn_cast<SourceFile>(file))
1311+
(void)SF->getTopLevelDecls();
1312+
}
13041313
return Context.hadError();
1314+
}
13051315

13061316
if (Action == FrontendOptions::ActionType::ScanDependencies) {
13071317
scanDependencies(Instance);

lib/IDE/Refactoring.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,6 @@ getNotableRegions(StringRef SourceText, unsigned NameOffset, StringRef Name,
11801180
if (Instance->setup(Invocation))
11811181
llvm_unreachable("Failed setup");
11821182

1183-
Instance->performParseOnly();
1184-
11851183
unsigned BufferId = Instance->getPrimarySourceFile()->getBufferID().getValue();
11861184
SourceManager &SM = Instance->getSourceMgr();
11871185
SourceLoc NameLoc = SM.getLocForOffset(BufferId, NameOffset);

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -814,10 +814,7 @@ static bool makeParserAST(CompilerInstance &CI, StringRef Text,
814814
Buf = llvm::MemoryBuffer::getMemBuffer(Text, "<module-interface>");
815815
Invocation.getFrontendOptions().InputsAndOutputs.addInput(
816816
InputFile(Buf.get()->getBufferIdentifier(), false, Buf.get()));
817-
if (CI.setup(Invocation))
818-
return true;
819-
CI.performParseOnly();
820-
return false;
817+
return CI.setup(Invocation);
821818
}
822819

823820
static void collectFuncEntities(std::vector<TextEntity> &Ents,
@@ -1407,7 +1404,6 @@ SourceFile *SwiftLangSupport::getSyntacticSourceFile(
14071404
Error = "Compiler invocation set up failed";
14081405
return nullptr;
14091406
}
1410-
ParseCI.performParseOnly();
14111407

14121408
SourceFile *SF = nullptr;
14131409
unsigned BufferID = ParseCI.getInputBufferIDs().back();

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,7 @@ static bool makeParserAST(CompilerInstance &CI, StringRef Text,
240240
Buf = llvm::MemoryBuffer::getMemBuffer(Text, "<module-interface>");
241241
Invocation.getFrontendOptions().InputsAndOutputs.addInput(
242242
InputFile(Buf.get()->getBufferIdentifier(), false, Buf.get()));
243-
if (CI.setup(Invocation))
244-
return true;
245-
CI.performParseOnly();
246-
return false;
243+
return CI.setup(Invocation);
247244
}
248245

249246
static void reportSyntacticAnnotations(CompilerInstance &CI,

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,9 +1727,7 @@ static int doPrintAST(const CompilerInvocation &InitInvok,
17271727
CI.getMainModule()->setDebugClient(DebuggerClient.get());
17281728
}
17291729

1730-
if (!RunTypeChecker)
1731-
CI.performParseOnly();
1732-
else
1730+
if (RunTypeChecker)
17331731
CI.performSema();
17341732

17351733
if (MangledNameToFind.empty()) {

tools/swift-refactor/swift-refactor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ int main(int argc, char *argv[]) {
268268
switch (options::Action) {
269269
case RefactoringKind::GlobalRename:
270270
case RefactoringKind::FindGlobalRenameRanges:
271-
CI.performParseOnly();
271+
// No type-checking required.
272272
break;
273273
default:
274274
CI.performSema();

0 commit comments

Comments
 (0)