Skip to content

Commit 09752d7

Browse files
committed
Register the symbol context's module's AST files 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. The AST blobs are registered in a DFS following the dynamic library dependencies in the binary. (cherry picked from commit 7dbd6b7)
1 parent e48f4e8 commit 09752d7

File tree

4 files changed

+71
-29
lines changed

4 files changed

+71
-29
lines changed

lldb/packages/Python/lldbsuite/test/make/Makefile.rules

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,18 +837,17 @@ DYLIB_SWIFT_FLAGS= \
837837
-Xlinker -install_name -Xlinker "$(DYLIB_INSTALL_NAME)" \
838838
$(LD_EXTRAS)
839839
ifeq "$(DYLIB_HIDE_SWIFTMODULE)" ""
840-
DYLIB_SWIFT_FLAGS+= -Xlinker -add_ast_path -Xlinker $(DYLIB_NAME).swiftmodule
840+
DYLIB_SWIFT_FLAGS+= -Xlinker -add_ast_path -Xlinker $(MODULENAME).swiftmodule
841841
endif
842842
else
843843
DYLIB_SWIFT_FLAGS=$(LD_EXTRAS)
844+
ifeq "$(DYLIB_HIDE_SWIFTMODULE)" ""
845+
DYLIB_OBJECTS += $(BUILDDIR)/$(MODULENAME).o
846+
endif
844847
endif
848+
845849
$(DYLIB_FILENAME) : $(DYLIB_OBJECTS) $(MODULE_INTERFACE)
846850
@echo "### Linking dynamic library $(DYLIB_NAME)"
847-
ifneq "$(DYLIB_HIDE_SWIFTMODULE)" ""
848-
$(SWIFTC) $(patsubst -g,,$(SWIFTFLAGS)) \
849-
-emit-library $(DYLIB_SWIFT_FLAGS) -o $@ \
850-
$(patsubst %.swiftmodule.o,,$(DYLIB_OBJECTS))
851-
else
852851
ifneq "$(FRAMEWORK)" ""
853852
mkdir -p $(FRAMEWORK).framework/Versions/A/Headers
854853
mkdir -p $(FRAMEWORK).framework/Versions/A/Modules
@@ -865,7 +864,6 @@ endif
865864
endif
866865
$(SWIFTC) $(patsubst -g,,$(SWIFTFLAGS)) \
867866
-emit-library $(DYLIB_SWIFT_FLAGS) -o $@ $(DYLIB_OBJECTS)
868-
endif
869867
ifneq "$(CODESIGN)" ""
870868
$(CODESIGN) -s - "$(DYLIB_FILENAME)"
871869
endif

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,10 +2979,56 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
29792979
plugin_search_options.begin(),
29802980
plugin_search_options.end());
29812981

2982+
// Register the symbol context's module first. This makes it more
2983+
// likely that compatible AST blobs are found first, since then the
2984+
// local AST blobs overwrite any ones with the same import path from
2985+
// another dylib.
2986+
llvm::DenseSet<Module *> visited_modules;
2987+
llvm::StringMap<ModuleSP> all_modules;
29822988
for (size_t mi = 0; mi != num_images; ++mi) {
2983-
std::vector<std::string> module_names;
2984-
auto module_sp = target.GetImages().GetModuleAtIndex(mi);
2985-
swift_ast_sp->RegisterSectionModules(*module_sp, module_names);
2989+
auto image_sp = target.GetImages().GetModuleAtIndex(mi);
2990+
std::string path = image_sp->GetSpecificationDescription();
2991+
all_modules.insert({path, image_sp});
2992+
all_modules.insert({llvm::sys::path::filename(path), image_sp});
2993+
}
2994+
std::vector<std::string> module_names;
2995+
std::function<void(ModuleSP, unsigned)> scan_module =
2996+
[&](ModuleSP cur_module_sp, unsigned indent) {
2997+
if (!cur_module_sp ||
2998+
!visited_modules.insert(cur_module_sp.get()).second)
2999+
return;
3000+
swift_ast_sp->RegisterSectionModules(*cur_module_sp, module_names);
3001+
if (GetLog(LLDBLog::Types)) {
3002+
std::string spacer(indent, '-');
3003+
LOG_PRINTF(GetLog(LLDBLog::Types), "+%s Dependency scan: %s",
3004+
spacer.c_str(),
3005+
cur_module_sp->GetSpecificationDescription().c_str());
3006+
}
3007+
if (auto object = cur_module_sp->GetObjectFile()) {
3008+
FileSpecList file_list;
3009+
object->GetDependentModules(file_list);
3010+
for (auto &fs : file_list) {
3011+
if (ModuleSP dependency = all_modules.lookup(fs.GetPath())) {
3012+
scan_module(dependency, indent + 1);
3013+
} else if (ModuleSP dependency =
3014+
all_modules.lookup(fs.GetFilename())) {
3015+
scan_module(dependency, indent + 1);
3016+
} else {
3017+
if (GetLog(LLDBLog::Types)) {
3018+
std::string spacer(indent, '-');
3019+
LOG_PRINTF(GetLog(LLDBLog::Types),
3020+
"+%s Could not find %s in images", spacer.c_str(),
3021+
fs.GetPath().c_str());
3022+
}
3023+
}
3024+
}
3025+
}
3026+
};
3027+
scan_module(module_sp, 0);
3028+
for (size_t mi = 0; mi != num_images; ++mi) {
3029+
auto image_sp = target.GetImages().GetModuleAtIndex(mi);
3030+
if (!visited_modules.count(image_sp.get()))
3031+
swift_ast_sp->RegisterSectionModules(*image_sp, module_names);
29863032
}
29873033

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

43714417
// Grab all the AST blobs from the symbol vendor.
43724418
auto ast_file_datas = module.GetASTData(eLanguageTypeSwift);
4419+
if (ast_file_datas.size())
43734420
LOG_PRINTF(GetLog(LLDBLog::Types),
43744421
"(\"%s\") retrieved %zu AST Data blobs from the symbol vendor "
43754422
"(filter=\"%s\").",

lldb/test/API/lang/swift/deployment_target/TestSwiftDeploymentTarget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ def test_swift_deployment_target_from_macho(self):
6565
self.expect("expression f", substrs=["i = 23"])
6666
self.filecheck('platform shell cat ""%s"' % log, __file__)
6767
# CHECK: SwiftASTContextForExpressions::SetTriple({{.*}}apple-macosx11.0.0
68-
# CHECK: SwiftASTContextForExpressions::RegisterSectionModules("a.out") retrieved 0 AST Data blobs
68+
# CHECK-NOT: SwiftASTContextForExpressions::RegisterSectionModules("a.out"){{.*}} AST Data blobs

lldb/test/API/lang/swift/expression/import_search_paths/TestSwiftImportSearchPaths.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,19 @@ def do_test(self, flag):
3030
self, 'break here', lldb.SBFileSpec('main.swift'),
3131
extra_images=['Direct', self.getBuildArtifact('hidden/libIndirect')])
3232

33-
log = self.getBuildArtifact("types.log")
34-
self.expect('settings show '
35-
+ 'target.experimental')
36-
self.expect("log enable lldb types -f " + log)
33+
types_log = self.getBuildArtifact("types.log")
34+
self.expect("log enable lldb types -f " + types_log)
3735
self.expect("expr -- x.inner.hidden", substrs=['=', '42'])
38-
39-
import io, re
40-
logfile = io.open(log, "r", encoding='utf-8')
41-
sanity = 0
42-
found = 0
43-
for line in logfile:
44-
if re.match(r'.*SwiftASTContextForModule\("a\.out"\)::LogConfiguration\(\).*hidden$',
45-
line.strip('\n')):
46-
sanity += 1
47-
elif re.match(r'.*SwiftASTContextForExpressions::LogConfiguration\(\).*hidden$',
48-
line.strip('\n')):
49-
found += 1
50-
self.assertEqual(sanity, 1)
51-
self.assertEqual(found, 1 if flag == 'true' else 0)
36+
if flag == 'true':
37+
prefix = 'POSITIVE'
38+
else:
39+
prefix = 'NEGATIVE'
40+
self.filecheck('platform shell cat "%s"' % types_log, __file__,
41+
'--check-prefix=CHECK_MOD_'+prefix)
42+
self.filecheck('platform shell cat "%s"' % types_log, __file__,
43+
'--check-prefix=CHECK_EXP_'+prefix)
44+
# CHECK_MOD_POSITIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}}
45+
# CHECK_MOD_NEGATIVE: SwiftASTContextForModule("a.out")::LogConfiguration(){{.*hidden$}}
46+
# CHECK_EXP_POSITIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}}
47+
# CHECK_EXP_NEGATIVE-NOT: SwiftASTContextForExpressions::LogConfiguration(){{.*hidden$}}
48+
# CHECK_EXP_NEGATIVE: SwiftASTContextForExpressions::LogConfiguration(){{.*}}Extra clang arguments

0 commit comments

Comments
 (0)