Add error messages for Swift module map parser

Parser errors with large Swift module map files can be hard to diagnose.
Refactor the parser to return an llvm::Error so clearer diagnostics can
be passed to the user.
This commit is contained in:
Richard Howell
2025-09-05 13:26:07 -07:00
parent 61cb1a9126
commit 0b829bfab1
5 changed files with 114 additions and 25 deletions

View File

@@ -2258,13 +2258,14 @@ struct ExplicitSwiftModuleLoader::Implementation {
return;
}
auto hasError = parser.parseSwiftExplicitModuleMap(
auto error = parser.parseSwiftExplicitModuleMap(
(*fileBufOrErr)->getMemBufferRef(), ExplicitModuleMap,
ExplicitClangModuleMap, ModuleAliases);
if (hasError)
llvm::handleAllErrors(std::move(error), [this, &fileName](
const llvm::StringError &E) {
Ctx.Diags.diagnose(SourceLoc(), diag::explicit_swift_module_map_corrupted,
fileName);
fileName, E.getMessage());
});
// A single module map can define multiple modules; keep track of the ones
// we've seen so that we don't generate duplicate flags.
@@ -2536,13 +2537,14 @@ struct ExplicitCASModuleLoader::Implementation {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBufOrErr =
llvm::MemoryBuffer::getFile(ID);
auto hasError = parser.parseSwiftExplicitModuleMap(
auto error = parser.parseSwiftExplicitModuleMap(
buf->getMemBufferRef(), ExplicitModuleMap, ExplicitClangModuleMap,
ModuleAliases);
if (hasError)
llvm::handleAllErrors(std::move(error), [this,
&ID](const llvm::StringError &E) {
Ctx.Diags.diagnose(SourceLoc(), diag::explicit_swift_module_map_corrupted,
ID);
ID, E.getMessage());
});
std::set<std::string> moduleMapsSeen;
std::vector<std::string> &extraClangArgs = Ctx.ClangImporterOpts.ExtraArgs;