Skip to content

Commit 6477cc6

Browse files
committed
[Serialization] Delay adding ModuleFiles until they're loaded
Rather than adding a ModuleFile to a parent module and then removing it afterwards if it fails to load, let's wait until we've loaded the file before deciding to add it to the parent module. This then allows us to get rid of `ModuleDecl::removeFile`. In addition, push down the calls to `addFile` into the callers of `loadAST` in preparation for `addFile` being replaced with a one-time-only call to a `setFiles` method.
1 parent f810cfc commit 6477cc6

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

include/swift/AST/Module.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
371371

372372
bool isClangModule() const;
373373
void addFile(FileUnit &newFile);
374-
void removeFile(FileUnit &existingFile);
375374

376375
/// Creates a map from \c #filePath strings to corresponding \c #file
377376
/// strings, diagnosing any conflicts.

lib/AST/Module.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -507,19 +507,6 @@ void ModuleDecl::addFile(FileUnit &newFile) {
507507
clearLookupCache();
508508
}
509509

510-
void ModuleDecl::removeFile(FileUnit &existingFile) {
511-
// Do a reverse search; usually the file to be deleted will be at the end.
512-
std::reverse_iterator<decltype(Files)::iterator> I(Files.end()),
513-
E(Files.begin());
514-
I = std::find(I, E, &existingFile);
515-
assert(I != E);
516-
517-
// Adjust for the std::reverse_iterator offset.
518-
++I;
519-
Files.erase(I.base());
520-
clearLookupCache();
521-
}
522-
523510
#define FORWARD(name, args) \
524511
for (const FileUnit *file : getFiles()) \
525512
file->name args;

lib/Frontend/Frontend.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,10 +837,16 @@ bool CompilerInstance::loadPartialModulesAndImplicitImports() {
837837
// Parse all the partial modules first.
838838
for (auto &PM : PartialModules) {
839839
assert(PM.ModuleBuffer);
840-
if (!SML->loadAST(*MainModule, SourceLoc(), /*moduleInterfacePath*/"",
841-
std::move(PM.ModuleBuffer), std::move(PM.ModuleDocBuffer),
842-
std::move(PM.ModuleSourceInfoBuffer), /*isFramework*/false))
840+
auto *file =
841+
SML->loadAST(*MainModule, SourceLoc(), /*moduleInterfacePath*/ "",
842+
std::move(PM.ModuleBuffer), std::move(PM.ModuleDocBuffer),
843+
std::move(PM.ModuleSourceInfoBuffer),
844+
/*isFramework*/ false);
845+
if (file) {
846+
MainModule->addFile(*file);
847+
} else {
843848
hadLoadError = true;
849+
}
844850
}
845851
return hadLoadError;
846852
}

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
680680

681681
// We've loaded the file. Now try to bring it into the AST.
682682
auto fileUnit = new (Ctx) SerializedASTFile(M, *loadedModuleFile);
683-
M.addFile(*fileUnit);
684683
if (extendedInfo.isTestable())
685684
M.setTestingEnabled();
686685
if (extendedInfo.arePrivateImportsEnabled())
@@ -705,8 +704,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
705704
findOverlayFiles(diagLoc.getValueOr(SourceLoc()), &M, fileUnit);
706705
return fileUnit;
707706
}
708-
709-
M.removeFile(*fileUnit);
710707
}
711708

712709
// From here on is the failure path.
@@ -960,12 +957,15 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
960957
StringRef moduleInterfacePathStr =
961958
Ctx.AllocateCopy(moduleInterfacePath.str());
962959

963-
if (!loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
964-
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
965-
std::move(moduleSourceInfoInputBuffer), isFramework)) {
960+
auto *file =
961+
loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
962+
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
963+
std::move(moduleSourceInfoInputBuffer), isFramework);
964+
if (file) {
965+
M->addFile(*file);
966+
} else {
966967
M->setFailedToLoad();
967968
}
968-
969969
return M;
970970
}
971971

@@ -996,11 +996,12 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
996996
auto *M = ModuleDecl::create(moduleID.Item, Ctx);
997997
SWIFT_DEFER { M->setHasResolvedImports(); };
998998

999-
if (!loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
1000-
std::move(moduleInputBuffer), {}, {}, isFramework)) {
999+
auto *file = loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
1000+
std::move(moduleInputBuffer), {}, {}, isFramework);
1001+
if (!file)
10011002
return nullptr;
1002-
}
10031003

1004+
M->addFile(*file);
10041005
Ctx.LoadedModules[moduleID.Item] = M;
10051006
return M;
10061007
}

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ static void indexModule(llvm::MemoryBuffer *Input,
195195
return;
196196
}
197197

198+
Mod->addFile(*FUnit);
198199
Mod->setHasResolvedImports();
199200
}
200201

0 commit comments

Comments
 (0)