Skip to content

Commit 70d2ba0

Browse files
committed
Deserialize search path options in ProcessModule (NFCi)
That patch is one step towards removing the need for a separate SwiftASTContext per lldb::Module. It moves the parsing of the headers of modules found in Swift AST sections directly into ProcessModule() since all that ProcessModule() needs is the search path options from the serialized swift::CompilerInvocation in the module. Right now this means that the compiler invocations are deserialized twice, but this should be a cheap operation. rdar://81717792
1 parent b2632c9 commit 70d2ba0

File tree

3 files changed

+72
-30
lines changed

3 files changed

+72
-30
lines changed

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

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,11 +1836,6 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
18361836
// Add Swift interfaces in the .dSYM at the end of the search paths.
18371837
// .swiftmodules win over .swiftinterfaces, when they are loaded
18381838
// directly from the .swift_ast section.
1839-
//
1840-
// FIXME: Since these paths also end up in the scratch context, we
1841-
// would need a mechanism to ensure that and newer versions
1842-
// (in the library evolution sense, not the date on disk) win
1843-
// over older versions of the same .swiftinterface.
18441839
if (auto dsym = GetDSYMBundle(module)) {
18451840
llvm::SmallString<256> path(*dsym);
18461841
llvm::Triple triple(swift_ast_sp->GetTriple());
@@ -1949,7 +1944,7 @@ static lldb::ModuleSP GetUnitTestModule(lldb_private::ModuleList &modules) {
19491944
return ModuleSP();
19501945
}
19511946

1952-
/// Scan a newly added lldb::Module fdor Swift modules and report any errors in
1947+
/// Scan a newly added lldb::Module for Swift modules and report any errors in
19531948
/// its module SwiftASTContext to Target.
19541949
static void
19551950
ProcessModule(ModuleSP module_sp, std::string m_description,
@@ -2068,20 +2063,60 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
20682063

20692064
if (ast_context->HasErrors())
20702065
return;
2071-
if (use_all_compiler_flags ||
2072-
target.GetExecutableModulePointer() == module_sp.get()) {
2073-
2074-
const auto &opts = ast_context->GetSearchPathOptions();
2075-
module_search_paths.insert(module_search_paths.end(),
2076-
opts.ImportSearchPaths.begin(),
2077-
opts.ImportSearchPaths.end());
2078-
for (const auto &fwsp : opts.FrameworkSearchPaths)
2079-
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
2080-
for (const std::string &arg : ast_context->GetClangArguments()) {
2081-
extra_clang_args.push_back(arg);
2082-
LOG_VERBOSE_PRINTF(LIBLLDB_LOG_TYPES, "adding Clang argument \"%s\".",
2083-
arg.c_str());
2084-
}
2066+
2067+
// Load search path options from the module.
2068+
if (!use_all_compiler_flags &&
2069+
target.GetExecutableModulePointer() != module_sp.get())
2070+
return;
2071+
2072+
// Add Swift interfaces in the .dSYM at the end of the search paths.
2073+
// .swiftmodules win over .swiftinterfaces, when they are loaded
2074+
// directly from the .swift_ast section.
2075+
//
2076+
// FIXME: Since these paths end up in the scratch context, we would
2077+
// need a mechanism to ensure that and newer versions (in the
2078+
// library evolution sense, not the date on disk) win over
2079+
// older versions of the same .swiftinterface.
2080+
if (auto dsym = GetDSYMBundle(*module_sp)) {
2081+
llvm::SmallString<256> path(*dsym);
2082+
llvm::Triple triple(ast_context->GetTriple());
2083+
StringRef arch = llvm::Triple::getArchTypeName(triple.getArch());
2084+
llvm::sys::path::append(path, "Contents", "Resources", "Swift", arch);
2085+
bool exists = false;
2086+
llvm::sys::fs::is_directory(path, exists);
2087+
if (exists)
2088+
module_search_paths.push_back(std::string(path));
2089+
}
2090+
2091+
// Create a one-off CompilerInvocation as a place to load the
2092+
// deserialized search path options into.
2093+
SymbolFile *sym_file = module_sp->GetSymbolFile();
2094+
if (!sym_file)
2095+
return;
2096+
bool found_swift_modules = false;
2097+
bool got_serialized_options = false;
2098+
llvm::SmallString<0> error;
2099+
llvm::raw_svector_ostream errs(error);
2100+
swift::CompilerInvocation invocation;
2101+
if (DeserializeAllCompilerFlags(invocation, *module_sp, m_description, errs,
2102+
got_serialized_options,
2103+
found_swift_modules)) {
2104+
// TODO: After removing DeserializeAllCompilerFlags from
2105+
// CreateInstance(per-Module), errs will need to be
2106+
// collected here and surfaced.
2107+
}
2108+
2109+
const auto &opts = invocation.getSearchPathOptions();
2110+
module_search_paths.insert(module_search_paths.end(),
2111+
opts.ImportSearchPaths.begin(),
2112+
opts.ImportSearchPaths.end());
2113+
for (const auto &fwsp : opts.FrameworkSearchPaths)
2114+
framework_search_paths.push_back({fwsp.Path, fwsp.IsSystem});
2115+
auto &clang_opts = invocation.getClangImporterOptions().ExtraArgs;
2116+
for (const std::string &arg : clang_opts) {
2117+
extra_clang_args.push_back(arg);
2118+
LOG_VERBOSE_PRINTF(LIBLLDB_LOG_TYPES, "adding Clang argument \"%s\".",
2119+
arg.c_str());
20852120
}
20862121
}
20872122

lldb/test/API/lang/swift/clangimporter/rewrite_clang_paths/TestSwiftRewriteClangPaths.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,21 @@ def dotest(self, remap):
9797
found_rel = 0
9898
found_abs = 0
9999
found_ovl = 0
100+
in_scratch_context = False
100101
logfile = open(log, "r")
101102
for line in logfile:
102103
self.assertFalse("remapped -iquote" in line)
103-
if " remapped " in line:
104-
if line[:-1].endswith('/user'): found_abs += 1;
105-
continue
106104
if "error: " in line and "Foo" in line:
107105
errs += 1
108106
continue
107+
if line.startswith(" SwiftASTContextForExpressions"):
108+
in_scratch_context = True
109+
if " remapped " in line:
110+
if line[:-1].endswith('/user'):
111+
found_abs += 1;
112+
continue
113+
if not in_scratch_context:
114+
continue
109115
if 'user/iquote-path' in line: found_iquote += 1; continue
110116
if 'user/I-single' in line: found_i1 += 1; continue
111117
if 'user/I-double' in line: found_i2 += 1; continue
@@ -116,13 +122,13 @@ def dotest(self, remap):
116122

117123
if remap:
118124
self.assertEqual(errs, 0, "expected no module import error")
119-
# Module context + scratch context.
120-
self.assertEqual(found_iquote, 2)
121-
self.assertEqual(found_i1, 2)
122-
self.assertEqual(found_i2, 2)
123-
self.assertEqual(found_f, 2)
125+
# Counting occurences in the scratch context.
126+
self.assertEqual(found_iquote, 1)
127+
self.assertEqual(found_i1, 1)
128+
self.assertEqual(found_i2, 1)
129+
self.assertEqual(found_f, 1)
124130
self.assertEqual(found_rel, 0)
125131
self.assertEqual(found_abs, 1)
126-
self.assertEqual(found_ovl, 2)
132+
self.assertEqual(found_ovl, 1)
127133
else:
128-
self.assertTrue(errs > 0, "expected module import error")
134+
self.assertGreater(errs, 0, "expected module import error")

lldb/test/Shell/Swift/astcontext_error.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# RUN: %target-swiftc -g %S/Inputs/ContextError.swift
44
# RUN: %lldb ContextError -s %s | FileCheck %S/Inputs/ContextError.swift
55

6+
# Test that re-running a process doesn't emit bogus warnings.
67
br set -p "here"
78
run
89
run

0 commit comments

Comments
 (0)