Skip to content

Don't create a Module SwiftASTContext when the stdlib is missing #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions lldb/source/Symbol/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1556,13 +1556,21 @@ static llvm::Optional<StringRef> GetDSYMBundle(Module &module) {
return dsym;
}

/// Detect whether a Swift module was "imported" by DWARFImporter.
/// All this *really* means is that it couldn't be loaded through any
/// other mechanism.
static bool IsDWARFImported(swift::ModuleDecl &module) {
return std::any_of(module.getFiles().begin(), module.getFiles().end(),
[](swift::FileUnit *file_unit) {
return (file_unit->getKind() ==
swift::FileUnitKind::DWARFModule);
});
}

lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
Module &module,
Target *target,
bool fallback) {
std::vector<std::string> module_search_paths;
std::vector<std::pair<std::string, bool>> framework_search_paths;

if (!SwiftASTContextSupportsLanguage(language))
return lldb::TypeSystemSP();

Expand All @@ -1576,16 +1584,33 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
module.GetDescription(ss, eDescriptionLevelBrief);
ss << '"' << ')';
}
std::vector<std::string> module_search_paths;
std::vector<std::pair<std::string, bool>> framework_search_paths;

LOG_PRINTF(LIBLLDB_LOG_TYPES, "(Module)");

auto logError = [&](const char *message) {
LOG_PRINTF(LIBLLDB_LOG_TYPES, "Failed to create module context - %s",
message);
};

ArchSpec arch = module.GetArchitecture();
if (!arch.IsValid()) {
logError("invalid module architecture");
return TypeSystemSP();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually write the default return value as return {}, since the concrete type doesn't matter that much to the reader.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really just stylistic preference :-)

}

ObjectFile *objfile = module.GetObjectFile();
if (!objfile)
return {};
if (!objfile) {
logError("no object file for module");
return TypeSystemSP();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want similar changes in CreateInstance(..., Target &) (the one that creates the expression context)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked through the other override and started re-arranging it, but decided to leave those changes out of this patch as they're entirely cosmetic. (I didn't find any missing checks.)


ArchSpec object_arch = objfile->GetArchitecture();
if (!object_arch.IsValid())
return {};
if (!object_arch.IsValid()) {
logError("invalid objfile architecture");
return TypeSystemSP();
}

lldb::CompUnitSP main_compile_unit_sp = module.GetCompileUnitAtIndex(0);

Expand Down Expand Up @@ -1641,9 +1666,6 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
swift_ast_sp->GetLanguageOptions().EnableAccessControl = false;
swift_ast_sp->GetLanguageOptions().EnableTargetOSChecking = false;

if (!arch.IsValid())
return TypeSystemSP();

swift_ast_sp->SetTriple(triple, &module);

bool set_triple = false;
Expand All @@ -1654,7 +1676,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
std::string target_triple;

if (sym_file) {
bool got_serialized_options;
bool got_serialized_options = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not so bad, I don't believe got_serialized_options is actually used, this is just some future-proofing. I can leave it out if you prefer.

llvm::SmallString<0> error;
llvm::raw_svector_ostream errs(error);
if (DeserializeAllCompilerFlags(*swift_ast_sp, module, m_description, errs,
Expand Down Expand Up @@ -1784,6 +1806,20 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
swift_ast_sp->LogConfiguration();
}
}

if (swift_ast_sp->HasFatalErrors()) {
logError(swift_ast_sp->GetFatalErrors().AsCString());
return {};
}

const bool can_create = true;
swift::ModuleDecl *stdlib =
swift_ast_sp->m_ast_context_ap->getStdlibModule(can_create);
if (!stdlib || IsDWARFImported(*stdlib)) {
logError("couldn't load the Swift stdlib");
return {};
}

return swift_ast_sp;
}

Expand Down Expand Up @@ -1840,17 +1876,6 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
return ModuleSP();
}

/// Detect whether a Swift module was "imported" by DWARFImporter.
/// All this *really* means is that it couldn't be loaded through any
/// other mechanism.
static bool IsDWARFImported(swift::ModuleDecl &module) {
return std::any_of(module.getFiles().begin(), module.getFiles().end(),
[](swift::FileUnit *file_unit) {
return (file_unit->getKind() ==
swift::FileUnitKind::DWARFModule);
});
}

lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
Target &target,
const char *extra_options) {
Expand Down