Skip to content

Commit 305cbfe

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-next
2 parents b84e995 + 98522b0 commit 305cbfe

File tree

10 files changed

+52
-56
lines changed

10 files changed

+52
-56
lines changed

include/swift/AST/FileUnit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ class LoadedFile : public FileUnit {
384384

385385
virtual bool isSystemModule() const { return false; }
386386

387+
/// Checks whether an error was encountered while loading the file.
388+
virtual bool hadLoadError() const { return false; }
389+
387390
/// Retrieve the set of generic signatures stored within this module.
388391
///
389392
/// \returns \c true if this module file supports retrieving all of the

include/swift/AST/Module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
363363
ArrayRef<ImplicitImport> getImplicitImports() const;
364364

365365
ArrayRef<FileUnit *> getFiles() {
366+
assert(!Files.empty() || failedToLoad());
366367
return Files;
367368
}
368369
ArrayRef<const FileUnit *> getFiles() const {
@@ -371,7 +372,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
371372

372373
bool isClangModule() const;
373374
void addFile(FileUnit &newFile);
374-
void removeFile(FileUnit &existingFile);
375375

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

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,13 @@ class SerializedModuleLoaderBase : public ModuleLoader {
149149
///
150150
/// If the AST cannot be loaded and \p diagLoc is present, a diagnostic is
151151
/// printed. (Note that \p diagLoc is allowed to be invalid.)
152-
FileUnit *loadAST(ModuleDecl &M, Optional<SourceLoc> diagLoc,
153-
StringRef moduleInterfacePath,
154-
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
155-
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
156-
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
157-
bool isFramework, bool treatAsPartialModule);
152+
FileUnit *
153+
loadAST(ModuleDecl &M, Optional<SourceLoc> diagLoc,
154+
StringRef moduleInterfacePath,
155+
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
156+
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
157+
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
158+
bool isFramework);
158159

159160
/// Check whether the module with a given name can be imported without
160161
/// importing it.
@@ -321,6 +322,8 @@ class SerializedASTFile final : public LoadedFile {
321322
/// file.
322323
const version::Version &getLanguageVersionBuiltWith() const;
323324

325+
virtual bool hadLoadError() const override;
326+
324327
virtual bool isSystemModule() const override;
325328

326329
virtual void lookupValue(DeclName name, NLKind lookupKind,

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,11 +1515,6 @@ void ASTContext::verifyAllLoadedModules() const {
15151515
FrontendStatsTracer tracer(Stats, "verify-all-loaded-modules");
15161516
for (auto &loader : getImpl().ModuleLoaders)
15171517
loader->verifyAllModules();
1518-
1519-
for (auto &topLevelModulePair : LoadedModules) {
1520-
ModuleDecl *M = topLevelModulePair.second;
1521-
assert(!M->getFiles().empty() || M->failedToLoad());
1522-
}
15231518
#endif
15241519
}
15251520

lib/AST/Module.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ bool ModuleDecl::isClangModule() const {
498498
}
499499

500500
void ModuleDecl::addFile(FileUnit &newFile) {
501+
// If this is a LoadedFile, make sure it loaded without error.
502+
assert(!(isa<LoadedFile>(newFile) &&
503+
cast<LoadedFile>(newFile).hadLoadError()));
504+
501505
// Require Main and REPL files to be the first file added.
502506
assert(Files.empty() ||
503507
!isa<SourceFile>(newFile) ||
@@ -507,19 +511,6 @@ void ModuleDecl::addFile(FileUnit &newFile) {
507511
clearLookupCache();
508512
}
509513

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-
523514
#define FORWARD(name, args) \
524515
for (const FileUnit *file : getFiles()) \
525516
file->name args;

lib/Frontend/Frontend.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,11 +845,16 @@ bool CompilerInstance::loadPartialModulesAndImplicitImports() {
845845
// Parse all the partial modules first.
846846
for (auto &PM : PartialModules) {
847847
assert(PM.ModuleBuffer);
848-
if (!SML->loadAST(*MainModule, SourceLoc(), /*moduleInterfacePath*/"",
849-
std::move(PM.ModuleBuffer), std::move(PM.ModuleDocBuffer),
850-
std::move(PM.ModuleSourceInfoBuffer), /*isFramework*/false,
851-
/*treatAsPartialModule*/true))
848+
auto *file =
849+
SML->loadAST(*MainModule, SourceLoc(), /*moduleInterfacePath*/ "",
850+
std::move(PM.ModuleBuffer), std::move(PM.ModuleDocBuffer),
851+
std::move(PM.ModuleSourceInfoBuffer),
852+
/*isFramework*/ false);
853+
if (file) {
854+
MainModule->addFile(*file);
855+
} else {
852856
hadLoadError = true;
857+
}
853858
}
854859
return hadLoadError;
855860
}

lib/Serialization/ModuleFile.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,9 +1977,7 @@ ModuleFile::ModuleFile(
19771977
}
19781978
}
19791979

1980-
Status ModuleFile::associateWithFileContext(FileUnit *file,
1981-
SourceLoc diagLoc,
1982-
bool treatAsPartialModule) {
1980+
Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc) {
19831981
PrettyStackTraceModuleFile stackEntry(*this);
19841982

19851983
assert(!hasError() && "error already detected; should not call this");
@@ -2033,8 +2031,12 @@ Status ModuleFile::associateWithFileContext(FileUnit *file,
20332031
continue;
20342032
}
20352033

2034+
// If this module file is being installed into the main module, it's treated
2035+
// as a partial module.
2036+
auto isPartialModule = M->isMainModule();
2037+
20362038
if (dependency.isImplementationOnly() &&
2037-
!(treatAsPartialModule || ctx.LangOpts.DebuggerSupport)) {
2039+
!(isPartialModule || ctx.LangOpts.DebuggerSupport)) {
20382040
// When building normally (and not merging partial modules), we don't
20392041
// want to bring in the implementation-only module, because that might
20402042
// change the set of visible declarations. However, when debugging we

lib/Serialization/ModuleFile.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -705,14 +705,10 @@ class ModuleFile
705705
/// This does not include diagnostics about \e this file failing to load,
706706
/// but rather other things that might be imported as part of bringing the
707707
/// file into the AST.
708-
/// \param treatAsPartialModule If true, processes implementation-only
709-
/// information instead of assuming the client won't need it and shouldn't
710-
/// see it.
711708
///
712709
/// \returns any error that occurred during association, such as being
713710
/// compiled for a different OS.
714-
Status associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
715-
bool treatAsPartialModule);
711+
Status associateWithFileContext(FileUnit *file, SourceLoc diagLoc);
716712

717713
/// Transfers ownership of a buffer that might contain source code where
718714
/// other parts of the compiler could have emitted diagnostics, to keep them

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
652652
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
653653
std::unique_ptr<llvm::MemoryBuffer> moduleDocInputBuffer,
654654
std::unique_ptr<llvm::MemoryBuffer> moduleSourceInfoInputBuffer,
655-
bool isFramework, bool treatAsPartialModule) {
655+
bool isFramework) {
656656
assert(moduleInputBuffer);
657657

658658
StringRef moduleBufferID = moduleInputBuffer->getBufferIdentifier();
@@ -681,16 +681,14 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
681681

682682
// We've loaded the file. Now try to bring it into the AST.
683683
auto fileUnit = new (Ctx) SerializedASTFile(M, *loadedModuleFile);
684-
M.addFile(*fileUnit);
685684
if (extendedInfo.isTestable())
686685
M.setTestingEnabled();
687686
if (extendedInfo.arePrivateImportsEnabled())
688687
M.setPrivateImportsEnabled();
689688

690689
auto diagLocOrInvalid = diagLoc.getValueOr(SourceLoc());
691690
loadInfo.status =
692-
loadedModuleFile->associateWithFileContext(fileUnit, diagLocOrInvalid,
693-
treatAsPartialModule);
691+
loadedModuleFile->associateWithFileContext(fileUnit, diagLocOrInvalid);
694692

695693
// FIXME: This seems wrong. Overlay for system Clang module doesn't
696694
// necessarily mean it's "system" module. User can make their own overlay
@@ -707,8 +705,6 @@ FileUnit *SerializedModuleLoaderBase::loadAST(
707705
findOverlayFiles(diagLoc.getValueOr(SourceLoc()), &M, fileUnit);
708706
return fileUnit;
709707
}
710-
711-
M.removeFile(*fileUnit);
712708
}
713709

714710
// From here on is the failure path.
@@ -962,13 +958,15 @@ SerializedModuleLoaderBase::loadModule(SourceLoc importLoc,
962958
StringRef moduleInterfacePathStr =
963959
Ctx.AllocateCopy(moduleInterfacePath.str());
964960

965-
if (!loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
966-
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
967-
std::move(moduleSourceInfoInputBuffer),
968-
isFramework, /*treatAsPartialModule*/false)) {
961+
auto *file =
962+
loadAST(*M, moduleID.Loc, moduleInterfacePathStr,
963+
std::move(moduleInputBuffer), std::move(moduleDocInputBuffer),
964+
std::move(moduleSourceInfoInputBuffer), isFramework);
965+
if (file) {
966+
M->addFile(*file);
967+
} else {
969968
M->setFailedToLoad();
970969
}
971-
972970
return M;
973971
}
974972

@@ -991,7 +989,6 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
991989
return nullptr;
992990

993991
bool isFramework = false;
994-
bool treatAsPartialModule = false;
995992
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer;
996993
moduleInputBuffer = std::move(bufIter->second);
997994
MemoryBuffers.erase(bufIter);
@@ -1000,12 +997,12 @@ MemoryBufferSerializedModuleLoader::loadModule(SourceLoc importLoc,
1000997
auto *M = ModuleDecl::create(moduleID.Item, Ctx);
1001998
SWIFT_DEFER { M->setHasResolvedImports(); };
1002999

1003-
if (!loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
1004-
std::move(moduleInputBuffer), {}, {},
1005-
isFramework, treatAsPartialModule)) {
1000+
auto *file = loadAST(*M, moduleID.Loc, /*moduleInterfacePath*/ "",
1001+
std::move(moduleInputBuffer), {}, {}, isFramework);
1002+
if (!file)
10061003
return nullptr;
1007-
}
10081004

1005+
M->addFile(*file);
10091006
Ctx.LoadedModules[moduleID.Item] = M;
10101007
return M;
10111008
}
@@ -1107,6 +1104,10 @@ bool SerializedASTFile::isSIB() const {
11071104
return File.IsSIB;
11081105
}
11091106

1107+
bool SerializedASTFile::hadLoadError() const {
1108+
return File.hasError();
1109+
}
1110+
11101111
bool SerializedASTFile::isSystemModule() const {
11111112
if (auto Mod = File.getUnderlyingModule()) {
11121113
return Mod->isSystemModule();

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ static void indexModule(llvm::MemoryBuffer *Input,
186186
// correct filename here.
187187
auto FUnit = Loader->loadAST(*Mod, None, /*moduleInterfacePath*/"",
188188
std::move(Buf), nullptr, nullptr,
189-
/*isFramework*/false,
190-
/*treatAsPartialModule*/false);
189+
/*isFramework*/false);
191190

192191
// FIXME: Not knowing what went wrong is pretty bad. loadModule() should be
193192
// more modular, rather than emitting diagnostics itself.
@@ -196,6 +195,7 @@ static void indexModule(llvm::MemoryBuffer *Input,
196195
return;
197196
}
198197

198+
Mod->addFile(*FUnit);
199199
Mod->setHasResolvedImports();
200200
}
201201

0 commit comments

Comments
 (0)