@@ -92,11 +92,11 @@ class ClangModulesDeclVendorImpl : public ClangModulesDeclVendor {
9292
9393 ~ClangModulesDeclVendorImpl () override = default ;
9494
95- bool AddModule (const SourceModule &module , ModuleVector *exported_modules ,
96- Stream &error_stream ) override ;
95+ llvm::Error AddModule (const SourceModule &module ,
96+ ModuleVector *exported_modules ) override ;
9797
98- bool AddModulesForCompileUnit (CompileUnit &cu, ModuleVector &exported_modules ,
99- Stream &error_stream ) override ;
98+ llvm::Error AddModulesForCompileUnit (CompileUnit &cu,
99+ ModuleVector &exported_modules ) override ;
100100
101101 uint32_t FindDecls (ConstString name, bool append, uint32_t max_matches,
102102 std::vector<CompilerDecl> &decls) override ;
@@ -273,16 +273,14 @@ void ClangModulesDeclVendorImpl::ReportModuleExports(
273273 exports.push_back (module );
274274}
275275
276- bool ClangModulesDeclVendorImpl::AddModule ( const SourceModule & module ,
277- ModuleVector *exported_modules ,
278- Stream &error_stream ) {
276+ llvm::Error
277+ ClangModulesDeclVendorImpl::AddModule ( const SourceModule & module ,
278+ ModuleVector *exported_modules ) {
279279 // Fail early.
280280
281- if (m_compiler_instance->hadModuleLoaderFatalFailure ()) {
282- error_stream.PutCString (" error: Couldn't load a module because the module "
283- " loader is in a fatal state.\n " );
284- return false ;
285- }
281+ if (m_compiler_instance->hadModuleLoaderFatalFailure ())
282+ return llvm::createStringError (
283+ " couldn't load a module because the module loader is in a fatal state" );
286284
287285 // Check if we've already imported this module.
288286
@@ -297,7 +295,7 @@ bool ClangModulesDeclVendorImpl::AddModule(const SourceModule &module,
297295 if (mi != m_imported_modules.end ()) {
298296 if (exported_modules)
299297 ReportModuleExports (*exported_modules, mi->second );
300- return true ;
298+ return llvm::Error::success () ;
301299 }
302300 }
303301
@@ -315,30 +313,30 @@ bool ClangModulesDeclVendorImpl::AddModule(const SourceModule &module,
315313 std::equal (sysroot_begin, sysroot_end, path_begin);
316314 // No need to inject search paths to modules in the sysroot.
317315 if (!is_system_module) {
318- auto error = [&]() {
319- error_stream.Printf (" error: No module map file in %s\n " ,
320- module .search_path .AsCString ());
321- return false ;
322- };
323-
324316 bool is_system = true ;
325317 bool is_framework = false ;
326318 auto dir = HS.getFileMgr ().getOptionalDirectoryRef (
327319 module .search_path .GetStringRef ());
328320 if (!dir)
329- return error ();
321+ return llvm::createStringError (
322+ " couldn't find module search path directory %s" ,
323+ module .search_path .GetCString ());
324+
330325 auto file = HS.lookupModuleMapFile (*dir, is_framework);
331326 if (!file)
332- return error ();
327+ return llvm::createStringError (" couldn't find modulemap file in %s" ,
328+ module .search_path .GetCString ());
329+
333330 if (HS.loadModuleMapFile (*file, is_system))
334- return error ();
331+ return llvm::createStringError (
332+ " failed to parse and load modulemap file in %s" ,
333+ module .search_path .GetCString ());
335334 }
336335 }
337- if (!HS.lookupModule (module .path .front ().GetStringRef ())) {
338- error_stream.Printf (" error: Header search couldn't locate module '%s'\n " ,
339- module .path .front ().AsCString ());
340- return false ;
341- }
336+
337+ if (!HS.lookupModule (module .path .front ().GetStringRef ()))
338+ return llvm::createStringError (" header search couldn't locate module '%s'" ,
339+ module .path .front ().AsCString ());
342340
343341 llvm::SmallVector<clang::IdentifierLoc, 4 > clang_path;
344342
@@ -364,22 +362,29 @@ bool ClangModulesDeclVendorImpl::AddModule(const SourceModule &module,
364362 clang::Module *top_level_module = DoGetModule (clang_path.front (), false );
365363
366364 if (!top_level_module) {
365+ lldb_private::StreamString error_stream;
367366 diagnostic_consumer->DumpDiagnostics (error_stream);
368- error_stream.Printf (" error: Couldn't load top-level module %s\n " ,
369- module .path .front ().AsCString ());
370- return false ;
367+
368+ return llvm::createStringError (llvm::formatv (
369+ " couldn't load top-level module {0}:\n {1}" ,
370+ module .path .front ().GetStringRef (), error_stream.GetString ()));
371371 }
372372
373373 clang::Module *submodule = top_level_module;
374374
375375 for (auto &component : llvm::ArrayRef<ConstString>(module .path ).drop_front ()) {
376- submodule = submodule->findSubmodule (component.GetStringRef ());
377- if (!submodule) {
376+ clang::Module *found = submodule->findSubmodule (component.GetStringRef ());
377+ if (!found) {
378+ lldb_private::StreamString error_stream;
378379 diagnostic_consumer->DumpDiagnostics (error_stream);
379- error_stream.Printf (" error: Couldn't load submodule %s\n " ,
380- component.GetCString ());
381- return false ;
380+
381+ return llvm::createStringError (llvm::formatv (
382+ " couldn't load submodule '{0}' of module '{1}':\n {2}" ,
383+ component.GetStringRef (), submodule->getFullModuleName (),
384+ error_stream.GetString ()));
382385 }
386+
387+ submodule = found;
383388 }
384389
385390 // If we didn't make the submodule visible here, Clang wouldn't allow LLDB to
@@ -399,10 +404,12 @@ bool ClangModulesDeclVendorImpl::AddModule(const SourceModule &module,
399404
400405 m_enabled = true ;
401406
402- return true ;
407+ return llvm::Error::success () ;
403408 }
404409
405- return false ;
410+ return llvm::createStringError (
411+ llvm::formatv (" unknown error while loading module {0}\n " ,
412+ module .path .front ().GetStringRef ()));
406413}
407414
408415bool ClangModulesDeclVendor::LanguageSupportsClangModules (
@@ -424,15 +431,18 @@ bool ClangModulesDeclVendor::LanguageSupportsClangModules(
424431 }
425432}
426433
427- bool ClangModulesDeclVendorImpl::AddModulesForCompileUnit (
428- CompileUnit &cu, ClangModulesDeclVendor::ModuleVector &exported_modules,
429- Stream &error_stream) {
430- if (LanguageSupportsClangModules (cu.GetLanguage ())) {
431- for (auto &imported_module : cu.GetImportedModules ())
432- if (!AddModule (imported_module, &exported_modules, error_stream))
433- return false ;
434- }
435- return true ;
434+ llvm::Error ClangModulesDeclVendorImpl::AddModulesForCompileUnit (
435+ CompileUnit &cu, ClangModulesDeclVendor::ModuleVector &exported_modules) {
436+ if (!LanguageSupportsClangModules (cu.GetLanguage ()))
437+ return llvm::Error::success ();
438+
439+ for (auto &imported_module : cu.GetImportedModules ())
440+ // TODO: don't short-circuit. Continue loading modules even if one of them
441+ // fails. Concatenate all the errors.
442+ if (auto err = AddModule (imported_module, &exported_modules))
443+ return err;
444+
445+ return llvm::Error::success ();
436446}
437447
438448// ClangImporter::lookupValue
0 commit comments