Skip to content

[Cherry-pick into next] Register the symbol context's module's AST files first. #8628

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
Show file tree
Hide file tree
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
12 changes: 5 additions & 7 deletions lldb/packages/Python/lldbsuite/test/make/Makefile.rules
Original file line number Diff line number Diff line change
Expand Up @@ -837,18 +837,17 @@ DYLIB_SWIFT_FLAGS= \
-Xlinker -install_name -Xlinker "$(DYLIB_INSTALL_NAME)" \
$(LD_EXTRAS)
ifeq "$(DYLIB_HIDE_SWIFTMODULE)" ""
DYLIB_SWIFT_FLAGS+= -Xlinker -add_ast_path -Xlinker $(DYLIB_NAME).swiftmodule
DYLIB_SWIFT_FLAGS+= -Xlinker -add_ast_path -Xlinker $(MODULENAME).swiftmodule
endif
else
DYLIB_SWIFT_FLAGS=$(LD_EXTRAS)
ifeq "$(DYLIB_HIDE_SWIFTMODULE)" ""
DYLIB_OBJECTS += $(BUILDDIR)/$(MODULENAME).o
endif
endif

$(DYLIB_FILENAME) : $(DYLIB_OBJECTS) $(MODULE_INTERFACE)
@echo "### Linking dynamic library $(DYLIB_NAME)"
ifneq "$(DYLIB_HIDE_SWIFTMODULE)" ""
$(SWIFTC) $(patsubst -g,,$(SWIFTFLAGS)) \
-emit-library $(DYLIB_SWIFT_FLAGS) -o $@ \
$(patsubst %.swiftmodule.o,,$(DYLIB_OBJECTS))
else
ifneq "$(FRAMEWORK)" ""
mkdir -p $(FRAMEWORK).framework/Versions/A/Headers
mkdir -p $(FRAMEWORK).framework/Versions/A/Modules
Expand All @@ -865,7 +864,6 @@ endif
endif
$(SWIFTC) $(patsubst -g,,$(SWIFTFLAGS)) \
-emit-library $(DYLIB_SWIFT_FLAGS) -o $@ $(DYLIB_OBJECTS)
endif
ifneq "$(CODESIGN)" ""
$(CODESIGN) -s - "$(DYLIB_FILENAME)"
endif
Expand Down
53 changes: 50 additions & 3 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2979,10 +2979,56 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
plugin_search_options.begin(),
plugin_search_options.end());

// Register the symbol context's module first. This makes it more
// likely that compatible AST blobs are found first, since then the
// local AST blobs overwrite any ones with the same import path from
// another dylib.
llvm::DenseSet<Module *> visited_modules;
llvm::StringMap<ModuleSP> all_modules;
for (size_t mi = 0; mi != num_images; ++mi) {
std::vector<std::string> module_names;
auto module_sp = target.GetImages().GetModuleAtIndex(mi);
swift_ast_sp->RegisterSectionModules(*module_sp, module_names);
auto image_sp = target.GetImages().GetModuleAtIndex(mi);
std::string path = image_sp->GetSpecificationDescription();
all_modules.insert({path, image_sp});
all_modules.insert({llvm::sys::path::filename(path), image_sp});
}
std::vector<std::string> module_names;
std::function<void(ModuleSP, unsigned)> scan_module =
[&](ModuleSP cur_module_sp, unsigned indent) {
if (!cur_module_sp ||
!visited_modules.insert(cur_module_sp.get()).second)
return;
swift_ast_sp->RegisterSectionModules(*cur_module_sp, module_names);
if (GetLog(LLDBLog::Types)) {
std::string spacer(indent, '-');
LOG_PRINTF(GetLog(LLDBLog::Types), "+%s Dependency scan: %s",
spacer.c_str(),
cur_module_sp->GetSpecificationDescription().c_str());
}
if (auto object = cur_module_sp->GetObjectFile()) {
FileSpecList file_list;
object->GetDependentModules(file_list);
for (auto &fs : file_list) {
if (ModuleSP dependency = all_modules.lookup(fs.GetPath())) {
scan_module(dependency, indent + 1);
} else if (ModuleSP dependency =
all_modules.lookup(fs.GetFilename())) {
scan_module(dependency, indent + 1);
} else {
if (GetLog(LLDBLog::Types)) {
std::string spacer(indent, '-');
LOG_PRINTF(GetLog(LLDBLog::Types),
"+%s Could not find %s in images", spacer.c_str(),
fs.GetPath().c_str());
}
}
}
}
};
scan_module(module_sp, 0);
for (size_t mi = 0; mi != num_images; ++mi) {
auto image_sp = target.GetImages().GetModuleAtIndex(mi);
if (!visited_modules.count(image_sp.get()))
swift_ast_sp->RegisterSectionModules(*image_sp, module_names);
}

LOG_PRINTF(GetLog(LLDBLog::Types), "((Target*)%p) = %p",
Expand Down Expand Up @@ -4370,6 +4416,7 @@ void SwiftASTContext::RegisterSectionModules(

// Grab all the AST blobs from the symbol vendor.
auto ast_file_datas = module.GetASTData(eLanguageTypeSwift);
if (ast_file_datas.size())
LOG_PRINTF(GetLog(LLDBLog::Types),
"(\"%s\") retrieved %zu AST Data blobs from the symbol vendor "
"(filter=\"%s\").",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def test_swift_deployment_target_from_macho(self):
self.expect("expression f", substrs=["i = 23"])
self.filecheck('platform shell cat ""%s"' % log, __file__)
# CHECK: SwiftASTContextForExpressions::SetTriple({{.*}}apple-macosx11.0.0
# CHECK: SwiftASTContextForExpressions::RegisterSectionModules("a.out") retrieved 0 AST Data blobs
# CHECK-NOT: SwiftASTContextForExpressions::RegisterSectionModules("a.out"){{.*}} AST Data blobs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@ def do_test(self, flag):
self, 'break here', lldb.SBFileSpec('main.swift'),
extra_images=['Direct', self.getBuildArtifact('hidden/libIndirect')])

log = self.getBuildArtifact("types.log")
self.expect('settings show '
+ 'target.experimental')
self.expect("log enable lldb types -f " + log)
types_log = self.getBuildArtifact("types.log")
self.expect("log enable lldb types -f " + types_log)
self.expect("expr -- x.inner.hidden", substrs=['=', '42'])

import io, re
logfile = io.open(log, "r", encoding='utf-8')
sanity = 0
found = 0
for line in logfile:
if re.match(r'.*SwiftASTContextForModule\("a\.out"\)::LogConfiguration\(\).*hidden$',
line.strip('\n')):
sanity += 1
elif re.match(r'.*SwiftASTContextForExpressions::LogConfiguration\(\).*hidden$',
line.strip('\n')):
found += 1
self.assertEqual(sanity, 1)
self.assertEqual(found, 1 if flag == 'true' else 0)
if flag == 'true':
prefix = 'POSITIVE'
else:
prefix = 'NEGATIVE'
self.filecheck('platform shell cat "%s"' % types_log, __file__,
'--check-prefix=CHECK_MOD_'+prefix)
self.filecheck('platform shell cat "%s"' % types_log, __file__,
'--check-prefix=CHECK_EXP_'+prefix)
# CHECK_MOD_POSITIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}}
# CHECK_MOD_NEGATIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}}
# CHECK_EXP_POSITIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}}
# CHECK_EXP_NEGATIVE-NOT: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}}
# CHECK_EXP_NEGATIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*}}Extra clang arguments